Skip to content

Commit 6fc2d48

Browse files
committed
Add feature - Update user profile
1 parent 475f5a8 commit 6fc2d48

15 files changed

+32
-6
lines changed

db.sqlite3

0 Bytes
Binary file not shown.

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
amqp==2.5.0
21
billiard==3.6.0.0
32
celery==4.3.0
43
clear-screen==0.1.12
@@ -9,4 +8,4 @@ Pillow==6.1.0
98
pytz==2019.1
109
sqlparse==0.3.0
1110
vine==1.3.0
12-
virtualenv==16.7.2
11+
virtualenv==16.7.2
128 Bytes
Binary file not shown.
242 Bytes
Binary file not shown.

users/__pycache__/apps.cpython-36.pyc

485 Bytes
Binary file not shown.
1.38 KB
Binary file not shown.
1 KB
Binary file not shown.
683 Bytes
Binary file not shown.
1.23 KB
Binary file not shown.

users/forms.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ class Meta:
2020
class ProfileUpdateForm(forms.ModelForm):
2121
class Meta:
2222
model = Profile
23-
fields = ['image']
23+
fields = ['image']
24+
Binary file not shown.
Binary file not shown.

users/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.db import models
22
from django.contrib.auth.models import User
3+
from PIL import Image
34

45
class Profile(models.Model):
56
user = models.OneToOneField(User, on_delete=models.CASCADE)
@@ -8,3 +9,13 @@ class Profile(models.Model):
89
def __str__(self):
910
return f'{self.user.username} Profile'
1011

12+
def save(self):
13+
super().save()
14+
15+
img = Image.open(self.image.path)
16+
17+
if img.height > 300 or img.width > 300:
18+
output_size = (300, 300)
19+
img.thumbnail(output_size)
20+
img.save(self.image.path)
21+

users/templates/users/profile.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ <h2 class="account-heading">{{ user.username }}</h2>
1212
<form method="POST" enctype="multipart/form-data">
1313
{% csrf_token %}
1414
<fieldset class="form-group">
15-
<legend class="border-button mb-4">Join Today</legend>
15+
<legend class="border-button mb-4">Profile Info</legend>
1616
{{ u_form|crispy }}
1717
{{ p_form|crispy }}
1818
</fieldset>

users/views.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,23 @@ def register(request):
1919

2020
@login_required
2121
def profile(request):
22-
u_form = UserUpdateForm()
23-
p_form = ProfileUpdateForm()
22+
23+
if request.method == 'POST':
24+
u_form = UserUpdateForm(request.POST, instance=request.user)
25+
p_form = ProfileUpdateForm(request.POST,
26+
request.FILES,
27+
instance=request.user.profile)
28+
29+
if u_form.is_valid() and p_form.is_valid():
30+
u_form.save()
31+
p_form.save()
32+
messages.success(request, f'Your account has been updated!')
33+
return redirect('profile')
34+
35+
36+
else:
37+
u_form = UserUpdateForm(instance=request.user)
38+
p_form = ProfileUpdateForm(instance=request.user.profile)
2439

2540
context = {
2641
'u_form': u_form,

0 commit comments

Comments
 (0)