diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-09-01 18:49:14 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-09-01 18:49:14 +0200 |
commit | caebc92f6e7f0a3e8445d054edd5587c58920c76 (patch) | |
tree | cfcbb351671ea74fd6b9ab43ea4b61d3e4fc661a /ishtar_common | |
parent | f0901e79cd11ead1d5ac2543ac4affcf23a5f05f (diff) | |
download | Ishtar-caebc92f6e7f0a3e8445d054edd5587c58920c76.tar.bz2 Ishtar-caebc92f6e7f0a3e8445d054edd5587c58920c76.zip |
Non-available types display on old item modification (refs #3179)
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/forms.py | 31 | ||||
-rw-r--r-- | ishtar_common/forms_common.py | 27 | ||||
-rw-r--r-- | ishtar_common/models.py | 23 |
3 files changed, 70 insertions, 11 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 89df1b1a5..79ca2c360 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -194,6 +194,37 @@ def get_data_from_formset(data): return values +class ManageOldType(object): + def __init__(self, *args, **kwargs): + """ + init_data is used to manage deactivated items in list when editing + old data + """ + prefix = kwargs.get('prefix') or '' + self.init_data = {} + if 'data' in kwargs and kwargs['data']: + for k in kwargs['data']: + if prefix not in k: + continue + new_k = k[len(prefix) + 1:] + for val in kwargs['data'].getlist(k): + if not val: + continue + if new_k not in self.init_data: + self.init_data[new_k] = [] + self.init_data[new_k].append(val) + if 'initial' in kwargs and kwargs['initial']: + for k in kwargs['initial']: + if k not in self.init_data or not self.init_data[k]: + for val in kwargs['initial'].getlist(k): + if not val: + continue + if k not in self.init_data: + self.init_data[k] = [] + self.init_data[k].append(val) + super(ManageOldType, self).__init__(*args, **kwargs) + + class DocumentGenerationForm(forms.Form): """ Form to generate document by choosing the template diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 2859ed4e5..f6889ae1f 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -33,7 +33,8 @@ from django.utils.translation import ugettext_lazy as _ import models import widgets -from forms import FinalForm, FormSet, reverse_lazy, name_validator, TableSelect +from forms import FinalForm, FormSet, reverse_lazy, name_validator, \ + TableSelect, ManageOldType def get_town_field(label=_(u"Town"), required=True): @@ -154,7 +155,7 @@ class TargetKeyForm(forms.ModelForm): self.instance.save() -class OrganizationForm(NewItemForm): +class OrganizationForm(ManageOldType, NewItemForm): form_label = _(u"Organization") associated_models = {'organization_type': models.OrganizationType} name = forms.CharField( @@ -178,7 +179,8 @@ class OrganizationForm(NewItemForm): def __init__(self, *args, **kwargs): super(OrganizationForm, self).__init__(*args, **kwargs) self.fields['organization_type'].choices = \ - models.OrganizationType.get_types() + models.OrganizationType.get_types( + initial=self.init_data.get('organization_type')) self.fields['organization_type'].help_text = \ models.OrganizationType.get_help() self.limit_fields() @@ -254,7 +256,7 @@ class PersonFormSelection(forms.Form): validators=[models.valid_id(models.Person)]) -class SimplePersonForm(NewItemForm): +class SimplePersonForm(ManageOldType, NewItemForm): form_label = _("Identity") associated_models = {'attached_to': models.Organization, 'title': models.TitleType} @@ -309,7 +311,8 @@ class SimplePersonForm(NewItemForm): def __init__(self, *args, **kwargs): super(SimplePersonForm, self).__init__(*args, **kwargs) self.fields['raw_name'].widget.attrs['readonly'] = True - self.fields['title'].choices = models.TitleType.get_types() + self.fields['title'].choices = models.TitleType.get_types( + initial=self.init_data.get('title')) class PersonUserSelect(PersonSelect): @@ -400,6 +403,7 @@ class PersonForm(SimplePersonForm): def __init__(self, *args, **kwargs): super(PersonForm, self).__init__(*args, **kwargs) self.fields['person_types'].choices = models.PersonType.get_types( + initial=self.init_data.get('person_types'), empty_first=False) self.fields['person_types'].help_text = models.PersonType.get_help() self.limit_fields() @@ -423,7 +427,7 @@ class NoOrgaPersonForm(PersonForm): self.fields.pop('attached_to') -class PersonTypeForm(forms.Form): +class PersonTypeForm(ManageOldType, forms.Form): form_label = _("Person type") base_model = 'person_type' associated_models = {'person_type': models.PersonType} @@ -434,6 +438,7 @@ class PersonTypeForm(forms.Form): def __init__(self, *args, **kwargs): super(PersonTypeForm, self).__init__(*args, **kwargs) self.fields['person_type'].choices = models.PersonType.get_types( + initial=self.init_data.get('person_type'), empty_first=False) self.fields['person_type'].help_text = models.PersonType.get_help() @@ -642,7 +647,7 @@ class MergeOrganizationForm(MergeForm): ###################### # Sources management # ###################### -class SourceForm(forms.Form): +class SourceForm(ManageOldType, forms.Form): form_label = _(u"Documentation informations") associated_models = {'source_type': models.SourceType} title = forms.CharField(label=_(u"Title"), @@ -675,7 +680,8 @@ class SourceForm(forms.Form): def __init__(self, *args, **kwargs): super(SourceForm, self).__init__(*args, **kwargs) - self.fields['source_type'].choices = models.SourceType.get_types() + self.fields['source_type'].choices = models.SourceType.get_types( + initial=self.init_data.get('source_type')) class SourceSelect(TableSelect): @@ -711,7 +717,7 @@ class SourceDeletionForm(FinalForm): ###################### -class AuthorForm(NewItemForm): +class AuthorForm(ManageOldType, NewItemForm): form_label = _(u"Author") associated_models = {'person': models.Person, 'author_type': models.AuthorType} @@ -724,7 +730,8 @@ class AuthorForm(NewItemForm): def __init__(self, *args, **kwargs): super(AuthorForm, self).__init__(*args, **kwargs) - self.fields['author_type'].choices = models.AuthorType.get_types() + self.fields['author_type'].choices = models.AuthorType.get_types( + initial=self.init_data.get('author_type')) self.limit_fields() def save(self, user): diff --git a/ishtar_common/models.py b/ishtar_common/models.py index e2142ef0c..04077b240 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -330,7 +330,28 @@ class GeneralType(Cached, models.Model): @classmethod def get_types(cls, dct={}, instances=False, exclude=[], empty_first=True, - default=None): + default=None, initial=None): + types = cls.pre_get_types(dct, instances, exclude, empty_first, + default) + if not initial: + return types + for value in initial: + try: + pk = int(value) + except ValueError: + continue + if pk in [idx for idx, lbl in types]: + continue + try: + extra_type = cls.objects.get(pk=pk) + types.append((extra_type.pk, unicode(extra_type))) + except cls.DoesNotExist: + continue + return types + + @classmethod + def pre_get_types(cls, dct={}, instances=False, exclude=[], + empty_first=True, default=None): # cache cache_key = None if not instances: |