Fix all the unit tests and clean up code
authorJessica Tallon <jessica@megworld.co.uk>
Thu, 4 Sep 2014 18:12:48 +0000 (19:12 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Mon, 6 Oct 2014 10:48:03 +0000 (11:48 +0100)
mediagoblin/db/migrations.py
mediagoblin/db/mixin.py
mediagoblin/db/models.py
mediagoblin/submit/lib.py
mediagoblin/templates/mediagoblin/federation/activity.html
mediagoblin/tools/federation.py
mediagoblin/user_pages/views.py

index e4e5d9b8448ef83b6b53d56b5cf246fd8583f4c3..c1a73795bdcb41d21c81c27c71847e1346bdec7e 100644 (file)
@@ -898,28 +898,28 @@ class Generator_R0(declarative_base()):
     updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
     object_type = Column(Unicode, nullable=False)
 
+class ActivityIntermediator_R0(declarative_base()):
+    __tablename__ = "core__activity_intermediators"
+    id = Column(Integer, primary_key=True)
+    type = Column(Unicode, nullable=False)
+
 class Activity_R0(declarative_base()):
     __tablename__ = "core__activities"
     id = Column(Integer, primary_key=True)
-    actor = Column(Integer, ForeignKey("core__users.id"), nullable=False)
+    actor = Column(Integer, ForeignKey(User.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)
-    generator = Column(Integer, ForeignKey("core__generators.id"), nullable=True)
+    generator = Column(Integer, ForeignKey(Generator_R0.id), nullable=True)
     object = Column(Integer,
-                    ForeignKey("core__activity_intermediators.id"),
+                    ForeignKey(ActivityIntermediator_R0.id),
                     nullable=False)
     target = Column(Integer,
-                    ForeignKey("core__activity_intermediators.id"),
+                    ForeignKey(ActivityIntermediator_R0.id),
                     nullable=True)
 
-class ActivityIntermediator_R0(declarative_base()):
-    __tablename__ = "core__activity_intermediators"
-    id = Column(Integer, primary_key=True)
-    type = Column(Unicode, nullable=False)
-
 @RegisterMigration(24, MIGRATIONS)
 def activity_migration(db):
     """
@@ -931,6 +931,7 @@ def activity_migration(db):
     """
     # Set constants we'll use later
     FOREIGN_KEY = "core__activity_intermediators.id"
+    ACTIVITY_COLUMN = "activity"
 
     # Create the new tables.
     ActivityIntermediator_R0.__table__.create(db.bind)
@@ -938,7 +939,6 @@ def activity_migration(db):
     Activity_R0.__table__.create(db.bind)
     db.commit()
 
-
     # Initiate the tables we want to use later
     metadata = MetaData(bind=db.bind)
     user_table = inspect_table(metadata, "core__users")
@@ -965,16 +965,16 @@ def activity_migration(db):
 
 
     # Now we want to modify the tables which MAY have an activity at some point
-    media_col = Column("activity", Integer, ForeignKey(FOREIGN_KEY))
+    media_col = Column(ACTIVITY_COLUMN, Integer, ForeignKey(FOREIGN_KEY))
     media_col.create(media_entry_table)
 
-    user_col = Column("activity", Integer, ForeignKey(FOREIGN_KEY))
+    user_col = Column(ACTIVITY_COLUMN, Integer, ForeignKey(FOREIGN_KEY))
     user_col.create(user_table)
 
-    comments_col = Column("activity", Integer, ForeignKey(FOREIGN_KEY))
+    comments_col = Column(ACTIVITY_COLUMN, Integer, ForeignKey(FOREIGN_KEY))
     comments_col.create(media_comments_table)
 
-    collection_col = Column("activity", Integer, ForeignKey(FOREIGN_KEY))
+    collection_col = Column(ACTIVITY_COLUMN, Integer, ForeignKey(FOREIGN_KEY))
     collection_col.create(collection_table)
     db.commit()
 
@@ -1005,32 +1005,31 @@ def activity_migration(db):
 
         # Add the AI to the media.
         db.execute(media_entry_table.update().values(
-            activity_as_object=db_ai.id
-        ).where(id=media.id))
+            activity=db_ai.id
+        ).where(media_entry_table.c.id==media.id))
 
     # Now we want to add all the comments people made
     for comment in db.execute(media_comments_table.select()):
         # Get the MediaEntry for the comment
         media_entry = db.execute(
-            media_entry_table.select(id=comment.media_entry_id))
+            media_entry_table.select(
+                media_entry_table.c.id==comment.media_entry
+        )).first()
 
         # Create an AI for target
