From 6d36f75f845759da50b81f9cdeb9769fe97c2281 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Thu, 4 Sep 2014 19:12:48 +0100 Subject: [PATCH] Fix all the unit tests and clean up code --- mediagoblin/db/migrations.py | 74 +++++++++++-------- mediagoblin/db/mixin.py | 18 ++--- mediagoblin/db/models.py | 4 +- mediagoblin/submit/lib.py | 10 ++- .../mediagoblin/federation/activity.html | 4 +- mediagoblin/tools/federation.py | 17 +++-- mediagoblin/user_pages/views.py | 6 +- 7 files changed, 74 insertions(+), 59 deletions(-) diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index e4e5d9b8..c1a73795 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -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() diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index 6a733510..aec35fc7 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -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 diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index 65096500..5f78275b 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -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(): diff --git a/mediagoblin/submit/lib.py b/mediagoblin/submit/lib.py index af25bfb7..347eff81 100644 --- a/mediagoblin/submit/lib.py +++ b/mediagoblin/submit/lib.py @@ -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)) diff --git a/mediagoblin/templates/mediagoblin/federation/activity.html b/mediagoblin/templates/mediagoblin/federation/activity.html index f380fd5f..14377a48 100644 --- a/mediagoblin/templates/mediagoblin/federation/activity.html +++ b/mediagoblin/templates/mediagoblin/federation/activity.html @@ -29,13 +29,13 @@ {% autoescape False %}

{{ activity.content }}

{% endautoescape %} - +
{% block mediagoblin_after_added_sidebar %} - View {{ activity.object.objectType }} + View {{ activity.get_object.object_type }} {% endblock %}
diff --git a/mediagoblin/tools/federation.py b/mediagoblin/tools/federation.py index 573b059a..281994cc 100644 --- a/mediagoblin/tools/federation.py +++ b/mediagoblin/tools/federation.py @@ -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() diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index cd0e843d..0d0f3e7b 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -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)) -- 2.25.1