From 6785b41aadda6308dd9f239f1a41ecf01656977f Mon Sep 17 00:00:00 2001 From: jeisenma Date: Mon, 7 Oct 2024 21:06:55 -0600 Subject: [PATCH 1/4] all changes --- calliope_app/account/forms.py | 61 +++++++++++++++++++ calliope_app/account/urls.py | 6 ++ calliope_app/account/views.py | 35 ++++++++++- .../client/templates/change_user.html | 24 ++++++++ calliope_app/client/views/engage.py | 2 +- calliope_app/requirements.txt | 2 +- 6 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 calliope_app/client/templates/change_user.html diff --git a/calliope_app/account/forms.py b/calliope_app/account/forms.py index 517d0b74..e56acfcf 100644 --- a/calliope_app/account/forms.py +++ b/calliope_app/account/forms.py @@ -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 @@ -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 diff --git a/calliope_app/account/urls.py b/calliope_app/account/urls.py index 13335979..10df8691 100644 --- a/calliope_app/account/urls.py +++ b/calliope_app/account/urls.py @@ -15,6 +15,12 @@ views.password_view, name='password' ), + path( + 'settings/user/', + views.user_view, + name='password' + ), + path( 'user_activation/', views.user_activation, diff --git a/calliope_app/account/views.py b/calliope_app/account/views.py index 2b4f9f10..47428088 100644 --- a/calliope_app/account/views.py +++ b/calliope_app/account/views.py @@ -1,4 +1,5 @@ import json +# from ..client.views import common_timezones from django.conf import settings from django.contrib import messages @@ -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 @@ -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): """ diff --git a/calliope_app/client/templates/change_user.html b/calliope_app/client/templates/change_user.html new file mode 100644 index 00000000..fab6b63d --- /dev/null +++ b/calliope_app/client/templates/change_user.html @@ -0,0 +1,24 @@ +{% extends 'base.html' %} +{% load i18n %} + +{% block head %} +Engage | Password +{% endblock %} + +{% block body %} +
+
+

{% trans "Change User Details" %}

+
+
+
+

+
+ {% csrf_token %} + {{ form.as_p }} + +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/calliope_app/client/views/engage.py b/calliope_app/client/views/engage.py index 085ddbfb..52a8ecbe 100644 --- a/calliope_app/client/views/engage.py +++ b/calliope_app/client/views/engage.py @@ -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, diff --git a/calliope_app/requirements.txt b/calliope_app/requirements.txt index b99711d2..88d8ce1f 100644 --- a/calliope_app/requirements.txt +++ b/calliope_app/requirements.txt @@ -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 From 36437eb9107d0722cdaebaff43f4d12dd624d4cd Mon Sep 17 00:00:00 2001 From: jeisenma Date: Mon, 7 Oct 2024 21:10:56 -0600 Subject: [PATCH 2/4] dropdown link --- calliope_app/client/templates/base.html | 1 + 1 file changed, 1 insertion(+) diff --git a/calliope_app/client/templates/base.html b/calliope_app/client/templates/base.html index 25b9d740..aae1347e 100644 --- a/calliope_app/client/templates/base.html +++ b/calliope_app/client/templates/base.html @@ -184,6 +184,7 @@ From 5005d31cae73f15a917b3bbce23f7af510518dc3 Mon Sep 17 00:00:00 2001 From: jeisenma Date: Mon, 16 Dec 2024 13:33:22 -0500 Subject: [PATCH 3/4] removing comment --- calliope_app/account/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/calliope_app/account/views.py b/calliope_app/account/views.py index 47428088..c69a783c 100644 --- a/calliope_app/account/views.py +++ b/calliope_app/account/views.py @@ -1,5 +1,4 @@ import json -# from ..client.views import common_timezones from django.conf import settings from django.contrib import messages From 7670458be9d5cdf1008637ae900a44b1ddf43885 Mon Sep 17 00:00:00 2001 From: jeisenma Date: Wed, 18 Dec 2024 19:48:02 -0500 Subject: [PATCH 4/4] migrating changes from branches to this branch with username' --- calliope_app/account/forms.py | 69 +++++++++++++------------ calliope_app/client/templates/base.html | 2 +- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/calliope_app/account/forms.py b/calliope_app/account/forms.py index e56acfcf..cd5fafae 100644 --- a/calliope_app/account/forms.py +++ b/calliope_app/account/forms.py @@ -5,7 +5,6 @@ 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 @@ -128,11 +127,22 @@ def get_invalid_login_error(self): code="invalid_login", params={"username": "email"}, ) + -class UserSettingsChangeForm(forms.ModelForm): +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=_("Username"), - max_length=150, + label=_("Email"), + max_length=30, required=True ) organization = forms.CharField( @@ -146,45 +156,36 @@ class UserSettingsChangeForm(forms.ModelForm): 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 + # 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 - 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 + # Pre-fill data from User_Profile model, if it exists try: - profile = self.user.user_profile + user_profile = user.user_profile + self.fields['organization'].initial = user_profile.organization + self.fields['timezone'].initial = user_profile.timezone except User_Profile.DoesNotExist: - profile = User_Profile(user=self.user) + 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 self.user + profile.save() + return user \ No newline at end of file diff --git a/calliope_app/client/templates/base.html b/calliope_app/client/templates/base.html index aae1347e..c36db427 100644 --- a/calliope_app/client/templates/base.html +++ b/calliope_app/client/templates/base.html @@ -183,7 +183,7 @@