aboutsummaryrefslogtreecommitdiff
path: root/users
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-01-08 12:02:41 -0500
committerBobby <[email protected]>2023-01-08 12:02:41 -0500
commit732b43800422e2a6d9c2c4d3de9d20be8b628127 (patch)
tree5cc16f325b08293aae4055671d2c5c94a3c715b7 /users
parentf4fb28ee30c124d520fec9a27b1ac1bbce9f04e4 (diff)
downloadthatcomputerscientist-732b43800422e2a6d9c2c4d3de9d20be8b628127.tar.xz
thatcomputerscientist-732b43800422e2a6d9c2c4d3de9d20be8b628127.zip
Update Profile Details and Show Success message on the same tab for account updates
Diffstat (limited to 'users')
-rw-r--r--users/forms.py31
-rw-r--r--users/views.py52
2 files changed, 49 insertions, 34 deletions
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')