This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
polynum/polynum/request/forms.py

356 lines
12 KiB
Python

# -*- coding: utf-8 -*-
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Submit, HTML, Button, Row, Field
from crispy_forms.bootstrap import AppendedText, PrependedText, FormActions
from widgets import FileInput
from polynum.base.models import *
import datetime
class DocumentUploadForm(forms.Form):
@classmethod
def display_name(self):
return _(u'Envoi du document')
def __init__(self, *args, **kwargs):
super(DocumentUploadForm, self).__init__(*args, **kwargs)
self.fields.update({
'document_file': forms.FileField(
widget = FileInput,
label = _(u'Document, en format PDF ou papier'),
required = False,
),
})
# crispy
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
HTML(_(u"""
<p>Téléchargez ici le document que vous désirez envoyer au service
reprographie.</p>
""")),
Field('document_file'),
)
def pprint_data(self, data):
'''
get the "cleaned" datas from the form, return a list of tuples
[ (u'description', u'value'), ... ]
'''
if 'document_file' in data:
return [ (_(u'Document'), data.get('document_file') \
or _(u'sous forme papier')) ]
else:
return []
class DocumentDetailsForm(forms.Form):
@classmethod
def display_name(self):
return _(u'Détails sur le document')
def __init__(self, *args, **kwargs):
super(DocumentDetailsForm, self).__init__(*args, **kwargs)
self.fields.update({
'document_title': forms.CharField(
label = _(u'Titre du document'),
max_length = 128,
required = True,
),
'document_pages': forms.IntegerField(
label = _(u'Nombre de pages du document'),
required = True,
help_text = _(u"""
Saisir le nombre de pages exact facilite le travail du service
reprographie.
""")
),
'sponsor': forms.CharField(
label = _(u'Commanditaire'),
max_length = 128,
required = False,
help_text = _(u"""
Complétez cette information si vous faites cette demande pour
un tiers.
""")
),
'usage': forms.ModelChoiceField(
label = _(u'Usage du document'),
queryset = DocumentUsage.objects.all(),
required = True,
),
'entity': forms.CharField(
label = _(u'Entité administratrive (code)'),
max_length = 64,
required = True,
help_text = _(u"""
Utilisez le sélecteur ci-dessous pour choisir le code de l'entité.
""")
),
})
# crispy
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Field('document_title', css_class='span12'),
Field('document_pages', css_class='input-mini'),
Field('sponsor', css_class='span12'),
Field('usage', css_class='span6'),
Field('entity', template = 'entityselector.html'),
)
def pprint_data(self, data):
ret = []
fields = [ 'document_title', 'document_pages', 'sponsor', 'usage', 'entity', ]
for field in fields:
ret += [ (self.fields[field].label, u'%s' % data.get(field, '')) ]
return ret
class ReproDetailsForm(forms.Form):
@classmethod
def display_name(self):
return _(u'Choix de reprographie')
def __init__(self, *args, **kwargs):
super(ReproDetailsForm, self).__init__(*args, **kwargs)
self.fields.update({
'copies': forms.IntegerField(
label = _(u'Nombre de copies'),
required = True,
),
'base_profile': forms.ChoiceField(
label = _(u'Profil de reprographie'),
widget = forms.RadioSelect,
choices = ((u'%s' % p.id, p) for p in Profile.objects.all()),
required = True,
help_text = _(u"""
Choissez le profil de base, puis complétez si besoin vos
choix dans les options ci-dessous.
"""),
),
'profile_additions': forms.CharField(
widget=forms.Textarea,
label = _(u'Prestations complétementaires'),
required = False,
),
})
# crispy
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Field('copies', css_class='input-mini'),
Field('base_profile', template = 'radioprofile.html'),
HTML(u'<hr /><h3>%s</h3>' % _(u'Détails de vos choix')),
)
optionfields = []
# build "options" fields
for option in ProfileOption.objects.all():
fieldname = 'profile_option_%s' % option.id
optionfields.append(fieldname)
choices = [(u'%s' % c.id, c.name) \
for c in ProfileOptionChoice.objects.filter(option=option)]
if option.list_type == 'L': # Liste (choix unique)
self.fields[fieldname] = forms.ChoiceField(
label = u'%s' % option.name,
choices = choices,
required = False,
help_text = option.description,
)
elif option.list_type == 'R': # Radio (choix unique)
self.fields[fieldname] = forms.ChoiceField(
label = u'%s' % option.name,
widget = forms.RadioSelect,
choices = choices,
required = False,
help_text = option.description,
)
elif option.list_type == 'C': # Cases à cocher (choix multiple)
self.fields[fieldname] = forms.MultipleChoiceField(
label = u'%s' % option.name,
widget = forms.CheckboxSelectMultiple,
choices = choices,
required = False,
help_text = option.description,
)
elif option.list_type == 'LM': # Liste (choix multiple)
self.fields[fieldname] = forms.MultipleChoiceField(
label = u'%s' % option.name,
choices = choices,
required = False,
help_text = option.description,
)
# hack to display fields side-by-side (should be done in a template)
for n in xrange(len(optionfields)/2):
left_field = Field(optionfields[n*2])
try:
right_field = Field(optionfields[n*2+1])
except:
right_field = HTML('')
self.helper.layout.fields.append(Div(
Div(left_field, css_class='span6'),
Div(right_field, css_class='span6'),
css_class="row",
))
self.helper.layout.fields.extend([
Field('profile_additions', css_class='span12'),
])
def pprint_data(self, data):
ret = []
fields = [ 'copies', ]
for field in fields:
ret += [ (self.fields[field].label, u'%s' % data.get(field, '')) ]
base_profile = data.get('base_profile')
if base_profile:
profile = Profile.objects.get(id=base_profile)
ret += [ (_(u'Profil de base'), u'%s' % profile) ]
# pprint only options not in profile
base_choices = [u'%s' % c.id for c in profile.choices.all()]
for k in [data[k] for k in data if k.startswith('profile_option_') and data[k]
and (data[k] not in base_choices)]:
choice = ProfileOptionChoice.objects.get(id=k)
ret += [ (u'%s' % choice.option.name, u'%s' % choice.name) ]
if data.get('profile_additions'):
ret += [ (_(u'Prestations complétementaires'), _(u'oui')) ]
return ret
class DeliveryForm(forms.Form):
@classmethod
def display_name(self):
return _(u"Options de livraison")
def __init__(self, *args, **kwargs):
super(DeliveryForm, self).__init__(*args, **kwargs)
self.fields.update({
'delivery_place': forms.ModelChoiceField(
label = _(u'Lieu de livraison'),
queryset = DeliveryPlace.objects.all(),
empty_label=_(u'Faites un choix'),
required = True,
),
'delivery_date': forms.DateField(
label = _(u'Date de livraison'),
initial = (datetime.date.today()+datetime.timedelta(3)),
),
})
# crispy
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Field('delivery_place', css_class='span6'),
Field('delivery_date', css_class='span2 datepicker'),
HTML(u"<em>TODO ici: données de contact, mail + téls</em>"),
)
def pprint_data(self, data):
ret = []
fields = [ 'delivery_place', 'delivery_date' ]
for field in fields:
ret += [ (self.fields[field].label, u'%s' % data.get(field, '')) ]
return ret
class CopyrigtsForm(forms.Form):
COPYRIGHT_CHOICES = (
(u'OK1', _(u"""
Le document ne contient pas d'extrait(s) d'oeuvre(s) soumis(s)
au le droit d'auteur
""")),
(u'OK2', _(u"""
Le document contient un ou des extrait(s) d'oeuvres(s)
soumise(s) au droit d'auteur
""")),
)
@classmethod
def display_name(self):
return _(u"Droits d'auteur")
def __init__(self, *args, **kwargs):
super(CopyrigtsForm, self).__init__(*args, **kwargs)
self.fields.update({
'licence': forms.ChoiceField(
label = _(u'Diffusion de votre fichier'),
widget = forms.RadioSelect,
choices = ((u'%s' % dl.id, dl) for dl in DocumentLicence.objects.all()),
required = True,
),
'copyright': forms.ChoiceField(
label = _(u'Droits de copie'),
widget = forms.RadioSelect,
choices = self.COPYRIGHT_CHOICES,
required = True,
),
})
# crispy
self.helper = FormHelper()
self.helper.form_tag = False
def pprint_data(self, data):
ret = []
licence = data.get('licence')
if licence:
ret += [ (self.fields['licence'].label, u'%s' % DocumentLicence.objects.get(id=licence)) ]
copyright = data.get('copyright')
if copyright:
ret += [ (self.fields['copyright'].label, u'%s' % dict(self.COPYRIGHT_CHOICES)[copyright]) ]
return ret
class ValidationForm(forms.Form):
@classmethod
def display_name(self):
return _(u"Validation")
def __init__(self, *args, **kwargs):
super(ValidationForm, self).__init__(*args, **kwargs)
# crispy
self.helper = FormHelper()
self.helper.form_tag = False
def pprint_data(self, data):
pass # no data in this step