Only have Model.activity for activity compatable objects/targets
authorJessica Tallon <jessica@megworld.co.uk>
Wed, 3 Sep 2014 14:58:40 +0000 (15:58 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Wed, 3 Sep 2014 14:58:40 +0000 (15:58 +0100)
mediagoblin/db/migrations.py
mediagoblin/db/models.py

index aab01c12bdf9c174ec3f989ba0daa676365cc582..e4e5d9b8448ef83b6b53d56b5cf246fd8583f4c3 100644 (file)
@@ -901,14 +901,13 @@ class Generator_R0(declarative_base()):
 class Activity_R0(declarative_base()):
     __tablename__ = "core__activities"
     id = Column(Integer, primary_key=True)
-    actor = Column(Integer, ForeignKey(User.id), nullable=False)
+    actor = Column(Integer, ForeignKey("core__users.id"), nullable=False)
     published = Column(DateTime, nullable=False, default=datetime.datetime.now)
     updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
     verb = Column(Unicode, nullable=False)
     content = Column(Unicode, nullable=True)
     title = Column(Unicode, nullable=True)
-    target = Column(Integer, ForeignKey(User.id), nullable=True)
-    generator = Column(Integer, ForeignKey(Generator.id), nullable=True)
+    generator = Column(Integer, ForeignKey("core__generators.id"), nullable=True)
     object = Column(Integer,
                     ForeignKey("core__activity_intermediators.id"),
                     nullable=False)
@@ -933,7 +932,6 @@ def activity_migration(db):
     # Set constants we'll use later
     FOREIGN_KEY = "core__activity_intermediators.id"
 
-
     # Create the new tables.
     ActivityIntermediator_R0.__table__.create(db.bind)
     Generator_R0.__table__.create(db.bind)
@@ -967,25 +965,17 @@ def activity_migration(db):
 
 
     # Now we want to modify the tables which MAY have an activity at some point
-    as_object = Column("activity_as_object", Integer, ForeignKey(FOREIGN_KEY))
-    as_object.create(media_entry_table)
-    as_target = Column("activity_as_target", Integer, ForeignKey(FOREIGN_KEY))
-    as_target.create(media_entry_table)
-
-    as_object = Column("activity_as_object", Integer, ForeignKey(FOREIGN_KEY))
-    as_object.create(user_table)
-    as_target = Column("activity_as_target", Integer, ForeignKey(FOREIGN_KEY))
-    as_target.create(user_table)
-
-    as_object = Column("activity_as_object", Integer, ForeignKey(FOREIGN_KEY))
-    as_object.create(media_comments_table)
-    as_target = Column("activity_as_target", Integer, ForeignKey(FOREIGN_KEY))
-    as_target.create(media_comments_table)
-
-    as_object = Column("activity_as_object", Integer, ForeignKey(FOREIGN_KEY))
-    as_object.create(collection_table)
-    as_target = Column("activity_as_target", Integer, ForeignKey(FOREIGN_KEY))
-    as_target.create(collection_table)
+    media_col = Column("activity", Integer, ForeignKey(FOREIGN_KEY))
+    media_col.create(media_entry_table)
+
+    user_col = Column("activity", Integer, ForeignKey(FOREIGN_KEY))
+    user_col.create(user_table)
+
+    comments_col = Column("activity", Integer, ForeignKey(FOREIGN_KEY))
+    comments_col.create(media_comments_table)
+
+    collection_col = Column("activity", Integer, ForeignKey(FOREIGN_KEY))
+    collection_col.create(collection_table)
     db.commit()
 
 
index 5bd40c2a6e75b6272fb9ddf021d71466324ca3cb..65096500f05db1e5dd81bdc4747ac8198231fcdb 100644 (file)
@@ -77,10 +77,7 @@ class User(Base, UserMixin):
     uploaded = Column(Integer, default=0)
     upload_limit = Column(Integer)
 
-    activity_as_object = Column(Integer,
-                                ForeignKey("core__activity_intermediators.id"))
-    activity_as_target = Column(Integer,
-                                ForeignKey("core__activity_intermediators.id"))
+    activity = Column(Integer, ForeignKey("core__activity_intermediators.id"))
 
     ## TODO
     # plugin data would be in a separate model
@@ -316,10 +313,7 @@ class MediaEntry(Base, MediaEntryMixin):
     media_metadata = Column(MutationDict.as_mutable(JSONEncoded),
         default=MutationDict())
 
-    activity_as_object = Column(Integer,
-                                ForeignKey("core__activity_intermediators.id"))
-    activity_as_target = Column(Integer,
-                                ForeignKey("core__activity_intermediators.id"))
+    activity = Column(Integer, ForeignKey("core__activity_intermediators.id"))
 
     ## TODO
     # fail_error
@@ -659,10 +653,7 @@ class MediaComment(Base, MediaCommentMixin):
                                                    cascade="all, delete-orphan"))
 
 
