util.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  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/>.
  16. import sys
  17. from mediagoblin import mg_globals as mgg
  18. from mediagoblin.db.models import MediaEntry, Tag, MediaTag, Collection
  19. from mediagoblin.gmg_commands.dbupdate import gather_database_data
  20. from mediagoblin.tools.transition import DISABLE_GLOBALS
  21. if not DISABLE_GLOBALS:
  22. from mediagoblin.db.base import Session
  23. ##########################
  24. # Random utility functions
  25. ##########################
  26. def atomic_update(table, query_dict, update_values):
  27. table.query.filter_by(**query_dict).update(update_values,
  28. synchronize_session=False)
  29. Session.commit()
  30. def check_media_slug_used(uploader_id, slug, ignore_m_id):
  31. query = MediaEntry.query.filter_by(uploader=uploader_id, slug=slug)
  32. if ignore_m_id is not None:
  33. query = query.filter(MediaEntry.id != ignore_m_id)
  34. does_exist = query.first() is not None
  35. return does_exist
  36. def media_entries_for_tag_slug(dummy_db, tag_slug):
  37. return MediaEntry.query \
  38. .join(MediaEntry.tags_helper) \
  39. .join(MediaTag.tag_helper) \
  40. .filter(
  41. (MediaEntry.state == u'processed')
  42. & (Tag.slug == tag_slug))
  43. def clean_orphan_tags(commit=True):
  44. """Search for unused MediaTags and delete them"""
  45. q1 = Session.query(Tag).outerjoin(MediaTag).filter(MediaTag.id==None)
  46. for t in q1:
  47. Session.delete(t)
  48. # The "let the db do all the work" version:
  49. # q1 = Session.query(Tag.id).outerjoin(MediaTag).filter(MediaTag.id==None)
  50. # q2 = Session.query(Tag).filter(Tag.id.in_(q1))
  51. # q2.delete(synchronize_session = False)
  52. if commit:
  53. Session.commit()
  54. def check_collection_slug_used(creator_id, slug, ignore_c_id):
  55. filt = (Collection.creator == creator_id) \
  56. & (Collection.slug == slug)
  57. if ignore_c_id is not None:
  58. filt = filt & (Collection.id != ignore_c_id)
  59. does_exist = Session.query(Collection.id).filter(filt).first() is not None
  60. return does_exist
  61. def check_db_up_to_date():
  62. """Check if the database is up to date and quit if not"""
  63. dbdatas = gather_database_data(mgg.global_config.get('plugins', {}).keys())
  64. for dbdata in dbdatas:
  65. session = Session()
  66. try:
  67. migration_manager = dbdata.make_migration_manager(session)
  68. if migration_manager.database_current_migration is None or \
  69. migration_manager.migrations_to_run():
  70. sys.exit("Your database is not up to date. Please run "
  71. "'gmg dbupdate' before starting MediaGoblin.")
  72. finally:
  73. Session.rollback()
  74. Session.remove()
  75. if __name__ == '__main__':
  76. from mediagoblin.db.open import setup_connection_and_db_from_config
  77. db = setup_connection_and_db_from_config({'sql_engine':'sqlite:///mediagoblin.db'})
  78. clean_orphan_tags()