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)
# 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)
# 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()
uploaded = Column(Integer, default=0)
upload_limit = Column(Integer)
- activity_as_object = Column(Integer,
- ForeignKey("core__activity_intermediators.id"))
- activity_as_target = Column(Integer,
- ForeignKey("core__activity_intermediators.id"))
+ activity = Column(Integer, ForeignKey("core__activity_intermediators.id"))
## TODO
# plugin data would be in a separate model
media_metadata = Column(MutationDict.as_mutable(JSONEncoded),
default=MutationDict())
- activity_as_object = Column(Integer,
- ForeignKey("core__activity_intermediators.id"))
- activity_as_target = Column(Integer,
- ForeignKey("core__activity_intermediators.id"))
+ activity = Column(Integer, ForeignKey("core__activity_intermediators.id"))
## TODO
# fail_error
cascade="all, delete-orphan"))
- activity_as_object = Column(Integer,
- ForeignKey("core__activity_intermediators.id"))
- activity_as_target = Column(Integer,
- ForeignKey("core__activity_intermediators.id"))
+ activity = Column(Integer, ForeignKey("core__activity_intermediators.id"))
def serialize(self, request):
""" Unserialize to python dictionary for API """
backref=backref("collections",
cascade="all, delete-orphan"))
- activity_as_object = Column(Integer,
- ForeignKey("core__activity_intermediators.id"))
- activity_as_target = Column(Integer,
- ForeignKey("core__activity_intermediators.id"))
+ activity = Column(Integer, ForeignKey("core__activity_intermediators.id"))
__table_args__ = (
UniqueConstraint('creator', 'slug'),
return None, None
- def set_object(self, obj):
- """ This sets itself as the object for an activity """
+ def set(self, obj):
+ """ This sets itself as the activity """
key, model = self._find_model(obj)
if key is None:
raise ValueError("Invalid type of object given")
obj.activity_as_object = self.id
self.type = key
- @property
- def get_object(self):
+ def get(self):
""" Finds the object for an activity """
if self.type is None:
return None
model = self.TYPES[self.type]
return model.query.filter_by(activity_as_object=self.id).first()
- def set_target(self, obj):
- """ This sets itself as the target for an activity """
- key, model = self._find_model(obj)
- if key is None:
- raise ValueError("Invalid type of object given")
-
- obj.activity_as_target = self.id
- self.type = key
-
- @property
- def get_target(self):
- """ Gets the target for an activity """
- if self.type is None:
- return None
-
- model = self.TYPES[self.type]
- return model.query.filter_by(activity_as_target=self.id).first()
-
def save(self, *args, **kwargs):
if self.type not in self.TYPES.keys():
raise ValueError("Invalid type set")
foreign_keys="Activity.actor", post_update=True)
get_generator = relationship(Generator)
- def set_object(self, *args, **kwargs):
- if self.object is None:
- ai = ActivityIntermediator()
- ai.set_object(*args, **kwargs)
- ai.save()
- self.object = ai.id
- return
-
- ai = ActivityIntermediator.query.filter_by(id=self.object).first()
- ai.set_object(*args, **kwargs)
- ai.save()
-
@property
def get_object(self):
- return self.object.get_object
+ if self.object is None:
+ return None
- def set_target(self, *args, **kwargs):
- if self.target is None:
- ai = ActivityIntermediator()
- ai.set_target(*args, **kwargs)
- ai.save()
- self.object = ai.id
- return
+ ai = ActivityIntermediator.query.filter_by(id=self.object).first()
+ return ai.get()
- ai = ActivityIntermediator.query.filter_by(id=self.target).first()
- ai.set_object(*args, **kwargs)
- ai.save()
+ def set_object(self, obj):
+ self.object = self._set_model(obj)
@property
def get_target(self):
if self.target is None:
return None
- return self.target.get_target
+ ai = ActivityIntermediator.query.filter_by(id=self.target).first()
+ return ai.get()
+
+ def set_target(self, obj):
+ self.target = self._set_model(obj)
+
+ def _set_model(self, obj):
+ # Firstly can we set obj
+ if not hasattr(obj, "activity"):
+ raise ValueError(
+ "{0!r} is unable to be set on activity".format(obj))
+
+ if obj.activity is None:
+ # We need to create a new AI
+ ai = ActivityIntermediator()
+ ai.set(obj)
+ ai.save()
+ return ai.id
+
+ # Okay we should have an existing AI
+ return ActivityIntermediator.query.filter_by(id=obj.activity).first().id
def save(self, set_updated=True, *args, **kwargs):
if set_updated: