class SendMessageForm(forms.Form): to = forms.TypedChoiceField( label=_("To"), choices=[], coerce=int, required=True, widget=forms.RadioSelect(attrs={"class": "form-control"})) subject = forms.CharField( label=_("Subject"), max_length=230, widget=forms.TextInput(attrs={"class": "form-control"})) message = forms.CharField( widget=forms.Textarea(attrs={"class": "form-control"}), label=_("Your message")) def __init__(self, foirequest, *args, **kwargs): super(SendMessageForm, self).__init__(*args, **kwargs) self.foirequest = foirequest choices = [(0, _("Default address of %(publicbody)s") % { "publicbody": foirequest.public_body.name })] choices.extend([ (m.id, m.reply_address_entry) for k, m in foirequest.possible_reply_addresses().items() ]) self.fields['to'].choices = choices if foirequest.law and foirequest.law.email_only: self.fields['send_address'] = forms.BooleanField( label=_("Send physical address"), help_text=(_( 'If the public body is asking for your post ' 'address, check this and we will append it to your message.' )), required=False) def save(self, user): if self.cleaned_data["to"] == 0: recipient_name = self.foirequest.public_body.name recipient_email = self.foirequest.public_body.email recipient_pb = self.foirequest.public_body else: message = list( filter(lambda x: x.id == self.cleaned_data["to"], list(self.foirequest.messages)))[0] recipient_name = message.sender_name recipient_email = message.sender_email recipient_pb = message.sender_public_body return self.foirequest.add_message(user, recipient_name, recipient_email, self.cleaned_data["subject"], self.cleaned_data['message'], recipient_pb=recipient_pb, send_address=self.cleaned_data.get( 'send_address', True))
class DonateForm(forms.Form): """Form for accepting a donation.""" name = pyoforms.StripCharField(max_length=200) email = forms.EmailField(max_length=255) phone = pyoforms.StripCharField(max_length=20) country_code = forms.TypedChoiceField( choices=model.Profile._meta.get_field('country_code').choices, widget=forms.RadioSelect(), ) school = pyoforms.ModelChoiceField( queryset=model.School.objects.filter(auto=False).order_by('name'), empty_label=u"I'm not affiliated with a school or program", required=False, widget=SchoolRadioSelect, initial=u'', ) addschool = forms.BooleanField( initial=False, required=False, widget=forms.HiddenInput) amount = pyoforms.StripCharField(max_length=20) def __init__(self, *args, **kwargs): """Also instantiate a nested SchoolForm.""" super(DonateForm, self).__init__(*args, **kwargs) self.addschool_form = SchoolForm(self.data or None, prefix='addschool') def clean(self): """ Verify password fields match and school is provided. If addschool is True, build a new School based on data in nested SchoolForm. If not, and no school was selected, auto-construct one. """ data = self.cleaned_data if data.get('addschool'): if self.addschool_form.is_valid(): data['school'] = self.addschool_form.save(commit=False) else: raise forms.ValidationError( "Could not add a school.") else: # reinstantiate unbound addschool_form to avoid spurious errors self.addschool_form = SchoolForm(prefix='addschool') if data.get('email') and not data.get('school'): data['school'] = model.School( name=(u"%f-%s" % (time.time(), data['email']))[:200], postcode="", auto=True, ) return data
class etat_nutri_form(floppyforms.Form): faim = floppyforms.IntegerField( widget=Slider, label="Avez-vous faim ? ", help_text= "Echelle : de 'pas du tout' (gauche) à 'extrèmement' (droite )") def clean_faim(self): faim = self.cleaned_data['faim'] if not 0 <= faim <= 100: raise forms.ValidationError("Enter a value between 0 and 100") return faim soif = floppyforms.IntegerField( widget=Slider, label="Avez-vous soif ? ", help_text= "Echelle : de 'pas du tout' (gauche) à 'extrèmement' (droite )") def clean_soif(self): soif = self.cleaned_data['soif'] if not 0 <= soif <= 100: raise forms.ValidationError("Enter a value between 0 and 100") return soif sensation_estomac = floppyforms.IntegerField( widget=Slider, label="Avez-vous la sensation d'avoir l'estomac rempli ? ", help_text= "Echelle : de 'pas du tout' (gauche) à 'extrèmement' (droite )") plaisir_manger = floppyforms.IntegerField( widget=Slider, label="Eprouveriez du plaisir à manger maintenant ? ", help_text= "Echelle : de 'pas du tout' (gauche) à 'extrèmement' (droite )") plaisir_boire = floppyforms.IntegerField( widget=Slider, label="Eprouveriez du plaisir à boire maintenant ? ", help_text= "Echelle : de 'pas du tout' (gauche) à 'extrèmement' (droite )") quantite_manger = floppyforms.IntegerField( widget=Slider, label="Quelle quantité seriez-vous capable de manger en ce moment ? ", help_text= "Echelle : de 'pas du tout' (gauche) à 'énormément' (droite )") quantite_boire = floppyforms.IntegerField( widget=Slider, label="Quelle quantité seriez-vous capable de boire en ce moment ? ", help_text= "Echelle : de 'pas du tout' (gauche) à 'énormément' (droite )") choix = [("00:15", "moins de 15 min"), ("00:30", "entre 15 et 30 min"), ("00:45", "45 min"), ("01:00", "1 heure"), ("01:30", "1h30"), ("02:00", "2h"), ("02:30", "2h30"), ("03:00", "3h"), ("03:30", "3h30"), ("04:00", "4h"), ("04:30", "4h30"), ("05:00", "5h"), ("06:00", "6h00"), ("07:00", "7h"), ("08:00", "8h"), ("09:00", "9h"), ("10:00", "10h"), ("11:00", "11h"), ("12:00", "12h ou plus")] heure_dernier_repas = floppyforms.ChoiceField( choices=choix, label="A combien de temps remonte votre dernier repas (hors snack)?") heure_derniere_prise = floppyforms.ChoiceField( choices=choix, label= "A combien de temps remonte votre dernière prise alimentaire (snack compris)?" ) Choices = [("petit-dej", "petit-dejeuner"), ("dej", "déjeuner"), ("snack", "goûter/snack/collation"), ("diner", "diner")] prochain_repas = floppyforms.ChoiceField( widget=floppyforms.RadioSelect(), choices=Choices, label="Quel est le prochain repas que vous pensez prendre ?")
class AuditeurLibreForm(DossierReceptionForm): choix = forms.ChoiceField(choices=(('complet', 'Complet'), ), widget=forms.RadioSelect(), required=True)
class RegistrationForm(forms.Form): """ Form for registering a new user account. Validates that the email address is not already in use, and requires the password to be entered twice to catch typos. Also allows user to either pick from an existing list of schools or enter a new one. """ name = pyoforms.StripCharField(max_length=200) role = pyoforms.StripCharField(max_length=200) password = forms.CharField(widget=forms.PasswordInput(render_value=False)) password_confirm = forms.CharField( label="confirm password", widget=forms.PasswordInput(render_value=False)) email = forms.EmailField(max_length=255) phone = pyoforms.StripCharField(max_length=20) country_code = forms.TypedChoiceField( choices=model.Profile._meta.get_field('country_code').choices, widget=forms.RadioSelect(), ) school = pyoforms.ModelChoiceField( queryset=model.School.objects.filter(auto=False).order_by('name'), empty_label=u"I'm not affiliated with a school or program", required=False, widget=SchoolRadioSelect, initial=u'', ) addschool = forms.BooleanField( initial=False, required=False, widget=forms.HiddenInput) terms_confirm = forms.BooleanField(required=True) def __init__(self, *args, **kwargs): """Also instantiate a nested SchoolForm.""" super(RegistrationForm, self).__init__(*args, **kwargs) self.addschool_form = SchoolForm(self.data or None, prefix='addschool') def clean(self): """ Verify password fields match and school is provided. If addschool is True, build a new School based on data in nested SchoolForm. If not, and no school was selected, auto-construct one. """ data = self.cleaned_data password = data.get('password') confirm = data.get('password_confirm') if password != confirm: raise forms.ValidationError("The passwords didn't match.") if data.get('addschool'): if self.addschool_form.is_valid(): data['school'] = self.addschool_form.save(commit=False) else: raise forms.ValidationError( "Could not add a school.") else: # reinstantiate unbound addschool_form to avoid spurious errors self.addschool_form = SchoolForm(prefix='addschool') if data.get('email') and not data.get('school'): data['school'] = model.School( name=(u"%f-%s" % (time.time(), data['email']))[:200], postcode="", auto=True, ) return data def clean_email(self): """Validate that the supplied email address is unique.""" if model.User.objects.filter( email__iexact=self.cleaned_data['email']): raise forms.ValidationError( "This email address is already in use. " "Please supply a different email address." ) return self.cleaned_data['email'] def save(self): """Save and return new user profile.""" school = self.cleaned_data['school'] if school.id is None: # this could just set country_code and then school.save(), but that # creates a race condition for two users creating same school at # same time, resulting in IntegrityError school, created = model.School.objects.get_or_create( name=school.name, postcode=school.postcode, defaults={ 'country_code': self.cleaned_data['country_code'], 'auto': school.auto, }, ) profile = model.Profile.create_with_user( name=self.cleaned_data['name'], email=self.cleaned_data['email'], password=self.cleaned_data['password'], role=self.cleaned_data['role'], country_code=self.cleaned_data['country_code'], school=school, school_staff=True, email_confirmed=False, is_active=True, email_notifications=True, ) return profile