Change structure of MediaEntry and add migration
authorJessica Tallon <tsyesika@tsyesika.se>
Wed, 19 Aug 2015 14:56:49 +0000 (16:56 +0200)
committerJessica Tallon <tsyesika@tsyesika.se>
Wed, 19 Aug 2015 15:18:37 +0000 (17:18 +0200)
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
mediagoblin/db/mixin.py
mediagoblin/db/models.py

index 8f9ce79b018c8104c2db85365ebc3ea43d6671f8..00b0add12ce0a372affc1abde1f474e560d89352 100644 (file)
@@ -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()
index 88df1f6b11ffc90a7c1367bb06b6a7185e608969..ab6de23098d3df73a62a9daff267a1bbd824d3d0 100644 (file)
@@ -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)
index 9d57753b4931c74cb1d17081b8c6cd19cfd97045..af6e2872e26d0b2f93289146821cea36354032e0 100644 (file)
@@ -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),