6a23e784881f6af032d6defcc767aa625bceb41f
[myslice.git] / portal / forms.py
1 # -*- coding: utf-8 -*-
2 #
3 # portal/forms.py: forms for the portal application
4 # This file is part of the Manifold project.
5 #
6 # Authors:
7 #   Jordan AugĂ© <jordan.auge@lip6.fr>
8 #   Mohammed-Yasin Rahman <mohammed-yasin.rahman@lip6.fr>
9 # Copyright 2013, UPMC Sorbonne UniversitĂ©s / LIP6
10 #
11 # This program is free software; you can redistribute it and/or modify it under
12 # the terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 3, or (at your option) any later version.
14
15 # This program is distributed in the hope that it will be useful, but WITHOUT
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18 # details.
19
20 # You should have received a copy of the GNU General Public License along with
21 # this program; see the file COPYING.  If not, write to the Free Software
22 # Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 from django import forms
25 from portal.models import PendingUser, PendingSlice
26 #from crispy_forms.helper import FormHelper
27 #from crispy_forms.layout import Submit
28 from django.utils.translation import ugettext_lazy as _
29
30 # xxx painful, but... 
31 # bootstrap3 requires the <input> fields to be tagged class='form-control'
32 # my first idea was to add this in the view template of course, BUT
33 # I can't find a way to access the 'type=' value for a given field
34 # I've looked rather deeply out there but to no avail so far
35 # so as we have a demo coming up soon, and until we can come with a less intrusive way to handle this...
36
37 # initial version was
38 #class ContactForm(forms.Form):
39 #    first_name = forms.CharField()
40 #    last_name = forms.CharField()
41 #    affiliation = forms.CharField()
42 #    subject = forms.CharField(max_length=100)
43 #    message = forms.CharField(widget=forms.Textarea)
44 #    email = forms.EmailField()
45 #    cc_myself = forms.BooleanField(required=False)
46
47 class ContactForm(forms.Form):
48     first_name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
49     last_name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
50     affiliation = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
51     email = forms.EmailField(widget=forms.TextInput(attrs={'class':'form-control'}))
52     subject = forms.CharField(max_length=100,widget=forms.TextInput(attrs={'class':'form-control'}))
53     message = forms.CharField(widget=forms.Textarea(attrs={'class':'form-control'}))
54     cc_myself = forms.BooleanField(required=False,widget=forms.CheckboxInput(attrs={'class':'form-control'}))
55
56 class SliceRequestForm(forms.Form):
57 #    slice_name = forms.CharField()
58 #    authority_hrn = forms.ChoiceField(choices=[(1, 'un')])
59 #    number_of_nodes  = forms.DecimalField()
60 #    type_of_nodes = forms.CharField()
61 #    purpose = forms.CharField(widget=forms.Textarea)
62 #    email = forms.EmailField()
63 #    cc_myself = forms.BooleanField(required=False)
64
65     slice_name = forms.CharField(
66         widget=forms.TextInput(attrs={'class':'form-control'}), 
67         help_text="Enter a name for the slice you wish to create")
68     authority_hrn = forms.ChoiceField(
69         widget    = forms.Select(attrs={'class':'form-control'}),
70         choices   = [],
71         help_text = "Please select an authority responsible for vetting your slice")
72     number_of_nodes = forms.DecimalField(
73         widget    = forms.TextInput(attrs={'class':'form-control'}),
74         help_text = "Enter the number of nodes you expect to request (informative only)")
75     type_of_nodes = forms.CharField(
76         widget    = forms.TextInput(attrs={'class':'form-control'}),
77         help_text = "Enter the type of nodes you expect to request (informative only)")
78     purpose = forms.CharField(
79         widget    = forms.Textarea(attrs={'class':'form-control'}),
80         help_text = "Enter the purpose of your experiment (informative only)")
81     email = forms.EmailField(
82         widget    = forms.TextInput(attrs={'class':'form-control'}),
83         help_text = "Enter your email address")
84     cc_myself = forms.BooleanField(
85         widget    = forms.CheckboxInput(attrs={'class':'form-control'}),
86         required  = False,
87         help_text = "Please indicate whether you would like to be CC'ed to the request email")
88
89     def __init__(self, *args, **kwargs):
90         initial =  kwargs.get('initial', {})
91         authority_hrn = initial.get('authority_hrn', None)
92
93         # set just the initial value
94         # in the real form needs something like this {'authority_hrn':'a'}
95         # but in this case you want {'authority_hrn':('a', 'letter_a')}
96         if authority_hrn:
97             kwargs['initial']['authority_hrn'] = authority_hrn[0]
98
99         # create the form
100         super(SliceRequestForm, self).__init__(*args, **kwargs)
101
102         # self.fields only exist after, so a double validation is needed
103         if authority_hrn:# and authority_hrn[0] not in (c[0] for c in authority_hrn):
104             # XXX This does not work, the choicefield is not updated...
105             #self.fields['authority_hrn'].choices.extend(authority_hrn)
106             self.fields['authority_hrn'] = forms.ChoiceField(
107                 widget    = forms.Select(attrs={'class':'form-control'}),
108                 choices   = authority_hrn,
109                 help_text = "Please select an authority responsible for vetting your slice")
110