# -*- coding: utf-8 -*- # # Code borrowed from the django-registration application # https://bitbucket.org/ubernostrum/django-registration from django.shortcuts import redirect from django.views.generic.edit import FormView from django.views.generic.base import TemplateView class _RequestPassingFormView(FormView): """ A version of FormView which passes extra arguments to certain methods, notably passing the HTTP request nearly everywhere, to enable finer-grained processing. """ def get(self, request, *args, **kwargs): # Pass request to get_form_class and get_form for per-request # form control. form_class = self.get_form_class(request) form = self.get_form(form_class) return self.render_to_response(self.get_context_data(form=form)) def post(self, request, *args, **kwargs): # Pass request to get_form_class and get_form for per-request # form control. form_class = self.get_form_class(request) form = self.get_form(form_class) if form.is_valid(): # Pass request to form_valid. return self.form_valid(request, form) else: return self.form_invalid(form) def get_form_class(self, request=None): return super(_RequestPassingFormView, self).get_form_class() def get_form_kwargs(self, request=None, form_class=None): return super(_RequestPassingFormView, self).get_form_kwargs() def get_initial(self, request=None): return super(_RequestPassingFormView, self).get_initial() def get_success_url(self, request=None, user=None): # We need to be able to use the request and the new user when # constructing success_url. return super(_RequestPassingFormView, self).get_success_url() def form_valid(self, form, request=None): return super(_RequestPassingFormView, self).form_valid(form) def form_invalid(self, form, request=None): return super(_RequestPassingFormView, self).form_invalid(form) class RegistrationView(_RequestPassingFormView): """ Base class for user registration views. """ disallowed_url = 'registration_disallowed' #form_class = RegistrationForm http_method_names = ['get', 'post', 'head', 'options', 'trace'] success_url = None template_name = 'user_register.html' #registration/registration_form.html' def dispatch(self, request, *args, **kwargs): """ Check that user signup is allowed before even bothering to dispatch or do other processing. """ if not self.registration_allowed(request): return redirect(self.disallowed_url) return super(RegistrationView, self).dispatch(request, *args, **kwargs) def form_valid(self, request, form): new_user = self.register(request, **form.cleaned_data) success_url = self.get_success_url(request, new_user) # success_url may be a simple string, or a tuple providing the # full argument set for redirect(). Attempting to unpack it # tells us which one it is. try: to, args, kwargs = success_url return redirect(to, *args, **kwargs) except ValueError: return redirect(success_url) def registration_allowed(self, request): """ Override this to enable/disable user registration, either globally or on a per-request basis. """ return True def register(self, request, **cleaned_data): """ Implement user-registration logic here. Access to both the request and the full cleaned_data of the registration form is available here. """ raise NotImplementedError class ActivationView(TemplateView): """ Base class for user activation views. """ http_method_names = ['get'] template_name = 'registration/activate.html' def get(self, request, *args, **kwargs): activated_user = self.activate(request, *args, **kwargs) if activated_user: signals.user_activated.send(sender=self.__class__, user=activated_user, request=request) success_url = self.get_success_url(request, activated_user) try: to, args, kwargs = success_url return redirect(to, *args, **kwargs) except ValueError: return redirect(success_url) return super(ActivationView, self).get(request, *args, **kwargs) def activate(self, request, *args, **kwargs): """ Implement account-activation logic here. """ raise NotImplementedError def get_success_url(self, request, user): raise NotImplementedError # DEPRECATED #def user_register(request): # DEPRECATED # if request.method == 'POST': # DEPRECATED # form = UserRegisterForm(request.POST) # DEPRECATED # if form.is_valid(): # DEPRECATED # first_name = form.cleaned_data['first_name'] # DEPRECATED # last_name = form.cleaned_data['last_name'] # DEPRECATED # email = form.cleaned_data['email'] # DEPRECATED # password = form.cleaned_data['password'] # DEPRECATED # password2 = form.cleaned_data['password2'] # DEPRECATED # keypair = form.cleaned_data['keypair'] # DEPRECATED # ## Ici nous pouvons traiter les données du formulaire # DEPRECATED # #sujet = form.cleaned_data['sujet'] # DEPRECATED # #message = form.cleaned_data['message'] # DEPRECATED # #envoyeur = form.cleaned_data['envoyeur'] # DEPRECATED # #renvoi = form.cleaned_data['renvoi'] # DEPRECATED # ## Nous pourrions ici envoyer l'e-mail grâce aux données que nous venons de récupérer # DEPRECATED # #envoi = True # DEPRECATED # # DEPRECATED # # DEPRECATED # else: # DEPRECATED # form = UserRegisterForm() # DEPRECATED # return render(request, 'user_register.html', locals()) # DEPRECATED # # DEPRECATED #def user_validate(request): # DEPRECATED # pass def slice_request(request): if request.method == 'POST': form = SliceRequestForm(request.POST) if form.is_valid(): slice_name = form.cleaned_data['slice_name'] else: form = SliceRequestForm() return render(request, 'slice_request.html', locals()) def slice_validate(request): pass