diff options
author | Mike Linksvayer <ml@gondwanaland.com> | 2012-12-20 12:50:43 -0800 |
---|---|---|
committer | Mike Linksvayer <ml@gondwanaland.com> | 2012-12-20 12:50:43 -0800 |
commit | 1d0148aef1493467dacb6c39c4e080ee4a5246d2 (patch) | |
tree | 5213418660d4e079eb8dac6bc8ea2e4fdc140088 /mediagoblin/db | |
parent | 200433dc38a2139462e0331be36ee49a80a81a12 (diff) | |
parent | 5c99cd01a70f2d597ac7669e8d944ddf79b05281 (diff) | |
download | mediagoblin-1d0148aef1493467dacb6c39c4e080ee4a5246d2.tar.lz mediagoblin-1d0148aef1493467dacb6c39c4e080ee4a5246d2.tar.xz mediagoblin-1d0148aef1493467dacb6c39c4e080ee4a5246d2.zip |
Merge branch 'master' of git://gitorious.org/mediagoblin/mediagoblin
Conflicts:
docs/source/siteadmin/media-types.rst
Diffstat (limited to 'mediagoblin/db')
-rw-r--r-- | mediagoblin/db/mixin.py | 38 | ||||
-rw-r--r-- | mediagoblin/db/sql/base.py | 16 | ||||
-rw-r--r-- | mediagoblin/db/sql/migrations.py | 133 | ||||
-rw-r--r-- | mediagoblin/db/sql/util.py | 12 |
4 files changed, 157 insertions, 42 deletions
diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index f6a15a12..3f395e90 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -27,8 +27,11 @@ These functions now live here and get "mixed in" into the real objects. """ +from werkzeug.utils import cached_property + from mediagoblin import mg_globals from mediagoblin.auth import lib as auth_lib +from mediagoblin.media_types import get_media_managers, FileTypeNotSupported from mediagoblin.tools import common, licenses from mediagoblin.tools.text import cleaned_markdown_conversion from mediagoblin.tools.url import slugify @@ -98,6 +101,7 @@ class MediaEntryMixin(object): def slug_or_id(self): return (self.slug or self._id) + def url_for_self(self, urlgen, **extra_args): """ Generate an appropriate url for ourselves @@ -112,6 +116,38 @@ class MediaEntryMixin(object): media=self.slug_or_id, **extra_args) + @property + def thumb_url(self): + """Return the thumbnail URL (for usage in templates) + Will return either the real thumbnail or a default fallback icon.""" + # TODO: implement generic fallback in case MEDIA_MANAGER does + # not specify one? + if u'thumb' in self.media_files: + thumb_url = mg_globals.app.public_store.file_url( + self.media_files[u'thumb']) + else: + # No thumbnail in media available. Get the media's + # MEDIA_MANAGER for the fallback icon and return static URL + # Raises FileTypeNotSupported in case no such manager is enabled + manager = self.media_manager + thumb_url = mg_globals.app.staticdirector(manager[u'default_thumb']) + return thumb_url + + @cached_property + def media_manager(self): + """Returns the MEDIA_MANAGER of the media's media_type + + Raises FileTypeNotSupported in case no such manager is enabled + """ + # TODO, we should be able to make this a simple lookup rather + # than iterating through all media managers. + for media_type, manager in get_media_managers(): + if media_type == self.media_type: + return manager + # Not found? Then raise an error + raise FileTypeNotSupported( + "MediaManager not in enabled types. Check media_types in config?") + def get_fail_exception(self): """ Get the exception that's appropriate for this error @@ -121,7 +157,7 @@ class MediaEntryMixin(object): def get_license_data(self): """Return license dict for requested license""" - return licenses.SUPPORTED_LICENSES[self.license or ""] + return licenses.get_license_by_url(self.license or "") def exif_display_iter(self): from mediagoblin.tools.exif import USEFUL_TAGS diff --git a/mediagoblin/db/sql/base.py b/mediagoblin/db/sql/base.py index 838080b0..ca0c8166 100644 --- a/mediagoblin/db/sql/base.py +++ b/mediagoblin/db/sql/base.py @@ -51,12 +51,18 @@ class GMGTableBase(object): query = Session.query_property() @classmethod - def find(cls, query_dict={}): + def find(cls, query_dict=None): + if query_dict is None: + query_dict = {} + _fix_query_dict(query_dict) return cls.query.filter_by(**query_dict) @classmethod - def find_one(cls, query_dict={}): + def find_one(cls, query_dict=None): + if query_dict is None: + query_dict = {} + _fix_query_dict(query_dict) return cls.query.filter_by(**query_dict).first() @@ -79,11 +85,13 @@ class GMGTableBase(object): sess.add(self) sess.commit() - def delete(self): + def delete(self, commit=True): + """Delete the object and commit the change immediately by default""" sess = object_session(self) assert sess is not None, "Not going to delete detached %r" % self sess.delete(self) - sess.commit() + if commit: + sess.commit() Base = declarative_base(cls=GMGTableBase) diff --git a/mediagoblin/db/sql/migrations.py b/mediagoblin/db/sql/migrations.py index 1d822cd9..99448bfa 100644 --- a/mediagoblin/db/sql/migrations.py +++ b/mediagoblin/db/sql/migrations.py @@ -17,11 +17,14 @@ import datetime from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger, - Integer, Unicode, UnicodeText, DateTime, ForeignKey) + Integer, Unicode, UnicodeText, DateTime, + ForeignKey) +from sqlalchemy.exc import ProgrammingError +from sqlalchemy.ext.declarative import declarative_base +from migrate.changeset.constraint import UniqueConstraint from mediagoblin.db.sql.util import RegisterMigration -from mediagoblin.db.sql.models import MediaEntry, Collection, User, \ - ProcessingMetaData +from mediagoblin.db.sql.models import MediaEntry, Collection, User MIGRATIONS = {} @@ -65,29 +68,45 @@ def add_transcoding_progress(db_conn): db_conn.commit() +class Collection_v0(declarative_base()): + __tablename__ = "core__collections" + + id = Column(Integer, primary_key=True) + title = Column(Unicode, nullable=False) + slug = Column(Unicode) + created = Column(DateTime, nullable=False, default=datetime.datetime.now, + index=True) + description = Column(UnicodeText) + creator = Column(Integer, ForeignKey(User.id), nullable=False) + items = Column(Integer, default=0) + +class CollectionItem_v0(declarative_base()): + __tablename__ = "core__collection_items" + + id = Column(Integer, primary_key=True) + media_entry = Column( + Integer, ForeignKey(MediaEntry.id), nullable=False, index=True) + collection = Column(Integer, ForeignKey(Collection.id), nullable=False) + note = Column(UnicodeText, nullable=True) + added = Column(DateTime, nullable=False, default=datetime.datetime.now) + position = Column(Integer) + + ## This should be activated, normally. + ## But this would change the way the next migration used to work. + ## So it's commented for now. + __table_args__ = ( + UniqueConstraint('collection', 'media_entry'), + {}) + +collectionitem_unique_constraint_done = False + @RegisterMigration(4, MIGRATIONS) def add_collection_tables(db_conn): - metadata = MetaData(bind=db_conn.bind) + Collection_v0.__table__.create(db_conn.bind) + CollectionItem_v0.__table__.create(db_conn.bind) - collection = Table('core__collections', metadata, - Column('id', Integer, primary_key=True), - Column('title', Unicode, nullable=False), - Column('slug', Unicode), - Column('created', DateTime, nullable=False, default=datetime.datetime.now, index=True), - Column('description', UnicodeText), - Column('creator', Integer, ForeignKey(User.id), nullable=False), - Column('items', Integer, default=0)) - - collection_item = Table('core__collection_items', metadata, - Column('id', Integer, primary_key=True), - Column('media_entry', Integer, ForeignKey(MediaEntry.id), nullable=False, index=True), - Column('collection', Integer, ForeignKey(Collection.id), nullable=False), - Column('note', UnicodeText, nullable=True), - Column('added', DateTime, nullable=False, default=datetime.datetime.now), - Column('position', Integer)) - - collection.create() - collection_item.create() + global collectionitem_unique_constraint_done + collectionitem_unique_constraint_done = True db_conn.commit() @@ -104,15 +123,67 @@ def add_mediaentry_collected(db_conn): db_conn.commit() +class ProcessingMetaData_v0(declarative_base()): + __tablename__ = 'core__processing_metadata' + + id = Column(Integer, primary_key=True) + media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False, + index=True) + callback_url = Column(Unicode) + @RegisterMigration(6, MIGRATIONS) def create_processing_metadata_table(db): - metadata = MetaData(bind=db.bind) + ProcessingMetaData_v0.__table__.create(db.bind) + db.commit() - metadata_table = Table('core__processing_metadata', metadata, - Column('id', Integer, primary_key=True), - Column('media_entry_id', Integer, ForeignKey(MediaEntry.id), - nullable=False, index=True), - Column('callback_url', Unicode)) - metadata_table.create() - db.commit() +# Okay, problem being: +# Migration #4 forgot to add the uniqueconstraint for the +# new tables. While creating the tables from scratch had +# the constraint enabled. +# +# So we have four situations that should end up at the same +# db layout: +# +# 1. Fresh install. +# Well, easy. Just uses the tables in models.py +# 2. Fresh install using a git version just before this migration +# The tables are all there, the unique constraint is also there. +# This migration should do nothing. +# But as we can't detect the uniqueconstraint easily, +# this migration just adds the constraint again. +# And possibly fails very loud. But ignores the failure. +# 3. old install, not using git, just releases. +# This one will get the new tables in #4 (now with constraint!) +# And this migration is just skipped silently. +# 4. old install, always on latest git. +# This one has the tables, but lacks the constraint. +# So this migration adds the constraint. +@RegisterMigration(7, MIGRATIONS) +def fix_CollectionItem_v0_constraint(db_conn): + """Add the forgotten Constraint on CollectionItem""" + + global collectionitem_unique_constraint_done + if collectionitem_unique_constraint_done: + # Reset it. Maybe the whole thing gets run again + # For a different db? + collectionitem_unique_constraint_done = False + return + + metadata = MetaData(bind=db_conn.bind) + + CollectionItem_table = Table('core__collection_items', + metadata, autoload=True, autoload_with=db_conn.bind) + + constraint = UniqueConstraint('collection', 'media_entry', + name='core__collection_items_collection_media_entry_key', + table=CollectionItem_table) + + try: + constraint.create() + except ProgrammingError: + # User probably has an install that was run since the + # collection tables were added, so we don't need to run this migration. + pass + + db_conn.commit() diff --git a/mediagoblin/db/sql/util.py b/mediagoblin/db/sql/util.py index 74b5d73e..f6a3dc17 100644 --- a/mediagoblin/db/sql/util.py +++ b/mediagoblin/db/sql/util.py @@ -39,7 +39,7 @@ class MigrationManager(object): - migration_registry: where we should find all migrations to run """ - self.name = name + self.name = unicode(name) self.models = models self.session = session self.migration_registry = migration_registry @@ -217,9 +217,9 @@ class MigrationManager(object): u' + Running migration %s, "%s"... ' % ( migration_number, migration_func.func_name)) migration_func(self.session) + self.set_current_migration(migration_number) self.printer('done.\n') - self.set_current_migration() return u'migrated' # Otherwise return None. Well it would do this anyway, but @@ -297,17 +297,17 @@ def media_entries_for_tag_slug(dummy_db, tag_slug): & (Tag.slug == tag_slug)) -def clean_orphan_tags(): +def clean_orphan_tags(commit=True): + """Search for unused MediaTags and delete them""" q1 = Session.query(Tag).outerjoin(MediaTag).filter(MediaTag.id==None) for t in q1: Session.delete(t) - # The "let the db do all the work" version: # q1 = Session.query(Tag.id).outerjoin(MediaTag).filter(MediaTag.id==None) # q2 = Session.query(Tag).filter(Tag.id.in_(q1)) # q2.delete(synchronize_session = False) - - Session.commit() + if commit: + Session.commit() def check_collection_slug_used(dummy_db, creator_id, slug, ignore_c_id): |