From 9ace7627d5e9cd7ae314f0499b2d0f4a659111fb Mon Sep 17 00:00:00 2001 From: Vitor Freitas Date: Sun, 13 Jan 2019 21:03:23 +0200 Subject: Add user timezone. Update account settings pages. --- colossus/apps/accounts/forms.py | 21 +++++- colossus/apps/accounts/middleware.py | 19 ++++++ .../apps/accounts/migrations/0002_user_timezone.py | 18 +++++ colossus/apps/accounts/models.py | 3 + .../apps/accounts/templates/accounts/profile.html | 11 +++ .../apps/accounts/templates/registration/base.html | 18 +++++ .../templates/registration/logged_out.html | 31 +++++++++ .../accounts/templates/registration/login.html | 22 ++++++ .../registration/password_change_done.html | 8 +++ .../registration/password_change_form.html | 12 ++++ .../registration/password_reset_complete.html | 16 +++++ .../registration/password_reset_confirm.html | 30 +++++++++ .../registration/password_reset_done.html | 20 ++++++ .../registration/password_reset_email.html | 2 + .../registration/password_reset_form.html | 30 +++++++++ colossus/apps/accounts/tests/test_forms.py | 23 +++++++ colossus/apps/accounts/tests/test_views.py | 78 ++++++++++++++++++++++ colossus/apps/accounts/urls.py | 22 +++--- colossus/apps/accounts/views.py | 17 +++++ .../campaigns/schedule_campaign_form.html | 38 +++++++++++ colossus/apps/campaigns/views.py | 4 +- colossus/apps/core/templates/core/settings.html | 13 +++- colossus/apps/core/templates/core/site_form.html | 2 +- colossus/settings.py | 1 + colossus/templates/registration/base.html | 18 ----- colossus/templates/registration/logged_out.html | 31 --------- colossus/templates/registration/login.html | 22 ------ .../registration/password_change_done.html | 16 ----- .../registration/password_change_form.html | 20 ------ .../registration/password_reset_complete.html | 16 ----- .../registration/password_reset_confirm.html | 30 --------- .../registration/password_reset_done.html | 20 ------ .../registration/password_reset_email.html | 2 - .../registration/password_reset_form.html | 30 --------- 34 files changed, 444 insertions(+), 220 deletions(-) create mode 100644 colossus/apps/accounts/middleware.py create mode 100644 colossus/apps/accounts/migrations/0002_user_timezone.py create mode 100644 colossus/apps/accounts/templates/accounts/profile.html create mode 100644 colossus/apps/accounts/templates/registration/base.html create mode 100644 colossus/apps/accounts/templates/registration/logged_out.html create mode 100644 colossus/apps/accounts/templates/registration/login.html create mode 100644 colossus/apps/accounts/templates/registration/password_change_done.html create mode 100644 colossus/apps/accounts/templates/registration/password_change_form.html create mode 100644 colossus/apps/accounts/templates/registration/password_reset_complete.html create mode 100644 colossus/apps/accounts/templates/registration/password_reset_confirm.html create mode 100644 colossus/apps/accounts/templates/registration/password_reset_done.html create mode 100644 colossus/apps/accounts/templates/registration/password_reset_email.html create mode 100644 colossus/apps/accounts/templates/registration/password_reset_form.html create mode 100644 colossus/apps/accounts/tests/test_forms.py create mode 100644 colossus/apps/accounts/tests/test_views.py create mode 100644 colossus/apps/campaigns/templates/campaigns/schedule_campaign_form.html delete mode 100644 colossus/templates/registration/base.html delete mode 100644 colossus/templates/registration/logged_out.html delete mode 100644 colossus/templates/registration/login.html delete mode 100644 colossus/templates/registration/password_change_done.html delete mode 100644 colossus/templates/registration/password_change_form.html delete mode 100644 colossus/templates/registration/password_reset_complete.html delete mode 100644 colossus/templates/registration/password_reset_confirm.html delete mode 100644 colossus/templates/registration/password_reset_done.html delete mode 100644 colossus/templates/registration/password_reset_email.html delete mode 100644 colossus/templates/registration/password_reset_form.html diff --git a/colossus/apps/accounts/forms.py b/colossus/apps/accounts/forms.py index 015f188..42271ad 100644 --- a/colossus/apps/accounts/forms.py +++ b/colossus/apps/accounts/forms.py @@ -1,7 +1,10 @@ -from django.contrib.auth import get_user_model +from django import forms from django.contrib.auth.forms import UserCreationForm +from django.utils.translation import gettext_lazy as _ -User = get_user_model() +import pytz + +from .models import User class AdminUserCreationForm(UserCreationForm): @@ -16,3 +19,17 @@ class AdminUserCreationForm(UserCreationForm): if commit: user.save() return user + + +class UserForm(forms.ModelForm): + TIMEZONE_CHOICES = (('', '---------'),) + tuple(map(lambda tz: (tz, tz), pytz.common_timezones)) + + timezone = forms.ChoiceField( + choices=TIMEZONE_CHOICES, + required=False, + label=_('Timezone') + ) + + class Meta: + model = User + fields = ('first_name', 'last_name', 'email', 'timezone') diff --git a/colossus/apps/accounts/middleware.py b/colossus/apps/accounts/middleware.py new file mode 100644 index 0000000..744ae7c --- /dev/null +++ b/colossus/apps/accounts/middleware.py @@ -0,0 +1,19 @@ +from django.utils import timezone + +import pytz +from pytz import UnknownTimeZoneError + + +class UserTimezoneMiddleware: + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + if request.user.is_authenticated: + try: + timezone.activate(pytz.timezone(request.user.timezone)) + except UnknownTimeZoneError: + timezone.deactivate() + + response = self.get_response(request) + return response diff --git a/colossus/apps/accounts/migrations/0002_user_timezone.py b/colossus/apps/accounts/migrations/0002_user_timezone.py new file mode 100644 index 0000000..12020b1 --- /dev/null +++ b/colossus/apps/accounts/migrations/0002_user_timezone.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.5 on 2019-01-13 16:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='timezone', + field=models.CharField(blank=True, max_length=50), + ), + ] diff --git a/colossus/apps/accounts/models.py b/colossus/apps/accounts/models.py index 44eba0c..d4ef2b7 100644 --- a/colossus/apps/accounts/models.py +++ b/colossus/apps/accounts/models.py @@ -1,6 +1,9 @@ from django.contrib.auth.models import AbstractUser +from django.db import models class User(AbstractUser): + timezone = models.CharField(max_length=50, blank=True) + class Meta: db_table = 'auth_user' diff --git a/colossus/apps/accounts/templates/accounts/profile.html b/colossus/apps/accounts/templates/accounts/profile.html new file mode 100644 index 0000000..04af980 --- /dev/null +++ b/colossus/apps/accounts/templates/accounts/profile.html @@ -0,0 +1,11 @@ +{% extends 'core/settings.html' %} + +{% load crispy_forms_filters i18n %} + +{% block settingscontent %} +
+{% endblock %} diff --git a/colossus/apps/accounts/templates/registration/base.html b/colossus/apps/accounts/templates/registration/base.html new file mode 100644 index 0000000..4a2dd19 --- /dev/null +++ b/colossus/apps/accounts/templates/registration/base.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} + +{% block stylesheet %} + +{% endblock %} + +{% block body %} +{% trans 'Password changed with success!' %}
+{% endblock %} diff --git a/colossus/apps/accounts/templates/registration/password_change_form.html b/colossus/apps/accounts/templates/registration/password_change_form.html new file mode 100644 index 0000000..0a0eab3 --- /dev/null +++ b/colossus/apps/accounts/templates/registration/password_change_form.html @@ -0,0 +1,12 @@ +{% extends 'core/settings.html' %} + +{% load crispy_forms_filters i18n %} + +{% block settingscontent %} ++ Go to the log in page. +
+The link you are trying to use has either been used or expired.
++ Request a new password reset link. +
++ Please click on the link we just sent to your email address to confirm the password reset. +
+If you did not receive our email, you can request a new link.
+Password changed with success!
-- Go to the log in page. -
-The link you are trying to use has either been used or expired.
-- Request a new password reset link. -
-- Please click on the link we just sent to your email address to confirm the password reset. -
-If you did not receive our email, you can request a new link.
-