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 ( diff --git a/project/accounts/forms.py b/project/accounts/forms.py index 68ca98c2e..19791d58c 100644 --- a/project/accounts/forms.py +++ b/project/accounts/forms.py @@ -215,6 +215,16 @@ 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 fields = [ @@ -332,4 +342,3 @@ def clean_profile_image(self): pass return profile_image - 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..261be5930 --- /dev/null +++ b/project/accounts/templates/accounts/utils/update_settings_form.html @@ -0,0 +1,48 @@ +
+
+
+ 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}} +
+
+
+ {%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..a247bc79f 100644 --- a/project/frontend_views/views.py +++ b/project/frontend_views/views.py @@ -11,11 +11,13 @@ 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 from django.contrib.auth import get_user_model + User = get_user_model() @@ -64,18 +66,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 @@ -123,9 +141,7 @@ def issue_thread(request, thread_id=None): }, "contributors": [ Account.objects.chip_summarize(a) - for a in Account.objects.filter( - pk__in=c_qs.values("author").distinct() - ) + for a in Account.objects.filter(pk__in=c_qs.values("author").distinct()) ], "category": {"id": t.category.id, "name": t.category.name}, "categories": [{"id": c.id, "name": c.name} for c in Category.objects.all()], diff --git a/project/threads/templates/threads/partials/account/account_base.html b/project/threads/templates/threads/partials/account/account_base.html index e8c2c159c..690b1d3e3 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 @@
    -
    -
    -
    -
    -
    Edit Profile
    - {% verbatim %} - -
    - - -
    - -
    -
    -
    - -
    -
    -
    This is only a preview image. Press confirm to finish your upload.
    -
    -
    - -
    -
    - -
    - Choose Image - -
    -
    - -
    -
    -
    +
    +
    + {% include "accounts/utils/update_settings_form.html" %}
    - {% endverbatim %}
    -
    -
    -