root / accounts / views.py @ 0492a5b5
History | View | Annotate | Download (4.6 kB)
1 |
# -*- coding: utf-8 -*- vim:fileencoding=utf-8:
|
---|---|
2 |
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
|
3 |
|
4 |
# Copyright (C) 2010-2014 GRNET S.A.
|
5 |
#
|
6 |
# This program is free software: you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation, either version 3 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful,
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
# GNU General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18 |
#
|
19 |
|
20 |
from django.conf import settings |
21 |
from django.core.mail import send_mail |
22 |
from django.contrib.sites.models import Site |
23 |
from django.shortcuts import render_to_response |
24 |
from django.template.context import RequestContext |
25 |
from django.template.loader import render_to_string |
26 |
from django.utils.translation import ugettext_lazy as _ |
27 |
from accounts.models import * |
28 |
from peers.models import * |
29 |
from flowspec.forms import * |
30 |
from registration.models import RegistrationProfile |
31 |
from registration.views import activate as registration_activate |
32 |
from django.views.decorators.cache import never_cache |
33 |
|
34 |
@never_cache
|
35 |
def activate(request, activation_key): |
36 |
account = None
|
37 |
if request.method == "GET": |
38 |
activation_key = activation_key.lower() # Normalize before trying anything with it.
|
39 |
context = RequestContext(request) |
40 |
try:
|
41 |
rp = RegistrationProfile.objects.get(activation_key=activation_key) |
42 |
|
43 |
except RegistrationProfile.DoesNotExist:
|
44 |
return render_to_response("registration/activate.html", |
45 |
{ 'account': account,
|
46 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
|
47 |
context_instance=context) |
48 |
try:
|
49 |
userProfile = rp.user.get_profile() |
50 |
except UserProfile.DoesNotExist:
|
51 |
return render_to_response("registration/activate.html", |
52 |
{ 'account': account,
|
53 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
|
54 |
context_instance=context) |
55 |
|
56 |
form = UserProfileForm(instance=userProfile) |
57 |
form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=rp.user.pk), empty_label=None) |
58 |
form.fields['peer'] = forms.ModelChoiceField(queryset=Peer.objects.all(), empty_label=None) |
59 |
|
60 |
return render_to_response("registration/activate_edit.html", |
61 |
{ 'account': account,
|
62 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
|
63 |
'form': form },
|
64 |
context_instance=context) |
65 |
|
66 |
if request.method == "POST": |
67 |
context = RequestContext(request) |
68 |
request_data = request.POST.copy() |
69 |
try:
|
70 |
user = User.objects.get(pk=request_data['user'])
|
71 |
up = user.get_profile() |
72 |
up.peer = Peer.objects.get(pk=request_data['peer'])
|
73 |
up.save() |
74 |
|
75 |
except:
|
76 |
return render_to_response("registration/activate_edit.html", |
77 |
{ 'account': account,
|
78 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS
|
79 |
}, |
80 |
context_instance=context) |
81 |
activation_key = activation_key.lower() # Normalize before trying anything with it.
|
82 |
try:
|
83 |
rp = RegistrationProfile.objects.get(activation_key=activation_key) |
84 |
account = RegistrationProfile.objects.activate_user(activation_key) |
85 |
except Exception as e: |
86 |
pass
|
87 |
|
88 |
if account:
|
89 |
# A user has been activated
|
90 |
email = render_to_string("registration/activation_complete.txt",
|
91 |
{"site": Site.objects.get_current(),
|
92 |
"user": account})
|
93 |
send_mail(_("%sUser account activated") % settings.EMAIL_SUBJECT_PREFIX,
|
94 |
email, settings.SERVER_EMAIL, [account.email]) |
95 |
context = RequestContext(request) |
96 |
return render_to_response("registration/activate.html", |
97 |
{ 'account': account,
|
98 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
|
99 |
context_instance=context) |