From 8c744a19c210348325fa222eed0562d5746cf650 Mon Sep 17 00:00:00 2001 From: adityacp Date: Sat, 15 Aug 2020 11:44:14 +0530 Subject: Add edit profile and change password --- workshop_app/forms.py | 66 ++++++++--- .../registration/password_change_done.html | 19 +++ .../registration/password_change_form.html | 62 ++++++++++ workshop_app/templates/workshop_app/base.html | 3 + .../templates/workshop_app/view_profile.html | 127 ++++++++++++++------- workshop_app/urls.py | 1 - workshop_app/views.py | 60 ++++------ 7 files changed, 240 insertions(+), 98 deletions(-) create mode 100644 workshop_app/templates/registration/password_change_done.html create mode 100644 workshop_app/templates/registration/password_change_form.html diff --git a/workshop_app/forms.py b/workshop_app/forms.py index 46049f0..2b993fd 100644 --- a/workshop_app/forms.py +++ b/workshop_app/forms.py @@ -125,24 +125,6 @@ class UserLoginForm(forms.Form): return user -class ProfileForm(forms.ModelForm): - """ profile form for coordinator and instructor """ - - class Meta: - model = Profile - fields = ['first_name', 'last_name', 'institute', 'department'] - - first_name = forms.CharField(max_length=32) - last_name = forms.CharField(max_length=32) - - def __init__(self, *args, **kwargs): - if 'user' in kwargs: - user = kwargs.pop('user') - super(ProfileForm, self).__init__(*args, **kwargs) - self.fields['first_name'].initial = user.first_name - self.fields['last_name'].initial = user.last_name - - class WorkshopForm(forms.ModelForm): """ Coordinators will propose a workshop and date @@ -215,3 +197,51 @@ class AttachmentFileForm(forms.ModelForm): class Meta: model = AttachmentFile exclude = ['workshop_type'] + + +class ProfileForm(forms.ModelForm): + """ profile form for coordinators and instructors """ + + class Meta: + model = Profile + exclude = ["user", "is_email_verified", "activation_key", + "key_expiry_time", "how_did_you_hear_about_us"] + + first_name = forms.CharField(max_length=30, widget=forms.TextInput( + {'class': "form-control", 'placeholder': "First Name"})) + last_name = forms.CharField(max_length=30, widget=forms.TextInput( + {'class': "form-control", 'placeholder': "Last Name"})) + + def __init__(self, *args, **kwargs): + if 'user' in kwargs: + user = kwargs.pop('user') + super(ProfileForm, self).__init__(*args, **kwargs) + self.fields['first_name'].initial = user.first_name + self.fields['first_name'].widget.attrs.update( + {'class': "form-control", 'placeholder': 'First Name'} + ) + self.fields['last_name'].initial = user.last_name + self.fields['last_name'].widget.attrs.update( + {'class': "form-control", 'placeholder': 'Last Name'} + ) + self.fields['institute'].widget.attrs.update( + {'class': "form-control", 'placeholder': 'Institute'} + ) + self.fields['department'].widget.attrs.update( + {'class': "custom-select"} + ) + self.fields['title'].widget.attrs.update( + {'class': "custom-select"} + ) + self.fields['state'].widget.attrs.update( + {'class': "custom-select"} + ) + self.fields['phone_number'].widget.attrs.update( + {'class': "form-control", 'placeholder': 'Phone Number'} + ) + self.fields['position'].widget.attrs.update( + {'class': "form-control", 'placeholder': 'Position'} + ) + self.fields['location'].widget.attrs.update( + {'class': "form-control", 'placeholder': 'Location'} + ) diff --git a/workshop_app/templates/registration/password_change_done.html b/workshop_app/templates/registration/password_change_done.html new file mode 100644 index 0000000..321f57f --- /dev/null +++ b/workshop_app/templates/registration/password_change_done.html @@ -0,0 +1,19 @@ +{% extends "workshop_app/base.html" %} + +{% block content %} +

+
+
+
+

Your password has been changed successfully.

+

Redirecting ...

+
+
+
+ +{% endblock %} diff --git a/workshop_app/templates/registration/password_change_form.html b/workshop_app/templates/registration/password_change_form.html new file mode 100644 index 0000000..99cdbdf --- /dev/null +++ b/workshop_app/templates/registration/password_change_form.html @@ -0,0 +1,62 @@ +{% extends "workshop_app/base.html" %} + + +{% block pagetitle %} +Change Password +{% endblock %} +{% block title %} Change Password {% endblock %} + +{% block content %} +
+ {% csrf_token %} +
+
+ + {% if form.errors %} + {% for field in form %} + {% for error in field.errors %} +
+ + {{ error|escape }} +
+ {% endfor %} + {% endfor %} + {% for error in form.non_field_errors %} +
+ + {{ error|escape }} +
+ {% endfor %} + {% endif %} + {% for field in form %} + + + + + {% endfor %} +
{{ field.label }}{{ field }} {{ field.help_text }}
+
+
+
+
+ + + Cancel + +
+
+ +{% endblock content %} diff --git a/workshop_app/templates/workshop_app/base.html b/workshop_app/templates/workshop_app/base.html index ba968ba..95f5fa0 100644 --- a/workshop_app/templates/workshop_app/base.html +++ b/workshop_app/templates/workshop_app/base.html @@ -97,6 +97,9 @@ diff --git a/workshop_app/templates/workshop_app/view_profile.html b/workshop_app/templates/workshop_app/view_profile.html index 5cb5c36..041d6ac 100644 --- a/workshop_app/templates/workshop_app/view_profile.html +++ b/workshop_app/templates/workshop_app/view_profile.html @@ -1,51 +1,49 @@ {% extends 'workshop_app/base.html' %} -{% block title %} - Coordinator Profile -{% endblock %} +{% block title %} Profile {% endblock %} {% csrf_token %} {% block content %}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
- {% if Workshops %} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+

Workshop Details

@@ -77,6 +75,55 @@


+ {% else %} +
+ {% csrf_token %} +
+
+ +
+ {% csrf_token %} + {% if form.errors %} + {% for field in form %} + {% for error in field.errors %} +
+ + {{field.label}} : {{ error|escape }} +
+ {% endfor %} + {% endfor %} + {% for error in form.non_field_errors %} +
+ + {{field.label}} : {{ error|escape }} +
+ {% endfor %} + {% endif %} + + + + + + + + + + +
{{form.title}}
{{form.first_name}}
{{form.last_name}}
{{form.phone_number}}
{{form.institute}}
{{form.department}}
{{form.position}}
{{form.location}}
{{form.state}}
+
+
+ + Cancel + + +
+
+ {% endif %}
{% endblock %} diff --git a/workshop_app/urls.py b/workshop_app/urls.py index b83e5cc..59e5ca5 100644 --- a/workshop_app/urls.py +++ b/workshop_app/urls.py @@ -25,7 +25,6 @@ urlpatterns = [ url(r'^activate_user/$', views.activate_user), url(r'^login/$', views.user_login, name="login"), url(r'^logout/$', views.user_logout, name="logout"), - url(r'^edit_profile/$', views.edit_profile), url(r'^status$', views.workshop_status_coordinator, name='workshop_status_coordinator'), url(r'^dashboard$', views.workshop_status_instructor, diff --git a/workshop_app/views.py b/workshop_app/views.py index 7be17d9..d59603d 100644 --- a/workshop_app/views.py +++ b/workshop_app/views.py @@ -165,43 +165,6 @@ def user_register(request): return render(request, "workshop_app/register.html", {"form": form}) -@login_required -def view_profile(request): - """ view instructor and coordinator profile """ - user = request.user - if user.is_superuser: - return redirect('/admin') - return render(request, "workshop_app/view_profile.html") - - -@login_required -def edit_profile(request): - """ edit profile details facility for instructor and coordinator """ - - user = request.user - if user.is_superuser: - return redirect('/admin') - - if request.method == 'POST': - form = ProfileForm(request.POST, user=user, instance=user.profile) - if form.is_valid(): - form_data = form.save(commit=False) - form_data.user = user - form_data.user.first_name = request.POST['first_name'] - form_data.user.last_name = request.POST['last_name'] - form_data.user.save() - form_data.save() - messages.add_message(request, messages.SUCCESS, "Profile updated.") - return redirect(reverse("workshop_app:view_own_profile")) - else: - messages.add_message( - request, messages.ERROR, "Profile update failed!" - ) - else: - form = ProfileForm(user=user, instance=user.profile) - return render(request, 'workshop_app/edit_profile.html', {'form': form}) - - # Workshop views @login_required @@ -514,7 +477,26 @@ def view_profile(request, user_id): def view_own_profile(request): """User can view own profile """ user = request.user - coordinator_profile = Profile.objects.get(user=user) + if user.is_superuser: + return redirect("admin") + profile = user.profile + if request.method == 'POST': + form = ProfileForm(request.POST, user=user, instance=profile) + if form.is_valid(): + form_data = form.save(commit=False) + form_data.user = user + form_data.user.first_name = request.POST['first_name'] + form_data.user.last_name = request.POST['last_name'] + form_data.user.save() + form_data.save() + messages.add_message(request, messages.SUCCESS, "Profile updated.") + return redirect(reverse("workshop_app:view_own_profile")) + else: + messages.add_message( + request, messages.ERROR, "Profile update failed!" + ) + else: + form = ProfileForm(user=user, instance=profile) return render(request, "workshop_app/view_profile.html", - {"coordinator_profile": coordinator_profile, "Workshops": None}) + {"profile": profile, "Workshops": None, "form": form}) -- cgit