diff options
-rw-r--r-- | archaeological_files/forms.py | 53 | ||||
-rw-r--r-- | archaeological_files_pdl/forms.py | 54 | ||||
-rw-r--r-- | archaeological_operations/forms.py | 57 | ||||
-rw-r--r-- | ishtar_common/models.py | 54 |
4 files changed, 139 insertions, 79 deletions
diff --git a/archaeological_files/forms.py b/archaeological_files/forms.py index 26b839940..04badfc10 100644 --- a/archaeological_files/forms.py +++ b/archaeological_files/forms.py @@ -29,7 +29,8 @@ from django.utils.translation import ugettext_lazy as _ from django.utils.safestring import mark_safe from ishtar_common.models import Person, PersonType, Organization, \ - OrganizationType, valid_id, Department + OrganizationType, valid_id, Department, person_type_pks_lazy, \ + person_type_pk_lazy, organization_type_pk_lazy, organization_type_pks_lazy from archaeological_operations.models import ActType, AdministrativeAct, \ OperationType import models @@ -38,15 +39,9 @@ from ishtar_common.forms import FinalForm, get_now, reverse_lazy, TableSelect, \ from ishtar_common.forms_common import get_town_field from archaeological_operations.forms import AdministrativeActOpeForm, \ AdministrativeActOpeFormSelection, \ - ParcelField, SLICING, HEAD_SCIENTIST, SRA_AGENT, AdministrativeActModifForm + ParcelField, SLICING, AdministrativeActModifForm from ishtar_common import widgets -GENERAL_CONTRACTOR, created = PersonType.objects.get_or_create( - txt_idx='general_contractor') - -GENERAL_CONTRACTOR_ORGA, created = OrganizationType.objects.get_or_create( - txt_idx='general_contractor') - class FileSelect(TableSelect): year = forms.IntegerField(label=_("Year")) @@ -70,21 +65,22 @@ class FileSelect(TableSelect): label=_(u"In charge"), widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-person', - args=[SRA_AGENT.pk]), + args=[person_type_pk_lazy('sra_agent')]), associated_model=Person), validators=[valid_id(Person)]) general_contractor = forms.IntegerField( label=_(u"General contractor"), widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-person', - args=[GENERAL_CONTRACTOR.pk]), + args=[person_type_pk_lazy('general_contractor')]), associated_model=Person), validators=[valid_id(Person)]) general_contractor__attached_to = forms.IntegerField( label=_(u"Organization of general contractor"), widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-organization', - args=[GENERAL_CONTRACTOR_ORGA.pk]), + args=[organization_type_pks_lazy([ + 'general_contractor'])]), associated_model=Organization), validators=[valid_id(Organization)]) history_creator = forms.IntegerField( @@ -205,8 +201,10 @@ class FileFormGeneral(ManageOldType, forms.Form): in_charge = forms.IntegerField( label=_("Person in charge"), widget=widgets.JQueryAutoComplete( - reverse_lazy('autocomplete-person', args=[SRA_AGENT.pk]), - limit={'person_types': [SRA_AGENT.pk]}, + reverse_lazy('autocomplete-person', args=[ + person_type_pks_lazy(['sra_agent'])]), + limit={'person_types': [ + person_type_pk_lazy('sra_agent')]}, associated_model=Person, new=True), validators=[valid_id(Person)]) year = forms.IntegerField(label=_("Year"), @@ -272,12 +270,6 @@ class FileFormGeneralRO(FileFormGeneral): _(u"Another file with this numeric id exists.")) return cleaned_data -RESPONSIBLE_PLANNING_SERVICE, created = PersonType.objects.get_or_create( - txt_idx='responsible_planning_service') - -RESPONSIBLE_PLANNING_SERVICE_ORGA, created = \ - OrganizationType.objects.get_or_create(txt_idx='planning_service') - class FileFormPreventive(ManageOldType, forms.Form): form_label = _(u"Preventive informations") @@ -290,10 +282,10 @@ class FileFormPreventive(ManageOldType, forms.Form): widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-person', - args=[PersonType.objects.get(txt_idx='general_contractor').pk] + args=[person_type_pks_lazy(['general_contractor'])] ), limit={'person_types': [ - PersonType.objects.get(txt_idx='general_contractor').pk]}, + person_type_pk_lazy('general_contractor')]}, associated_model=Person, new=True), validators=[valid_id(Person)]) responsible_town_planning_service = forms.IntegerField( @@ -302,10 +294,11 @@ class FileFormPreventive(ManageOldType, forms.Form): widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-person', - args=[RESPONSIBLE_PLANNING_SERVICE.pk] + args=[person_type_pks_lazy(['responsible_planning_service'])] ), limit={'person_types': [ - RESPONSIBLE_PLANNING_SERVICE.pk]}, + person_type_pk_lazy('responsible_planning_service') + ]}, associated_model=Person, new=True), validators=[valid_id(Person)]) permit_type = forms.ChoiceField(label=_(u"Permit type"), required=False, @@ -351,10 +344,9 @@ class FileFormResearch(ManageOldType, forms.Form): widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-person', - args=["_".join([unicode(HEAD_SCIENTIST.pk), - unicode(SRA_AGENT.pk)])]), - limit={'person_types': [unicode(HEAD_SCIENTIST.pk), - unicode(SRA_AGENT.pk)]}, + args=[person_type_pks_lazy(['head_scientist', 'sra_agent'])]), + limit={'person_types': [person_type_pk_lazy('head_scientist'), + person_type_pk_lazy('sra_agent')]}, associated_model=Person, new=True), label=_(u"Scientist in charge")) requested_operation_type = forms.ChoiceField( @@ -454,8 +446,7 @@ class AdministrativeActFileSelect(TableSelect): widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-person', - args=[ - PersonType.objects.get(txt_idx='general_contractor').pk]), + args=[person_type_pk_lazy('general_contractor')]), associated_model=Person), validators=[valid_id(Person)]) associated_file__general_contractor__attached_to = forms.IntegerField( @@ -463,7 +454,7 @@ class AdministrativeActFileSelect(TableSelect): widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-organization', - args=[GENERAL_CONTRACTOR_ORGA.pk]), + args=[organization_type_pks_lazy(['general_contractor'])]), associated_model=Organization), validators=[valid_id(Organization)]) associated_file__numeric_reference = forms.IntegerField( @@ -476,7 +467,7 @@ class AdministrativeActFileSelect(TableSelect): widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-person', - args=[SRA_AGENT.pk]), + args=[person_type_pk_lazy('sra_agent')]), associated_model=Person), validators=[valid_id(Person)]) associated_file__permit_reference = forms.CharField( diff --git a/archaeological_files_pdl/forms.py b/archaeological_files_pdl/forms.py index 99dc97137..5c92f689a 100644 --- a/archaeological_files_pdl/forms.py +++ b/archaeological_files_pdl/forms.py @@ -24,15 +24,12 @@ from django.core import validators from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ -from ishtar_common.models import Person, Town, Department, valid_id +from ishtar_common.models import Person, Town, Department, valid_id, \ + person_type_pk_lazy, person_type_pks_lazy, organization_type_pks_lazy, \ + organization_type_pk_lazy, organization_type_lazy, person_type_lazy from archaeological_files import models from ishtar_common.forms import get_now, reverse_lazy, ManageOldType -from archaeological_files.forms import GENERAL_CONTRACTOR, \ - GENERAL_CONTRACTOR_ORGA, RESPONSIBLE_PLANNING_SERVICE, \ - RESPONSIBLE_PLANNING_SERVICE_ORGA - -from archaeological_operations.forms import SRA_AGENT from ishtar_common import widgets @@ -135,10 +132,10 @@ class FileFormResearchAddress(forms.Form): class PersonOrgaForm(forms.Form): PERSON_FIELD = 'TO BE DEFINED' - PERSON_TYPE = GENERAL_CONTRACTOR + PERSON_TYPE = person_type_lazy('general_contractor') PERSON_LABEL = "" ORGA_FIELD = 'TO BE DEFINED' - ORGA_TYPE = GENERAL_CONTRACTOR_ORGA + ORGA_TYPE = organization_type_lazy('general_contractor') ORGA_LABEL = "" def _media(self): @@ -238,12 +235,15 @@ class FileFormGeneralContractor(PersonOrgaForm): required=False, widget=widgets.JQueryPersonOrganization( reverse_lazy('autocomplete-organization', - args=[GENERAL_CONTRACTOR_ORGA.pk]), + args=[ + organization_type_pks_lazy(['general_contractor'])] + ), reverse_lazy('organization_create'), model=models.Organization, limit={ - 'organization_type': [GENERAL_CONTRACTOR_ORGA.pk] - }, + 'organization_type': [ + organization_type_pk_lazy('general_contractor') + ]}, js_template='ishtar/blocks/JQueryCorporationPerson.js', new=True), validators=[valid_id(models.Organization)] @@ -253,9 +253,13 @@ class FileFormGeneralContractor(PersonOrgaForm): required=False, widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-person', - args=[GENERAL_CONTRACTOR.pk]), + args=[ + person_type_pks_lazy(['general_contractor']) + ]), associated_model=Person, - limit={'person_types': [GENERAL_CONTRACTOR.pk]}, + limit={'person_types': [ + person_type_pk_lazy(['general_contractor']) + ]}, dynamic_limit=['general_contractor'], url_new='new-person-noorga', new=True), @@ -263,10 +267,10 @@ class FileFormGeneralContractor(PersonOrgaForm): ) PERSON_FIELD = 'general_contractor' - PERSON_TYPE = GENERAL_CONTRACTOR + PERSON_TYPE = person_type_lazy('general_contractor') PERSON_LABEL = _(u"General contractor") ORGA_FIELD = 'corporation_general_contractor' - ORGA_TYPE = GENERAL_CONTRACTOR_ORGA + ORGA_TYPE = organization_type_lazy('general_contractor') ORGA_LABEL = _(u"General contractor") def __init__(self, *args, **kwargs): @@ -349,12 +353,14 @@ class FileFormPlanningService(forms.Form): label=_("Planning service"), required=False, widget=widgets.JQueryPersonOrganization( - reverse_lazy('autocomplete-organization', - args=[RESPONSIBLE_PLANNING_SERVICE_ORGA.pk]), + reverse_lazy( + 'autocomplete-organization', + args=[organization_type_pks_lazy(['planning_service'])]), reverse_lazy('organization_create'), model=models.Organization, limit={ - 'organization_type': [RESPONSIBLE_PLANNING_SERVICE_ORGA.pk] + 'organization_type': + [organization_type_pk_lazy(['planning_service'])], }, js_template='ishtar/blocks/JQueryCorporationPerson.js', new=True), @@ -365,9 +371,13 @@ class FileFormPlanningService(forms.Form): required=False, widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-person', - args=[RESPONSIBLE_PLANNING_SERVICE.pk]), + args=[ + person_type_pks_lazy( + ['responsible_planning_service'])]), associated_model=Person, - limit={'person_types': [RESPONSIBLE_PLANNING_SERVICE.pk]}, + limit={'person_types': [ + person_type_pk_lazy('responsible_planning_service') + ]}, dynamic_limit=['planning_service'], url_new='new-person-noorga', new=True), @@ -384,10 +394,10 @@ class FileFormInstruction(forms.Form): widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-person', - args=[SRA_AGENT.pk]), + args=[person_type_pks_lazy(["sra_agent"])]), limit={ 'person_types': [ - SRA_AGENT.pk] + person_type_pk_lazy('sra_agent')] }, associated_model=Person, new=True), validators=[valid_id(Person)]) diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index a12f245cb..37c35f4be 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -34,7 +34,9 @@ from django.utils.translation import ugettext_lazy as _, pgettext_lazy from django.utils.safestring import mark_safe from ishtar_common.models import valid_id, PersonType, Person, Town, \ - DocumentTemplate, Organization, OrganizationType, get_current_profile + DocumentTemplate, Organization, OrganizationType, get_current_profile, \ + person_type_pks_lazy, person_type_pk_lazy, organization_type_pks_lazy, \ + organization_type_pk_lazy from ishtar_common.wizards import MultiValueDict @@ -448,12 +450,6 @@ class RecordRelationsForm(ManageOldType, forms.Form): RecordRelationsFormSet = formset_factory(RecordRelationsForm, can_delete=True) RecordRelationsFormSet.form_label = _(u"Relations") -SRA_AGENT, created = PersonType.objects.get_or_create(txt_idx='sra_agent') -HEAD_SCIENTIST, created = PersonType.objects.get_or_create( - txt_idx='head_scientist') - -OPERATOR, created = OrganizationType.objects.get_or_create(txt_idx='operator') - class OperationSelect(TableSelect): year = forms.IntegerField(label=_("Year")) @@ -476,22 +472,23 @@ class OperationSelect(TableSelect): widget=widgets.JQueryAutoComplete( reverse_lazy( 'autocomplete-person-permissive', - args=["_".join( - [unicode(PersonType.objects.get(txt_idx='sra_agent').pk)])] + args=[person_type_pks_lazy(['sra_agent'])] ), associated_model=Person), label=_(u"In charge")) scientist = forms.IntegerField( widget=widgets.JQueryAutoComplete( - reverse_lazy('autocomplete-person-permissive', - args=["_".join([unicode(HEAD_SCIENTIST.pk), - unicode(SRA_AGENT.pk)])]), + reverse_lazy( + 'autocomplete-person-permissive', + args=[person_type_pks_lazy(['sra_agent', 'head_scientist'])]), associated_model=Person), label=_(u"Scientist in charge")) operator = forms.IntegerField( label=_("Operator"), widget=widgets.JQueryAutoComplete( - reverse_lazy('autocomplete-organization', args=[OPERATOR.pk]), + reverse_lazy( + 'autocomplete-organization', + args=[organization_type_pks_lazy(['operator'])]), associated_model=Organization), validators=[valid_id(Organization)]) # operator_reference = forms.CharField(label=_(u"Operator reference"), @@ -762,18 +759,21 @@ class OperationFormGeneral(ManageOldType, forms.Form): scientist = forms.IntegerField( label=_("Head scientist"), widget=widgets.JQueryAutoComplete( - reverse_lazy('autocomplete-person', - args=["_".join([unicode(HEAD_SCIENTIST.pk), - unicode(SRA_AGENT.pk)])]), + reverse_lazy( + 'autocomplete-person', + args=[person_type_pks_lazy(['head_scientist', 'sra_agent'])]), associated_model=Person, - limit={'person_types': (HEAD_SCIENTIST.pk, SRA_AGENT.pk)}, + limit={ + 'person_types': (person_type_pk_lazy('head_scientist'), + person_type_pk_lazy('sra_agent'))}, new=True), validators=[valid_id(Person)], required=False) operator = forms.IntegerField( label=_("Operator"), widget=widgets.JQueryAutoComplete( - reverse_lazy('autocomplete-organization', args=[OPERATOR.pk]), - limit={'organization_type': (OPERATOR.pk,)}, + reverse_lazy('autocomplete-organization', + args=[organization_type_pk_lazy('operator')]), + limit={'organization_type': organization_type_pk_lazy('operator')}, associated_model=Organization, new=True), validators=[valid_id(Organization)], required=False) operator_reference = forms.CharField(label=_(u"Operator reference"), @@ -781,10 +781,12 @@ class OperationFormGeneral(ManageOldType, forms.Form): in_charge = forms.IntegerField( label=_("In charge"), widget=widgets.JQueryAutoComplete( - reverse_lazy('autocomplete-person', - args=["_".join([unicode(SRA_AGENT.pk)])]), + reverse_lazy( + 'autocomplete-person', + args=[person_type_pks_lazy(['sra_agent'])]), associated_model=Person, - limit={'person_types': [SRA_AGENT.pk]}, new=True), + limit={'person_types': [person_type_pk_lazy('sra_agent')]}, + new=True), validators=[valid_id(Person)], required=False) surface = forms.IntegerField( required=False, widget=widgets.AreaWidget, @@ -812,10 +814,13 @@ class OperationFormGeneral(ManageOldType, forms.Form): cira_rapporteur = forms.IntegerField( label=u"Rapporteur CIRA", widget=widgets.JQueryAutoComplete( - reverse_lazy('autocomplete-person', - args=["_".join([unicode(HEAD_SCIENTIST.pk), - unicode(SRA_AGENT.pk)])]), - limit={'person_types': [SRA_AGENT.pk, HEAD_SCIENTIST.pk]}, + reverse_lazy( + 'autocomplete-person', + args=[person_type_pks_lazy(['head_scientist', + 'sra_agent'])]), + limit={'person_types': [ + person_type_pk_lazy('sra_agent'), + person_type_pk_lazy('head_scientist')]}, associated_model=Person, new=True), validators=[valid_id(Person)], required=False) documentation_deadline = forms.DateField( diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 3453a53c5..814387bb4 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -43,6 +43,7 @@ from django.db.utils import DatabaseError from django.db.models import Q, Max, Count from django.db.models.base import ModelBase from django.db.models.signals import post_save, pre_delete, post_delete +from django.utils.functional import lazy from django.utils.translation import ugettext_lazy as _, ugettext, \ pgettext_lazy @@ -334,6 +335,51 @@ class GeneralType(Cached, models.Model): return self.label @classmethod + def get_or_create(cls, slug, label=''): + """ + Get or create a new item. + + :param slug: textual id + :param label: label for initialization if the item doesn't exist (not + mandatory) + + :return: instancied item of the base class + """ + + item = cls.get_cache(slug) + if item: + return item + item, created = cls.objects.get_or_create( + txt_idx=slug, defaults={'label': label}) + return item + + @classmethod + def get_or_create_pk(cls, slug): + """ + Get an id from a slug. Create the associated item if needed. + + :param slug: textual id + + :return: the id of the item + """ + return cls.get_or_create(slug).pk + + @classmethod + def get_or_create_pks(cls, slugs): + """ + Get and merge a list of ids from a slug list. Create the associated + items if needed. + + :param slugs: textual ids + + :return: string with ids separated by "_" + """ + items = [] + for slug in slugs: + items.append(str(cls.get_or_create(slug).pk)) + return "_".join(items) + + @classmethod def get_help(cls, dct={}, exclude=[]): help_text = cls.HELP_TEXT c_rank = -1 @@ -1534,6 +1580,10 @@ class OrganizationType(GeneralType): post_save.connect(post_save_cache, sender=OrganizationType) post_delete.connect(post_save_cache, sender=OrganizationType) +organization_type_lazy = lazy(OrganizationType.get_or_create, OrganizationType) +organization_type_pk_lazy = lazy(OrganizationType.get_or_create_pk, int) +organization_type_pks_lazy = lazy(OrganizationType.get_or_create_pks, unicode) + IMPORTER_CLASSES = {} IMPORTER_CLASSES.update({ @@ -2324,6 +2374,10 @@ class PersonType(GeneralType): post_save.connect(post_save_cache, sender=PersonType) post_delete.connect(post_save_cache, sender=PersonType) +person_type_lazy = lazy(PersonType.get_or_create_pk, PersonType) +person_type_pk_lazy = lazy(PersonType.get_or_create_pk, int) +person_type_pks_lazy = lazy(PersonType.get_or_create_pks, unicode) + class TitleType(GeneralType): class Meta: |