get and set metadata for a MediaFile
authorRodney Ewing <ewing.rj@gmail.com>
Wed, 21 Aug 2013 21:34:11 +0000 (14:34 -0700)
committerRodney Ewing <ewing.rj@gmail.com>
Wed, 21 Aug 2013 21:44:13 +0000 (14:44 -0700)
mediagoblin/db/migrations.py
mediagoblin/db/models.py

index 374ab4c8f6b6bbba969d2b8ccf399cdb62931207..508fcbab9f98acc0952024012a5d41d23fb4bf0a 100644 (file)
@@ -425,7 +425,7 @@ class RequestToken_v0(declarative_base()):
     callback = Column(Unicode, nullable=False, default=u"oob")
     created = Column(DateTime, nullable=False, default=datetime.datetime.now)
     updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
-    
+
 class AccessToken_v0(declarative_base()):
     """
         Model for representing the access tokens
@@ -438,7 +438,7 @@ class AccessToken_v0(declarative_base()):
     request_token = Column(Unicode, ForeignKey(RequestToken_v0.token))
     created = Column(DateTime, nullable=False, default=datetime.datetime.now)
     updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
+
 
 class NonceTimestamp_v0(declarative_base()):
     """
@@ -460,3 +460,14 @@ def create_oauth1_tables(db):
     NonceTimestamp_v0.__table__.create(db.bind)
 
     db.commit()
+
+
+@RegisterMigration(15, MIGRATIONS)
+def add_file_metadata(db):
+    """Add file_metadata to MediaFile"""
+    metadata = MetaData(bind=db.bind)
+    media_file_table = inspect_table(metadata, "core__mediafiles")
+
+    col = Column('file_metadata', JSONEncoded)
+    col.create(media_file_table)
+    db.commit()
index 9cb39ff4821c4a8e622034d3a4341d0c2febec28..aa0c54d3310672b42198abb27db5048f033c89e5 100644 (file)
@@ -263,6 +263,34 @@ class MediaEntry(Base, MediaEntryMixin):
         if media is not None:
             return media.url_for_self(urlgen)
 
+    def get_file_metadata(self, file_key, metadata_key=None):
+        """
+        Return the file_metadata dict of a MediaFile. If metadata_key is given,
+        return the value of the key.
+        """
+        media_file = MediaFile.query.filter_by(media_entry=self.id,
+                                               name=file_key).first()
+
+        if media_file:
+            if metadata_key:
+                return media_file.file_metadata.get(metadata_key, None)
+
+            return media_file.file_metadata
+
+    def set_file_metadata(self, file_key, **kwargs):
+        """
+        Update the file_metadata of a MediaFile.
+        """
+        media_file = MediaFile.query.filter_by(media_entry=self.id,
+                                               name=file_key).first()
+
+        file_metadata = media_file.file_metadata or {}
+
+        for key, value in kwargs.iteritems():
+            file_metadata[key] = value
+
+        media_file.file_metadata = file_metadata
+
     @property
     def media_data(self):
         return getattr(self, self.media_data_ref)
@@ -359,6 +387,7 @@ class MediaFile(Base):
         nullable=False)
     name_id = Column(SmallInteger, ForeignKey(FileKeynames.id), nullable=False)
     file_path = Column(PathTupleWithSlashes)
+    file_metadata = Column(JSONEncoded)
 
     __table_args__ = (
         PrimaryKeyConstraint('media_entry', 'name_id'),