forked from gergely/calendar-social
Add user settings with time zones
This commit is contained in:
parent
d07e34a182
commit
bba44a3c7f
@ -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__)
|
||||
|
@ -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)
|
||||
|
@ -38,6 +38,7 @@
|
||||
{% else %}
|
||||
<li><a href="{{ url_for('security.logout') }}">{% trans %}Logout{% endtrans %}</a></li>
|
||||
<li><a href="{{ url_for('hello') }}">{% trans %}Calendar view{% endtrans %}</a></li>
|
||||
<li><a href="{{ url_for('settings') }}">{% trans %}Settings{% endtrans %}</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</nav>
|
||||
|
18
calsocial/templates/user-settings.html
Normal file
18
calsocial/templates/user-settings.html
Normal file
@ -0,0 +1,18 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
{{ form.hidden_tag() }}
|
||||
|
||||
{{ form.errors }}
|
||||
<br>
|
||||
|
||||
{{ form.timezone.errors }}
|
||||
{{ form.timezone.label }}
|
||||
{{ form.timezone}}
|
||||
<br>
|
||||
|
||||
<button type="submit">{% trans %}Save{% endtrans %}</button>
|
||||
<a href="{{ url_for('hello') }}">Cancel</a>
|
||||
</form>
|
||||
{% endblock content %}
|
Loading…
Reference in New Issue
Block a user