diff options
Diffstat (limited to 'archaeological_warehouse/models.py')
-rw-r--r-- | archaeological_warehouse/models.py | 63 |
1 files changed, 50 insertions, 13 deletions
diff --git a/archaeological_warehouse/models.py b/archaeological_warehouse/models.py index 96814339c..1804d70d6 100644 --- a/archaeological_warehouse/models.py +++ b/archaeological_warehouse/models.py @@ -21,10 +21,10 @@ import datetime from django.conf import settings from django.contrib.gis.db import models -from django.db.models import Q, Count +from django.db.models import Q from django.db.models.signals import post_save, post_delete from django.template.defaultfilters import slugify -from django.utils.translation import ugettext_lazy as _, ugettext +from django.utils.translation import ugettext_lazy as _ from ishtar_common.utils import cached_label_changed @@ -65,11 +65,11 @@ class Warehouse(Address, DashboardFormItem, OwnPerms): verbose_name = _(u"Warehouse") verbose_name_plural = _(u"Warehouses") permissions = ( - ("view_warehouse", ugettext(u"Can view all Warehouses")), - ("view_own_warehouse", ugettext(u"Can view own Warehouse")), - ("add_own_warehouse", ugettext(u"Can add own Warehouse")), - ("change_own_warehouse", ugettext(u"Can change own Warehouse")), - ("delete_own_warehouse", ugettext(u"Can delete own Warehouse")), + ("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"), ) def __unicode__(self): @@ -81,8 +81,8 @@ class Warehouse(Address, DashboardFormItem, OwnPerms): slugify(unicode(self)) @classmethod - def get_query_owns(cls, user): - return Q(person_in_charge__ishtaruser=user.ishtaruser) + def get_query_owns(cls, ishtaruser): + return Q(person_in_charge__ishtaruser=ishtaruser) @property def number_of_finds(self): @@ -310,10 +310,10 @@ class Container(LightHistorizedItem, ImageModel): return cached_label @classmethod - def get_query_owns(cls, user): - return Q(history_creator=user) | \ - Q(location__person_in_charge__ishtaruser=user.ishtaruser) | \ - Q(responsible__person_in_charge__ishtaruser=user.ishtaruser) + def get_query_owns(cls, ishtaruser): + return Q(history_creator=ishtaruser.user_ptr) | \ + Q(location__person_in_charge__ishtaruser=ishtaruser) | \ + Q(responsible__person_in_charge__ishtaruser=ishtaruser) @property def associated_filename(self): @@ -328,6 +328,43 @@ class Container(LightHistorizedItem, ImageModel): def precise_location(self): return self.location.name + u" - " + self.divisions_lbl + def get_localisations(self): + """ + Get precise localisation of the container in the warehouse. + + :return: tuple of strings with localisations + """ + return tuple(( + loca.reference + for loca in ContainerLocalisation.objects.filter( + container=self).order_by('division__order') + )) + + def set_localisation(self, place, value): + """ + Set the reference for the localisation number "place" (starting from 0) + :param place: the number of the localisation + :param value: the reference to be set + :return: the container location object or None if the place does not + exist + """ + q = WarehouseDivisionLink.objects.filter( + warehouse=self.location).order_by('order') + for idx, division_link in enumerate(q.all()): + if idx == place: + break + else: + return + dct = {'container': self, 'division': division_link} + if not value: + if ContainerLocalisation.objects.filter(**dct).count(): + c = ContainerLocalisation.objects.filter(**dct).all()[0] + c.delete() + return + dct['defaults'] = {'reference': value} + obj, created = ContainerLocalisation.objects.update_or_create(**dct) + return obj + @property def divisions_lbl(self): locas = [ |