Fix all the unit tests and clean up code

This commit is contained in:
Jessica Tallon 2014-09-04 19:12:48 +01:00
parent b61519ce53
commit 6d36f75f84
7 changed files with 74 additions and 59 deletions

View File

@ -898,28 +898,28 @@ class Generator_R0(declarative_base()):
updated = Column(DateTime, nullable=False, default=datetime.datetime.now) updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
object_type = Column(Unicode, nullable=False) 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()): class Activity_R0(declarative_base()):
__tablename__ = "core__activities" __tablename__ = "core__activities"
id = Column(Integer, primary_key=True) 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) published = Column(DateTime, nullable=False, default=datetime.datetime.now)
updated = Column(DateTime, nullable=False, default=datetime.datetime.now) updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
verb = Column(Unicode, nullable=False) verb = Column(Unicode, nullable=False)
content = Column(Unicode, nullable=True) content = Column(Unicode, nullable=True)
title = 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, object = Column(Integer,
ForeignKey("core__activity_intermediators.id"), ForeignKey(ActivityIntermediator_R0.id),
nullable=False) nullable=False)
target = Column(Integer, target = Column(Integer,
ForeignKey("core__activity_intermediators.id"), ForeignKey(ActivityIntermediator_R0.id),
nullable=True) 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) @RegisterMigration(24, MIGRATIONS)
def activity_migration(db): def activity_migration(db):
""" """
@ -931,6 +931,7 @@ def activity_migration(db):
""" """
# Set constants we'll use later # Set constants we'll use later
FOREIGN_KEY = "core__activity_intermediators.id" FOREIGN_KEY = "core__activity_intermediators.id"
ACTIVITY_COLUMN = "activity"
# Create the new tables. # Create the new tables.
ActivityIntermediator_R0.__table__.create(db.bind) ActivityIntermediator_R0.__table__.create(db.bind)
@ -938,7 +939,6 @@ def activity_migration(db):
Activity_R0.__table__.create(db.bind) Activity_R0.__table__.create(db.bind)
db.commit() db.commit()
# Initiate the tables we want to use later # Initiate the tables we want to use later
metadata = MetaData(bind=db.bind) metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users") 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 # 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) 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) 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) 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) collection_col.create(collection_table)
db.commit() db.commit()
@ -1005,32 +1005,31 @@ def activity_migration(db):
# Add the AI to the media. # Add the AI to the media.
db.execute(media_entry_table.update().values( db.execute(media_entry_table.update().values(
activity_as_object=db_ai.id activity=db_ai.id
).where(id=media.id)) ).where(media_entry_table.c.id==media.id))
# Now we want to add all the comments people made # Now we want to add all the comments people made
for comment in db.execute(media_comments_table.select()): for comment in db.execute(media_comments_table.select()):
# Get the MediaEntry for the comment # Get the MediaEntry for the comment
media_entry = db.execute( 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 # Create an AI for target
db_ai_media = db.execute(ai_table.insert().values(
type="media"
))
db_ai_media = db.execute(ai_table.select( 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( db.execute(
media_entry_table.update().values( media_comments_table.update().values(
activity_as_target=db_ai_media.id activity=db_ai_media
).where(id=media_entry.id)) ).where(media_comments_table.c.id==media_entry.id))
# Now create the AI for the comment # Now create the AI for the comment
db_ai_comment = db.execute(ai_table.insert().values( db_ai_comment = db.execute(ai_table.insert().values(
type="comment" type="comment"
)) )).inserted_primary_key[0]
activity = { activity = {
"verb": "comment", "verb": "comment",
@ -1038,12 +1037,17 @@ def activity_migration(db):
"published": comment.created, "published": comment.created,
"updated": comment.created, "updated": comment.created,
"generator": gmg_generator.id, "generator": gmg_generator.id,
"object": db_ai_comment.id, "object": db_ai_comment,
"target": db_ai_media.id, "target": db_ai_media,
} }
# Now add the comment object # 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 # Create 'create' activities for all collections
for collection in db.execute(collection_table.select()): for collection in db.execute(collection_table.select()):
@ -1051,11 +1055,14 @@ def activity_migration(db):
db_ai = db.execute(ai_table.insert().values( db_ai = db.execute(ai_table.insert().values(
type="collection" 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 # Now add link the collection to the AI
db.execute(collection_table.update().values( db.execute(collection_table.update().values(
activity_as_object=db_ai.id activity=db_ai.id
).where(id=collection.id)) ).where(collection_table.c.id==collection.id))
activity = { activity = {
"verb": "create", "verb": "create",
@ -1068,6 +1075,9 @@ def activity_migration(db):
db.execute(activity_table.insert().values(**activity)) 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() db.commit()

View File

@ -421,8 +421,8 @@ class ActivityMixin(object):
"tag": {"simple": _("{username} tagged {object}")}, "tag": {"simple": _("{username} tagged {object}")},
} }
obj = self.get_object() obj = self.get_object
target = self.get_target() target = self.get_target
actor = self.get_actor actor = self.get_actor
content = verb_to_content.get(self.verb, None) content = verb_to_content.get(self.verb, None)
@ -432,13 +432,13 @@ class ActivityMixin(object):
if target is None or "targetted" not in content: if target is None or "targetted" not in content:
self.content = content["simple"].format( self.content = content["simple"].format(
username=actor.username, username=actor.username,
object=obj.objectType object=obj.object_type
) )
else: else:
self.content = content["targetted"].format( self.content = content["targetted"].format(
username=actor.username, username=actor.username,
object=obj.objectType, object=obj.object_type,
target=target.objectType, target=target.object_type,
) )
return self.content return self.content
@ -452,19 +452,19 @@ class ActivityMixin(object):
"updated": self.updated.isoformat(), "updated": self.updated.isoformat(),
"content": self.content, "content": self.content,
"url": self.get_url(request), "url": self.get_url(request),
"object": self.get_object().serialize(request), "object": self.get_object.serialize(request),
"objectType": self.object_type, "objectType": self.object_type,
} }
if self.generator: if self.generator:
obj["generator"] = self.get_generator.seralize(request) obj["generator"] = self.get_generator.serialize(request)
if self.title: if self.title:
obj["title"] = self.title obj["title"] = self.title
target = self.get_target() target = self.get_target
if target is not None: if target is not None:
obj["target"] = target.seralize(request) obj["target"] = target.serialize(request)
return obj return obj

View File

@ -459,7 +459,7 @@ class MediaEntry(Base, MediaEntryMixin):
"self": { "self": {
"href": request.urlgen( "href": request.urlgen(
"mediagoblin.federation.object", "mediagoblin.federation.object",
object_type=self.objectType, object_type=self.object_type,
id=self.id, id=self.id,
qualified=True qualified=True
), ),
@ -1127,7 +1127,7 @@ class ActivityIntermediator(Base):
return None return None
model = self.TYPES[self.type] 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): def save(self, *args, **kwargs):
if self.type not in self.TYPES.keys(): if self.type not in self.TYPES.keys():

View File

@ -202,7 +202,8 @@ def submit_media(mg_app, user, submitted_file, filename,
add_comment_subscription(user, entry) add_comment_subscription(user, entry)
# Create activity # Create activity
create_activity("post", entry) entry.activity = create_activity("post", entry, entry.uploader).id
entry.save()
return entry return entry
@ -295,6 +296,7 @@ def api_add_to_feed(request, entry):
add_comment_subscription(request.user, entry) add_comment_subscription(request.user, entry)
# Create activity # Create activity
create_activity("post", entry) entry.activity = create_activity("post", entry, entry.uploader).id
entry.save()
return json_response(entry.serialize(request)) return json_response(entry.serialize(request))

View File

@ -35,7 +35,7 @@
<a href="{{ activity.url(request) }}" <a href="{{ activity.url(request) }}"
class="button_action" class="button_action"
id="button_reportmedia"> id="button_reportmedia">
View {{ activity.object.objectType }} View {{ activity.get_object.object_type }}
</a> </a>
{% endblock %} {% endblock %}
</div> </div>

View File

@ -17,7 +17,7 @@
from mediagoblin.db.models import Activity, Generator, User, MediaEntry, \ from mediagoblin.db.models import Activity, Generator, User, MediaEntry, \
MediaComment, Collection 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 This will create an Activity object which for the obj if possible
and save it. The verb should be one of the following: 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 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 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 # 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 # could change later to allow arbitrary verbs but at the moment we'll play
@ -40,13 +37,19 @@ def create_activity(verb, obj, target=None, actor=None):
# This should exist as we're creating it by the migration for Generator # This should exist as we're creating it by the migration for Generator
generator = Generator.query.filter_by(name="GNU MediaGoblin").first() 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 = Activity(verb=verb)
activity.set_object(obj) activity.set_object(obj)
if target is not None: if target is not None:
activity.set_target(target) activity.set_target(target)
if actor is not None:
# If they've set it override the actor from the obj. # If they've set it override the actor from the obj.
activity.actor = actor.id if isinstance(actor, User) else actor activity.actor = actor.id if isinstance(actor, User) else actor

View File

@ -200,7 +200,7 @@ def media_post_comment(request, media):
_('Your comment has been posted!')) _('Your comment has been posted!'))
trigger_notification(comment, media, request) trigger_notification(comment, media, request)
create_activity("post", comment) create_activity("post", comment, comment.author)
add_comment_subscription(request.user, media) add_comment_subscription(request.user, media)
return redirect_obj(request, media) return redirect_obj(request, media)
@ -262,7 +262,7 @@ def media_collect(request, media):
collection.creator = request.user.id collection.creator = request.user.id
collection.generate_slug() collection.generate_slug()
collection.save() collection.save()
create_activity("create", collection) create_activity("create", collection, collection.creator)
# Otherwise, use the collection selected from the drop-down # Otherwise, use the collection selected from the drop-down
else: else:
@ -289,7 +289,7 @@ def media_collect(request, media):
% (media.title, collection.title)) % (media.title, collection.title))
else: # Add item to collection else: # Add item to collection
add_media_to_collection(collection, media, form.note.data) 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, messages.add_message(request, messages.SUCCESS,
_('"%s" added to collection "%s"') _('"%s" added to collection "%s"')
% (media.title, collection.title)) % (media.title, collection.title))