-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviews.py
More file actions
148 lines (114 loc) · 4.69 KB
/
views.py
File metadata and controls
148 lines (114 loc) · 4.69 KB
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
import logging
from django.conf import settings
from django.contrib import messages
from django.contrib.auth import login
from django.contrib.auth.models import User
from django.shortcuts import render, redirect
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.views.generic import View
from django.views.generic.edit import CreateView
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView
from django.urls import reverse_lazy
from api import serializers
from api.exceptions import ServiceUnavailable, DryccException
from api.serializers import RegisterForm
from api.utils import account_activation_token, get_local_host
from api.viewset import NormalUserViewSet
logger = logging.getLogger(__name__)
class ReadinessCheckView(View):
"""
Simple readiness check view to determine DB connection / query.
"""
def get(self, request):
try:
import django.db
with django.db.connection.cursor() as c:
c.execute("SELECT 0")
except django.db.Error as e:
raise ServiceUnavailable("Database health check failed") from e
return HttpResponse("OK")
head = get
class LivenessCheckView(View):
"""
Simple liveness check view to determine if the server
is responding to HTTP requests.
"""
def get(self, request):
return HttpResponse("OK")
head = get
class RegisterView(CreateView):
form_class = RegisterForm
template_name = 'registration/register.html'
success_url = reverse_lazy('register_done')
def post(self, request, *args, **kwargs):
if settings.LDAP_ENDPOINT:
raise DryccException("You cannot register user when ldap is enabled.")
form = self.form_class(request.POST)
self.object = None
if form.is_valid():
user = form.save(commit=False)
user.is_staff = False
user.is_active = False
user.save()
domain = get_local_host(request)
mail_subject = 'Activate your account.'
message = render_to_string(
'registration/account_activation_email.html', {
'user': user,
'domain': domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
user.email_user(mail_subject, message)
messages.success(request, (
'Please Confirm your email to complete registration.'))
return self.form_valid(form)
else:
return self.form_invalid(form)
class RegisterDoneView(TemplateView):
template_name = 'registration/register_done.html'
title = _('Activate email sent')
class ActivateAccount(View):
def get(self, request, uidb64, token, *args, **kwargs):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(
user, token):
user.is_active = True
user.is_staff = True
user.save()
# login(request, user)
login(request, user, backend='django.contrib.auth.backends.ModelBackend')
messages.success(request, 'Your account have been confirmed.')
return redirect('/accounts/activate/done/')
else:
messages.warning(request, (
'The confirmation link was invalid, possibly because it has already been used.')) # noqa
return redirect('/accounts/activate/fail/')
class ActivateAccountDoneView(TemplateView):
template_name = 'registration/account_activation_done.html'
title = _('Activate account done')
class ActivateAccountFailView(TemplateView):
template_name = 'registration/account_activation_fail.html'
title = _('Activate account fail')
def index(request):
return render(request, 'registration/login.html')
class UserDetailView(NormalUserViewSet):
serializer_class = serializers.UserSerializer
required_scopes = ['openid']
def get_object(self):
return self.request.user
class UserEmailView(NormalUserViewSet):
serializer_class = serializers.UserEmailSerializer
required_scopes = ['openid']
def get_object(self):
return self.request.user
class LoginDoneView(TemplateView):
template_name = 'registration/Heroku_Login.html'