-    activity_as_object = Column(Integer,
-                                ForeignKey("core__activity_intermediators.id"))
-    activity_as_target = Column(Integer,
-                                ForeignKey("core__activity_intermediators.id"))
+    activity = Column(Integer, ForeignKey("core__activity_intermediators.id"))
 
     def serialize(self, request):
         """ Unserialize to python dictionary for API """
@@ -728,10 +719,7 @@ class Collection(Base, CollectionMixin):
                                backref=backref("collections",
                                                cascade="all, delete-orphan"))
 
-    activity_as_object = Column(Integer,
-                                ForeignKey("core__activity_intermediators.id"))
-    activity_as_target = Column(Integer,
-                                ForeignKey("core__activity_intermediators.id"))
+    activity = Column(Integer, ForeignKey("core__activity_intermediators.id"))
 
     __table_args__ = (
         UniqueConstraint('creator', 'slug'),
@@ -1123,8 +1111,8 @@ class ActivityIntermediator(Base):
 
         return None, None
 
-    def set_object(self, obj):
-        """ This sets itself as the object for an activity """
+    def set(self, obj):
+        """ This sets itself as the activity """
         key, model = self._find_model(obj)
         if key is None:
             raise ValueError("Invalid type of object given")
@@ -1133,8 +1121,7 @@ class ActivityIntermediator(Base):
         obj.activity_as_object = self.id
         self.type = key
 
-    @property
-    def get_object(self):
+    def get(self):
         """ Finds the object for an activity """
         if self.type is None:
             return None
@@ -1142,24 +1129,6 @@ class ActivityIntermediator(Base):
         model = self.TYPES[self.type]
         return model.query.filter_by(activity_as_object=self.id).first()
 
-    def set_target(self, obj):
-        """ This sets itself as the target for an activity """
-        key, model = self._find_model(obj)
-        if key is None:
-            raise ValueError("Invalid type of object given")
-
-        obj.activity_as_target = self.id
-        self.type = key
-
-    @property
-    def get_target(self):
-        """ Gets the target for an activity """
-        if self.type is None:
-            return None
-
-        model = self.TYPES[self.type]
-        return model.query.filter_by(activity_as_target=self.id).first()
-
     def save(self, *args, **kwargs):
         if self.type not in self.TYPES.keys():
             raise ValueError("Invalid type set")
@@ -1195,40 +1164,43 @@ class Activity(Base, ActivityMixin):
         foreign_keys="Activity.actor", post_update=True)
     get_generator = relationship(Generator)
 
-    def set_object(self, *args, **kwargs):
-        if self.object is None:
-            ai = ActivityIntermediator()
-            ai.set_object(*args, **kwargs)
-            ai.save()
-            self.object = ai.id
-            return
-
-        ai = ActivityIntermediator.query.filter_by(id=self.object).first()
-        ai.set_object(*args, **kwargs)
-        ai.save()
-
     @property
     def get_object(self):
-        return self.object.get_object
+        if self.object is None:
+            return None
 
-    def set_target(self, *args, **kwargs):
-        if self.target is None:
-            ai = ActivityIntermediator()
-            ai.set_target(*args, **kwargs)
-            ai.save()
-            self.object = ai.id
-            return
+        ai = ActivityIntermediator.query.filter_by(id=self.object).first()
+        return ai.get()
 
-        ai = ActivityIntermediator.query.filter_by(id=self.target).first()
-        ai.set_object(*args, **kwargs)
-        ai.save()
+    def set_object(self, obj):
+        self.object = self._set_model(obj)
 
     @property
     def get_target(self):
         if self.target is None:
             return None
 
-        return self.target.get_target
+        ai = ActivityIntermediator.query.filter_by(id=self.target).first()
+        return ai.get()
+
+    def set_target(self, obj):
+        self.target = self._set_model(obj)
+
+    def _set_model(self, obj):
+        # Firstly can we set obj
+        if not hasattr(obj, "activity"):
+            raise ValueError(
+                "{0!r} is unable to be set on activity".format(obj))
+
+        if obj.activity is None:
+            # We need to create a new AI
+            ai = ActivityIntermediator()
+            ai.set(obj)
+            ai.save()
+            return ai.id
+
+        # Okay we should have an existing AI
+        return ActivityIntermediator.query.filter_by(id=obj.activity).first().id
 
     def save(self, set_updated=True, *args, **kwargs):
         if set_updated: