1 # -*- coding: utf-8 -*- vim:fileencoding=utf-8:
2 # vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
4 # Copyright © 2011-2014 Greek Research and Technology Network (GRNET S.A.)
5 # Copyright © 2011-2014 Leonidas Poulopoulos (@leopoul)
7 # Permission to use, copy, modify, and/or distribute this software for any
8 # purpose with or without fee is hereby granted, provided that the above
9 # copyright notice and this permission notice appear in all copies.
11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12 # TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
13 # FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
14 # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
15 # DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 from django.conf import settings
20 from django.core.mail import send_mail
21 from django.contrib.sites.models import Site
22 from django.shortcuts import render_to_response
23 from django.template.context import RequestContext
24 from django.template.loader import render_to_string
25 from django.utils.translation import ugettext_lazy as _
26 from accounts.models import *
27 from peers.models import *
28 from flowspec.forms import *
29 from registration.models import RegistrationProfile
30 from registration.views import activate as registration_activate
31 from django.views.decorators.cache import never_cache
34 def activate(request, activation_key):
36 if request.method == "GET":
37 activation_key = activation_key.lower() # Normalize before trying anything with it.
38 context = RequestContext(request)
40 rp = RegistrationProfile.objects.get(activation_key=activation_key)
42 except RegistrationProfile.DoesNotExist:
43 return render_to_response("registration/activate.html",
45 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
46 context_instance=context)
48 userProfile = rp.user.get_profile()
49 except UserProfile.DoesNotExist:
50 return render_to_response("registration/activate.html",
52 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
53 context_instance=context)
55 form = UserProfileForm(instance=userProfile)
56 form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=rp.user.pk), empty_label=None)
57 form.fields['peer'] = forms.ModelChoiceField(queryset=Peer.objects.all(), empty_label=None)
59 return render_to_response("registration/activate_edit.html",
61 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
63 context_instance=context)
65 if request.method == "POST":
66 context = RequestContext(request)
67 request_data = request.POST.copy()
69 user = User.objects.get(pk=request_data['user'])
70 up = user.get_profile()
71 up.peer = Peer.objects.get(pk=request_data['peer'])
75 return render_to_response("registration/activate_edit.html",
77 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS
79 context_instance=context)
80 activation_key = activation_key.lower() # Normalize before trying anything with it.
82 rp = RegistrationProfile.objects.get(activation_key=activation_key)
83 account = RegistrationProfile.objects.activate_user(activation_key)
84 except Exception as e:
88 # A user has been activated
89 email = render_to_string("registration/activation_complete.txt",
90 {"site": Site.objects.get_current(),
92 send_mail(_("%sUser account activated") % settings.EMAIL_SUBJECT_PREFIX,
93 email, settings.SERVER_EMAIL, [account.email])
94 context = RequestContext(request)
95 return render_to_response("registration/activate.html",
97 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
98 context_instance=context)