updated portal application
[myslice.git] / portal / views.py
1 #-*- coding: utf-8 -*-
2
3 from portal.portalpage  import PortalPage
4 from plugins.wizard     import Wizard
5 from plugins.form       import CreateForm
6 from plugins.raw.raw    import Raw          # XXX
7
8 from myslice.viewutils  import the_user
9
10 from django.template.loader import render_to_string
11 from django.template import RequestContext
12 from django.views import generic
13 from django.shortcuts import render
14
15 from portal.forms  import RegisterUserForm
16
17 from django.contrib.formtools.wizard.views import NamedUrlSessionWizardView
18 #from django.core.files.storage import FileSystemStorage
19 from django.core.files.storage import default_storage
20
21 #class MerlinWizard(NamedUrlSessionWizardView):
22 #
23 #    ...
24 #    ...
25 #
26 #    @classonlymethod
27 #    def as_view(cls, *args, **kwargs):
28 #        kwargs.update({
29 #            'form_list': [
30 #                NameForm,
31 #                QuestForm,
32 #                ColorForm,
33 #            ],
34 #            'url_name': 'merlin_wizard'
35 #        })
36 #        return super(MerlinWizard, cls).as_view(*args, **kwargs)
37
38 class RegisterUserWizardView(NamedUrlSessionWizardView):
39 #class RegisterUserWizardView(LoginRequiredMixin, NamedUrlSessionWizardView):
40     # Notice that I specify a file storage instance. If you don't specify this,
41     # and you need to support FileField or ImageField in your forms, you'll get
42     # errors from Django. This is something else I think could be handled by
43     # the views better. Seems to me that it should just use whatever the
44     # default/specified storage is for the rest of your project/application.
45     file_storage = default_storage # FileSystemStorage()
46     template_name = "register_user_wizard.html"
47
48     def done(self, form_list, **kwargs):
49         step1_form = form_list[0]
50         step2_form = form_list[1]
51
52         productext = self.create_product(product_form)
53         shippings = self.create_shippings(productext, shipping_forms)
54         images = self.create_images(productext, image_forms)
55
56         if all([productext, shippings, images]):
57             del self.request.session["wizard_product_wizard_view"]
58
59             messages.success(self.request,
60                 _("Your product has been created."))
61             return HttpResponseRedirect(self.get_success_url(productext))
62
63         messages.error(self.request, _("Something went wrong creating your "
64             "product. Please try again or contact support."))
65         return HttpResponseRedirect(reverse("register_wizard"))
66
67     #def get_form_kwargs(self, step):
68     #    if step == "product":
69     #        return {"user": self.request.user}
70     #    return {}
71
72 # The portal should hook the slice and user creation pages
73
74 def register_user(request):
75     
76     if request.method == 'POST':
77         form = RegisterUserForm(request.POST) # Nous reprenons les données
78         if form.is_valid():
79             first_name = form.cleaned_data['first_name']
80             last_name  = form.cleaned_data['last_name']
81             email      = form.cleaned_data['email']
82             password   = form.cleaned_data['password']
83             password2  = form.cleaned_data['password2']
84             keypair    = form.cleaned_data['keypair']
85             ## Ici nous pouvons traiter les données du formulaire
86             #sujet = form.cleaned_data['sujet']
87             #message = form.cleaned_data['message']
88             #envoyeur = form.cleaned_data['envoyeur']
89             #renvoi = form.cleaned_data['renvoi']
90             ## Nous pourrions ici envoyer l'e-mail grâce aux données que nous venons de récupérer
91             #envoi = True
92     else:
93         form = RegisterUserForm()
94     return render(request, 'register_user.html', locals())
95
96 def index(request):
97
98     WIZARD_TITLE = 'User registration'
99     STEP1_TITLE  = 'Enter your details'
100     STEP2_TITLE  = 'Select your institution'
101     STEP3_TITLE  = 'Authentication'
102     STEP4_TITLE  = 'Request a slice (optional)'
103     STEP5_TITLE  = 'Waiting for validation'
104     STEP6_TITLE  = 'Account validated'
105
106     STEP0 = render_to_string('account_validated.html', context_instance=RequestContext(request))
107     STEP2_HTML   = """
108     coucou
109     """
110     STEP4 = """
111     mede
112     """
113     STEP5 = render_to_string('account_validated.html', context_instance=RequestContext(request))
114
115     p = PortalPage(request)
116
117     # This is redundant with the Wizard title
118     p << "<h3>User registration</h3>"
119
120     sons = []
121     start_step = 1
122
123     # STEP 1
124     # If the user already exists (is logged), let's display a summary of his account details
125     # Otherwise propose a form to fill in
126     if the_user(request):
127         # Fill a disabled form with user info
128         # Please logout to register another user
129         sons.append(Raw(page=p, title=STEP1_TITLE, togglable=False, html=STEP0))
130         start_step += 1
131     else:
132         # We could pass a list of fields also, instead of retrieving them from metadata
133         # Otherwise we need some heuristics to display nice forms
134         # XXX Could we log the user in after the form is validated ?
135         # XXX Explain the password is for XXX
136         field_list = [{
137             'name'        : 'First name',
138             'field'       : 'firstname',
139             'type'        : 'text',
140             'validate_rx' : '^[a-zA-Z -]+$',
141             'validate_err': 'Your first name must be comprised of letters only',
142             'description' : 'Enter your first name',
143         }, {
144             'name'        : 'Last name',
145             'field'       : 'lastname',
146             'type'        : 'text',
147             'validate_rx' : '^[a-zA-Z -]+$',
148             'validate_err': 'Your last name must be comprised of letters only',
149             'description' : 'Enter your last name',
150         }, { 
151             'name'        : 'Email',
152             'field'       : 'email',
153             'type'        : 'text',
154             'description' : 'Enter your email address',
155         }, {
156             'name'        : 'Password',
157             'field'       : 'password',
158             'type'        : 'password',
159             'description' : 'Enter your password',
160         }, {
161             'name'        : 'Confirm password',
162             'field'       : 'password2',
163             'type'        : 'password',
164             'description' : 'Enter your password again',
165         }]
166         sons.append(CreateForm(page = p, title = STEP1_TITLE, togglable = False, object = 'local:user', fields = field_list))
167
168     # STEP 2
169     # If the user already exists (is logged), let's display a summary of its institution
170     # Otherwise propose a form to fill in (we should base our selection on the email)
171     if the_user(request):
172         # Fill a disabled form with institution
173         # Please logout to register another user
174         sons.append(Raw(page=p, title=STEP2_TITLE, togglable=False, html="User created"))
175         start_step += 1
176     else:
177         sons.append(CreateForm(page = p, title = STEP2_TITLE, togglable = False, object = 'slice')) #institution'))
178
179     # STEP3
180     # Please should your prefered authentication method
181     # This step should allow the user to either choose the user or managed mode in MySlice
182     sons.append(Raw(page = p, title = STEP3_TITLE, togglable = False, html = STEP2_HTML))
183
184     # Step 4: Request a slice (optional)
185     sons.append(CreateForm(page = p, title = STEP4_TITLE, togglable = False, object = 'slice'))
186
187     # Step 5: Your request is waiting for validation
188     # Periodic refresh
189     sons.append(Raw(page = p, title = STEP5_TITLE, togglable = False, html = STEP4))
190
191     # Step 6: Account validation  = welcome for newly validated users
192     # . delegation
193     # . platforms
194     # . slice
195     # . pointers
196     sons.append(Raw(page = p, title = STEP6_TITLE, togglable = False, html = STEP5))
197
198     wizard = Wizard(
199         page       = p,
200         title      = WIZARD_TITLE,
201         togglable  = False,
202         sons       = sons,
203         start_step = start_step,
204     )
205
206     p << wizard.render(request) # in portal page if possible
207
208     return p.render()