Commit | Line | Data |
---|---|---|
1815f5ce | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
1815f5ce CAW |
3 | # |
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. | |
8 | # | |
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. | |
13 | # | |
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/>. | |
a050e776 | 16 | |
26583b2c RE |
17 | import sys |
18 | ||
19 | from mediagoblin import mg_globals as mgg | |
39dc3bf8 | 20 | from mediagoblin.db.base import Session |
b0c8328e | 21 | from mediagoblin.db.models import MediaEntry, Tag, MediaTag, Collection |
26583b2c | 22 | from mediagoblin.gmg_commands.dbupdate import gather_database_data |
1e46dc25 SS |
23 | |
24 | ########################## | |
25 | # Random utility functions | |
26 | ########################## | |
27 | ||
28 | ||
29 | def atomic_update(table, query_dict, update_values): | |
44082b12 | 30 | table.query.filter_by(**query_dict).update(update_values, |
1e46dc25 SS |
31 | synchronize_session=False) |
32 | Session.commit() | |
33 | ||
34 | ||
65969d3f SS |
35 | def check_media_slug_used(uploader_id, slug, ignore_m_id): |
36 | query = MediaEntry.query.filter_by(uploader=uploader_id, slug=slug) | |
1e46dc25 | 37 | if ignore_m_id is not None: |
65969d3f SS |
38 | query = query.filter(MediaEntry.id != ignore_m_id) |
39 | does_exist = query.first() is not None | |
1e46dc25 SS |
40 | return does_exist |
41 | ||
42 | ||
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) \ | |
47 | .filter( | |
48 | (MediaEntry.state == u'processed') | |
49 | & (Tag.slug == tag_slug)) | |
50 | ||
51 | ||
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) | |
55 | for t in q1: | |
56 | Session.delete(t) | |
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) | |
61 | if commit: | |
62 | Session.commit() | |
63 | ||
64 | ||
455fd36f | 65 | def check_collection_slug_used(creator_id, slug, ignore_c_id): |
1e46dc25 SS |
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 | |
71 | return does_exist | |
72 | ||
73 | ||
26583b2c RE |
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()) | |
77 | ||
78 | for dbdata in dbdatas: | |
79 | migration_manager = dbdata.make_migration_manager(Session()) | |
80 | if migration_manager.database_current_migration is None or \ | |
81 | migration_manager.migrations_to_run(): | |
82 | sys.exit("Your database is not up to date. Please run " | |
64eab630 | 83 | "'gmg dbupdate' before starting MediaGoblin.") |
26583b2c RE |
84 | |
85 | ||
1e46dc25 SS |
86 | if __name__ == '__main__': |
87 | from mediagoblin.db.open import setup_connection_and_db_from_config | |
88 | ||
89 | db = setup_connection_and_db_from_config({'sql_engine':'sqlite:///mediagoblin.db'}) | |
90 | ||
91 | clean_orphan_tags() |