aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-07-29 23:37:04 +0530
committerBobby <[email protected]>2022-07-29 23:37:04 +0530
commit3ff2a1fc2363a8ce0416189b29357bae72fff11d (patch)
tree2981b595f7504599e4f9c7365f170bedbaef31ff
parent1a68b4097cd5c1e27fc72717eff9e3ace7bb5178 (diff)
downloadthatcomputerscientist-3ff2a1fc2363a8ce0416189b29357bae72fff11d.tar.xz
thatcomputerscientist-3ff2a1fc2363a8ce0416189b29357bae72fff11d.zip
Update Profile::account working
-rw-r--r--blog/static/css/main.css16
-rw-r--r--blog/templates/account.html8
-rw-r--r--users/urls.py1
-rw-r--r--users/views.py37
4 files changed, 61 insertions, 1 deletions
diff --git a/blog/static/css/main.css b/blog/static/css/main.css
index 29541258..c79e538d 100644
--- a/blog/static/css/main.css
+++ b/blog/static/css/main.css
@@ -8,6 +8,8 @@ body {
padding: 0;
min-width: calc(100vw - 16px);
font-family: serif;
+ margin: 0;
+ padding: 0;
}
.sidebar {
@@ -102,6 +104,20 @@ nav > ul > li {
color: green;
}
+.alert.success {
+ background-color: #d4ffbd;
+ padding: 10px;
+ color: rgb(5, 74, 5);
+ font-weight: bold;
+}
+
+.alert.error {
+ background-color: #ffbdbd;
+ padding: 10px;
+ color: rgb(74, 5, 5);
+ font-weight: bold;
+}
+
.account {
display: flex;
flex-direction: row;
diff --git a/blog/templates/account.html b/blog/templates/account.html
index a1687cc8..521b1565 100644
--- a/blog/templates/account.html
+++ b/blog/templates/account.html
@@ -11,6 +11,11 @@
<li>Please note that this is a support request related to your account. Please do not file any bugs here. If you have noticed a bug, please report it to the <a href="https://github.com/luciferreeves/thatcomputerscientist/issues">GitHub Issues</a> page.</li>
</ul>
<p>Your avatar is fetched from gravatar. Update your gravatar email to fetch the avatar. If you don't have an account, you can sign up for one <a href="https://en.gravatar.com/" target="_blank">here</a>. If you haven't set up your gravatar email, we would try to fetch your profile picture from your account email, by default. If your account email and gravatar email are the same, you do not need to set a gravatar email.</p>
+ {% for message in messages %}
+ <div class="alert {{message.tags}}">
+ <p>{{ message }}</p>
+ </div>
+ {% endfor %}
<div class="account">
<div class="ac-sidebar">
<fieldset>
@@ -35,7 +40,8 @@
</form>
</div>
<div class="ac-main">
- <form method="post" onsubmit="event.preventDefault();">
+ <form method="post"action="{% url 'users:update' %} ">
+ {% csrf_token %}
<fieldset>
<legend>Account Details</legend>
<label for="firstname">First Name</label>
diff --git a/users/urls.py b/users/urls.py
index 5c024aad..f649d11a 100644
--- a/users/urls.py
+++ b/users/urls.py
@@ -7,6 +7,7 @@ app_name = 'users'
urlpatterns = [
path('login', views.login_user, name='login'),
path('logout', views.logout_user, name='logout'),
+ path('update', views.update_user, name='update'),
]
# Configure Admin Site
diff --git a/users/views.py b/users/views.py
index f0d423b1..56ea9dab 100644
--- a/users/views.py
+++ b/users/views.py
@@ -2,6 +2,8 @@ from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
+from .models import UserProfile
+from django.contrib.auth.models import User
# Create your views here.
@@ -28,3 +30,38 @@ def login_user(request):
def logout_user(request):
logout(request)
return redirect('/')
+
+def update_user(request):
+ username = request.user
+ first_name = request.POST['firstname']
+ last_name = request.POST['lastname']
+ location = request.POST['location']
+ gravatar_email = request.POST['gravatarEmail']
+ bio = request.POST['bio']
+ is_public = True if request.POST['isPublic'] == '1' else False
+ email_public = False
+ if 'emailPublic' in request.POST:
+ email_public = True if request.POST['emailPublic'] == '1' 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.gravatar_email = gravatar_email
+ 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, gravatar_email=gravatar_email, bio=bio, is_public=is_public, email_public=email_public)
+ user_profile.save()
+ messages.success(request, 'Profile was successfully updated!')
+ return redirect('/account')
+ else:
+ messages.error(request, 'Unable to update profile! Please try again later.')
+ return redirect('/')
+