public
Last active

Class-based django-registration view

  • Download Gist
registration_view.py
Python
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
from django.views.generic.edit import FormView
from django.views.generic.base import RedirectView
 
from registration.models import RegistrationProfile
from forms import RegistrationForm
 
class AccountRegistrationView(FormView):
template_name = 'authentication/registration_form.html'
form_class = RegistrationForm
url = lazy_reverse('core:core-welcome')
 
extra_context = {
'page_robots': u'INDEX, NOFOLLOW',
'page_description': u'Napravi novi nalog',
'page_keywords': u'registracija, registriranje, novi nalog, napravi nalog',
}
def form_valid(self, form):
AccountRegistrationView.register_user(self.request, **form.cleaned_data)
return super(AccountRegistrationView, self).form_valid(self, form)
def get_context_data(self, **kwargs):
context = super(AccountRegistrationView, self).get_context_data(**kwargs)
context.update(self.extra_context)
return context
@classmethod
def register_user(cls, request, **kwargs):
'''
Given an email address and password, create a new
user account, which will initially be inactive.
'''
email, password = kwargs['email'], kwargs['password1']
user = cls.create_unique_user(email, password)
cls.user_registered(user, request)
@classmethod
def user_registered(cls, user, request):
'''
Handles successful user registrations.
Creates a registration success messages and sends
a registration success email to the user.
'''
activation_key = RegistrationProfile.objects.get(user=user).activation_key
context = {}
messages.success(request,
_(u'Uspješno je napravljen novi nalog! '
u'Provjeri svoj inbox, poslali smo ti email na %s '
u'sa linkom da potvrdiš email i aktiviraš svoj nalog.' % user.email))
context['user'] = user
context['verification_url'] = (settings.SITE_ROOT +
reverse('authentication:auth-activate',
kwargs=dict(activation_key=activation_key)))
user_email(user, _('Zdravo!'),
'authentication/email_registration_success.txt',
'authentication/email_registration_success.html',
context)
@classmethod
def create_unique_user(cls, email, password):
'''
Creates a unique user. Later, I'll burn in hell for this.
'''
 
create_inactive_user = RegistrationProfile.objects.create_inactive_user
 
if Site._meta.installed: #@UndefinedVariable
site = Site.objects.get_current()
else:
site = RequestSite(request)
 
try:
username = uuid.uuid1().hex[:30]
user = create_inactive_user(username, email, password, site, send_email=False)
 
return user
except IntegrityError:
return cls.create_unique_user(email, password)
 
def dispatch(self, request, *args, **kwargs):
if request.user.is_active:
# Redirect registered users
return HttpResponseRedirect(reverse('submissions:submissions-submission-list'))
else:
return super(AccountRegistrationView, self).dispatch(request, *args, **kwargs)
 
 
class AccountActivationView(RedirectView):
url = lazy_reverse('core:core-welcome')
def get(self, request, activation_key=None, *args, **kwargs):
activated = RegistrationProfile.objects.activate_user(activation_key)
if activated:
messages.success(request, _(u'Super! Tvoj nalog je sada aktivan i možes se njim prijaviti.'))
else:
messages.error(request,
_(u'Hej, nismo našli tvoj nalog! '
u'Provjeri još jednom link za aktiviraje naloga.'))
return super(AccountActivationView, self).get(request, *args, **kwargs)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.