From a862e6ca5df54d9538adae9aa5d3f326cfd08b04 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 16 Jul 2018 12:12:35 +0200 Subject: [PATCH] Add the Event.visibility field This shows if the event is visible to anyone or just people who are invited. The calendar view already respects this flag. --- calsocial/calendar_system/gregorian.py | 14 +++++++++-- calsocial/forms.py | 33 ++++++++++++++++++++++++++ calsocial/models.py | 20 ++++++++++++++++ calsocial/templates/event-edit.html | 1 + 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/calsocial/calendar_system/gregorian.py b/calsocial/calendar_system/gregorian.py index d1de8fb..3d1fab9 100644 --- a/calsocial/calendar_system/gregorian.py +++ b/calsocial/calendar_system/gregorian.py @@ -199,12 +199,14 @@ class GregorianCalendar(CalendarSystem): """Returns all events for a given day """ - from ..models import Event, Profile + from ..models import Event, EventVisibility, Invitation, Profile, Response events = Event.query if user: - events = events.join(Profile) \ + events = events.outerjoin(Invitation) \ + .outerjoin(Response) \ + .join(Profile, Event.profile) \ .filter(Profile.user == user) start_timestamp = date.replace(hour=0, minute=0, second=0, microsecond=0) @@ -216,4 +218,12 @@ class GregorianCalendar(CalendarSystem): (Event.end_time < end_timestamp))) \ .order_by('start_time', 'end_time') + if user is None: + events = events.filter(Event.visibility == EventVisibility.public) + else: + events = events.filter((Event.visibility == EventVisibility.public) | + (Event.profile == user.profile) | + (Invitation.invitee == user.profile) | + (Response.profile == user.profile)) + return events diff --git a/calsocial/forms.py b/calsocial/forms.py index ac90df5..b072689 100644 --- a/calsocial/forms.py +++ b/calsocial/forms.py @@ -26,6 +26,8 @@ from wtforms.ext.dateutil.fields import DateTimeField from wtforms.validators import DataRequired, Email, StopValidation, ValidationError from wtforms.widgets import TextArea +from calsocial.models import EventVisibility, EVENT_VISIBILITY_TRANSLATIONS + class UsernameAvailable(object): # pylint: disable=too-few-public-methods """Checks if a username is available @@ -169,6 +171,36 @@ class TimezoneField(SelectField): yield (value, label, value == self.data) +class EnumField(SelectField): + def __init__(self, enum_type, translations, *args, **kwargs): + kwargs.update({'choices': [(value, None) for value in enum_type]}) + self.data = None + self.enum_type = enum_type + self.translations = translations + SelectField.__init__(self, *args, **kwargs) + + def process_formdata(self, valuelist): + if not valuelist: + self.data = None + + return + + try: + self.data = self.enum_type[valuelist[0]] + except KeyError: + raise ValueError('Unknown value') + + def iter_choices(self): + for value in self.enum_type: + label = self.translations[value] if self.translations else value.name + + yield ( + value.name, + self.gettext(self.translations[value]), + value == self.data + ) + + class EventForm(FlaskForm): """Form for event creation/editing """ @@ -179,6 +211,7 @@ class EventForm(FlaskForm): end_time = DateTimeField(_('End time'), validators=[DataRequired()]) all_day = BooleanField(_('All day')) description = StringField(_('Description'), widget=TextArea()) + visibility = EnumField(EventVisibility, EVENT_VISIBILITY_TRANSLATIONS, label=_('Visibility')) def populate_obj(self, obj): """Populate ``obj`` with event data diff --git a/calsocial/models.py b/calsocial/models.py index 9ac6220..fce8bcd 100644 --- a/calsocial/models.py +++ b/calsocial/models.py @@ -103,6 +103,23 @@ class ResponseType(Enum): return Enum.__eq__(self, other) +class EventVisibility(Enum): + """Enumeration for event visibility + """ + + #: The event is private, only attendees and people invited can see the details + private = 0 + + #: The event is public, anyone can see the details + public = 5 + + +EVENT_VISIBILITY_TRANSLATIONS = { + EventVisibility.private: _('Visible only to attendees'), + EventVisibility.public: _('Visible to everyone'), +} + + class SettingsProxy: """Proxy object to get settings for a user """ @@ -399,6 +416,9 @@ class Event(db.Model): #: The description of the event description = db.Column(db.UnicodeText()) + #: The visibility of the event + visibility = db.Column(db.Enum(EventVisibility), nullable=False) + def __as_tz(self, timestamp, as_timezone=None): from pytz import timezone, utc diff --git a/calsocial/templates/event-edit.html b/calsocial/templates/event-edit.html index 970d06f..5ed748f 100644 --- a/calsocial/templates/event-edit.html +++ b/calsocial/templates/event-edit.html @@ -19,6 +19,7 @@ {{ field(form.end_time) }} {{ field(form.all_day) }} {{ field(form.description) }} + {{ field(form.visibility) }} Cancel