From dc0b2954c1147673754d4d480985be1e7f0128cb Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 12 Jul 2018 11:08:13 +0200 Subject: [PATCH] [Refactor] Move invitation code to the Event model --- calsocial/__init__.py | 12 ++---------- calsocial/models.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 6efa91d..49d515d 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -255,7 +255,7 @@ class CalendarSocialApp(Flask): """ from .forms import InviteForm - from .models import db, Event, Invitation, Notification, NotificationAction + from .models import db, Event try: event = Event.query.filter(Event.event_uuid == event_uuid).one() @@ -267,15 +267,7 @@ class CalendarSocialApp(Flask): form = InviteForm(event) if form.validate_on_submit(): - invite = Invitation(event=event, sender=current_user.profile) - form.populate_obj(invite) - db.session.add(invite) - - notification = Notification(profile=form.invitee.data, - actor=current_user.profile, - item=event, - action=NotificationAction.invite) - db.session.add(notification) + event.invite(current_user.profile, invitee=form.invitee.data) db.session.commit() diff --git a/calsocial/models.py b/calsocial/models.py index f310619..8c05c4a 100644 --- a/calsocial/models.py +++ b/calsocial/models.py @@ -417,6 +417,23 @@ class Event(db.Model): return url_for('event_details', event_uuid=self.event_uuid) + def invite(self, inviter, invited): + """Invite ``invited`` to the event + + The invitation will arrive from ``inviter``. + """ + + invite = Invitation(event=self, sender=inviter, invitee=invited) + db.session.add(invite) + + notification = Notification(profile=invited, + actor=inviter, + item=self, + action=NotificationAction.invite) + db.session.add(notification) + + return invite + class UserSetting(db.Model): # pylint: disable=too-few-public-methods """Database model for user settings