diff options
Diffstat (limited to 'archaeological_warehouse/models.py')
| -rw-r--r-- | archaeological_warehouse/models.py | 104 | 
1 files changed, 55 insertions, 49 deletions
| diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index e62575ccf..53e50976d 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -35,7 +35,8 @@ from ishtar_common.models import Document, GeneralType, get_external_id, \      LightHistorizedItem, OwnPerms, Address, Person, post_save_cache, \      DashboardFormItem, ShortMenuItem, Organization, OrganizationType, \      document_attached_changed, SearchAltName, DynamicRequest, GeoItem, \ -    QRCodeItem, SearchVectorConfig, DocumentItem, QuickAction, MainItem +    QRCodeItem, SearchVectorConfig, DocumentItem, QuickAction, MainItem, \ +    Merge  from ishtar_common.model_merging import merge_model_objects  from ishtar_common.utils import cached_label_changed, \      cached_label_and_geo_changed @@ -43,8 +44,8 @@ from ishtar_common.utils import cached_label_changed, \  class WarehouseType(GeneralType):      class Meta: -        verbose_name = _(u"Warehouse type") -        verbose_name_plural = _(u"Warehouse types") +        verbose_name = _("Warehouse type") +        verbose_name_plural = _("Warehouse types")          ordering = ('label',) @@ -97,7 +98,7 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,      QA_LOCK = QuickAction(          url="warehouse-qa-lock", icon_class="fa fa-lock", -        text=_(u"Lock/Unlock"), target="many", +        text=_("Lock/Unlock"), target="many",          rights=['change_warehouse', 'change_own_warehouse']      )      QUICK_ACTIONS = [QA_LOCK] @@ -105,41 +106,41 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,      objects = UUIDModelManager()      uuid = models.UUIDField(default=uuid.uuid4) -    name = models.CharField(_(u"Name"), max_length=200) +    name = models.CharField(_("Name"), max_length=200)      warehouse_type = models.ForeignKey(WarehouseType, -                                       verbose_name=_(u"Warehouse type")) +                                       verbose_name=_("Warehouse type"))      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) +        verbose_name=_("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) +    comment = models.TextField(_("Comment"), null=True, blank=True)      associated_divisions = models.ManyToManyField(          'WarehouseDivision', verbose_name=_("Divisions"), blank=True,          through='WarehouseDivisionLink'      )      documents = models.ManyToManyField( -        Document, related_name='warehouses', verbose_name=_(u"Documents"), +        Document, related_name='warehouses', verbose_name=_("Documents"),          blank=True)      main_image = models.ForeignKey(          Document, related_name='main_image_warehouses',          on_delete=models.SET_NULL, -        verbose_name=_(u"Main image"), blank=True, null=True) -    external_id = models.TextField(_(u"External ID"), blank=True, null=True) +        verbose_name=_("Main image"), blank=True, null=True) +    external_id = models.TextField(_("External ID"), blank=True, null=True)      auto_external_id = models.BooleanField( -        _(u"External ID is set automatically"), default=False) +        _("External ID is set automatically"), default=False)      SUB_ADDRESSES = ["organization", "person_in_charge"]      class Meta: -        verbose_name = _(u"Warehouse") -        verbose_name_plural = _(u"Warehouses") +        verbose_name = _("Warehouse") +        verbose_name_plural = _("Warehouses")          permissions = ( -            ("view_warehouse", u"Can view all Warehouses"), -            ("view_own_warehouse", u"Can view own Warehouse"), -            ("add_own_warehouse", u"Can add own Warehouse"), -            ("change_own_warehouse", u"Can change own Warehouse"), -            ("delete_own_warehouse", u"Can delete own Warehouse"), +            ("view_warehouse", "Can view all Warehouses"), +            ("view_own_warehouse", "Can view own Warehouse"), +            ("add_own_warehouse", "Can add own Warehouse"), +            ("change_own_warehouse", "Can change own Warehouse"), +            ("delete_own_warehouse", "Can delete own Warehouse"),          )          indexes = [              GinIndex(fields=['data']), @@ -152,7 +153,7 @@ class Warehouse(Address, DocumentItem, GeoItem, QRCodeItem, DashboardFormItem,          return (self.uuid, )      def _get_base_image_path(self): -        return u"{}/{}".format(self.SLUG, self.external_id) +        return "{}/{}".format(self.SLUG, self.external_id)      def create_attached_organization(self):          """ @@ -360,8 +361,8 @@ post_save.connect(cached_label_and_geo_changed, sender=Warehouse)  class WarehouseDivision(GeneralType):      class Meta: -        verbose_name = _(u"Warehouse division type") -        verbose_name_plural = _(u"Warehouse division types") +        verbose_name = _("Warehouse division type") +        verbose_name_plural = _("Warehouse division types")  post_save.connect(post_save_cache, sender=WarehouseDivision) @@ -379,16 +380,16 @@ class ContainerType(GeneralType):          _("Stationary"), default=False,          help_text=_("Container that usually will not be moved. Ex: building, "                      "room.")) -    length = models.IntegerField(_(u"Length (mm)"), blank=True, null=True) -    width = models.IntegerField(_(u"Width (mm)"), blank=True, null=True) -    height = models.IntegerField(_(u"Height (mm)"), blank=True, null=True) -    volume = models.FloatField(_(u"Volume (l)"), blank=True, null=True) -    reference = models.CharField(_(u"Ref."), max_length=300, blank=True, +    length = models.IntegerField(_("Length (mm)"), blank=True, null=True) +    width = models.IntegerField(_("Width (mm)"), blank=True, null=True) +    height = models.IntegerField(_("Height (mm)"), blank=True, null=True) +    volume = models.FloatField(_("Volume (l)"), blank=True, null=True) +    reference = models.CharField(_("Ref."), max_length=300, blank=True,                                   null=True)      class Meta: -        verbose_name = _(u"Container type") -        verbose_name_plural = _(u"Container types") +        verbose_name = _("Container type") +        verbose_name_plural = _("Container types")          ordering = ('label',) @@ -413,7 +414,7 @@ class WarehouseDivisionLink(models.Model):          unique_together = ('warehouse', 'division')      def __str__(self): -        return u"{} - {}".format(self.warehouse, self.division) +        return "{} - {}".format(self.warehouse, self.division)      def natural_key(self):          return self.warehouse.uuid, self.division.txt_idx @@ -458,7 +459,7 @@ class ContainerTree:      """ -class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem, +class Container(DocumentItem, Merge, LightHistorizedItem, QRCodeItem, GeoItem,                  OwnPerms, MainItem):      SLUG = 'container'      APP = "archaeological-warehouse" @@ -659,7 +660,7 @@ class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem,      QA_LOCK = QuickAction(          url="container-qa-lock", icon_class="fa fa-lock", -        text=_(u"Lock/Unlock"), target="many", +        text=_("Lock/Unlock"), target="many",          rights=['change_container', 'change_own_container']      )      QUICK_ACTIONS = [QA_LOCK] @@ -678,13 +679,13 @@ class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem,      )      container_type = models.ForeignKey(ContainerType,                                         verbose_name=_("Container type")) -    reference = models.TextField(_(u"Container ref.")) -    comment = models.TextField(_(u"Comment"), null=True, blank=True) -    cached_label = models.TextField(_(u"Localisation"), null=True, blank=True, +    reference = models.TextField(_("Container ref.")) +    comment = models.TextField(_("Comment"), null=True, blank=True) +    cached_label = models.TextField(_("Localisation"), null=True, blank=True,                                      db_index=True) -    cached_location = models.TextField(_(u"Cached location"), +    cached_location = models.TextField(_("Cached location"),                                         null=True, blank=True, db_index=True) -    cached_division = models.TextField(_(u"Cached division"), +    cached_division = models.TextField(_("Cached division"),                                         null=True, blank=True, db_index=True)      parent = models.ForeignKey("Container", verbose_name=_("Parent container"),                                 on_delete=models.SET_NULL, @@ -712,11 +713,11 @@ class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem,                             ('location', 'container_type', 'parent',                              'reference')]          permissions = ( -            ("view_container", u"Can view all Containers"), -            ("view_own_container", u"Can view own Container"), -            ("add_own_container", u"Can add own Container"), -            ("change_own_container", u"Can change own Container"), -            ("delete_own_container", u"Can delete own Container"), +            ("view_container", "Can view all Containers"), +            ("view_own_container", "Can view own Container"), +            ("add_own_container", "Can add own Container"), +            ("change_own_container", "Can change own Container"), +            ("delete_own_container", "Can delete own Container"),          )          indexes = [              GinIndex(fields=['data']), @@ -726,6 +727,10 @@ class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem,          return self.cached_label or ""      @property +    def name(self): +        return "{} - {}".format(self.container_type.name, self.reference) + +    @property      def short_label(self):          return "{} {}".format(self.container_type.label, self.reference) @@ -767,6 +772,7 @@ class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem,          return self.location._get_base_image_path() + "/" + self.external_id      def merge(self, item, keep_old=False): +        # TODO: change localisation management          locas = [              cl.division.division.txt_idx              for cl in ContainerLocalisation.objects.filter(container=self).all() @@ -1050,7 +1056,7 @@ class Container(DocumentItem, LightHistorizedItem, QRCodeItem, GeoItem,          if can_edit_find:              actions += [                  (reverse('container-add-treatment', args=[self.pk]), -                 _(u"Add treatment"), "fa fa-flask", "", "", False), +                 _("Add treatment"), "fa fa-flask", "", "", False),              ]          return actions @@ -1137,21 +1143,21 @@ class ContainerLocalisationManager(models.Manager):  class ContainerLocalisation(models.Model): -    container = models.ForeignKey(Container, verbose_name=_(u"Container"), +    container = models.ForeignKey(Container, verbose_name=_("Container"),                                    related_name='division')      division = models.ForeignKey(WarehouseDivisionLink, -                                 verbose_name=_(u"Division")) -    reference = models.CharField(_(u"Reference"), max_length=200, default='') +                                 verbose_name=_("Division")) +    reference = models.CharField(_("Reference"), max_length=200, default='')      objects = ContainerLocalisationManager()      class Meta: -        verbose_name = _(u"Container localisation") -        verbose_name_plural = _(u"Container localisations") +        verbose_name = _("Container localisation") +        verbose_name_plural = _("Container localisations")          unique_together = ('container', 'division')          ordering = ('container', 'division__order')      def __str__(self): -        lbl = u" - ".join((str(self.container), +        lbl = " - ".join((str(self.container),                             str(self.division), self.reference))          return lbl | 
