From f25203a257174ca9ef147cabda1a024ee169ac0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sun, 8 Oct 2017 20:22:04 +0000 Subject: [PATCH 1/2] Evite les doublons dans les id et les names des forms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- cotisations/forms.py | 12 +++-- machines/forms.py | 52 +++++++++++++++---- .../templatetags/bootstrap_form_typeahead.py | 38 +++++++------- machines/views.py | 2 +- preferences/forms.py | 23 +++++--- topologie/forms.py | 26 +++++++++- users/forms.py | 45 +++++++++++++--- 7 files changed, 149 insertions(+), 49 deletions(-) diff --git a/cotisations/forms.py b/cotisations/forms.py index 90c9e826..9b34b332 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -30,7 +30,8 @@ from .models import Article, Paiement, Facture, Banque, Vente class NewFactureForm(ModelForm): 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['banque'].required = False self.fields['cheque'].label = 'Numero de chèque' @@ -102,7 +103,8 @@ class ArticleForm(ModelForm): fields = '__all__' 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" class DelArticleForm(Form): @@ -114,7 +116,8 @@ class PaiementForm(ModelForm): fields = ['moyen', 'type_paiement'] 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['type_paiement'].label = 'Type de paiement à ajouter' @@ -127,7 +130,8 @@ class BanqueForm(ModelForm): fields = ['name'] 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' class DelBanqueForm(Form): diff --git a/machines/forms.py b/machines/forms.py index 63539888..25ffad0c 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -40,7 +40,8 @@ class EditMachineForm(ModelForm): fields = '__all__' 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' class NewMachineForm(EditMachineForm): @@ -57,7 +58,8 @@ class EditInterfaceForm(ModelForm): fields = ['machine', 'type', 'ipv4', 'mac_address', 'details'] 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['type'].label = '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'] def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'domain') if 'infra' in kwargs: infra = kwargs.pop('infra') - super(AliasForm, self).__init__(*args, **kwargs) + super(AliasForm, self).__init__(*args, prefix=prefix, **kwargs) class DomainForm(AliasForm): class Meta(AliasForm.Meta): @@ -125,7 +128,8 @@ class DomainForm(AliasForm): initial = kwargs.get('initial', {}) initial['name'] = user.get_next_domain_name() 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): alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple) @@ -141,7 +145,8 @@ class MachineTypeForm(ModelForm): fields = ['type','ip_type'] 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['ip_type'].label = "Type d'ip relié" @@ -153,9 +158,9 @@ class IpTypeForm(ModelForm): model = IpType fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'prefix_v6', 'vlan'] - 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' class EditIpTypeForm(IpTypeForm): @@ -171,7 +176,8 @@ class ExtensionForm(ModelForm): fields = ['name', 'need_infra', 'origin'] 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['origin'].label = 'Enregistrement A origin' @@ -184,7 +190,8 @@ class MxForm(ModelForm): fields = ['zone', 'priority', 'name'] 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) class DelMxForm(Form): @@ -196,7 +203,8 @@ class NsForm(ModelForm): fields = ['zone', 'ns'] 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) class DelNsForm(Form): @@ -207,6 +215,10 @@ class TextForm(ModelForm): model = Text fields = '__all__' + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'text') + super(TextForm, self).__init__(*args, prefix=prefix, **kwargs) + class DelTextForm(Form): text = forms.ModelMultipleChoiceField(queryset=Text.objects.all(), label="Enregistrements Text actuels", widget=forms.CheckboxSelectMultiple) @@ -215,6 +227,10 @@ class NasForm(ModelForm): model = Nas fields = '__all__' + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'nas') + super(NasForm, self).__init__(*args, prefix=prefix, **kwargs) + class DelNasForm(Form): nas = forms.ModelMultipleChoiceField(queryset=Nas.objects.all(), label="Enregistrements Nas actuels", widget=forms.CheckboxSelectMultiple) @@ -223,6 +239,10 @@ class ServiceForm(ModelForm): model = Service 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): instance = super(ServiceForm, self).save(commit=False) if commit: @@ -238,6 +258,10 @@ class VlanForm(ModelForm): model = Vlan fields = '__all__' + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'vlan') + super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs) + class DelVlanForm(Form): vlan = forms.ModelMultipleChoiceField(queryset=Vlan.objects.all(), label="Vlan actuels", widget=forms.CheckboxSelectMultiple) @@ -246,8 +270,16 @@ class EditOuverturePortConfigForm(ModelForm): model = Interface 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 Meta: model = OuverturePortList fields = '__all__' + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'ouvertureportlist') + super(EditOuverturePortListForm, self).__init__(*args, prefix=prefix, **kwargs) + diff --git a/machines/templatetags/bootstrap_form_typeahead.py b/machines/templatetags/bootstrap_form_typeahead.py index 05dd3147..7ccab390 100644 --- a/machines/templatetags/bootstrap_form_typeahead.py +++ b/machines/templatetags/bootstrap_form_typeahead.py @@ -195,20 +195,20 @@ def bootstrap_form_typeahead(django_form, typeahead_fields, *args, **kwargs): return mark_safe( form ) -def input_id( f_name ) : +def input_id( f_bound ) : """ 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 """ - return 'typeahead_hidden_'+f_name + return input_id( f_bound ) +'_hidden' def hidden_tag( f_bound, f_name ): """ The HTML hidden input element """ return render_tag( 'input', attrs={ - 'id': hidden_id(f_name), + 'id': hidden_id( f_bound ), 'name': f_name, 'type': 'hidden', 'value': f_bound.value() or "" @@ -249,10 +249,10 @@ def typeahead_js( f_name, f_value, f_bound, f_name = f_name, choices = choices, engine = engine, - input_id = input_id( f_name ), + input_id = input_id( f_bound ), datasets = default_datasets( f_name, match_func ), - updater = typeahead_updater( f_name ), - change = typeahead_change( f_name ), + updater = typeahead_updater( f_bound ), + change = typeahead_change( f_bound ), updates = ''.join( [ ( '$( "#{u_id}" ).change( function() {{' 'setup_{f_name}();' @@ -260,7 +260,7 @@ def typeahead_js( f_name, f_value, f_bound, '}} );' ).format( u_id = u_id, - reset_input = reset_input( f_name ), + reset_input = reset_input( f_bound ), f_name = f_name ) for u_id in update_on ] ), @@ -276,24 +276,24 @@ def init_input( f_name, f_bound ) : '$( "#{input_id}" ).typeahead("val", {init_val});' '$( "#{hidden_id}" ).val( {init_key} );' ).format( - input_id = input_id( f_name ), + input_id = input_id( f_bound ), init_val = '""' if init_key == '""' else 'engine_{f_name}.get( {init_key} )[0].value'.format( f_name = f_name, 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 """ return ( '$( "#{input_id}" ).typeahead("val", "");' '$( "#{hidden_id}" ).val( "" );' ).format( - input_id = input_id( f_name ), - hidden_id = hidden_id( f_name ) + input_id = input_id( f_bound ), + hidden_id = hidden_id( f_bound ) ) def default_choices( f_value ) : @@ -355,7 +355,7 @@ def default_match_func ( 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 selected through typeahead """ return ( @@ -365,10 +365,10 @@ def typeahead_updater( f_name ): 'return item;' '}}' ).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 (i.e. looses focus and value has changed since the moment it gained focus """ @@ -380,7 +380,7 @@ def typeahead_change( f_name ): '}}' '}}' ).format( - input_id = input_id( f_name ), - hidden_id = hidden_id( f_name ) + input_id = input_id( f_bound ), + hidden_id = hidden_id( f_bound ) ) diff --git a/machines/views.py b/machines/views.py index ac37d8c6..2e5fab2b 100644 --- a/machines/views.py +++ b/machines/views.py @@ -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 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 ) : """ Generate the parameter choices for the bootstrap_form_typeahead tag diff --git a/preferences/forms.py b/preferences/forms.py index 20c7ca95..2cb9334f 100644 --- a/preferences/forms.py +++ b/preferences/forms.py @@ -33,7 +33,8 @@ class EditOptionalUserForm(ModelForm): fields = '__all__' 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['user_solde'].label = 'Activation du solde pour les utilisateurs' @@ -43,7 +44,8 @@ class EditOptionalMachineForm(ModelForm): fields = '__all__' 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['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" @@ -54,7 +56,8 @@ class EditOptionalTopologieForm(ModelForm): fields = '__all__' 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_nok'].label = "Vlan où placer les machines après rejet RADIUS" @@ -64,7 +67,8 @@ class EditGeneralOptionForm(ModelForm): fields = '__all__' 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['pagination_number'].label = 'Items par page, taille normale (ex users)' self.fields['pagination_large_number'].label = 'Items par page, taille élevée (machines)' @@ -78,7 +82,8 @@ class EditAssoOptionForm(ModelForm): fields = '__all__' 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['siret'].label = 'SIRET' self.fields['adresse1'].label = 'Adresse (ligne 1)' @@ -94,7 +99,8 @@ class EditMailMessageOptionForm(ModelForm): fields = '__all__' 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_en'].label = 'Message dans le mail de bienvenue en anglais' @@ -103,5 +109,10 @@ class ServiceForm(ModelForm): model = Service fields = '__all__' + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'service') + super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs) + class DelServiceForm(Form): services = forms.ModelMultipleChoiceField(queryset=Service.objects.all(), label="Enregistrements service actuels", widget=forms.CheckboxSelectMultiple) + diff --git a/topologie/forms.py b/topologie/forms.py index 87a3917d..5396f843 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -31,12 +31,17 @@ class PortForm(ModelForm): model = Port fields = '__all__' + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'port') + super(PortForm, self).__init__(*args, prefix=prefix, **kwargs) + class EditPortForm(ModelForm): class Meta(PortForm.Meta): fields = ['room', 'related', 'machine_interface', 'radius', 'vlan_force', 'details'] 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['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): 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 Meta: model = Stack fields = '__all__' + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'stack') + super(StackForm, self).__init__(*args, prefix=prefix, **kwargs) + class EditSwitchForm(ModelForm): class Meta: model = Switch fields = '__all__' 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['number'].label = 'Nombre de ports' @@ -63,8 +77,16 @@ class NewSwitchForm(ModelForm): class Meta(EditSwitchForm.Meta): 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 Meta: model = Room fields = '__all__' + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'room') + super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs) + diff --git a/users/forms.py b/users/forms.py index 0099176f..faf3fbaf 100644 --- a/users/forms.py +++ b/users/forms.py @@ -54,6 +54,10 @@ class UserCreationForm(forms.ModelForm): password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput, validators=[MinLengthValidator(8)], max_length=255) 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: model = User 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) 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: model = ServiceUser fields = ('pseudo',) @@ -112,7 +120,8 @@ class UserChangeForm(forms.ModelForm): fields = ('pseudo', 'password', 'name', 'surname', 'email') 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) self.initial['is_admin'] = kwargs['instance'].is_admin @@ -137,6 +146,10 @@ class ServiceUserChangeForm(forms.ModelForm): """ password = ReadOnlyPasswordHashField() + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'serviceuser') + super(ServiceUserChangeForm, self).__init__(*args, prefix=prefix, **kwargs) + class Meta: model = ServiceUser fields = ('pseudo',) @@ -163,7 +176,8 @@ class MassArchiveForm(forms.Form): class BaseInfoForm(ModelForm): 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['surname'].label = 'Nom' self.fields['school'].label = 'Établissement' @@ -226,6 +240,10 @@ class PasswordForm(ModelForm): model = User 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): """ 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) @@ -234,6 +252,10 @@ class ServiceUserForm(ModelForm): model = ServiceUser 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 Meta(ServiceUserForm.Meta): fields = ['access_group','comment'] @@ -244,6 +266,10 @@ class StateForm(ModelForm): model = User fields = ['state'] + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', 'user') + super(StateForm, self).__init__(*args, prefix=prefix, **kwargs) + class SchoolForm(ModelForm): class Meta: @@ -251,7 +277,8 @@ class SchoolForm(ModelForm): fields = ['name'] 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' class ListRightForm(ModelForm): @@ -260,7 +287,8 @@ class ListRightForm(ModelForm): fields = ['listright', 'details'] 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' class NewListRightForm(ListRightForm): @@ -279,7 +307,8 @@ class DelSchoolForm(Form): class RightForm(ModelForm): 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'].empty_label = "Choisir un nouveau droit" @@ -297,7 +326,8 @@ class DelRightForm(Form): class BanForm(ModelForm): 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' class Meta: @@ -313,7 +343,8 @@ class BanForm(ModelForm): class WhitelistForm(ModelForm): 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' class Meta: From 2ef003faf14e4a591485096a0bd21371c5d44ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sun, 8 Oct 2017 23:34:49 +0000 Subject: [PATCH 2/2] =?UTF-8?q?Remplace=20le=20nom=20donn=C3=A9=20=C3=A0?= =?UTF-8?q?=20la=20main=20par=20le=20nom=20de=20la=20classe=20automatiquem?= =?UTF-8?q?ent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/forms.py | 8 ++++---- machines/forms.py | 30 +++++++++++++++--------------- machines/views.py | 2 +- preferences/forms.py | 14 +++++++------- topologie/forms.py | 14 +++++++------- users/forms.py | 26 +++++++++++++------------- 6 files changed, 47 insertions(+), 47 deletions(-) diff --git a/cotisations/forms.py b/cotisations/forms.py index 9b34b332..97bc82ab 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -30,7 +30,7 @@ from .models import Article, Paiement, Facture, Banque, Vente class NewFactureForm(ModelForm): def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'facture') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(NewFactureForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['cheque'].required = False self.fields['banque'].required = False @@ -103,7 +103,7 @@ class ArticleForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'article') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(ArticleForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].label = "Désignation de l'article" @@ -116,7 +116,7 @@ class PaiementForm(ModelForm): fields = ['moyen', 'type_paiement'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'paiement') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(PaiementForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['moyen'].label = 'Moyen de paiement à ajouter' self.fields['type_paiement'].label = 'Type de paiement à ajouter' @@ -130,7 +130,7 @@ class BanqueForm(ModelForm): fields = ['name'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'banque') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(BanqueForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].label = 'Banque à ajouter' diff --git a/machines/forms.py b/machines/forms.py index 25ffad0c..7ff85695 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -40,7 +40,7 @@ class EditMachineForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'machine') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditMachineForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].label = 'Nom de la machine' @@ -58,7 +58,7 @@ class EditInterfaceForm(ModelForm): fields = ['machine', 'type', 'ipv4', 'mac_address', 'details'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'interface') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditInterfaceForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['mac_address'].label = 'Adresse mac' self.fields['type'].label = 'Type de machine' @@ -112,7 +112,7 @@ class AliasForm(ModelForm): fields = ['name','extension'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'domain') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) if 'infra' in kwargs: infra = kwargs.pop('infra') super(AliasForm, self).__init__(*args, prefix=prefix, **kwargs) @@ -128,7 +128,7 @@ class DomainForm(AliasForm): initial = kwargs.get('initial', {}) initial['name'] = user.get_next_domain_name() kwargs['initial'] = initial - prefix = kwargs.pop('prefix', 'domain') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(DomainForm, self).__init__(*args, prefix=prefix, **kwargs) class DelAliasForm(Form): @@ -145,7 +145,7 @@ class MachineTypeForm(ModelForm): fields = ['type','ip_type'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'machinetype') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(MachineTypeForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['type'].label = 'Type de machine à ajouter' self.fields['ip_type'].label = "Type d'ip relié" @@ -159,7 +159,7 @@ class IpTypeForm(ModelForm): fields = ['type','extension','need_infra','domaine_ip_start','domaine_ip_stop', 'prefix_v6', 'vlan'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'iptype') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(IpTypeForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['type'].label = 'Type ip à ajouter' @@ -176,7 +176,7 @@ class ExtensionForm(ModelForm): fields = ['name', 'need_infra', 'origin'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'extension') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(ExtensionForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].label = 'Extension à ajouter' self.fields['origin'].label = 'Enregistrement A origin' @@ -190,7 +190,7 @@ class MxForm(ModelForm): fields = ['zone', 'priority', 'name'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'mx') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(MxForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].queryset = Domain.objects.exclude(interface_parent=None) @@ -203,7 +203,7 @@ class NsForm(ModelForm): fields = ['zone', 'ns'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'ns') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(NsForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['ns'].queryset = Domain.objects.exclude(interface_parent=None) @@ -216,7 +216,7 @@ class TextForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'text') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(TextForm, self).__init__(*args, prefix=prefix, **kwargs) class DelTextForm(Form): @@ -228,7 +228,7 @@ class NasForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'nas') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(NasForm, self).__init__(*args, prefix=prefix, **kwargs) class DelNasForm(Form): @@ -240,7 +240,7 @@ class ServiceForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'service') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs) def save(self, commit=True): @@ -259,7 +259,7 @@ class VlanForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'vlan') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(VlanForm, self).__init__(*args, prefix=prefix, **kwargs) class DelVlanForm(Form): @@ -271,7 +271,7 @@ class EditOuverturePortConfigForm(ModelForm): fields = ['port_lists'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'interface') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditOuverturePortConfigForm, self).__init__(*args, prefix=prefix, **kwargs) class EditOuverturePortListForm(ModelForm): @@ -280,6 +280,6 @@ class EditOuverturePortListForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'ouvertureportlist') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditOuverturePortListForm, self).__init__(*args, prefix=prefix, **kwargs) diff --git a/machines/views.py b/machines/views.py index 2e5fab2b..3f6d8b37 100644 --- a/machines/views.py +++ b/machines/views.py @@ -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 type. Depends on the fact that type is generate using typeahead or not """ - return 'id_interface-type_hidden' if is_type_tt else 'id_interface-type' + return 'id_Interface-type_hidden' if is_type_tt else 'id_Interface-type' def generate_ipv4_choices( form ) : """ Generate the parameter choices for the bootstrap_form_typeahead tag diff --git a/preferences/forms.py b/preferences/forms.py index 2cb9334f..887d768d 100644 --- a/preferences/forms.py +++ b/preferences/forms.py @@ -33,7 +33,7 @@ class EditOptionalUserForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'optionaluser') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditOptionalUserForm, self).__init__(*args, prefix=prefix, **kwargs) 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' @@ -44,7 +44,7 @@ class EditOptionalMachineForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'optionalmachine') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditOptionalMachineForm, self).__init__(*args, prefix=prefix, **kwargs) 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" @@ -56,7 +56,7 @@ class EditOptionalTopologieForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'optionaltopologie') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) 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_nok'].label = "Vlan où placer les machines après rejet RADIUS" @@ -67,7 +67,7 @@ class EditGeneralOptionForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'generaloption') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditGeneralOptionForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['search_display_page'].label = 'Resultats affichés dans une recherche' self.fields['pagination_number'].label = 'Items par page, taille normale (ex users)' @@ -82,7 +82,7 @@ class EditAssoOptionForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'assooption') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditAssoOptionForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].label = 'Nom de l\'asso' self.fields['siret'].label = 'SIRET' @@ -99,7 +99,7 @@ class EditMailMessageOptionForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'mailmessageoption') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) 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_en'].label = 'Message dans le mail de bienvenue en anglais' @@ -110,7 +110,7 @@ class ServiceForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'service') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs) class DelServiceForm(Form): diff --git a/topologie/forms.py b/topologie/forms.py index 5396f843..8c82afba 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -32,7 +32,7 @@ class PortForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'port') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(PortForm, self).__init__(*args, prefix=prefix, **kwargs) class EditPortForm(ModelForm): @@ -40,7 +40,7 @@ class EditPortForm(ModelForm): fields = ['room', 'related', 'machine_interface', 'radius', 'vlan_force', 'details'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'port') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditPortForm, self).__init__(*args, prefix=prefix, **kwargs) 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') @@ -50,7 +50,7 @@ class AddPortForm(ModelForm): fields = ['port', 'room', 'machine_interface', 'related', 'radius', 'vlan_force', 'details'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'port') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(AddPortForm, self).__init__(*args, prefix=prefix, **kwargs) class StackForm(ModelForm): @@ -59,7 +59,7 @@ class StackForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'stack') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(StackForm, self).__init__(*args, prefix=prefix, **kwargs) class EditSwitchForm(ModelForm): @@ -68,7 +68,7 @@ class EditSwitchForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'switch') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditSwitchForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['location'].label = 'Localisation' self.fields['number'].label = 'Nombre de ports' @@ -78,7 +78,7 @@ class NewSwitchForm(ModelForm): fields = ['location', 'number', 'details', 'stack', 'stack_member_id'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'switch') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(NewSwitchForm, self).__init__(*args, prefix=prefix, **kwargs) class EditRoomForm(ModelForm): @@ -87,6 +87,6 @@ class EditRoomForm(ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'room') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs) diff --git a/users/forms.py b/users/forms.py index faf3fbaf..a8b1a219 100644 --- a/users/forms.py +++ b/users/forms.py @@ -55,7 +55,7 @@ class UserCreationForm(forms.ModelForm): is_admin = forms.BooleanField(label='is admin') def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'user') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(UserCreationForm, self).__init__(*args, prefix=prefix, **kwargs) class Meta: @@ -85,7 +85,7 @@ class ServiceUserCreationForm(forms.ModelForm): 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') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(ServiceUserCreationForm, self).__init__(*args, prefix=prefix, **kwargs) class Meta: @@ -120,7 +120,7 @@ class UserChangeForm(forms.ModelForm): fields = ('pseudo', 'password', 'name', 'surname', 'email') def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'user') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(UserChangeForm, self).__init__(*args, prefix=prefix, **kwargs) print("User is admin : %s" % kwargs['instance'].is_admin) self.initial['is_admin'] = kwargs['instance'].is_admin @@ -147,7 +147,7 @@ class ServiceUserChangeForm(forms.ModelForm): password = ReadOnlyPasswordHashField() def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'serviceuser') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(ServiceUserChangeForm, self).__init__(*args, prefix=prefix, **kwargs) class Meta: @@ -176,7 +176,7 @@ class MassArchiveForm(forms.Form): class BaseInfoForm(ModelForm): def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'user') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(BaseInfoForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].label = 'Prénom' self.fields['surname'].label = 'Nom' @@ -241,7 +241,7 @@ class PasswordForm(ModelForm): fields = ['password', 'pwd_ntlm'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'user') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(PasswordForm, self).__init__(*args, prefix=prefix, **kwargs) class ServiceUserForm(ModelForm): @@ -253,7 +253,7 @@ class ServiceUserForm(ModelForm): fields = ('pseudo','access_group') def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'serviceuser') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(ServiceUserForm, self).__init__(*args, prefix=prefix, **kwargs) class EditServiceUserForm(ServiceUserForm): @@ -267,7 +267,7 @@ class StateForm(ModelForm): fields = ['state'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'user') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(StateForm, self).__init__(*args, prefix=prefix, **kwargs) @@ -277,7 +277,7 @@ class SchoolForm(ModelForm): fields = ['name'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'school') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(SchoolForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['name'].label = 'Établissement' @@ -287,7 +287,7 @@ class ListRightForm(ModelForm): fields = ['listright', 'details'] def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'listright') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(ListRightForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['listright'].label = 'Nom du droit/groupe' @@ -307,7 +307,7 @@ class DelSchoolForm(Form): class RightForm(ModelForm): def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'right') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(RightForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['right'].label = 'Droit' self.fields['right'].empty_label = "Choisir un nouveau droit" @@ -326,7 +326,7 @@ class DelRightForm(Form): class BanForm(ModelForm): def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'ban') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(BanForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['date_end'].label = 'Date de fin' @@ -343,7 +343,7 @@ class BanForm(ModelForm): class WhitelistForm(ModelForm): def __init__(self, *args, **kwargs): - prefix = kwargs.pop('prefix', 'whitelist') + prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(WhitelistForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['date_end'].label = 'Date de fin'