From 35fbad47d7e74faa7eaa87ade534a9fb10df6d36 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Wed, 19 Aug 2015 16:56:49 +0200 Subject: [PATCH] Change structure of MediaEntry and add migration This makes the changes needed for federating MediaEntry objects as well as adding the migration and necessary methods to get the public_id just in time (JIT). --- mediagoblin/db/migrations.py | 45 +++++++++++++++++++++++++++++++++--- mediagoblin/db/mixin.py | 19 +++++++++++++++ mediagoblin/db/models.py | 19 ++++++++------- 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index 8f9ce79b..00b0add1 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -1461,14 +1461,14 @@ def federation_user_create_tables(db): """ Create all the tables """ - metadata = MetaData(bind=db.bind) - user_table = inspect_table(metadata, "core__users") - # Create tables needed LocalUser_V0.__table__.create(db.bind) RemoteUser_V0.__table__.create(db.bind) db.commit() + metadata = MetaData(bind=db.bind) + user_table = inspect_table(metadata, "core__users") + # Create the fields updated_column = Column( "updated", @@ -1575,5 +1575,44 @@ def federation_remove_fields(db): wants_notifications_column = user_table.columns["wants_notifications"] wants_notifications_column.drop() + db.commit() + +@RegisterMigration(35, MIGRATIONS) +def federation_media_entry(db): + metadata = MetaData(bind=db.bind) + media_entry_table = inspect_table(metadata, "core__media_entries") + + # Add new fields + public_id_column = Column( + "public_id", + Unicode, + unique=True, + nullable=True + ) + public_id_column.create( + media_entry_table, + unique_name="media_public_id" + ) + + remote_column = Column( + "remote", + Boolean, + default=False + ) + remote_column.create(media_entry_table) + + updated_column = Column( + "updated", + DateTime, + default=datetime.datetime.utcnow, + ) + updated_column.create(media_entry_table) + + # Data migration + for entry in db.execute(media_entry_table.select()): + db.execute(media_entry_table.update().values( + updated=entry.created, + remote=False + )) db.commit() diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index 88df1f6b..ab6de230 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -196,6 +196,25 @@ class MediaEntryMixin(GenerateSlugMixin): media=self.slug_or_id, **extra_args) + def get_public_id(self, request): + """ Returns the public_id of the MediaEntry + + If the MediaEntry has no public ID one will be produced from the + current request. + """ + if self.public_id is None: + self.public_id = request.urlgen( + "mediagoblin.api.object", + object_type=self.object_type, + id=self.id, + qualified=True + ) + # We need to ensure this ID is reused once we've given it out. + self.save() + + return self.public_id + + @property def thumb_url(self): """Return the thumbnail URL (for usage in templates) diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index 9d57753b..af6e2872 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -494,11 +494,12 @@ class MediaEntry(Base, MediaEntryMixin): __tablename__ = "core__media_entries" id = Column(Integer, primary_key=True) + public_id = Column(Unicode, unique=True, nullable=True) + remote = Column(Boolean, default=False) + uploader = Column(Integer, ForeignKey(User.id), nullable=False, index=True) title = Column(Unicode, nullable=False) slug = Column(Unicode) - created = Column(DateTime, nullable=False, default=datetime.datetime.utcnow, - index=True) description = Column(UnicodeText) # ?? media_type = Column(Unicode, nullable=False) state = Column(Unicode, default=u'unprocessed', nullable=False) @@ -508,6 +509,10 @@ class MediaEntry(Base, MediaEntryMixin): location = Column(Integer, ForeignKey("core__locations.id")) get_location = relationship("Location", lazy="joined") + created = Column(DateTime, nullable=False, default=datetime.datetime.utcnow, + index=True) + updated = Column(DateTime, nullable=False, default=datetime.datetime.utcnow) + fail_error = Column(Unicode) fail_metadata = Column(JSONEncoded) @@ -681,17 +686,11 @@ class MediaEntry(Base, MediaEntryMixin): def serialize(self, request, show_comments=True): """ Unserialize MediaEntry to object """ - href = request.urlgen( - "mediagoblin.api.object", - object_type=self.object_type, - id=self.id, - qualified=True - ) author = self.get_uploader published = UTC.localize(self.created) - updated = UTC.localize(self.created) + updated = UTC.localize(self.updated) context = { - "id": href, + "id": self.get_public_id(request), "author": author.serialize(request), "objectType": self.object_type, "url": self.url_for_self(request.urlgen, qualified=True), -- 2.25.1