8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-25 22:22:26 +00:00

Evite les doublons dans les id et les names des forms

Force la variable prefix a être setup pour chacun des modelForm avec
le nom du model mais n'override pas si une autre value est donnée.
L'id et le name des champs HTML généré sont donc prefixé par le nom du
model et on peut mettre plusieurs modelForms basé sur des models
différent dans la même page HTML sans souci de duplication
This commit is contained in:
Maël Kervella 2017-10-08 20:22:04 +00:00
parent 147cb78e9e
commit d65fbbf8d1
7 changed files with 149 additions and 49 deletions

View file

@ -30,7 +30,8 @@ from .models import Article, Paiement, Facture, Banque, Vente
class NewFactureForm(ModelForm): class NewFactureForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(NewFactureForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'facture')
super(NewFactureForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['cheque'].required = False self.fields['cheque'].required = False
self.fields['banque'].required = False self.fields['banque'].required = False
self.fields['cheque'].label = 'Numero de chèque' self.fields['cheque'].label = 'Numero de chèque'
@ -102,7 +103,8 @@ class ArticleForm(ModelForm):
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ArticleForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'article')
super(ArticleForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = "Désignation de l'article" self.fields['name'].label = "Désignation de l'article"
class DelArticleForm(Form): class DelArticleForm(Form):
@ -114,7 +116,8 @@ class PaiementForm(ModelForm):
fields = ['moyen', 'type_paiement'] fields = ['moyen', 'type_paiement']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(PaiementForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'paiement')
super(PaiementForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['moyen'].label = 'Moyen de paiement à ajouter' self.fields['moyen'].label = 'Moyen de paiement à ajouter'
self.fields['type_paiement'].label = 'Type de paiement à ajouter' self.fields['type_paiement'].label = 'Type de paiement à ajouter'
@ -127,7 +130,8 @@ class BanqueForm(ModelForm):
fields = ['name'] fields = ['name']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(BanqueForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'banque')
super(BanqueForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = 'Banque à ajouter' self.fields['name'].label = 'Banque à ajouter'
class DelBanqueForm(Form): class DelBanqueForm(Form):

View file

@ -40,7 +40,8 @@ class EditMachineForm(ModelForm):
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditMachineForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'machine')
super(EditMachineForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = 'Nom de la machine' self.fields['name'].label = 'Nom de la machine'
class NewMachineForm(EditMachineForm): class NewMachineForm(EditMachineForm):
@ -57,7 +58,8 @@ class EditInterfaceForm(ModelForm):
fields = ['machine', 'type', 'ipv4', 'mac_address', 'details'] fields = ['machine', 'type', 'ipv4', 'mac_address', 'details']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditInterfaceForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'interface')
super(EditInterfaceForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['mac_address'].label = 'Adresse mac' self.fields['mac_address'].label = 'Adresse mac'
self.fields['type'].label = 'Type de machine' self.fields['type'].label = 'Type de machine'
self.fields['type'].empty_label = "Séléctionner un type de machine" self.fields['type'].empty_label = "Séléctionner un type de machine"
@ -110,9 +112,10 @@ class AliasForm(ModelForm):
fields = ['name','extension'] fields = ['name','extension']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'domain')
if 'infra' in kwargs: if 'infra' in kwargs:
infra = kwargs.pop('infra') infra = kwargs.pop('infra')
super(AliasForm, self).__init__(*args, **kwargs) super(AliasForm, self).__init__(*args, prefix=prefix, **kwargs)
class DomainForm(AliasForm): class DomainForm(AliasForm):
class Meta(AliasForm.Meta): class Meta(AliasForm.Meta):
@ -125,7 +128,8 @@ class DomainForm(AliasForm):
initial = kwargs.get('initial', {}) initial = kwargs.get('initial', {})
initial['name'] = user.get_next_domain_name() initial['name'] = user.get_next_domain_name()
kwargs['initial'] = initial kwargs['initial'] = initial
super(DomainForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'domain')
super(DomainForm, self).__init__(*args, prefix=prefix, **kwargs)
class DelAliasForm(Form): class DelAliasForm(Form):
alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple) alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple)
@ -141,7 +145,8 @@ class MachineTypeForm(ModelForm):
fields = ['type','ip_type'] fields = ['type','ip_type']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(MachineTypeForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'machinetype')
super(MachineTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['type'].label = 'Type de machine à ajouter' self.fields['type'].label = 'Type de machine à ajouter'
self.fields['ip_type'].label = "Type d'ip relié" self.fields['ip_type'].label = "Type d'ip relié"
@ -153,9 +158,9 @@ class IpTypeForm(ModelForm):
model = IpType model = IpType
fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'prefix_v6', 'vlan'] fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'prefix_v6', 'vlan']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(IpTypeForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'iptype')
super(IpTypeForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['type'].label = 'Type ip à ajouter' self.fields['type'].label = 'Type ip à ajouter'
class EditIpTypeForm(IpTypeForm): class EditIpTypeForm(IpTypeForm):
@ -171,7 +176,8 @@ class ExtensionForm(ModelForm):
fields = ['name', 'need_infra', 'origin'] fields = ['name', 'need_infra', 'origin']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ExtensionForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'extension')
super(ExtensionForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = 'Extension à ajouter' self.fields['name'].label = 'Extension à ajouter'
self.fields['origin'].label = 'Enregistrement A origin' self.fields['origin'].label = 'Enregistrement A origin'
@ -184,7 +190,8 @@ class MxForm(ModelForm):
fields = ['zone', 'priority', 'name'] fields = ['zone', 'priority', 'name']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(MxForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'mx')
super(MxForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].queryset = Domain.objects.exclude(interface_parent=None) self.fields['name'].queryset = Domain.objects.exclude(interface_parent=None)
class DelMxForm(Form): class DelMxForm(Form):
@ -196,7 +203,8 @@ class NsForm(ModelForm):
fields = ['zone', 'ns'] fields = ['zone', 'ns']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(NsForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'ns')
super(NsForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['ns'].queryset = Domain.objects.exclude(interface_parent=None) self.fields['ns'].queryset = Domain.objects.exclude(interface_parent=None)
class DelNsForm(Form): class DelNsForm(Form):
@ -207,6 +215,10 @@ class TextForm(ModelForm):
model = Text model = Text
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'text')
super(TextForm, self).__init__(*args, prefix=prefix, **kwargs)
class DelTextForm(Form): class DelTextForm(Form):
text = forms.ModelMultipleChoiceField(queryset=Text.objects.all(), label="Enregistrements Text actuels", widget=forms.CheckboxSelectMultiple) text = forms.ModelMultipleChoiceField(queryset=Text.objects.all(), label="Enregistrements Text actuels", widget=forms.CheckboxSelectMultiple)
@ -215,6 +227,10 @@ class NasForm(ModelForm):
model = Nas model = Nas
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'nas')
super(NasForm, self).__init__(*args, prefix=prefix, **kwargs)
class DelNasForm(Form): class DelNasForm(Form):
nas = forms.ModelMultipleChoiceField(queryset=Nas.objects.all(), label="Enregistrements Nas actuels", widget=forms.CheckboxSelectMultiple) nas = forms.ModelMultipleChoiceField(queryset=Nas.objects.all(), label="Enregistrements Nas actuels", widget=forms.CheckboxSelectMultiple)
@ -223,6 +239,10 @@ class ServiceForm(ModelForm):
model = Service model = Service
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'service')
super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
def save(self, commit=True): def save(self, commit=True):
instance = super(ServiceForm, self).save(commit=False) instance = super(ServiceForm, self).save(commit=False)
if commit: if commit:
@ -238,6 +258,10 @@ class VlanForm(ModelForm):
model = Vlan model = Vlan
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'vlan')
super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs)
class DelVlanForm(Form): class DelVlanForm(Form):
vlan = forms.ModelMultipleChoiceField(queryset=Vlan.objects.all(), label="Vlan actuels", widget=forms.CheckboxSelectMultiple) vlan = forms.ModelMultipleChoiceField(queryset=Vlan.objects.all(), label="Vlan actuels", widget=forms.CheckboxSelectMultiple)
@ -246,8 +270,16 @@ class EditOuverturePortConfigForm(ModelForm):
model = Interface model = Interface
fields = ['port_lists'] fields = ['port_lists']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'interface')
super(EditOuverturePortConfigForm, self).__init__(*args, prefix=prefix, **kwargs)
class EditOuverturePortListForm(ModelForm): class EditOuverturePortListForm(ModelForm):
class Meta: class Meta:
model = OuverturePortList model = OuverturePortList
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'ouvertureportlist')
super(EditOuverturePortListForm, self).__init__(*args, prefix=prefix, **kwargs)

