Add migration to remove ActivityIntermediator
authorJessica Tallon <jessica@megworld.co.uk>
Tue, 28 Apr 2015 18:43:15 +0000 (20:43 +0200)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 26 May 2015 14:48:59 +0000 (16:48 +0200)
Migration to drop the table and removal of it from the model as it has
now been superseeded by the GenericForeignKey field.

mediagoblin/db/migrations.py
mediagoblin/db/models.py

index 8661c95a081a1c516743d1e1b4f7a4cdd8191267..70bf6234201eee608bedbbd0c41665cf137e80b3 100644 (file)
@@ -1398,8 +1398,17 @@ def rename_and_remove_object_and_target(db):
     # Commit the changes to the database.
     db.commit()
 
+@RegisterMigration(31, MIGRATIONS)
+def remove_activityintermediator(db):
+    """
+    This removes the old specific ActivityIntermediator model which has been
+    superseeded by the GenericForeignKey field.
+    """
+    metadata = MetaData(bind=db.bind)
 
+    # Drop the table
+    ai_table = inspect_table(metadata, "core__activity_intermediators")
+    ai_table.drop()
 
-
-
-
+    # Commit the changes
+    db.commit()
index b4cfe2a82622addcfdcaf41ca605eb951f14c3c7..a771ec2f1c3f9d5bfa03dfb10c6c32bc8fcd708e 100644 (file)
@@ -1372,62 +1372,6 @@ class Generator(Base):
         if "displayName" in data:
             self.name = data["displayName"]
 
-
-class ActivityIntermediator(Base):
-    """
-    This is used so that objects/targets can have a foreign key back to this
-    object and activities can a foreign key to this object. This objects to be
-    used multiple times for the activity object or target and also allows for
-    different types of objects to be used as an Activity.
-    """
-    __tablename__ = "core__activity_intermediators"
-
-    id = Column(Integer, primary_key=True)
-    type = Column(Unicode, nullable=False)
-
-    TYPES = {
-        "user": User,
-        "media": MediaEntry,
-        "comment": MediaComment,
-        "collection": Collection,
-    }
-
-    def _find_model(self, obj):
-        """ Finds the model for a given object """
-        for key, model in self.TYPES.items():
-            if isinstance(obj, model):
-                return key, model
-
-        return None, None
-
-    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")
-
-        self.type = key
-
-        # We need to populate the self.id so we need to save but, we don't
-        # want to save this AI in the database (yet) so commit=False.
-        self.save(commit=False)
-        obj.activity = self.id
-        obj.save()
-
-    def get(self):
-        """ Finds the object for an activity """
-        if self.type is None:
-            return None
-
-        model = self.TYPES[self.type]
-        return model.query.filter_by(activity=self.id).first()
-
-    @validates("type")
-    def validate_type(self, key, value):
-        """ Validate that the type set is a valid type """
-        assert value in self.TYPES
-        return value
-
 class Activity(Base, ActivityMixin):
     """
     This holds all the metadata about an activity such as uploading an image,