This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
views.py
155 lines (121 loc) · 5.45 KB
/
views.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import Http404
from django.shortcuts import redirect
from django.views import generic
from braces.views import LoginRequiredMixin
import django_browserid.views
from random import randint
import re
from tower import ugettext as _
from oneanddone.base.urlresolvers import reverse_lazy
from oneanddone.tasks.models import TaskAttempt
from oneanddone.users.forms import UserProfileForm, SignUpForm
from oneanddone.users.mixins import UserProfileRequiredMixin
from oneanddone.users.models import UserProfile
def default_username(email, counter):
if not counter:
random_username = re.sub(r'[\W_]+', '', email.split('@')[0])
else:
random_username = re.sub(r'[\W_]+', '', email.split('@')[0] + str(randint(1, 100)))
if not UserProfile.objects.filter(username=random_username).exists():
return random_username
else:
return default_username(email, counter + 1)
class CreateProfileView(generic.CreateView):
model = UserProfile
form_class = SignUpForm
template_name = 'users/profile/edit.html'
def dispatch(self, request, *args, **kwargs):
if (not request.user.is_authenticated() or
UserProfile.objects.filter(user=request.user).exists()):
return redirect('base.home')
else:
return super(CreateProfileView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form):
profile = form.save(commit=False)
profile.user = self.request.user
profile.save()
messages.success(self.request, _('Your profile has been created.'))
return redirect('base.home')
def get_initial(self):
return {
'username': default_username(self.request.user.email, 0),
}
class DeleteProfileView(UserProfileRequiredMixin, generic.DeleteView):
model = UserProfile
success_url = reverse_lazy('base.home')
template_name = 'users/profile/delete.html'
def get_object(self):
return self.request.user.profile
class LoginView(generic.TemplateView):
template_name = 'users/login.html'
class ProfileDetailsView(generic.DetailView):
model = UserProfile
template_name = 'users/profile/detail.html'
def get_context_data(self, **kwargs):
context = super(ProfileDetailsView, self).get_context_data(**kwargs)
all_attempts_finished = self.object.user.taskattempt_set.filter(state=TaskAttempt.FINISHED)
paginator = Paginator(all_attempts_finished, 20)
page = self.request.GET.get('page', 1)
try:
attempts_finished = paginator.page(page)
except PageNotAnInteger:
attempts_finished = paginator.page(1)
except EmptyPage:
attempts_finished = paginator.page(paginator.num_pages)
context['attempts_finished'] = attempts_finished
context['page'] = 'ProfileDetails'
return context
def get_object(self, *args, **kwargs):
username = self.kwargs.get('username')
if username is None:
try:
return self.get_queryset().get(user=self.kwargs.get('id'))
except ObjectDoesNotExist:
raise Http404(_(u'No UserProfiles found matching the userid'))
try:
return self.get_queryset().get(username=username)
except (ObjectDoesNotExist, MultipleObjectsReturned):
raise Http404(_(u'No UserProfiles found matching the username'))
class MyProfileDetailsView(ProfileDetailsView):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated():
return redirect('base.home')
return super(MyProfileDetailsView, self).dispatch(request, *args, **kwargs)
def get_object(self, *args, **kwargs):
return self.request.user.profile
class UpdateProfileView(LoginRequiredMixin, generic.UpdateView):
model = UserProfile
template_name = 'users/profile/edit.html'
def form_valid(self, form):
form.save()
messages.success(self.request, _('Your profile has been updated.'))
return redirect('users.profile.mydetails')
def get_context_data(self, *args, **kwargs):
ctx = super(UpdateProfileView, self).get_context_data(*args, **kwargs)
ctx['action'] = 'Update'
return ctx
def get_form_class(self):
if self.request.user.profile.privacy_policy_accepted:
return UserProfileForm
else:
return SignUpForm
def get_initial(self):
if not self.request.user.profile.username:
return {
'username': default_username(self.request.user.email, 0),
}
def get_object(self):
return self.request.user.profile
class Verify(django_browserid.views.Verify):
def login_failure(self, *args, **kwargs):
messages.error(self.request, _("""
There was a problem signing you in. Please try again. If you continue to have issues
logging in, let us know by emailing <a href="mailto:{email}">{email}</a>.
""").format(email='oneanddone@mozilla.com'), extra_tags='safe')
return super(Verify, self).login_failure(*args, **kwargs)