View file

@ -195,20 +195,20 @@ def bootstrap_form_typeahead(django_form, typeahead_fields, *args, **kwargs):
return mark_safe( form ) return mark_safe( form )
def input_id( f_name ) : def input_id( f_bound ) :
""" The id of the HTML input element """ """ The id of the HTML input element """
return 'id_'+f_name return f_bound.auto_id
def hidden_id( f_name ): def hidden_id( f_bound ):
""" The id of the HTML hidden input element """ """ The id of the HTML hidden input element """
return 'typeahead_hidden_'+f_name return input_id( f_bound ) +'_hidden'
def hidden_tag( f_bound, f_name ): def hidden_tag( f_bound, f_name ):
""" The HTML hidden input element """ """ The HTML hidden input element """
return render_tag( return render_tag(
'input', 'input',
attrs={ attrs={
'id': hidden_id(f_name), 'id': hidden_id( f_bound ),
'name': f_name, 'name': f_name,
'type': 'hidden', 'type': 'hidden',
'value': f_bound.value() or "" 'value': f_bound.value() or ""
@ -249,10 +249,10 @@ def typeahead_js( f_name, f_value, f_bound,
f_name = f_name, f_name = f_name,
choices = choices, choices = choices,
engine = engine, engine = engine,
input_id = input_id( f_name ), input_id = input_id( f_bound ),
datasets = default_datasets( f_name, match_func ), datasets = default_datasets( f_name, match_func ),
updater = typeahead_updater( f_name ), updater = typeahead_updater( f_bound ),
change = typeahead_change( f_name ), change = typeahead_change( f_bound ),
updates = ''.join( [ ( updates = ''.join( [ (
'$( "#{u_id}" ).change( function() {{' '$( "#{u_id}" ).change( function() {{'
'setup_{f_name}();' 'setup_{f_name}();'
@ -260,7 +260,7 @@ def typeahead_js( f_name, f_value, f_bound,
'}} );' '}} );'
).format( ).format(
u_id = u_id, u_id = u_id,
reset_input = reset_input( f_name ), reset_input = reset_input( f_bound ),
f_name = f_name f_name = f_name
) for u_id in update_on ] ) for u_id in update_on ]
), ),
@ -276,24 +276,24 @@ def init_input( f_name, f_bound ) :
'$( "#{input_id}" ).typeahead("val", {init_val});' '$( "#{input_id}" ).typeahead("val", {init_val});'
'$( "#{hidden_id}" ).val( {init_key} );' '$( "#{hidden_id}" ).val( {init_key} );'
).format( ).format(
input_id = input_id( f_name ), input_id = input_id( f_bound ),
init_val = '""' if init_key == '""' else init_val = '""' if init_key == '""' else
'engine_{f_name}.get( {init_key} )[0].value'.format( 'engine_{f_name}.get( {init_key} )[0].value'.format(
f_name = f_name, f_name = f_name,
init_key = init_key init_key = init_key
), ),
init_key = init_key, init_key = init_key,
hidden_id = hidden_id( f_name ) hidden_id = hidden_id( f_bound )
) )
def reset_input( f_name ) : def reset_input( f_bound ) :
""" The JS script to reset the fields values """ """ The JS script to reset the fields values """
return ( return (
'$( "#{input_id}" ).typeahead("val", "");' '$( "#{input_id}" ).typeahead("val", "");'
'$( "#{hidden_id}" ).val( "" );' '$( "#{hidden_id}" ).val( "" );'
).format( ).format(
input_id = input_id( f_name ), input_id = input_id( f_bound ),
hidden_id = hidden_id( f_name ) hidden_id = hidden_id( f_bound )
) )
def default_choices( f_value ) : def default_choices( f_value ) :
@ -355,7 +355,7 @@ def default_match_func ( f_name ) :
f_name = f_name f_name = f_name
) )
def typeahead_updater( f_name ): def typeahead_updater( f_bound ):
""" The JS script creating the function triggered when an item is """ The JS script creating the function triggered when an item is
selected through typeahead """ selected through typeahead """
return ( return (
@ -365,10 +365,10 @@ def typeahead_updater( f_name ):
'return item;' 'return item;'
'}}' '}}'
).format( ).format(
hidden_id = hidden_id( f_name ) hidden_id = hidden_id( f_bound )
) )
def typeahead_change( f_name ): def typeahead_change( f_bound ):
""" The JS script creating the function triggered when an item is changed """ The JS script creating the function triggered when an item is changed
(i.e. looses focus and value has changed since the moment it gained focus (i.e. looses focus and value has changed since the moment it gained focus
""" """
@ -380,7 +380,7 @@ def typeahead_change( f_name ):
'}}' '}}'
'}}' '}}'
).format( ).format(
input_id = input_id( f_name ), input_id = input_id( f_bound ),
hidden_id = hidden_id( f_name ) hidden_id = hidden_id( f_bound )
) )

View file

@ -82,7 +82,7 @@ def f_type_id( is_type_tt ):
""" The id that will be used in HTML to store the value of the field """ The id that will be used in HTML to store the value of the field
type. Depends on the fact that type is generate using typeahead or not type. Depends on the fact that type is generate using typeahead or not
""" """
return hidden_id('type') if is_type_tt else input_id('type') return 'id_interface-type_hidden' if is_type_tt else 'id_interface-type'
def generate_ipv4_choices( form ) : def generate_ipv4_choices( form ) :
""" Generate the parameter choices for the bootstrap_form_typeahead tag """ Generate the parameter choices for the bootstrap_form_typeahead tag

View file

@ -33,7 +33,8 @@ class EditOptionalUserForm(ModelForm):
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditOptionalUserForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'optionaluser')
super(EditOptionalUserForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['is_tel_mandatory'].label = 'Exiger un numéro de téléphone' self.fields['is_tel_mandatory'].label = 'Exiger un numéro de téléphone'
self.fields['user_solde'].label = 'Activation du solde pour les utilisateurs' self.fields['user_solde'].label = 'Activation du solde pour les utilisateurs'
@ -43,7 +44,8 @@ class EditOptionalMachineForm(ModelForm):
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditOptionalMachineForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'optionalmachine')
super(EditOptionalMachineForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['password_machine'].label = "Possibilité d'attribuer un mot de passe par interface" self.fields['password_machine'].label = "Possibilité d'attribuer un mot de passe par interface"
self.fields['max_lambdauser_interfaces'].label = "Maximum d'interfaces autorisées pour un user normal" self.fields['max_lambdauser_interfaces'].label = "Maximum d'interfaces autorisées pour un user normal"
self.fields['max_lambdauser_aliases'].label = "Maximum d'alias dns autorisés pour un user normal" self.fields['max_lambdauser_aliases'].label = "Maximum d'alias dns autorisés pour un user normal"
@ -54,7 +56,8 @@ class EditOptionalTopologieForm(ModelForm):
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditOptionalTopologieForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'optionaltopologie')
super(EditOptionalTopologieForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['vlan_decision_ok'].label = "Vlan où placer les machines après acceptation RADIUS" self.fields['vlan_decision_ok'].label = "Vlan où placer les machines après acceptation RADIUS"
self.fields['vlan_decision_nok'].label = "Vlan où placer les machines après rejet RADIUS" self.fields['vlan_decision_nok'].label = "Vlan où placer les machines après rejet RADIUS"
@ -64,7 +67,8 @@ class EditGeneralOptionForm(ModelForm):
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditGeneralOptionForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'generaloption')
super(EditGeneralOptionForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['search_display_page'].label = 'Resultats affichés dans une recherche' self.fields['search_display_page'].label = 'Resultats affichés dans une recherche'
self.fields['pagination_number'].label = 'Items par page, taille normale (ex users)' self.fields['pagination_number'].label = 'Items par page, taille normale (ex users)'
self.fields['pagination_large_number'].label = 'Items par page, taille élevée (machines)' self.fields['pagination_large_number'].label = 'Items par page, taille élevée (machines)'
@ -78,7 +82,8 @@ class EditAssoOptionForm(ModelForm):
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditAssoOptionForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'assooption')
super(EditAssoOptionForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = 'Nom de l\'asso' self.fields['name'].label = 'Nom de l\'asso'
self.fields['siret'].label = 'SIRET' self.fields['siret'].label = 'SIRET'
self.fields['adresse1'].label = 'Adresse (ligne 1)' self.fields['adresse1'].label = 'Adresse (ligne 1)'
@ -94,7 +99,8 @@ class EditMailMessageOptionForm(ModelForm):
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditMailMessageOptionForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'mailmessageoption')
super(EditMailMessageOptionForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['welcome_mail_fr'].label = 'Message dans le mail de bienvenue en français' self.fields['welcome_mail_fr'].label = 'Message dans le mail de bienvenue en français'
self.fields['welcome_mail_en'].label = 'Message dans le mail de bienvenue en anglais' self.fields['welcome_mail_en'].label = 'Message dans le mail de bienvenue en anglais'
@ -103,5 +109,10 @@ class ServiceForm(ModelForm):
model = Service model = Service
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'service')
super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
class DelServiceForm(Form): class DelServiceForm(Form):
services = forms.ModelMultipleChoiceField(queryset=Service.objects.all(), label="Enregistrements service actuels", widget=forms.CheckboxSelectMultiple) services = forms.ModelMultipleChoiceField(queryset=Service.objects.all(), label="Enregistrements service actuels", widget=forms.CheckboxSelectMultiple)

View file

@ -31,12 +31,17 @@ class PortForm(ModelForm):
model = Port model = Port
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'port')
super(PortForm, self).__init__(*args, prefix=prefix, **kwargs)
class EditPortForm(ModelForm): class EditPortForm(ModelForm):
class Meta(PortForm.Meta): class Meta(PortForm.Meta):
fields = ['room', 'related', 'machine_interface', 'radius', 'vlan_force', 'details'] fields = ['room', 'related', 'machine_interface', 'radius', 'vlan_force', 'details']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditPortForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'port')
super(EditPortForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['machine_interface'].queryset = Interface.objects.all().select_related('domain__extension') self.fields['machine_interface'].queryset = Interface.objects.all().select_related('domain__extension')
self.fields['related'].queryset = Port.objects.all().select_related('switch__switch_interface__domain__extension').order_by('switch', 'port') self.fields['related'].queryset = Port.objects.all().select_related('switch__switch_interface__domain__extension').order_by('switch', 'port')
@ -44,18 +49,27 @@ class AddPortForm(ModelForm):
class Meta(PortForm.Meta): class Meta(PortForm.Meta):
fields = ['port', 'room', 'machine_interface', 'related', 'radius', 'vlan_force', 'details'] fields = ['port', 'room', 'machine_interface', 'related', 'radius', 'vlan_force', 'details']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'port')
super(AddPortForm, self).__init__(*args, prefix=prefix, **kwargs)
class StackForm(ModelForm): class StackForm(ModelForm):
class Meta: class Meta:
model = Stack model = Stack
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'stack')
super(StackForm, self).__init__(*args, prefix=prefix, **kwargs)
class EditSwitchForm(ModelForm): class EditSwitchForm(ModelForm):
class Meta: class Meta:
model = Switch model = Switch
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(EditSwitchForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'switch')
super(EditSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['location'].label = 'Localisation' self.fields['location'].label = 'Localisation'
self.fields['number'].label = 'Nombre de ports' self.fields['number'].label = 'Nombre de ports'
@ -63,8 +77,16 @@ class NewSwitchForm(ModelForm):
class Meta(EditSwitchForm.Meta): class Meta(EditSwitchForm.Meta):
fields = ['location', 'number', 'details', 'stack', 'stack_member_id'] fields = ['location', 'number', 'details', 'stack', 'stack_member_id']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'switch')
super(NewSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
class EditRoomForm(ModelForm): class EditRoomForm(ModelForm):
class Meta: class Meta:
model = Room model = Room
fields = '__all__' fields = '__all__'
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'room')
super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs)

