forked from gergely/calendar-social
Allow users to invite other users to events
This commit is contained in:
@@ -194,3 +194,68 @@ class LoginForm(BaseLoginForm):
|
||||
AuditLog.log(self.user, AuditLog.TYPE_LOGIN_FAIL)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
class ProfileField(StringField):
|
||||
"""Input field for profiles
|
||||
"""
|
||||
|
||||
def process_formdata(self, valuelist):
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from .models import User, Profile
|
||||
|
||||
if not valuelist:
|
||||
self.data = None
|
||||
|
||||
return
|
||||
|
||||
value = valuelist[0]
|
||||
|
||||
if value.startswith('@'):
|
||||
value = value[1:]
|
||||
|
||||
try:
|
||||
self.data = Profile.query.join(User).filter(User.username == value).one()
|
||||
except NoResultFound:
|
||||
self.data = None
|
||||
|
||||
raise ValueError('Unknown user')
|
||||
|
||||
def _value(self):
|
||||
if self.data:
|
||||
return self.data.fqn
|
||||
|
||||
return ''
|
||||
|
||||
|
||||
class InviteForm(FlaskForm):
|
||||
"""Form for event invitations
|
||||
"""
|
||||
|
||||
invitee = ProfileField(validators=[DataRequired()])
|
||||
|
||||
def __init__(self, event, *args, **kwargs):
|
||||
FlaskForm.__init__(self, *args, **kwargs)
|
||||
|
||||
self.event = event
|
||||
|
||||
def validate_invitee(self, field):
|
||||
"""Validate the value of the invitee field
|
||||
|
||||
:raises ValidationError: If the given user is already invited
|
||||
"""
|
||||
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from .models import Invitation
|
||||
|
||||
try:
|
||||
Invitation.query \
|
||||
.filter(Invitation.event == self.event) \
|
||||
.filter(Invitation.invitee == field.data) \
|
||||
.one()
|
||||
|
||||
raise ValidationError(_('User is already invited'))
|
||||
except NoResultFound:
|
||||
pass
|
||||
|
Reference in New Issue
Block a user