1 # -*- coding: utf-8 -*-
3 # Code borrowed from the django-registration application
4 # https://bitbucket.org/ubernostrum/django-registration
6 from django.shortcuts import redirect
7 from django.views.generic.edit import FormView
8 from django.views.generic.base import TemplateView
10 class _RequestPassingFormView(FormView):
12 A version of FormView which passes extra arguments to certain
13 methods, notably passing the HTTP request nearly everywhere, to
14 enable finer-grained processing.
17 def get(self, request, *args, **kwargs):
18 # Pass request to get_form_class and get_form for per-request
20 form_class = self.get_form_class(request)
21 form = self.get_form(form_class)
22 return self.render_to_response(self.get_context_data(form=form))
24 def post(self, request, *args, **kwargs):
25 # Pass request to get_form_class and get_form for per-request
27 form_class = self.get_form_class(request)
28 form = self.get_form(form_class)
30 # Pass request to form_valid.
31 return self.form_valid(request, form)
33 return self.form_invalid(form)
35 def get_form_class(self, request=None):
36 return super(_RequestPassingFormView, self).get_form_class()
38 def get_form_kwargs(self, request=None, form_class=None):
39 return super(_RequestPassingFormView, self).get_form_kwargs()
41 def get_initial(self, request=None):
42 return super(_RequestPassingFormView, self).get_initial()
44 def get_success_url(self, request=None, user=None):
45 # We need to be able to use the request and the new user when
46 # constructing success_url.
47 return super(_RequestPassingFormView, self).get_success_url()
49 def form_valid(self, form, request=None):
50 return super(_RequestPassingFormView, self).form_valid(form)
52 def form_invalid(self, form, request=None):
53 return super(_RequestPassingFormView, self).form_invalid(form)
55 class RegistrationView(_RequestPassingFormView):
57 Base class for user registration views.
60 disallowed_url = 'registration_disallowed'
61 #form_class = RegistrationForm
62 http_method_names = ['get', 'post', 'head', 'options', 'trace']
64 template_name = 'user_register.html' #registration/registration_form.html'
66 def dispatch(self, request, *args, **kwargs):
68 Check that user signup is allowed before even bothering to
69 dispatch or do other processing.
72 if not self.registration_allowed(request):
73 return redirect(self.disallowed_url)
74 return super(RegistrationView, self).dispatch(request, *args, **kwargs)
76 def form_valid(self, request, form):
77 new_user = self.register(request, **form.cleaned_data)
78 success_url = self.get_success_url(request, new_user)
80 # success_url may be a simple string, or a tuple providing the
81 # full argument set for redirect(). Attempting to unpack it
82 # tells us which one it is.
84 to, args, kwargs = success_url
85 return redirect(to, *args, **kwargs)
87 return redirect(success_url)
89 def registration_allowed(self, request):
91 Override this to enable/disable user registration, either
92 globally or on a per-request basis.
97 def register(self, request, **cleaned_data):
99 Implement user-registration logic here. Access to both the
100 request and the full cleaned_data of the registration form is
104 raise NotImplementedError
106 class ActivationView(TemplateView):
108 Base class for user activation views.
111 http_method_names = ['get']
112 template_name = 'registration/activate.html'
114 def get(self, request, *args, **kwargs):
115 activated_user = self.activate(request, *args, **kwargs)
117 signals.user_activated.send(sender=self.__class__,
120 success_url = self.get_success_url(request, activated_user)
122 to, args, kwargs = success_url
123 return redirect(to, *args, **kwargs)
125 return redirect(success_url)
126 return super(ActivationView, self).get(request, *args, **kwargs)
128 def activate(self, request, *args, **kwargs):
130 Implement account-activation logic here.
133 raise NotImplementedError
135 def get_success_url(self, request, user):
136 raise NotImplementedError
139 # DEPRECATED #def user_register(request):
140 # DEPRECATED # if request.method == 'POST':
141 # DEPRECATED # form = UserRegisterForm(request.POST)
142 # DEPRECATED # if form.is_valid():
143 # DEPRECATED # first_name = form.cleaned_data['first_name']
144 # DEPRECATED # last_name = form.cleaned_data['last_name']
145 # DEPRECATED # email = form.cleaned_data['email']
146 # DEPRECATED # password = form.cleaned_data['password']
147 # DEPRECATED # password2 = form.cleaned_data['password2']
148 # DEPRECATED # keypair = form.cleaned_data['keypair']
149 # DEPRECATED # ## Ici nous pouvons traiter les données du formulaire
150 # DEPRECATED # #sujet = form.cleaned_data['sujet']
151 # DEPRECATED # #message = form.cleaned_data['message']
152 # DEPRECATED # #envoyeur = form.cleaned_data['envoyeur']
153 # DEPRECATED # #renvoi = form.cleaned_data['renvoi']
154 # DEPRECATED # ## Nous pourrions ici envoyer l'e-mail grâce aux données que nous venons de récupérer
155 # DEPRECATED # #envoi = True
159 # DEPRECATED # form = UserRegisterForm()
160 # DEPRECATED # return render(request, 'user_register.html', locals())
162 # DEPRECATED #def user_validate(request):
165 def slice_request(request):
166 if request.method == 'POST':
167 form = SliceRequestForm(request.POST)
169 slice_name = form.cleaned_data['slice_name']
171 form = SliceRequestForm()
172 return render(request, 'slice_request.html', locals())
174 def slice_validate(request):