Skip to content

Commit

Permalink
user can update profile pic
Browse files Browse the repository at this point in the history
  • Loading branch information
harshraj22 committed Apr 10, 2020
1 parent 2b67909 commit dc2431b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 39 deletions.
30 changes: 0 additions & 30 deletions cse_hub/homepage/forms.py
Original file line number Diff line number Diff line change
@@ -1,30 +0,0 @@
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class RegistrationForm(UserCreationForm):
email = forms.EmailField(required = True)
profile_pic = forms.ImageField(required=False)

class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'email',
'password1',
'password2',
'profile_pic'
)

def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']

if commit:
user.save()

return user
2 changes: 1 addition & 1 deletion cse_hub/homepage/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from homepage.forms import RegistrationForm
from users.forms import RegistrationForm
from django.contrib import messages
from django.urls import reverse
from users.models import Profile
Expand Down
34 changes: 33 additions & 1 deletion cse_hub/users/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.forms import ModelForm
from .models import Profile
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django import forms

class ProfileUpdateForm(ModelForm):
class Meta:
Expand All @@ -8,4 +11,33 @@ class Meta:
# widgets = {
# 'password': forms.PasswordInput(),
# }
fields = []
fields = [
'profile_pic'
]

class RegistrationForm(UserCreationForm):
email = forms.EmailField(required = True)
profile_pic = forms.ImageField(required=False)

class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'email',
'password1',
'password2',
'profile_pic'
)

def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']

if commit:
user.save()

return user
4 changes: 3 additions & 1 deletion cse_hub/users/templates/users/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
<img class="card-img-top border-danger" src="{% static 'users/' %}{{pic}}" height="260px;" alt='{{pic}}'>
<div class="card-body">
<h4 class="card-title text-center">{{user.username}}</h4>
<p class="card-text text-center">Some data about user.</p>
<p class="card-text text-center">
{{ user.email }}
</p>
{% if user.username == request.user.username%}
<a class="btn btn-info btn-block" href="{% url 'user-submissions' user.username %}">My Submissions</a> <br>
<a class="btn btn-info btn-block" href="{% url 'user-profile-edit' user.username %}">Edit Profile</a>
Expand Down
10 changes: 5 additions & 5 deletions cse_hub/users/templates/users/profile_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
{% endfor %}
{% endif %}

<p>
Editing profile page of {{ request.user.username }}, More functionalities to be added
Please make sure to edit views. Currently it is not validating/ accepting form responses.
</p>
<form method="POST">
<h5>
Editing profile page of {{ request.user.username }}.
</h5>
<br>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}

Expand Down
12 changes: 11 additions & 1 deletion cse_hub/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .forms import ProfileUpdateForm
from django.contrib import messages
from django.conf import settings
from .models import Profile
import os


Expand All @@ -27,8 +29,16 @@ def profile_edit(request, username):
if username != request.user.username:
return HttpResponseRedirect(reverse('user-profile-edit', kwargs={'username':request.user.username}))

profile = Profile.objects.get(user=request.user)
# ===========Implement the followint logic ==================
# if request.method == 'POST':
if request.method == 'POST':
form = ProfileUpdateForm(request.POST, request.FILES, instance=profile)
if form.is_valid():
profile = form.save(commit=False)
profile.save()
messages.success(request, 'Succefully Updated')
else:
messages.error(request, 'Error updating')

context = {'form': ProfileUpdateForm()}
return render(request, 'users/profile_edit.html', context)
Expand Down

0 comments on commit dc2431b

Please sign in to comment.