View file

@ -54,6 +54,10 @@ class UserCreationForm(forms.ModelForm):
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput, validators=[MinLengthValidator(8)], max_length=255) password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput, validators=[MinLengthValidator(8)], max_length=255)
is_admin = forms.BooleanField(label='is admin') is_admin = forms.BooleanField(label='is admin')
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'user')
super(UserCreationForm, self).__init__(*args, prefix=prefix, **kwargs)
class Meta: class Meta:
model = User model = User
fields = ('pseudo', 'name', 'surname', 'email') fields = ('pseudo', 'name', 'surname', 'email')
@ -80,6 +84,10 @@ class ServiceUserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', widget=forms.PasswordInput, min_length=8, max_length=255) password1 = forms.CharField(label='Password', widget=forms.PasswordInput, min_length=8, max_length=255)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput, min_length=8, max_length=255) password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput, min_length=8, max_length=255)
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'serviceuser')
super(ServiceUserCreationForm, self).__init__(*args, prefix=prefix, **kwargs)
class Meta: class Meta:
model = ServiceUser model = ServiceUser
fields = ('pseudo',) fields = ('pseudo',)
@ -112,7 +120,8 @@ class UserChangeForm(forms.ModelForm):
fields = ('pseudo', 'password', 'name', 'surname', 'email') fields = ('pseudo', 'password', 'name', 'surname', 'email')
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(UserChangeForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'user')
super(UserChangeForm, self).__init__(*args, prefix=prefix, **kwargs)
print("User is admin : %s" % kwargs['instance'].is_admin) print("User is admin : %s" % kwargs['instance'].is_admin)
self.initial['is_admin'] = kwargs['instance'].is_admin self.initial['is_admin'] = kwargs['instance'].is_admin
@ -137,6 +146,10 @@ class ServiceUserChangeForm(forms.ModelForm):
""" """
password = ReadOnlyPasswordHashField() password = ReadOnlyPasswordHashField()
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'serviceuser')
super(ServiceUserChangeForm, self).__init__(*args, prefix=prefix, **kwargs)
class Meta: class Meta:
model = ServiceUser model = ServiceUser
fields = ('pseudo',) fields = ('pseudo',)
@ -163,7 +176,8 @@ class MassArchiveForm(forms.Form):
class BaseInfoForm(ModelForm): class BaseInfoForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(BaseInfoForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'user')
super(BaseInfoForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = 'Prénom' self.fields['name'].label = 'Prénom'
self.fields['surname'].label = 'Nom' self.fields['surname'].label = 'Nom'
self.fields['school'].label = 'Établissement' self.fields['school'].label = 'Établissement'
@ -226,6 +240,10 @@ class PasswordForm(ModelForm):
model = User model = User
fields = ['password', 'pwd_ntlm'] fields = ['password', 'pwd_ntlm']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'user')
super(PasswordForm, self).__init__(*args, prefix=prefix, **kwargs)
class ServiceUserForm(ModelForm): class ServiceUserForm(ModelForm):
""" Modification d'un service user""" """ Modification d'un service user"""
password = forms.CharField(label=u'Nouveau mot de passe', max_length=255, validators=[MinLengthValidator(8)], widget=forms.PasswordInput, required=False) password = forms.CharField(label=u'Nouveau mot de passe', max_length=255, validators=[MinLengthValidator(8)], widget=forms.PasswordInput, required=False)
@ -234,6 +252,10 @@ class ServiceUserForm(ModelForm):
model = ServiceUser model = ServiceUser
fields = ('pseudo','access_group') fields = ('pseudo','access_group')
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'serviceuser')
super(ServiceUserForm, self).__init__(*args, prefix=prefix, **kwargs)
class EditServiceUserForm(ServiceUserForm): class EditServiceUserForm(ServiceUserForm):
class Meta(ServiceUserForm.Meta): class Meta(ServiceUserForm.Meta):
fields = ['access_group','comment'] fields = ['access_group','comment']
@ -244,6 +266,10 @@ class StateForm(ModelForm):
model = User model = User
fields = ['state'] fields = ['state']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', 'user')
super(StateForm, self).__init__(*args, prefix=prefix, **kwargs)
class SchoolForm(ModelForm): class SchoolForm(ModelForm):
class Meta: class Meta:
@ -251,7 +277,8 @@ class SchoolForm(ModelForm):
fields = ['name'] fields = ['name']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(SchoolForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'school')
super(SchoolForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['name'].label = 'Établissement' self.fields['name'].label = 'Établissement'
class ListRightForm(ModelForm): class ListRightForm(ModelForm):
@ -260,7 +287,8 @@ class ListRightForm(ModelForm):
fields = ['listright', 'details'] fields = ['listright', 'details']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ListRightForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'listright')
super(ListRightForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['listright'].label = 'Nom du droit/groupe' self.fields['listright'].label = 'Nom du droit/groupe'
class NewListRightForm(ListRightForm): class NewListRightForm(ListRightForm):
@ -279,7 +307,8 @@ class DelSchoolForm(Form):
class RightForm(ModelForm): class RightForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(RightForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'right')
super(RightForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['right'].label = 'Droit' self.fields['right'].label = 'Droit'
self.fields['right'].empty_label = "Choisir un nouveau droit" self.fields['right'].empty_label = "Choisir un nouveau droit"
@ -297,7 +326,8 @@ class DelRightForm(Form):
class BanForm(ModelForm): class BanForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(BanForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'ban')
super(BanForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['date_end'].label = 'Date de fin' self.fields['date_end'].label = 'Date de fin'
class Meta: class Meta:
@ -313,7 +343,8 @@ class BanForm(ModelForm):
class WhitelistForm(ModelForm): class WhitelistForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(WhitelistForm, self).__init__(*args, **kwargs) prefix = kwargs.pop('prefix', 'whitelist')
super(WhitelistForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['date_end'].label = 'Date de fin' self.fields['date_end'].label = 'Date de fin'
class Meta: class Meta: