Add main_transcoding_progress column migration
[mediagoblin.git] / mediagoblin / db / models.py
index 97dc91dc96ee862918b67eceb17307e6d97ed385..b2dcb6ad32068a5cbdbec368ce678ebce55b0078 100644 (file)
@@ -25,7 +25,7 @@ import datetime
 
 from sqlalchemy import Column, Integer, Unicode, UnicodeText, DateTime, \
         Boolean, ForeignKey, UniqueConstraint, PrimaryKeyConstraint, \
-        SmallInteger, Date, types
+        SmallInteger, Date, types, Float
 from sqlalchemy.orm import relationship, backref, with_polymorphic, validates, \
         class_mapper
 from sqlalchemy.orm.collections import attribute_mapped_collection
@@ -542,7 +542,8 @@ class MediaEntry(Base, MediaEntryMixin, CommentingMixin):
     fail_error = Column(Unicode)
     fail_metadata = Column(JSONEncoded)
 
-    transcoding_progress = Column(SmallInteger)
+    transcoding_progress = Column(Float, default=0)
+    main_transcoding_progress = Column(Float, default=0)
 
     queued_media_file = Column(PathTupleWithSlashes)
 
@@ -586,6 +587,16 @@ class MediaEntry(Base, MediaEntryMixin, CommentingMixin):
     ## TODO
     # fail_error
 
+    @property
+    def get_uploader(self):
+        # for compatibility
+        return self.get_actor
+
+    @property
+    def uploader(self):
+        # for compatibility
+        return self.actor
+
     @property
     def collections(self):
         """ Get any collections that this MediaEntry is in """
@@ -607,10 +618,10 @@ class MediaEntry(Base, MediaEntryMixin, CommentingMixin):
         if ascending:
             query = query.order_by(Comment.added.asc())
         else:
-            qury = query.order_by(Comment.added.desc())
-        
-        return FakeCursor(query, lambda c:c.comment())
+            query = query.order_by(Comment.added.desc())
+
+        return query
+
     def url_to_prev(self, urlgen):
         """get the next 'newer' entry by this user"""
         media = MediaEntry.query.filter(
@@ -778,7 +789,7 @@ class MediaEntry(Base, MediaEntryMixin, CommentingMixin):
 
         if show_comments:
             comments = [
-                comment.serialize(request) for comment in self.get_comments()]
+                l.comment().serialize(request) for l in self.get_comments()]
             total = len(comments)
             context["replies"] = {
                 "totalItems": total,
@@ -941,7 +952,7 @@ class MediaTag(Base):
 class Comment(Base):
     """
     Link table between a response and another object that can have replies.
-    
+
     This acts as a link table between an object and the comments on it, it's
     done like this so that you can look up all the comments without knowing
     whhich comments are on an object before hand. Any object can be a comment
@@ -952,7 +963,7 @@ class Comment(Base):
     __tablename__ = "core__comment_links"
 
     id = Column(Integer, primary_key=True)
-    
+
     # The GMR to the object the comment is on.
     target_id = Column(
         Integer,
@@ -981,7 +992,25 @@ class Comment(Base):
 
     # When it was added
     added = Column(DateTime, nullable=False, default=datetime.datetime.utcnow)
-    
+
+    @property
+    def get_author(self):
+        # for compatibility
+        return self.comment().get_actor  # noqa
+
+    def __getattr__(self, attr):
+        if attr.startswith('_'):
+            # if attr starts with '_', then it's probably some internal
+            # sqlalchemy variable. Since __getattr__ is called when
+            # non-existing attributes are being accessed, we should not try to
+            # fetch it from self.comment()
+            raise AttributeError
+        try:
+            _log.debug('Old attr is being accessed: {0}'.format(attr))
+            return getattr(self.comment(), attr)  # noqa
+        except Exception as e:
+            _log.error(e)
+            raise
 
 class TextComment(Base, TextCommentMixin, CommentingMixin):
     """
@@ -1008,17 +1037,6 @@ class TextComment(Base, TextCommentMixin, CommentingMixin):
                                               cascade="all, delete-orphan"))
     deletion_mode = Base.SOFT_DELETE
 
-    def soft_delete(self, *args, **kwargs):
-        # Find the GMR for this model.
-        gmr = GenericModelReference.query.filter_by(
-            obj_pk=self.id,
-            model_type=self.__tablename__
-        ).first()
-
-        # Delete the Comment object for this comment
-        Comment.query.filter_by(comment_id=gmr.id).delete()
-        return super(TextComment, self).soft_delete(*args, **kwargs)
-
     def serialize(self, request):
         """ Unserialize to python dictionary for API """
         target = self.get_reply_to()
@@ -1026,7 +1044,7 @@ class TextComment(Base, TextCommentMixin, CommentingMixin):
         if target is None:
             target = {}
         else:
-            target = target.serialize(request, show_comments=False)        
+            target = target.serialize(request, show_comments=False)
 
 
         author = self.get_actor
@@ -1054,7 +1072,7 @@ class TextComment(Base, TextCommentMixin, CommentingMixin):
         if "location" in data:
             Location.create(data["location"], self)
 
-    
+
         # Handle changing the reply ID
         if "inReplyTo" in data:
             # Validate that the ID is correct
@@ -1085,7 +1103,7 @@ class TextComment(Base, TextCommentMixin, CommentingMixin):
             link.target = media
             link.comment = self
             link.save()
-        
+
         return True
 
 class Collection(Base, CollectionMixin, CommentingMixin):
@@ -1284,7 +1302,7 @@ class Notification(Base):
     seen = Column(Boolean, default=lambda: False, index=True)
     user = relationship(
         User,
-        backref=backref('notifications', cascade='all, delete-orphan')) 
+        backref=backref('notifications', cascade='all, delete-orphan'))
 
     def __repr__(self):
         return '<{klass} #{id}: {user}: {subject} ({seen})>'.format(
@@ -1329,7 +1347,7 @@ class Report(Base):
                                             which points to the reported object.
     """
     __tablename__ = 'core__reports'
-    
+
     id = Column(Integer, primary_key=True)
     reporter_id = Column(Integer, ForeignKey(User.id), nullable=False)
     reporter =  relationship(
@@ -1357,7 +1375,7 @@ class Report(Base):
 
     resolved = Column(DateTime)
     result = Column(UnicodeText)
-    
+
     object_id = Column(Integer, ForeignKey(GenericModelReference.id), nullable=True)
     object_helper = relationship(GenericModelReference)
     obj = association_proxy("object_helper", "get_object",