Skip to content
Snippets Groups Projects
Commit 5974db48 authored by Timothee P's avatar Timothee P :sunflower:
Browse files

refactor(sonarQube): reduce complexity and remove unused functions

parent 5881d849
No related branches found
No related tags found
1 merge request!536REDMINE_ISSUE-24432 | Corriger les erreurs SonarQube & réactiver dans CI
Pipeline #42182 passed
......@@ -368,93 +368,6 @@ class FeatureBaseForm(forms.ModelForm):
return instance
class FeatureExtraForm(forms.Form):
def __init__(self, *args, **kwargs):
extra = kwargs.pop('extra', None)
feature = kwargs.pop('feature', None)
super().__init__(*args, **kwargs)
for custom_field in extra.order_by('position'):
if custom_field.field_type == 'boolean':
self.fields[custom_field.name] = forms.BooleanField(
label=custom_field.label, initial=False, required=False,
)
if custom_field.field_type == 'char':
self.fields[custom_field.name] = forms.CharField(
label=custom_field.label, max_length=256, required=False)
if custom_field.field_type == 'date':
self.fields[custom_field.name] = forms.DateField(
label=custom_field.label, required=False,
)
if custom_field.field_type == 'integer':
self.fields[custom_field.name] = forms.IntegerField(
label=custom_field.label, required=False)
if custom_field.field_type == 'decimal':
self.fields[custom_field.name] = forms.DecimalField(
label=custom_field.label, required=False,
widget=forms.TextInput(attrs={
'localization': False
}))
if custom_field.field_type == 'text':
self.fields[custom_field.name] = forms.CharField(
label=custom_field.label, required=False, widget=forms.Textarea())
if custom_field.field_type == 'list' and custom_field.options:
self.fields[custom_field.name] = forms.ChoiceField(
label=custom_field.label,
choices=[(str(xx), str(xx)) for xx in custom_field.options],
required=False)
self.fields[custom_field.name].widget.attrs.update({
'field_type': custom_field.field_type
})
if feature and isinstance(feature.feature_data, dict):
for custom_field in extra:
self.fields[custom_field.name].initial = feature.feature_data.get(custom_field.name)
class FeatureLinkForm(forms.ModelForm):
feature_to = forms.ModelChoiceField(
label="Signalement lié", queryset=Feature.objects.all(),
empty_label=None)
class Meta:
model = FeatureLink
fields = (
'relation_type',
'feature_to',
)
def __init__(self, *args, **kwargs):
feature_type = kwargs.pop('feature_type', None)
feature = kwargs.pop('feature', None)
super().__init__(*args, **kwargs)
qs = Feature.objects.all()
if feature_type:
qs = qs.filter(
feature_type=feature_type
)
if feature:
qs = qs.exclude(
feature_id=feature.feature_id
)
try:
self.fields['feature_to'].queryset = qs
self.fields['feature_to'].label_from_instance = lambda obj: "{} ({} - {})".format(
obj.title, obj.display_creator, obj.created_on.strftime("%d/%m/%Y %H:%M"))
except Exception:
logger.exception('No related features found')
class FeatureTypeModelForm(forms.ModelForm):
title = forms.CharField(label='Titre', required=True)
......
......@@ -91,46 +91,53 @@ class FeatureExtraForm(forms.Form):
feature = kwargs.pop('feature', None)
super().__init__(*args, **kwargs)
for custom_field in extra.order_by('position'):
if custom_field.field_type == 'boolean':
self.fields[custom_field.name] = forms.BooleanField(
label=custom_field.label, initial=False, required=False,
)
if custom_field.field_type == 'char':
self.fields[custom_field.name] = forms.CharField(
label=custom_field.label, max_length=256, required=False)
if custom_field.field_type == 'date':
self.fields[custom_field.name] = forms.DateField(
label=custom_field.label, required=False,
)
if custom_field.field_type == 'integer':
self.fields[custom_field.name] = forms.IntegerField(
label=custom_field.label, required=False)
if custom_field.field_type == 'decimal':
self.fields[custom_field.name] = forms.DecimalField(
label=custom_field.label, required=False,
widget=forms.TextInput(attrs={
'localization': False
}))
if custom_field.field_type == 'text':
self.fields[custom_field.name] = forms.CharField(
label=custom_field.label, required=False, widget=forms.Textarea())
if custom_field.field_type == 'list' and custom_field.options:
self.fields[custom_field.name] = forms.ChoiceField(
label=custom_field.label,
choices=[(str(xx), str(xx)) for xx in custom_field.options],
required=False)
if not extra:
return
self._build_fields(extra)
self._populate_initial_values(extra, feature)
def _build_fields(self, extra):
for custom_field in extra.order_by('position'):
self.fields[custom_field.name] = self._create_form_field(custom_field)
self.fields[custom_field.name].widget.attrs.update({
'field_type': custom_field.field_type
})
def _create_form_field(self, custom_field):
"""Return a Django form field instance based on the field type."""
field_type = custom_field.field_type
common_kwargs = {'label': custom_field.label, 'required': False}
if field_type == 'boolean':
return forms.BooleanField(initial=False, **common_kwargs)
if field_type == 'char':
return forms.CharField(max_length=256, **common_kwargs)
if field_type == 'date':
return forms.DateField(**common_kwargs)
if field_type == 'integer':
return forms.IntegerField(**common_kwargs)
if field_type == 'decimal':
return forms.DecimalField(
widget=forms.TextInput(attrs={'localization': False}),
**common_kwargs
)
if field_type == 'text':
return forms.CharField(widget=forms.Textarea(), **common_kwargs)
if field_type == 'list' and custom_field.options:
choices = [(str(opt), str(opt)) for opt in custom_field.options]
return forms.ChoiceField(choices=choices, **common_kwargs)
# Default fallback (optional, or raise exception)
return forms.CharField(**common_kwargs)
def _populate_initial_values(self, extra, feature):
if feature and isinstance(feature.feature_data, dict):
for custom_field in extra:
self.fields[custom_field.name].initial = feature.feature_data.get(custom_field.name)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment