1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from mediagoblin
import mg_globals
as mgg
20 from mediagoblin
.db
.base
import Session
21 from mediagoblin
.db
.models
import MediaEntry
, Tag
, MediaTag
, Collection
22 from mediagoblin
.gmg_commands
.dbupdate
import gather_database_data
24 ##########################
25 # Random utility functions
26 ##########################
29 def atomic_update(table
, query_dict
, update_values
):
30 table
.query
.filter_by(**query_dict
).update(update_values
,
31 synchronize_session
=False)
35 def check_media_slug_used(uploader_id
, slug
, ignore_m_id
):
36 query
= MediaEntry
.query
.filter_by(uploader
=uploader_id
, slug
=slug
)
37 if ignore_m_id
is not None:
38 query
= query
.filter(MediaEntry
.id != ignore_m_id
)
39 does_exist
= query
.first() is not None
43 def media_entries_for_tag_slug(dummy_db
, tag_slug
):
44 return MediaEntry
.query \
45 .join(MediaEntry
.tags_helper
) \
46 .join(MediaTag
.tag_helper
) \
48 (MediaEntry
.state
== u
'processed')
49 & (Tag
.slug
== tag_slug
))
52 def clean_orphan_tags(commit
=True):
53 """Search for unused MediaTags and delete them"""
54 q1
= Session
.query(Tag
).outerjoin(MediaTag
).filter(MediaTag
.id==None)
57 # The "let the db do all the work" version:
58 # q1 = Session.query(Tag.id).outerjoin(MediaTag).filter(MediaTag.id==None)
59 # q2 = Session.query(Tag).filter(Tag.id.in_(q1))
60 # q2.delete(synchronize_session = False)
65 def check_collection_slug_used(creator_id
, slug
, ignore_c_id
):
66 filt
= (Collection
.creator
== creator_id
) \
67 & (Collection
.slug
== slug
)
68 if ignore_c_id
is not None:
69 filt
= filt
& (Collection
.id != ignore_c_id
)
70 does_exist
= Session
.query(Collection
.id).filter(filt
).first() is not None
74 def check_db_up_to_date():
75 """Check if the database is up to date and quit if not"""
76 dbdatas
= gather_database_data(mgg
.global_config
.get('plugins', {}).keys())
78 for dbdata
in dbdatas
:
81 migration_manager
= dbdata
.make_migration_manager(session
)
82 if migration_manager
.database_current_migration
is None or \
83 migration_manager
.migrations_to_run():
84 sys
.exit("Your database is not up to date. Please run "
85 "'gmg dbupdate' before starting MediaGoblin.")
91 if __name__
== '__main__':
92 from mediagoblin
.db
.open import setup_connection_and_db_from_config
94 db
= setup_connection_and_db_from_config({'sql_engine':'sqlite:///mediagoblin.db'})