root / accounts / views.py @ 1698da77
History | View | Annotate | Download (3.9 kB)
1 |
from django.conf import settings |
---|---|
2 |
from django.core.mail import send_mail |
3 |
from django.contrib.sites.models import Site |
4 |
from django.shortcuts import render_to_response |
5 |
from django.template.context import RequestContext |
6 |
from django.template.loader import render_to_string |
7 |
from django.utils.translation import ugettext_lazy as _ |
8 |
from flowspy.accounts.models import * |
9 |
from flowspy.peers.models import * |
10 |
from flowspy.flowspec.forms import * |
11 |
from registration.models import RegistrationProfile |
12 |
from registration.views import activate as registration_activate |
13 |
from django.views.decorators.cache import never_cache |
14 |
|
15 |
@never_cache
|
16 |
def activate(request, activation_key): |
17 |
account = None
|
18 |
if request.method == "GET": |
19 |
activation_key = activation_key.lower() # Normalize before trying anything with it.
|
20 |
context = RequestContext(request) |
21 |
try:
|
22 |
rp = RegistrationProfile.objects.get(activation_key=activation_key) |
23 |
|
24 |
except RegistrationProfile.DoesNotExist:
|
25 |
return render_to_response("registration/activate.html", |
26 |
{ 'account': account,
|
27 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
|
28 |
context_instance=context) |
29 |
try:
|
30 |
userProfile = rp.user.get_profile() |
31 |
except UserProfile.DoesNotExist:
|
32 |
return render_to_response("registration/activate.html", |
33 |
{ 'account': account,
|
34 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
|
35 |
context_instance=context) |
36 |
|
37 |
form = UserProfileForm(instance=userProfile) |
38 |
form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=rp.user.pk), empty_label=None) |
39 |
form.fields['peer'] = forms.ModelChoiceField(queryset=Peer.objects.all(), empty_label=None) |
40 |
|
41 |
return render_to_response("registration/activate_edit.html", |
42 |
{ 'account': account,
|
43 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
|
44 |
'form': form },
|
45 |
context_instance=context) |
46 |
|
47 |
if request.method == "POST": |
48 |
context = RequestContext(request) |
49 |
request_data = request.POST.copy() |
50 |
try:
|
51 |
user = User.objects.get(pk=request_data['user'])
|
52 |
up = user.get_profile() |
53 |
up.peer = Peer.objects.get(pk=request_data['peer'])
|
54 |
up.save() |
55 |
|
56 |
except:
|
57 |
return render_to_response("registration/activate_edit.html", |
58 |
{ 'account': account,
|
59 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS
|
60 |
}, |
61 |
context_instance=context) |
62 |
activation_key = activation_key.lower() # Normalize before trying anything with it.
|
63 |
try:
|
64 |
rp = RegistrationProfile.objects.get(activation_key=activation_key) |
65 |
account = RegistrationProfile.objects.activate_user(activation_key) |
66 |
except Exception as e: |
67 |
pass
|
68 |
|
69 |
if account:
|
70 |
# A user has been activated
|
71 |
email = render_to_string("registration/activation_complete.txt",
|
72 |
{"site": Site.objects.get_current(),
|
73 |
"user": account})
|
74 |
send_mail(_("%sUser account activated") % settings.EMAIL_SUBJECT_PREFIX,
|
75 |
email, settings.SERVER_EMAIL, [account.email]) |
76 |
context = RequestContext(request) |
77 |
return render_to_response("registration/activate.html", |
78 |
{ 'account': account,
|
79 |
'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
|
80 |
context_instance=context) |