1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# -*- coding: utf-8 -*-
"""
views.py of app 'account'
"""
from django.shortcuts import render
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView, UpdateView
from django.views.generic.list import ListView
from django.core.urlresolvers import reverse_lazy
from django.http import HttpResponseRedirect
from account.models import UserProfile
from account.forms import ResendEmailForm, UpdateProfileForm, UserFileFormSet
###### Class-based views ######
class ResendEmailView(FormView):
"""
class-based view used to resend activation email
if user provided different email, then update email to User instance
"""
form_class = ResendEmailForm
template_name = 'account/email_resend.html'
success_url = reverse_lazy('email_resend_done')
def form_valid(self, form):
form.update_email()
form.resend_email()
return super(ResendEmailView, self).form_valid(form)
class ProfileView(TemplateView):
"""
class view to show profile page
"""
template_name = 'account/profile.html'
def get_context_data(self, **kwargs):
context = super(ProfileView, self).get_context_data(**kwargs)
user = self.request.user
profile = user.userprofile_set.get(user=user)
userfiles = user.userfile_set.all()
context['user'] = user
context['profile'] = profile
context['userfiles'] = userfiles
return context
class UpdateProfileView(UpdateView):
form_class = UpdateProfileForm
model = UserProfile
template_name = 'account/profile_update.html'
success_url = reverse_lazy('profile_update_done')
# get profile object
def get_object(self, queryset=None):
user = self.request.user
profile = user.userprofile_set.get(user=user)
return profile
def get(self, request, *args, **kwargs):
"""
Returns the keyword arguments for instantiating the form
and related formsets.
modify this method to add 'email' data
"""
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
# initialize form 'email' field
user = self.request.user
form.fields['email'].initial = user.email
# formset and initialize with instances
qset = user.userfile_set.all()
formset = UserFileFormSet(instance=user, queryset=qset)
return self.render_to_response(
self.get_context_data(form=form, formset=formset))
def post(self, request, *args, **kwargs):
"""
handle POST requests, instantiating a form instance and
related formset with passed POST data and then validate.
"""
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
## formset
# must pass 'instance' here, otherwise raise IndexError
user = self.request.user
formset = UserFileFormSet(self.request.POST, self.request.FILES,
instance=user)
if (form.is_valid() and formset.is_valid()):
return self.form_valid(form, formset)
else:
return self.form_invalid(form, formset)
def form_valid(self, form, formset):
"""
modify 'form_valid' to update email field
"""
form_data = form.cleaned_data
# save object
self.object = form.save()
# update email and save
user = self.request.user
user.email = form_data.get('email', user.email)
user.save()
# formset
formset.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, formset):
"""
re-render the context data with the data-filled forms and errors
"""
return self.render_to_response(
self.get_context_data(form=form, formset=formset))
class ListApprovedView(ListView):
"""
class-based view to show list of approved users
"""
queryset = UserProfile.objects.filter(is_approved='Y')
template_name = 'account/list_approved.html'
# allow queryset/model returns no object, otherwise 404error raised
allow_empty = True
def get_context_data(self, **kwargs):
"""
add 'user' and 'profile' context
"""
context = super(ListApprovedView, self).get_context_data(**kwargs)
user = self.request.user
profile = user.userprofile_set.get(user=user)
context['user'] = user
context['profile'] = profile
return context
|