From b949201152b2ff3a5b072107ae903ddac309a530 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Fri, 22 Aug 2014 18:53:29 +0100 Subject: Create activity model and add activity creation This creates the Activity and Genrator models from the Activity Streams spec and. I then created a migration which retro-actively create activities for media uploaded and comments created. Through out the code I've added so automatically activties are created when a user peforms an action (uploading media, commenting, etc.). --- mediagoblin/db/migrations.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'mediagoblin/db/migrations.py') diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index 04588ad1..72f85369 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -579,6 +579,29 @@ PRIVILEGE_FOUNDATIONS_v0 = [{'privilege_name':u'admin'}, {'privilege_name':u'active'}] +class Activity_R0(declarative_base()): + __tablename__ = "core__activities" + id = Column(Integer, primary_key=True) + 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=False) + title = Column(Unicode, nullable=True) + target = Column(Integer, ForeignKey(User.id), nullable=True) + object_comment = Column(Integer, ForeignKey(MediaComment.id), nullable=True) + object_collection = Column(Integer, ForeignKey(Collection.id), nullable=True) + object_media = Column(Integer, ForeignKey(MediaEntry.id), nullable=True) + object_user = Column(Integer, ForeignKey(User.id), nullable=True) + +class Generator(declarative_base()): + __tablename__ = "core__generators" + id = Column(Integer, primary_key=True) + name = Column(Unicode, nullable=False) + published = Column(DateTime, nullable=False, default=datetime.datetime.now) + updated = Column(DateTime, nullable=False, default=datetime.datetime.now) + object_type = Column(Unicode, nullable=False) + # vR1 stands for "version Rename 1". This only exists because we need # to deal with dropping some booleans and it's otherwise impossible # with sqlite. @@ -890,3 +913,38 @@ def revert_username_index(db): db.rollback() db.commit() + +@RegisterMigration(24, MIGRATIONS) +def create_activity_table(db): + """ This will create the activity table """ + Activity_R0.__table__.create(db.bind) + Generator_R0.__table__.create(db.bind) + db.commit() + + # Create the GNU MediaGoblin generator + gmg_generator = Generator(name="GNU MediaGoblin", object_type="service") + gmg_generator.save() + + # Now we want to retroactively add what activities we can + # first we'll add activities when people uploaded media. + for media in MediaEntry.query.all(): + activity = Activity_R0( + verb="create", + actor=media.uploader, + published=media.created, + object_media=media.id, + ) + activity.generate_content() + activity.save() + + # Now we want to add all the comments people made + for comment in MediaComment.query.all(): + activity = Activity_R0( + verb="comment", + actor=comment.author, + published=comment.created, + ) + activity.generate_content() + activity.save() + + db.commit() -- cgit v1.2.3 From ce46470c02371ff92db2c0412af97dfef33e58ee Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Wed, 27 Aug 2014 14:34:07 +0100 Subject: Add ActivityIntermediator table and refactor some of Activity model - This has introduced a intermediatory table between object/target and the activity. This allows for multiple activities to be associated with one object/target. - This moves some of the methods off Activity model into a mixin which didn't need to interact with database things. - This also cleaned up the migrations as well as adding retroactive creation of activities for collection creation. --- mediagoblin/db/migrations.py | 145 ++++++++++++++++++++++++++++++++----------- 1 file changed, 108 insertions(+), 37 deletions(-) (limited to 'mediagoblin/db/migrations.py') diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index 72f85369..f467253f 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -29,7 +29,7 @@ from mediagoblin.db.extratypes import JSONEncoded, MutationDict from mediagoblin.db.migration_tools import ( RegisterMigration, inspect_table, replace_table_hack) from mediagoblin.db.models import (MediaEntry, Collection, MediaComment, User, - Privilege) + Privilege, Generator) from mediagoblin.db.extratypes import JSONEncoded, MutationDict @@ -578,30 +578,6 @@ PRIVILEGE_FOUNDATIONS_v0 = [{'privilege_name':u'admin'}, {'privilege_name':u'commenter'}, {'privilege_name':u'active'}] - -class Activity_R0(declarative_base()): - __tablename__ = "core__activities" - id = Column(Integer, primary_key=True) - 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=False) - title = Column(Unicode, nullable=True) - target = Column(Integer, ForeignKey(User.id), nullable=True) - object_comment = Column(Integer, ForeignKey(MediaComment.id), nullable=True) - object_collection = Column(Integer, ForeignKey(Collection.id), nullable=True) - object_media = Column(Integer, ForeignKey(MediaEntry.id), nullable=True) - object_user = Column(Integer, ForeignKey(User.id), nullable=True) - -class Generator(declarative_base()): - __tablename__ = "core__generators" - id = Column(Integer, primary_key=True) - name = Column(Unicode, nullable=False) - published = Column(DateTime, nullable=False, default=datetime.datetime.now) - updated = Column(DateTime, nullable=False, default=datetime.datetime.now) - object_type = Column(Unicode, nullable=False) - # vR1 stands for "version Rename 1". This only exists because we need # to deal with dropping some booleans and it's otherwise impossible # with sqlite. @@ -914,17 +890,91 @@ def revert_username_index(db): db.commit() +class Generator_R0(declarative_base()): + __tablename__ = "core__generators" + id = Column(Integer, primary_key=True) + name = Column(Unicode, nullable=False) + published = Column(DateTime, nullable=False, default=datetime.datetime.now) + updated = Column(DateTime, nullable=False, default=datetime.datetime.now) + object_type = Column(Unicode, nullable=False) + +class Activity_R0(declarative_base()): + __tablename__ = "core__activities" + id = Column(Integer, primary_key=True) + 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=False) + title = Column(Unicode, nullable=True) + target = Column(Integer, ForeignKey(User.id), nullable=True) + generator = Column(Integer, ForeignKey(Generator.id), nullable=True) + +class ActivityIntermediator_R0(declarative_base()): + __tablename__ = "core__acitivity_intermediators" + id = Column(Integer, primary_key=True) + type = Column(Integer, nullable=False) + @RegisterMigration(24, MIGRATIONS) -def create_activity_table(db): - """ This will create the activity table """ +def activity_migration(db): + """ + Creates everything to create activities in GMG + - Adds Activity, ActivityIntermediator and Generator table + - Creates GMG service generator for activities produced by the server + - Adds the activity_as_object and activity_as_target to objects/targets + - Retroactively adds activities for what we can acurately work out + """ + # Set constants we'll use later + FOREIGN_KEY = "core__acitivity_intermediators.id" + + + # Create the new tables. Activity_R0.__table__.create(db.bind) Generator_R0.__table__.create(db.bind) + ActivityIntermediator_R0.__table__.create(db.bind) db.commit() - - # Create the GNU MediaGoblin generator - gmg_generator = Generator(name="GNU MediaGoblin", object_type="service") - gmg_generator.save() - + + + # Initiate the tables we want to use later + metadata = MetaData(bind=db.bind) + user_table = inspect_table(metadata, "core__users") + generator_table = inspect_table(metadata, "core__generators") + collection_table = inspect_table(metadata, "core__collections") + media_entry_table = inspect_table(metadata, "core__media_entries") + media_comments_table = inspect_table(metadata, "core__media_comments") + + + # Create the foundations for Generator + db.execute(generator_table.insert().values( + name="GNU Mediagoblin", + object_type="service" + )) + db.commit() + + + # 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) + db.commit() + + # Now we want to retroactively add what activities we can # first we'll add activities when people uploaded media. for media in MediaEntry.query.all(): @@ -932,19 +982,40 @@ def create_activity_table(db): verb="create", actor=media.uploader, published=media.created, - object_media=media.id, + updated=media.created, + generator=gmg_generator.id ) activity.generate_content() - activity.save() - + activity.save(set_updated=False) + activity.set_object(media) + media.save() + # Now we want to add all the comments people made for comment in MediaComment.query.all(): activity = Activity_R0( verb="comment", actor=comment.author, published=comment.created, + updated=comment.created, + generator=gmg_generator.id + ) + activity.generate_content() + activity.save(set_updated=False) + activity.set_object(comment) + comment.save() + + # Create 'create' activities for all collections + for collection in Collection.query.all(): + activity = Activity_R0( + verb="create", + actor=collection.creator, + published=collection.created, + updated=collection.created, + generator=gmg_generator.id ) activity.generate_content() - activity.save() - + activity.save(set_updated=False) + activity.set_object(collection) + collection.save() + db.commit() -- cgit v1.2.3 From 0421fc5ee8e10606426a803b51bfe4333d3ab406 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Fri, 29 Aug 2014 13:49:48 +0100 Subject: Fix migrations and refactor object_type - Make changes to objectType to be more pythonic "object_type" - Move object_type to mixins rather than be on the models - Convert migrations to sqlalchemy core rather than ORM (fix) - Change TYPES to use descriptive strings rather than numbers --- mediagoblin/db/migrations.py | 148 ++++++++++++++++++++++++++++++------------- 1 file changed, 105 insertions(+), 43 deletions(-) (limited to 'mediagoblin/db/migrations.py') diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index f467253f..aab01c12 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -905,15 +905,21 @@ class Activity_R0(declarative_base()): 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=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) + object = Column(Integer, + ForeignKey("core__activity_intermediators.id"), + nullable=False) + target = Column(Integer, + ForeignKey("core__activity_intermediators.id"), + nullable=True) class ActivityIntermediator_R0(declarative_base()): - __tablename__ = "core__acitivity_intermediators" + __tablename__ = "core__activity_intermediators" id = Column(Integer, primary_key=True) - type = Column(Integer, nullable=False) + type = Column(Unicode, nullable=False) @RegisterMigration(24, MIGRATIONS) def activity_migration(db): @@ -925,32 +931,40 @@ def activity_migration(db): - Retroactively adds activities for what we can acurately work out """ # Set constants we'll use later - FOREIGN_KEY = "core__acitivity_intermediators.id" + FOREIGN_KEY = "core__activity_intermediators.id" # Create the new tables. - Activity_R0.__table__.create(db.bind) - Generator_R0.__table__.create(db.bind) ActivityIntermediator_R0.__table__.create(db.bind) + Generator_R0.__table__.create(db.bind) + 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") + activity_table = inspect_table(metadata, "core__activities") generator_table = inspect_table(metadata, "core__generators") collection_table = inspect_table(metadata, "core__collections") media_entry_table = inspect_table(metadata, "core__media_entries") media_comments_table = inspect_table(metadata, "core__media_comments") + ai_table = inspect_table(metadata, "core__activity_intermediators") # Create the foundations for Generator db.execute(generator_table.insert().values( name="GNU Mediagoblin", - object_type="service" + object_type="service", + published=datetime.datetime.now(), + updated=datetime.datetime.now() )) db.commit() + # Get the ID of that generator + gmg_generator = db.execute(generator_table.select( + generator_table.c.name==u"GNU Mediagoblin")).first() + # 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)) @@ -977,45 +991,93 @@ def activity_migration(db): # Now we want to retroactively add what activities we can # first we'll add activities when people uploaded media. - for media in MediaEntry.query.all(): - activity = Activity_R0( - verb="create", - actor=media.uploader, - published=media.created, - updated=media.created, - generator=gmg_generator.id - ) - activity.generate_content() - activity.save(set_updated=False) - activity.set_object(media) - media.save() + # these can't have content as it's not fesible to get the + # correct content strings. + for media in db.execute(media_entry_table.select()): + # Now we want to create the intermedaitory + db_ai = db.execute(ai_table.insert().values( + type="media", + )) + db_ai = db.execute(ai_table.select( + ai_table.c.id==db_ai.inserted_primary_key[0] + )).first() + + # Add the activity + activity = { + "verb": "create", + "actor": media.uploader, + "published": media.created, + "updated": media.created, + "generator": gmg_generator.id, + "object": db_ai.id + } + db.execute(activity_table.insert().values(**activity)) + + # Add the AI to the media. + db.execute(media_entry_table.update().values( + activity_as_object=db_ai.id + ).where(id=media.id)) # Now we want to add all the comments people made - for comment in MediaComment.query.all(): - activity = Activity_R0( - verb="comment", - actor=comment.author, - published=comment.created, - updated=comment.created, - generator=gmg_generator.id - ) - activity.generate_content() - activity.save(set_updated=False) - activity.set_object(comment) - comment.save() + 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)) + + # 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] + )) + + db.execute( + media_entry_table.update().values( + activity_as_target=db_ai_media.id + ).where(id=media_entry.id)) + + # Now create the AI for the comment + db_ai_comment = db.execute(ai_table.insert().values( + type="comment" + )) + + activity = { + "verb": "comment", + "actor": comment.author, + "published": comment.created, + "updated": comment.created, + "generator": gmg_generator.id, + "object": db_ai_comment.id, + "target": db_ai_media.id, + } + + # Now add the comment object + db.execute(media_comments_table.insert().values(**activity)) # Create 'create' activities for all collections - for collection in Collection.query.all(): - activity = Activity_R0( - verb="create", - actor=collection.creator, - published=collection.created, - updated=collection.created, - generator=gmg_generator.id - ) - activity.generate_content() - activity.save(set_updated=False) - activity.set_object(collection) - collection.save() + for collection in db.execute(collection_table.select()): + # create AI + db_ai = db.execute(ai_table.insert().values( + type="collection" + )) + + # 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 = { + "verb": "create", + "actor": collection.creator, + "published": collection.created, + "updated": collection.created, + "generator": gmg_generator.id, + "object": db_ai.id, + } + + db.execute(activity_table.insert().values(**activity)) + + db.commit() -- cgit v1.2.3 From b61519ce53d9839c4277132967b2073ad6299daf Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Wed, 3 Sep 2014 15:58:40 +0100 Subject: Only have Model.activity for activity compatable objects/targets --- mediagoblin/db/migrations.py | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) (limited to 'mediagoblin/db/migrations.py') diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index aab01c12..e4e5d9b8 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -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() -- cgit v1.2.3 From 6d36f75f845759da50b81f9cdeb9769fe97c2281 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Thu, 4 Sep 2014 19:12:48 +0100 Subject: Fix all the unit tests and clean up code --- mediagoblin/db/migrations.py | 74 +++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 32 deletions(-) (limited to 'mediagoblin/db/migrations.py') 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() -- cgit v1.2.3