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.
This commit is contained in:
Gergely Polonkai 2018-07-16 12:12:35 +02:00
parent f2f7ef72dd
commit a862e6ca5d
4 changed files with 66 additions and 2 deletions

View File

@ -199,12 +199,14 @@ class GregorianCalendar(CalendarSystem):
"""Returns all events for a given day """Returns all events for a given day
""" """
from ..models import Event, Profile from ..models import Event, EventVisibility, Invitation, Profile, Response
events = Event.query events = Event.query
if user: if user:
events = events.join(Profile) \ events = events.outerjoin(Invitation) \
.outerjoin(Response) \
.join(Profile, Event.profile) \
.filter(Profile.user == user) .filter(Profile.user == user)
start_timestamp = date.replace(hour=0, minute=0, second=0, microsecond=0) start_timestamp = date.replace(hour=0, minute=0, second=0, microsecond=0)
@ -216,4 +218,12 @@ class GregorianCalendar(CalendarSystem):
(Event.end_time < end_timestamp))) \ (Event.end_time < end_timestamp))) \
.order_by('start_time', 'end_time') .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 return events

View File

@ -26,6 +26,8 @@ from wtforms.ext.dateutil.fields import DateTimeField
from wtforms.validators import DataRequired, Email, StopValidation, ValidationError from wtforms.validators import DataRequired, Email, StopValidation, ValidationError
from wtforms.widgets import TextArea from wtforms.widgets import TextArea
from calsocial.models import EventVisibility, EVENT_VISIBILITY_TRANSLATIONS
class UsernameAvailable(object): # pylint: disable=too-few-public-methods class UsernameAvailable(object): # pylint: disable=too-few-public-methods
"""Checks if a username is available """Checks if a username is available
@ -169,6 +171,36 @@ class TimezoneField(SelectField):
yield (value, label, value == self.data) 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): class EventForm(FlaskForm):
"""Form for event creation/editing """Form for event creation/editing
""" """
@ -179,6 +211,7 @@ class EventForm(FlaskForm):
end_time = DateTimeField(_('End time'), validators=[DataRequired()]) end_time = DateTimeField(_('End time'), validators=[DataRequired()])
all_day = BooleanField(_('All day')) all_day = BooleanField(_('All day'))
description = StringField(_('Description'), widget=TextArea()) description = StringField(_('Description'), widget=TextArea())
visibility = EnumField(EventVisibility, EVENT_VISIBILITY_TRANSLATIONS, label=_('Visibility'))
def populate_obj(self, obj): def populate_obj(self, obj):
"""Populate ``obj`` with event data """Populate ``obj`` with event data

View File

@ -103,6 +103,23 @@ class ResponseType(Enum):
return Enum.__eq__(self, other) 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: class SettingsProxy:
"""Proxy object to get settings for a user """Proxy object to get settings for a user
""" """
@ -399,6 +416,9 @@ class Event(db.Model):
#: The description of the event #: The description of the event
description = db.Column(db.UnicodeText()) 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): def __as_tz(self, timestamp, as_timezone=None):
from pytz import timezone, utc from pytz import timezone, utc

View File

@ -19,6 +19,7 @@
{{ field(form.end_time) }} {{ field(form.end_time) }}
{{ field(form.all_day) }} {{ field(form.all_day) }}
{{ field(form.description) }} {{ field(form.description) }}
{{ field(form.visibility) }}
<button type="submit" class="ui primary button">{% trans %}Save{% endtrans %}</button> <button type="submit" class="ui primary button">{% trans %}Save{% endtrans %}</button>
<a href="{{ url_for('hello') }}" class="ui button">Cancel</a> <a href="{{ url_for('hello') }}" class="ui button">Cancel</a>