cleanup of imports in portal.views
[unfold.git] / portal / util.py
1 # -*- coding: utf-8 -*-
2 #
3 # Code borrowed from the django-registration application
4 # https://bitbucket.org/ubernostrum/django-registration
5
6 from django.shortcuts import redirect
7 from django.views.generic.edit import FormView
8 from django.views.generic.base  import TemplateView
9
10 class _RequestPassingFormView(FormView):
11     """
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.
15     
16     """
17     def get(self, request, *args, **kwargs):
18         # Pass request to get_form_class and get_form for per-request
19         # form control.
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))
23
24     def post(self, request, *args, **kwargs):
25         # Pass request to get_form_class and get_form for per-request
26         # form control.
27         form_class = self.get_form_class(request)
28         form = self.get_form(form_class)
29         if form.is_valid():
30             # Pass request to form_valid.
31             return self.form_valid(request, form)
32         else:
33             return self.form_invalid(form)
34
35     def get_form_class(self, request=None):
36         return super(_RequestPassingFormView, self).get_form_class()
37
38     def get_form_kwargs(self, request=None, form_class=None):
39         return super(_RequestPassingFormView, self).get_form_kwargs()
40
41     def get_initial(self, request=None):
42         return super(_RequestPassingFormView, self).get_initial()
43
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()
48
49     def form_valid(self, form, request=None):
50         return super(_RequestPassingFormView, self).form_valid(form)
51
52     def form_invalid(self, form, request=None):
53         return super(_RequestPassingFormView, self).form_invalid(form)
54
55 class RegistrationView(_RequestPassingFormView):
56     """
57     Base class for user registration views.
58     
59     """
60     disallowed_url = 'registration_disallowed'
61     #form_class =  RegistrationForm
62     http_method_names = ['get', 'post', 'head', 'options', 'trace']
63     success_url = None
64     template_name = 'user_register.html' #registration/registration_form.html'
65
66     def dispatch(self, request, *args, **kwargs):
67         """
68         Check that user signup is allowed before even bothering to
69         dispatch or do other processing.
70         
71         """
72         if not self.registration_allowed(request):
73             return redirect(self.disallowed_url)
74         return super(RegistrationView, self).dispatch(request, *args, **kwargs)
75
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)
79         
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.
83         try:
84             to, args, kwargs = success_url
85             return redirect(to, *args, **kwargs)
86         except ValueError:
87             return redirect(success_url)
88
89     def registration_allowed(self, request):
90         """
91         Override this to enable/disable user registration, either
92         globally or on a per-request basis.
93         
94         """
95         return True
96
97     def register(self, request, **cleaned_data):
98         """
99         Implement user-registration logic here. Access to both the
100         request and the full cleaned_data of the registration form is
101         available here.
102         
103         """
104         raise NotImplementedError
105
106 class ActivationView(TemplateView):
107     """
108     Base class for user activation views.
109     
110     """
111     http_method_names = ['get']
112     template_name = 'registration/activate.html'
113
114     def get(self, request, *args, **kwargs):
115         activated_user = self.activate(request, *args, **kwargs)
116         if activated_user:
117             signals.user_activated.send(sender=self.__class__,
118                                         user=activated_user,
119                                         request=request)
120             success_url = self.get_success_url(request, activated_user)
121             try:
122                 to, args, kwargs = success_url
123                 return redirect(to, *args, **kwargs)
124             except ValueError:
125                 return redirect(success_url)
126         return super(ActivationView, self).get(request, *args, **kwargs)
127
128     def activate(self, request, *args, **kwargs):
129         """
130         Implement account-activation logic here.
131         
132         """
133         raise NotImplementedError
134
135     def get_success_url(self, request, user):
136         raise NotImplementedError
137
138
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
156 # DEPRECATED #
157 # DEPRECATED #            
158 # DEPRECATED #    else:
159 # DEPRECATED #        form = UserRegisterForm()
160 # DEPRECATED #    return render(request, 'user_register.html', locals())
161 # DEPRECATED #
162 # DEPRECATED #def user_validate(request):
163 # DEPRECATED #    pass
164
165 def slice_request(request):
166     if request.method == 'POST':
167         form = SliceRequestForm(request.POST)
168         if form.is_valid():
169             slice_name = form.cleaned_data['slice_name']
170     else:
171         form = SliceRequestForm()
172     return render(request, 'slice_request.html', locals())
173
174 def slice_validate(request):
175     pass