diff options
Diffstat (limited to 'archaeological_warehouse/models.py')
| -rw-r--r-- | archaeological_warehouse/models.py | 33 | 
1 files changed, 32 insertions, 1 deletions
| diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 97898e5c1..3dd3f08e8 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -31,7 +31,7 @@ from ishtar_common.data_importer import post_importer_action  from ishtar_common.model_managers import ExternalIdManager  from ishtar_common.models import Document, GeneralType, get_external_id, \      LightHistorizedItem, OwnPerms, Address, Person, post_save_cache, \ -    DashboardFormItem, ShortMenuItem, \ +    DashboardFormItem, ShortMenuItem, Organization, OrganizationType, \      document_attached_changed, SearchAltName, DynamicRequest, GeoItem, \      QRCodeItem, SearchVectorConfig, DocumentItem  from ishtar_common.model_merging import merge_model_objects @@ -89,6 +89,9 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,      person_in_charge = models.ForeignKey(          Person, on_delete=models.SET_NULL, related_name='warehouse_in_charge',          verbose_name=_(u"Person in charge"), null=True, blank=True) +    organization = models.ForeignKey( +        Organization, blank=True, null=True, related_name='warehouses', +        verbose_name=_("Organization"), on_delete=models.SET_NULL)      comment = models.TextField(_(u"Comment"), null=True, blank=True)      associated_divisions = models.ManyToManyField(          'WarehouseDivision', verbose_name=_("Divisions"), blank=True, @@ -104,6 +107,7 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,      external_id = models.TextField(_(u"External ID"), blank=True, null=True)      auto_external_id = models.BooleanField(          _(u"External ID is set automatically"), default=False) +    SUB_ADDRESSES = ["organization", "person_in_charge"]      class Meta:          verbose_name = _(u"Warehouse") @@ -125,6 +129,33 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,      def _get_base_image_path(self):          return u"{}/{}".format(self.SLUG, self.external_id) +    def create_attached_organization(self): +        """ +        Create an attached organization from warehouse fields +        """ +        dct_orga = {} +        for k in Address.FIELDS: +            dct_orga[k] = getattr(self, k) + +        q = OrganizationType.objects.filter(txt_idx="warehouse") +        if q.count(): +            orga_type = q.all()[0] +        else: +            orga_type, __ = OrganizationType.objects.get_or_create( +                txt_idx="undefined", +                defaults={"label": _("Undefined")} +            ) +        dct_orga["organization_type"] = orga_type +        dct_orga["name"] = self.name +        orga = Organization.objects.create(**dct_orga) +        self.organization = orga +        for k in Address.FIELDS: +            if k == "alt_address_is_prefered": +                setattr(self, k, False) +            else: +                setattr(self, k, None) +        self.save() +      @property      def location_types(self):          return [ | 