-        db_ai_media = db.execute(ai_table.insert().values(
-            type="media"
-        ))
         db_ai_media = db.execute(ai_table.select(
-            ai_table.c.id==db_ai_media.inserted_primary_key[0]
-        ))
+            ai_table.c.id==media_entry.activity
+        )).first().id
 
         db.execute(
-            media_entry_table.update().values(
-                activity_as_target=db_ai_media.id
-        ).where(id=media_entry.id))
+            media_comments_table.update().values(
+                activity=db_ai_media
+        ).where(media_comments_table.c.id==media_entry.id))
 
         # Now create the AI for the comment
         db_ai_comment = db.execute(ai_table.insert().values(
             type="comment"
-        ))
+        )).inserted_primary_key[0]
 
         activity = {
             "verb": "comment",
@@ -1038,12 +1037,17 @@ def activity_migration(db):
             "published": comment.created,
             "updated": comment.created,
             "generator": gmg_generator.id,
-            "object": db_ai_comment.id,
-            "target": db_ai_media.id,
+            "object": db_ai_comment,
+            "target": db_ai_media,
         }
 
         # Now add the comment object
-        db.execute(media_comments_table.insert().values(**activity))
+        db.execute(activity_table.insert().values(**activity))
+
+        # Now add activity to comment
+        db.execute(media_comments_table.update().values(
+            activity=db_ai_comment
+        ).where(media_comments_table.c.id==comment.id))
 
     # Create 'create' activities for all collections
     for collection in db.execute(collection_table.select()):
@@ -1051,11 +1055,14 @@ def activity_migration(db):
         db_ai = db.execute(ai_table.insert().values(
             type="collection"
         ))
+        db_ai = db.execute(ai_table.select(
+            ai_table.c.id==db_ai.inserted_primary_key[0]
+        )).first()
 
         # Now add link the collection to the AI
         db.execute(collection_table.update().values(
-            activity_as_object=db_ai.id
-        ).where(id=collection.id))
+            activity=db_ai.id
+        ).where(collection_table.c.id==collection.id))
 
         activity = {
             "verb": "create",
@@ -1068,6 +1075,9 @@ def activity_migration(db):
 
         db.execute(activity_table.insert().values(**activity))
 
-
+        # Now add the activity to the collection
+        db.execute(collection_table.update().values(
+            activity=db_ai.id
+        ).where(collection_table.c.id==collection.id))
 
     db.commit()
index 6a733510bffd42d6907be85a350213e588a1fb23..aec35fc777de74fe0436228ff476b1572555e374 100644 (file)
@@ -421,8 +421,8 @@ class ActivityMixin(object):
             "tag": {"simple": _("{username} tagged {object}")},
         }
 
-        obj = self.get_object()
-        target = self.get_target()
+        obj = self.get_object
+        target = self.get_target
         actor = self.get_actor
         content = verb_to_content.get(self.verb, None)
 
@@ -432,13 +432,13 @@ class ActivityMixin(object):
         if target is None or "targetted" not in content:
             self.content = content["simple"].format(
                 username=actor.username,
-                object=obj.objectType
+                object=obj.object_type
             )
         else:
             self.content = content["targetted"].format(
                 username=actor.username,
-                object=obj.objectType,
-                target=target.objectType,
+                object=obj.object_type,
+                target=target.object_type,
             )
 
         return self.content
@@ -452,19 +452,19 @@ class ActivityMixin(object):
             "updated": self.updated.isoformat(),
             "content": self.content,
             "url": self.get_url(request),
-            "object": self.get_object().serialize(request),
+            "object": self.get_object.serialize(request),
             "objectType": self.object_type,
         }
 
         if self.generator:
-            obj["generator"] = self.get_generator.seralize(request)
+            obj["generator"] = self.get_generator.serialize(request)
 
         if self.title:
             obj["title"] = self.title
 
-        target = self.get_target()
+        target = self.get_target
         if target is not None:
-            obj["target"] = target.seralize(request)
+            obj["target"] = target.serialize(request)
 
         return obj
 
