forked from gergely/calendar-social
[Refactor] Move notification creation to the Profile model
This commit is contained in:
parent
dc0b2954c1
commit
496b5b6c04
@ -326,14 +326,37 @@ class Profile(db.Model): # pylint: disable=too-few-public-methods
|
|||||||
|
|
||||||
user_follow = UserFollow(follower=follower, followed=self, accepted_at=datetime.utcnow())
|
user_follow = UserFollow(follower=follower, followed=self, accepted_at=datetime.utcnow())
|
||||||
db.session.add(user_follow)
|
db.session.add(user_follow)
|
||||||
notification = Notification(profile=self,
|
notification = self.notify(follower, self, NotificationAction.follow)
|
||||||
actor=follower,
|
|
||||||
item=self,
|
|
||||||
action=NotificationAction.follow)
|
|
||||||
db.session.add(notification)
|
db.session.add(notification)
|
||||||
|
|
||||||
return user_follow
|
return user_follow
|
||||||
|
|
||||||
|
def notify(self, actor, item, action):
|
||||||
|
"""Notify this profile about ``action`` on ``item`` by ``actor``
|
||||||
|
|
||||||
|
:param actor: the actor who generated the notification
|
||||||
|
:type actor: Profile
|
||||||
|
:param item: the item ``action`` was performed on
|
||||||
|
:type item: any
|
||||||
|
:param action: the type of the action
|
||||||
|
:type action: NotificationAction, str
|
||||||
|
:raises TypeError: if ``actor`` is not a `Profile` object
|
||||||
|
:returns: the generated notification. It is already added to the database session, but
|
||||||
|
not committed
|
||||||
|
:rtype: Notification
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not isinstance(actor, Profile):
|
||||||
|
raise TypeError('actor must be a Profile instance')
|
||||||
|
|
||||||
|
if isinstance(action, str):
|
||||||
|
action = NotificationAction[action]
|
||||||
|
|
||||||
|
notification = Notification(profile=self, actor=actor, item=item, action=action)
|
||||||
|
|
||||||
|
return notification
|
||||||
|
|
||||||
|
|
||||||
class Event(db.Model):
|
class Event(db.Model):
|
||||||
"""Database model for events
|
"""Database model for events
|
||||||
@ -426,10 +449,7 @@ class Event(db.Model):
|
|||||||
invite = Invitation(event=self, sender=inviter, invitee=invited)
|
invite = Invitation(event=self, sender=inviter, invitee=invited)
|
||||||
db.session.add(invite)
|
db.session.add(invite)
|
||||||
|
|
||||||
notification = Notification(profile=invited,
|
notification = invited.notify(inviter, self, NotificationAction.invite)
|
||||||
actor=inviter,
|
|
||||||
item=self,
|
|
||||||
action=NotificationAction.invite)
|
|
||||||
db.session.add(notification)
|
db.session.add(notification)
|
||||||
|
|
||||||
return invite
|
return invite
|
||||||
|
Loading…
Reference in New Issue
Block a user