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