index 65096500f05db1e5dd81bdc4747ac8198231fcdb..5f78275bde452e01b4632415375ec9067425b58b 100644 (file)
@@ -459,7 +459,7 @@ class MediaEntry(Base, MediaEntryMixin):
                 "self": {
                     "href": request.urlgen(
                         "mediagoblin.federation.object",
-                        object_type=self.objectType,
+                        object_type=self.object_type,
                         id=self.id,
                         qualified=True
                     ),
@@ -1127,7 +1127,7 @@ class ActivityIntermediator(Base):
             return None
 
         model = self.TYPES[self.type]
-        return model.query.filter_by(activity_as_object=self.id).first()
+        return model.query.filter_by(activity=self.id).first()
 
     def save(self, *args, **kwargs):
         if self.type not in self.TYPES.keys():
index af25bfb7a8c743cb7389f701812e2e9a63dc8cee..347eff81f42e65b2dff19dd867396bab0965c1d1 100644 (file)
@@ -200,9 +200,10 @@ def submit_media(mg_app, user, submitted_file, filename,
     run_process_media(entry, feed_url)
 
     add_comment_subscription(user, entry)
-    
+
     # Create activity
-    create_activity("post", entry)
+    entry.activity = create_activity("post", entry, entry.uploader).id
+    entry.save()
 
     return entry
 
@@ -293,8 +294,9 @@ def api_add_to_feed(request, entry):
 
     run_process_media(entry, feed_url)
     add_comment_subscription(request.user, entry)
-    
+
     # Create activity
-    create_activity("post", entry)
+    entry.activity = create_activity("post", entry, entry.uploader).id
+    entry.save()
 
     return json_response(entry.serialize(request))
index f380fd5fb2d1f85dbd46338b712d34bad10861fe..14377a4842e50b53ca173bc9d0f83f9b849ea816 100644 (file)
   {% autoescape False %}
     <p> {{ activity.content }} </p>
   {% endautoescape %}
-  
+
   <div class="media_sidebar">
     {% block mediagoblin_after_added_sidebar %}
       <a href="{{ activity.url(request) }}"
          class="button_action"
          id="button_reportmedia">
-            View {{ activity.object.objectType }}
+            View {{ activity.get_object.object_type }}
       </a>
     {% endblock %}
   </div>
index 573b059a3e6a5561b22c0e8a36ce0051e2daa8cb..281994cc6228a792281a9f6eb791adac267c3a56 100644 (file)
@@ -17,7 +17,7 @@
 from mediagoblin.db.models import Activity, Generator, User, MediaEntry, \
                                   MediaComment, Collection
 
-def create_activity(verb, obj, target=None, actor=None):
+def create_activity(verb, obj, actor, target=None):
     """
     This will create an Activity object which for the obj if possible
     and save it. The verb should be one of the following:
@@ -27,9 +27,6 @@ def create_activity(verb, obj, target=None, actor=None):
 
     If none of those fit you might not want/need to create an activity for
     the object. The list is in mediagoblin.db.models.Activity.VALID_VERBS
-
-    If no actor is supplied it'll take the actor/author/uploader/etc. from
-    the object if possible, else raise a ValueError
     """
     # exception when we try and generate an activity with an unknow verb
     # could change later to allow arbitrary verbs but at the moment we'll play
@@ -40,15 +37,21 @@ def create_activity(verb, obj, target=None, actor=None):
 
     # This should exist as we're creating it by the migration for Generator
     generator = Generator.query.filter_by(name="GNU MediaGoblin").first()
+    if generator is None:
+        generator = Generator(
+            name="GNU MediaGoblin",
+            object_type="service"
+        )
+        generator.save()
+
     activity = Activity(verb=verb)
     activity.set_object(obj)
 
     if target is not None:
         activity.set_target(target)
 
-    if actor is not None:
-        # If they've set it override the actor from the obj.
-        activity.actor = actor.id if isinstance(actor, User) else actor
+   # If they've set it override the actor from the obj.
+    activity.actor = actor.id if isinstance(actor, User) else actor
 
     activity.generator = generator.id
     activity.save()
index cd0e843d8329d0f385134393379f0020a71fa80d..0d0f3e7b7dd2bd376d1d1d947a51f7fa7f1115a8 100644 (file)
@@ -200,7 +200,7 @@ def media_post_comment(request, media):
             _('Your comment has been posted!'))
 
         trigger_notification(comment, media, request)
-        create_activity("post", comment)
+        create_activity("post", comment, comment.author)
         add_comment_subscription(request.user, media)
 
     return redirect_obj(request, media)
@@ -262,7 +262,7 @@ def media_collect(request, media):
         collection.creator = request.user.id
         collection.generate_slug()
         collection.save()
-        create_activity("create", collection)
+        create_activity("create", collection, collection.creator)
 
     # Otherwise, use the collection selected from the drop-down
     else:
@@ -289,7 +289,7 @@ def media_collect(request, media):
                              % (media.title, collection.title))
     else: # Add item to collection
         add_media_to_collection(collection, media, form.note.data)
-        create_activity("add", media, target=collection)
+        create_activity("add", media, request.user, target=collection)
         messages.add_message(request, messages.SUCCESS,
                              _('"%s" added to collection "%s"')
                              % (media.title, collection.title))