Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Engage75 change user #232

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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
61 changes: 61 additions & 0 deletions calliope_app/account/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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
Expand Down Expand Up @@ -127,3 +128,63 @@ def get_invalid_login_error(self):
code="invalid_login",
params={"username": "email"},
)

class UserSettingsChangeForm(forms.ModelForm):
username = forms.CharField(
label=_("Username"),
max_length=150,
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,
)

class Meta:
model = User_Profile
fields = ('organization', 'timezone')

def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = user
# Handle the case where User_Profile may not exist
try:
user_profile = user.user_profile
except User_Profile.DoesNotExist:
user_profile = User_Profile(user=user)
user_profile.save()

# Pre-fill the form fields with the current data
self.fields['username'].initial = user.username
self.fields['organization'].initial = user_profile.organization
# self.fields['timezone'].initial = user_profile.timezone

def clean_username(self):
username = self.cleaned_data['username']
if username != self.user.username:
if User.objects.filter(username=username).exists():
raise ValidationError(_("A user with that username already exists."))
return username

def save(self, commit=True):
# Update the User model
self.user.username = self.cleaned_data['username']
self.user.save()

# Update the User_Profile model
try:
profile = self.user.user_profile
except User_Profile.DoesNotExist:
profile = User_Profile(user=self.user)

profile.organization = self.cleaned_data['organization']
profile.timezone = self.cleaned_data['timezone']
if commit:
profile.save()
return self.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
35 changes: 34 additions & 1 deletion calliope_app/account/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
# from ..client.views import common_timezones
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment


from django.conf import settings
from django.contrib import messages
Expand All @@ -11,7 +12,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 +126,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
1 change: 1 addition & 0 deletions calliope_app/client/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
</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><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