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):
"""
"""
# 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)
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")
# 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()
# 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",
"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()):
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",
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()
"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)
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
"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
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:
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
# 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()
_('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)
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:
% (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))