diff options
| author | Bobby <[email protected]> | 2023-01-08 12:02:41 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-01-08 12:02:41 -0500 |
| commit | 732b43800422e2a6d9c2c4d3de9d20be8b628127 (patch) | |
| tree | 5cc16f325b08293aae4055671d2c5c94a3c715b7 | |
| parent | f4fb28ee30c124d520fec9a27b1ac1bbce9f04e4 (diff) | |
| download | thatcomputerscientist-732b43800422e2a6d9c2c4d3de9d20be8b628127.tar.xz thatcomputerscientist-732b43800422e2a6d9c2c4d3de9d20be8b628127.zip | |
Update Profile Details and Show Success message on the same tab for account updates
| -rw-r--r-- | blog/views.py | 13 | ||||
| -rw-r--r-- | templates/blog/account.html | 13 | ||||
| -rw-r--r-- | users/forms.py | 31 | ||||
| -rw-r--r-- | users/views.py | 52 |
4 files changed, 70 insertions, 39 deletions
diff --git a/blog/views.py b/blog/views.py index 6697a9f7..4bcaae44 100644 --- a/blog/views.py +++ b/blog/views.py @@ -8,7 +8,7 @@ from string import ascii_letters, digits from .models import Category, Post, Comment from .context_processors import recent_posts, avatar_list, add_excerpt, add_num_comments, highlight_code_blocks from announcements.models import Announcement -from users.forms import RegisterForm +from users.forms import RegisterForm, UpdateUserDetailsForm from users.tokens import CaptchaTokenGenerator from django.contrib import messages from bs4 import BeautifulSoup @@ -40,8 +40,7 @@ def account(request): avatarlist[key] = [re.sub(r'\.png$', '', string) for string in avatarlist[key]] avatarlist[key].sort(key=natural_keys) avatarlist = {k: avatarlist[k] for k in sorted(avatarlist)} - - print(avatarlist) + if user.is_authenticated: try: user_profile = UserProfile.objects.get(user=user) @@ -58,7 +57,13 @@ def account(request): avatar_file = choice(avatarlist[avatar_dir]) user_profile.avatar_url = avatar_dir + '/' + avatar_file user_profile.save() - return render(request, 'blog/account.html', {'title': 'Account', 'user_profile': user_profile, 'avatarlist': avatarlist}) + + if request.GET.get('tab') == 'details': + update_form = UpdateUserDetailsForm(user=user, initial={'first_name': user.first_name, 'last_name': user.last_name, 'bio': user_profile.bio, 'is_public': user_profile.is_public, 'email_public': user_profile.email_public, 'location': user_profile.location}) + else: + update_form = None + + return render(request, 'blog/account.html', {'title': 'Account', 'user_profile': user_profile, 'avatarlist': avatarlist, 'update_form': update_form}) else: # Redirect to login page return redirect('blog:home') diff --git a/templates/blog/account.html b/templates/blog/account.html index 6c9ecf8c..d6d5dc85 100644 --- a/templates/blog/account.html +++ b/templates/blog/account.html @@ -7,7 +7,7 @@ <img src="{% static 'images/avatars/' %}{{ user_profile.avatar_url }}.png" alt="{{ user.username }}'s avatar" width="140" height="140" style="display: block; margin: 0 auto;" /> </div> <div id="side-links"> - <p><a href="{% url 'blog:account' %}">Account Settings</a></p> + <p><a href="{% url 'blog:account' %}">Account Home</a></p> <p><a href="{% url 'blog:account' %}?tab=avatar">Change avatar</a></p> <p><a href="{% url 'blog:account' %}?tab=details">Change details</a></p> <p><a href="{% url 'blog:account' %}?tab=email">Change email</a></p> @@ -48,6 +48,17 @@ <input type="submit" value="Change Avatar" class="button button-special" /> </form> </div> + {% elif request.GET.tab == 'details' %} + <div id="details"> + <p>Change your account details here. You can change your first name, last name, bio, email and activity visibility.</p> + <form method="post" action="{% url 'users:update' %}"> + {% csrf_token %} + <table> + {{ update_form.as_table }} + </table> + <input type="submit" value="Update Details" class="button button-special" /> + </form> + </div> {% else %} <div id="help"> <p>You can change account settings for <strong>{{ user.username }}</strong> here. If you wish to have additional support, please contact me at <a href="mailto:[email protected]?subject=[URGENT]%20Support%20Request%20for%20{{ user.username }}">[email protected]</a>. Please take care of the following points before you submit your support request:</p> diff --git a/users/forms.py b/users/forms.py index aebad567..5ef95543 100644 --- a/users/forms.py +++ b/users/forms.py @@ -65,3 +65,34 @@ class RegisterForm(forms.Form): return user + +class UpdateUserDetailsForm(forms.Form): + first_name = forms.CharField(label='First name', max_length=30, required=False, widget=forms.TextInput(attrs={'placeholder': 'First name'})) + last_name = forms.CharField(label='Last name', max_length=30, required=False, widget=forms.TextInput(attrs={'placeholder': 'Last name'})) + location = forms.CharField(label='Location', max_length=30, required=False, widget=forms.TextInput(attrs={'placeholder': 'Location'})) + bio = forms.CharField(label='Bio', max_length=500, required=False, widget=forms.Textarea(attrs={'placeholder': 'Bio'})) + is_public = forms.ChoiceField(label='Activity Visibility', choices=((True, 'Public'), (False, 'Private')), widget=forms.RadioSelect) + email_public = forms.ChoiceField(label='Email Visibility', choices=((True, 'Public'), (False, 'Private')), widget=forms.RadioSelect) + + + def __init__(self, *args, **kwargs): + self.user = kwargs.pop('user') + super().__init__(*args, **kwargs) + + def clean(self): + cleaned_data = super().clean() + return cleaned_data + + def save(self): + self.user.first_name = self.cleaned_data.get('first_name') + self.user.last_name = self.cleaned_data.get('last_name') + self.user.save() + + user_profile = UserProfile.objects.get(user=self.user) + user_profile.location = self.cleaned_data.get('location') + user_profile.bio = self.cleaned_data.get('bio') + user_profile.is_public = self.cleaned_data.get('is_public') + user_profile.email_public = self.cleaned_data.get('email_public') + user_profile.save() + + return (self.user, user_profile)
\ No newline at end of file diff --git a/users/views.py b/users/views.py index 5027bd64..64449f20 100644 --- a/users/views.py +++ b/users/views.py @@ -1,5 +1,5 @@ from django.http import HttpResponseRedirect -from django.shortcuts import redirect +from django.shortcuts import redirect, reverse from django.contrib.auth import authenticate, login, logout, update_session_auth_hash from django.contrib import messages from .models import UserProfile @@ -13,6 +13,7 @@ from django.utils.http import urlsafe_base64_encode from django.contrib.sites.shortcuts import get_current_site from .tokens import account_activation_token, EmailChangeTokenGenerator from django.utils.http import urlsafe_base64_decode +from .forms import UpdateUserDetailsForm # Create your views here. def login_user(request): @@ -47,38 +48,21 @@ def logout_user(request): return HttpResponseRedirect(request.META.get('HTTP_REFERER')) def update_user(request): - username = request.user - first_name = request.POST['firstname'] - last_name = request.POST['lastname'] - location = request.POST['location'] - bio = request.POST['bio'] - is_public = False - email_public = False - if 'emailPublic' in request.POST: - email_public = True if request.POST['emailPublic'] == '1' and is_public else False - - if 'isPublic' in request.POST: - is_public = True if request.POST['isPublic'] == '1' and is_public else False - - if username is not None: - user = User.objects.get(username=username) - user.first_name = first_name - user.last_name = last_name - user.save() - try: - user_profile = UserProfile.objects.get(user=username) - user_profile.location = location - user_profile.bio = bio - user_profile.is_public = is_public - user_profile.email_public = email_public - user_profile.save() - except UserProfile.DoesNotExist: - user_profile = UserProfile(user=username, location=location, bio=bio, is_public=is_public, email_public=email_public) - user_profile.save() - messages.success(request, 'Profile was successfully updated!') - return redirect('blog:account') + user = request.user + if user is not None: + if request.method == 'POST': + form = UpdateUserDetailsForm(request.POST, user=user) + if form.is_valid(): + form.save() + messages.success(request, 'Profile was successfully updated!') + return redirect(reverse('blog:account') + '?tab=details') + else: + messages.error(request, 'Unable to update profile! Please try again later.') + return redirect(reverse('blog:account') + '?tab=details') + else: + return redirect(reverse('blog:account') + '?tab=details') else: - messages.error(request, 'Unable to update profile! Please try again later.') + messages.error(request, 'You must be logged in to update your profile!') return redirect('blog:home') def update_avatar(request): @@ -89,10 +73,10 @@ def update_avatar(request): user_profile.avatar_url = request.POST['avatar'] user_profile.save() messages.success(request, 'Avatar was successfully updated!') - return redirect('blog:account') + return redirect(reverse('blog:account') + '?tab=avatar') else: messages.error(request, 'Unable to update avatar! Please try again later.') - return redirect('blog:home') + return redirect(reverse('blog:account') + '?tab=avatar') else: messages.error(request, 'You must be logged in to update your avatar!') return redirect('blog:home') |
