From bba44a3c7f39bc61b6e9845bbc8bae585b7b2f62 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 3 Jul 2018 13:15:17 +0200 Subject: [PATCH] Add user settings with time zones --- calsocial/__init__.py | 21 +++++++++++++++++++++ calsocial/forms.py | 24 ++++++++++++++++++++++++ calsocial/templates/base.html | 1 + calsocial/templates/user-settings.html | 18 ++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 calsocial/templates/user-settings.html diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 693a875..0bc5d08 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -183,5 +183,26 @@ class CalendarSocialApp(Flask): return render_template('event-edit.html', form=form) + @staticmethod + @route('/settings', methods=['GET', 'POST']) + @login_required + def settings(): + """View for user settings + """ + + from .forms import SettingsForm + from .models import db + + form = SettingsForm(current_user) + + if form.validate_on_submit(): + form.populate_obj(current_user) + + db.session.commit() + + return redirect(url_for('hello')) + + return render_template('user-settings.html', form=form) + app = CalendarSocialApp(__name__) diff --git a/calsocial/forms.py b/calsocial/forms.py index a5973eb..7e17a06 100644 --- a/calsocial/forms.py +++ b/calsocial/forms.py @@ -135,3 +135,27 @@ class EventForm(FlaskForm): if field.data < self.start_time.data: raise ValidationError(_('End time must be later than start time!')) + + +class SettingsForm(FlaskForm): + """Form for user settings + """ + + timezone = TimezoneField(_('Time zone'), validators=[DataRequired()]) + + def __init__(self, user, *args, **kwargs): + self.user = user + + kwargs.setdefault('timezone', user.timezone) + + FlaskForm.__init__(self, *args, **kwargs) + + def populate_obj(self, user): + """Populate ``obj`` with event data + """ + + for name, field in self._fields.items(): + if not (hasattr(self.__class__, name) and not hasattr(FlaskForm, name)): + continue + + user.settings[name] = str(field.data) diff --git a/calsocial/templates/base.html b/calsocial/templates/base.html index df70f3f..f06841c 100644 --- a/calsocial/templates/base.html +++ b/calsocial/templates/base.html @@ -38,6 +38,7 @@ {% else %}
  • {% trans %}Logout{% endtrans %}
  • {% trans %}Calendar view{% endtrans %}
  • +
  • {% trans %}Settings{% endtrans %}
  • {% endif %} diff --git a/calsocial/templates/user-settings.html b/calsocial/templates/user-settings.html new file mode 100644 index 0000000..5b10af8 --- /dev/null +++ b/calsocial/templates/user-settings.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} + +{% block content %} +
    + {{ form.hidden_tag() }} + + {{ form.errors }} +
    + + {{ form.timezone.errors }} + {{ form.timezone.label }} + {{ form.timezone}} +
    + + + Cancel +
    +{% endblock content %}