From 19621752b58fc590264eed5c331b1ae8b1090e21 Mon Sep 17 00:00:00 2001 From: SDE Date: Sun, 22 Oct 2023 16:46:25 +0300 Subject: [PATCH] 0.7.67 profile change --- AuthApp/forms.py | 17 ++++++++++++++++- AuthApp/js_views.py | 31 ++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/AuthApp/forms.py b/AuthApp/forms.py index 87f51cf..c5cae30 100644 --- a/AuthApp/forms.py +++ b/AuthApp/forms.py @@ -29,7 +29,22 @@ class RegistrationForm(forms.Form): tel = forms.CharField() agreement = forms.BooleanField(initial=False, required=True) - # def clean(self): + def clean(self): + pass + # cleaned_data = super().clean() + # for item in self.changed_data: + # if item in self.data: + # + # + # cc_myself = cleaned_data.get("cc_myself") + # subject = cleaned_data.get("subject") + # + # if cc_myself and subject: + # # Only do something if both fields are valid so far. + # if "help" not in subject: + # raise ValidationError( + # "Did not send for 'help' in the subject despite " "CC'ing yourself." + # ) diff --git a/AuthApp/js_views.py b/AuthApp/js_views.py index 034b7b3..0140607 100644 --- a/AuthApp/js_views.py +++ b/AuthApp/js_views.py @@ -103,10 +103,9 @@ def change_profile_confirm_ajax(request): from .forms import RegistrationForm form = RegistrationForm(data) - if not form.is_valid(): - raise ValidationError(form.errors) data_for_save = {} + users = User.objects.filter(id=request.user.id) if 'firstname' in data: data_for_save.update({'first_name': data['firstname']}) if 'lastname' in data: @@ -114,23 +113,41 @@ def change_profile_confirm_ajax(request): if 'email' in data: data_for_save.update({'email': data['email']}) data_for_save.update({'username': data['email']}) - request.user.update(**data_for_save) + + if data_for_save: + users.update(**data_for_save) data_for_save = {} + + password = None + confirm_password = None if 'password' in data: - data_for_save.update({'password': data['password']}) + password = data['password'] if 'confirm_password' in data: - data_for_save.update({'confirm_password': data['confirm_password']}) - request.user.u.set_password(data['password']) + confirm_password = data['confirm_password'] + if password and confirm_password: + if password != confirm_password: + errors = { + 'password': 'Не совпадают пароли', + 'confirm_password': 'Не совпадают пароли', + } + raise ValidationError(errors) + + request.user.set_password(password) + request.user.save() + data_for_save = {} + user_profiles = UserProfile.objects.filter(user__in=users) if 'country' in data: data_for_save.update({'country': data['country']}) if 'city' in data: data_for_save.update({'city': data['city']}) if 'tel' in data: data_for_save.update({'phone': data['tel']}) - request.user.user_profile.update(**data_for_save) + + if data_for_save: + user_profiles.update(**data_for_save) html = get_profile_change_page_content_html(request) return JsonResponse({'html': html}, status=200)