From 8a0527d116c0c62673625496e1f75be87a19fb44 Mon Sep 17 00:00:00 2001 From: wassaf Date: Sat, 11 Sep 2021 14:02:34 +0500 Subject: [PATCH 1/5] fixed issue in user profile setup --- project/accounts/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/accounts/api.py b/project/accounts/api.py index 9b31969db..4b4627c6f 100644 --- a/project/accounts/api.py +++ b/project/accounts/api.py @@ -3,7 +3,7 @@ from rest_framework.decorators import action from rest_framework.response import Response from api.permissions import IsAccountOwnerOrDuringRegistrationOrReadOnly - +from api.utils import get_account from api.models import Thread from accounts.models import Account from api.serializers import ( From 3a7e416fac95773ca2034a9db1dd73f5de44b84d Mon Sep 17 00:00:00 2001 From: wassaf Date: Sat, 11 Sep 2021 14:02:58 +0500 Subject: [PATCH 2/5] de coupled setting form from page to make it reusable --- .../accounts/utils/update_settings.html | 47 +------------------ .../accounts/utils/update_settings_form.html | 46 ++++++++++++++++++ 2 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 project/accounts/templates/accounts/utils/update_settings_form.html diff --git a/project/accounts/templates/accounts/utils/update_settings.html b/project/accounts/templates/accounts/utils/update_settings.html index 7b685d345..99c78eb3a 100644 --- a/project/accounts/templates/accounts/utils/update_settings.html +++ b/project/accounts/templates/accounts/utils/update_settings.html @@ -15,52 +15,7 @@
-
-
-
- Account Settings -
-
-
- {{form.non_field_errors}} -
- {% csrf_token %} -
-
- {{form.first_name.errors}} - {{form.first_name.label_tag}} - {{form.first_name}} -
-
- {{form.last_name.errors}} - {{form.last_name.label_tag}} - {{form.last_name}} -
-
-
-
- {{form.username.errors}} - {{form.username.label_tag}} - {{form.username}} -
-
- {{form.email.errors}} - {{form.email.label_tag}} - {{form.email}} -
-
-
-
- {{form.about_me.errors}} - {{form.about_me.label_tag}} - {{form.about_me}} -
-
-
- -
-
-
+ {% include "accounts/utils/update_settings_form.html" %}
diff --git a/project/accounts/templates/accounts/utils/update_settings_form.html b/project/accounts/templates/accounts/utils/update_settings_form.html new file mode 100644 index 000000000..84b807624 --- /dev/null +++ b/project/accounts/templates/accounts/utils/update_settings_form.html @@ -0,0 +1,46 @@ +
+
+
+ Account Settings +
+
+
+ {{form.non_field_errors}} +
+ {% csrf_token %} +
+
+ {{form.first_name.errors}} + {{form.first_name.label_tag}} + {{form.first_name}} +
+
+ {{form.last_name.errors}} + {{form.last_name.label_tag}} + {{form.last_name}} +
+
+
+
+ {{form.username.errors}} + {{form.username.label_tag}} + {{form.username}} +
+
+ {{form.email.errors}} + {{form.email.label_tag}} + {{form.email}} +
+
+
+
+ {{form.about_me.errors}} + {{form.about_me.label_tag}} + {{form.about_me}} +
+
+
+ +
+
+
\ No newline at end of file From 115149c129c91b4115f6ac9b6bb2c535289133fd Mon Sep 17 00:00:00 2001 From: wassaf Date: Sat, 11 Sep 2021 19:17:51 +0500 Subject: [PATCH 3/5] Added profile settings --- project/accounts/forms.py | 9 ++++ .../accounts/utils/update_settings_form.html | 6 ++- project/frontend_views/views.py | 41 +++++++++++----- .../partials/account/account_base.html | 2 +- .../partials/account/account_settings.html | 48 ++----------------- 5 files changed, 46 insertions(+), 60 deletions(-) diff --git a/project/accounts/forms.py b/project/accounts/forms.py index 68ca98c2e..fad66a042 100644 --- a/project/accounts/forms.py +++ b/project/accounts/forms.py @@ -214,6 +214,15 @@ class UpdateAccount(forms.ModelForm): """ Form for updating Account data """ + def __init__(self, *args, **kwargs): + readonly = kwargs.pop("readonly", False) + super(UpdateAccount, self).__init__(*args, **kwargs) + if readonly: + self.disable_fields() + + def disable_fields(self): + for _, field in self.fields.items(): + field.disabled = True class Meta: model = Account diff --git a/project/accounts/templates/accounts/utils/update_settings_form.html b/project/accounts/templates/accounts/utils/update_settings_form.html index 84b807624..261be5930 100644 --- a/project/accounts/templates/accounts/utils/update_settings_form.html +++ b/project/accounts/templates/accounts/utils/update_settings_form.html @@ -40,7 +40,9 @@
- -
+ {%if not readonly %} + + {% endif%} + \ No newline at end of file diff --git a/project/frontend_views/views.py b/project/frontend_views/views.py index c1b64d6a9..5cdc7a8af 100644 --- a/project/frontend_views/views.py +++ b/project/frontend_views/views.py @@ -11,6 +11,7 @@ from api.models import Category, Thread, Civi, Activity from accounts.models import Account +from accounts.forms import UpdateAccount from api.forms import UpdateProfileImage from core.constants import US_STATES from core.custom_decorators import login_required, full_account @@ -64,18 +65,34 @@ def base_view(request): @login_required @full_account def user_profile(request, username=None): - if not username: - return HttpResponseRedirect("/profile/{0}".format(request.user)) - else: - try: - user = User.objects.get(username=username) - except User.DoesNotExist: - return HttpResponseRedirect("/404") - data = { - "username": user, - "profile_image_form": UpdateProfileImage, - } - return TemplateResponse(request, "account.html", data) + if request.method == "GET": + if not username: + return HttpResponseRedirect("/profile/{0}".format(request.user)) + else: + is_owner = username == request.user.username + try: + user = User.objects.get(username=username) + account = user.account_set.first() + except User.DoesNotExist: + return HttpResponseRedirect("/404") + + form = UpdateAccount( + initial={ + "username": user.username, + "email": user.email, + "first_name": account.first_name or None, + "last_name": account.last_name or None, + "about_me": account.about_me or None, + }, + readonly=True + ) + data = { + "username": user, + "profile_image_form": UpdateProfileImage, + "form": form if is_owner else None, + "readonly": True + } + return TemplateResponse(request, "account.html", data) @login_required diff --git a/project/threads/templates/threads/partials/account/account_base.html b/project/threads/templates/threads/partials/account/account_base.html index e8c2c159c..628faa77e 100644 --- a/project/threads/templates/threads/partials/account/account_base.html +++ b/project/threads/templates/threads/partials/account/account_base.html @@ -14,7 +14,7 @@
  • MY ISSUES
  • {% if request.user.username == username|stringformat:"s" %}
  • - EDIT PROFILE + SETTINGS
  • {% endif %} diff --git a/project/threads/templates/threads/partials/account/account_settings.html b/project/threads/templates/threads/partials/account/account_settings.html index 681250838..81b25d688 100644 --- a/project/threads/templates/threads/partials/account/account_settings.html +++ b/project/threads/templates/threads/partials/account/account_settings.html @@ -1,52 +1,10 @@