[Refactor] Move invitation code to the Event model

This commit is contained in:
Gergely Polonkai 2018-07-12 11:08:13 +02:00
parent 36c2f0fd77
commit dc0b2954c1
2 changed files with 19 additions and 10 deletions

View File

@ -255,7 +255,7 @@ class CalendarSocialApp(Flask):
""" """
from .forms import InviteForm from .forms import InviteForm
from .models import db, Event, Invitation, Notification, NotificationAction from .models import db, Event
try: try:
event = Event.query.filter(Event.event_uuid == event_uuid).one() event = Event.query.filter(Event.event_uuid == event_uuid).one()
@ -267,15 +267,7 @@ class CalendarSocialApp(Flask):
form = InviteForm(event) form = InviteForm(event)
if form.validate_on_submit(): if form.validate_on_submit():
invite = Invitation(event=event, sender=current_user.profile) event.invite(current_user.profile, invitee=form.invitee.data)
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)
db.session.commit() db.session.commit()

View File

@ -417,6 +417,23 @@ class Event(db.Model):
return url_for('event_details', event_uuid=self.event_uuid) 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 class UserSetting(db.Model): # pylint: disable=too-few-public-methods
"""Database model for user settings """Database model for user settings