Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion calliope_app/account/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from pytz import common_timezones
from django import forms
from django.contrib.auth import password_validation, authenticate
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

from api.models.engage import User_Profile
from account.validators import email_validator, unicode_email_validator, unicode_chars_validator

Expand Down Expand Up @@ -127,3 +127,65 @@ def get_invalid_login_error(self):
code="invalid_login",
params={"username": "email"},
)


class UserSettingsChangeForm(forms.Form):
first_name = forms.CharField(
label=_("First Name"),
max_length=30,
required=True
)
last_name = forms.CharField(
label=_("Last Name"),
max_length=30,
required=True
)
username = forms.CharField(
label=_("Email"),
max_length=30,
required=True
)
organization = forms.CharField(
label=_("Organization"),
max_length=255,
required=False,
)
timezone = forms.ChoiceField(
label=_("Time Zone"),
choices=[(tz, tz) for tz in common_timezones],
required=True,
)

def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = user

# Pre-fill data from User model
self.fields['first_name'].initial = user.first_name
self.fields['last_name'].initial = user.last_name
self.fields['username'].initial = user.username

# Pre-fill data from User_Profile model, if it exists
try:
user_profile = user.user_profile
self.fields['organization'].initial = user_profile.organization
self.fields['timezone'].initial = user_profile.timezone
except User_Profile.DoesNotExist:
self.fields['organization'].initial = ''
self.fields['timezone'].initial = ''

def save(self, commit=True):
user = User.objects.get(pk=self.user.pk)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.username = self.cleaned_data['username'].lower()
if commit:
user.save()
profile = User_Profile.objects.get(user=user)
profile.user = user
profile.organization = self.cleaned_data['organization']
profile.timezone = self.cleaned_data['timezone']

if commit:
profile.save()
return user
6 changes: 6 additions & 0 deletions calliope_app/account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
views.password_view,
name='password'
),
path(
'settings/user/',
views.user_view,
name='password'
),

path(
'user_activation/<activation_uuid>',
views.user_activation,
Expand Down
34 changes: 33 additions & 1 deletion calliope_app/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.urls import reverse
from django.views.decorators.csrf import csrf_protect

from account.forms import UserRegistrationForm, UserAuthenticationForm
from account.forms import UserRegistrationForm, UserAuthenticationForm, UserSettingsChangeForm
from api.models.engage import User_Profile


Expand Down Expand Up @@ -125,6 +125,38 @@ def password_view(request):
return render(request, "password.html", context)


@login_required
def user_view(request):
"""
View the "User Settings" page

Returns: HttpResponse

Example:
http://0.0.0.0:8000/settings/user/
"""
user = request.user

if request.method == 'POST':
form = UserSettingsChangeForm(user, request.POST)
if form.is_valid():
form.save()
update_session_auth_hash(request, user) # Important if username changed
messages.success(request, "Your settings were successfully updated!")
return redirect('/')
else:
messages.error(request, 'Please correct the error below.')
else:
form = UserSettingsChangeForm(user)

context = {
'user': user,
'form': form,
}

return render(request, "change_user.html", context)


@csrf_protect
def set_timezone(request):
"""
Expand Down
3 changes: 2 additions & 1 deletion calliope_app/client/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@
&nbsp;&nbsp;<i class="fas fa-user"></i>&nbsp;&nbsp;<span class="caret"></span>&nbsp;
</button>
<ul class="dropdown-menu dropdown-menu-left">
<li>&nbsp;&nbsp;{{ user.first_name }} {{ user.last_name }}<br>&nbsp;&nbsp;<span class="text-sm">{{ user.email }}<br>&nbsp;&nbsp;{{ user.user_profile.organization }}</span><br><br></li>
<li>&nbsp;&nbsp;{{ user.first_name }} {{ user.last_name }}<br>&nbsp;&nbsp;<span class="text-sm">{{ user.username }}<br>&nbsp;&nbsp;{{ user.user_profile.organization }}</span><br><br></li>
<li><a href="/settings/user/"><h6>&nbsp;&nbsp;<i class="fa fa-user"></i>&nbsp;&nbsp;{% trans "Change User" %}</a>&nbsp;&nbsp;</h6></a></li>
<li><a href="/settings/password/"><h6>&nbsp;&nbsp;<i class="fa fa-key"></i>&nbsp;&nbsp;{% trans "Change Password" %}</a>&nbsp;&nbsp;</h6></a></li>
<li><a href="/logout/?next=/login/"><h6>&nbsp;&nbsp;<i class="fas fa-sign-out-alt"></i>&nbsp;&nbsp;{% trans "Log out" %}</a>&nbsp;&nbsp;</h6></a></li>
</ul>
Expand Down
24 changes: 24 additions & 0 deletions calliope_app/client/templates/change_user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'base.html' %}
{% load i18n %}

{% block head %}
<title>Engage | Password</title>
{% endblock %}

{% block body %}
<div class="row">
<div class="col-12 centered">
<h1>{% trans "Change User Details" %}</h1>
</div>
<div class="col-2"></div>
<div class="col-8">
<br><br>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">{% trans "Save changes" %}</button>
</form>
</div>
<div class="col-2"></div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion calliope_app/client/views/engage.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def share_view(request):
id=request.user.id).order_by('last_name', 'first_name')

context = {
"timezones": common_timezones,
# "timezones": common_timezones,
"user": request.user,
"users": users,
"user_models": user_models,
Expand Down
2 changes: 1 addition & 1 deletion calliope_app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pint==0.21
psycopg2-binary==2.9.3
pyyaml==6.0
requests>=2.21.0

pytz==2024.1
pyutilib>=6.0.0
# amqp==2.6.1
# vine==1.3.0
Expand Down