summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/models.py
blob: 1ab28504edc6344ec78085297a4204427602c5b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

import datetime

from django.conf import settings
from django.contrib.gis.db import models
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 ishtar_common.utils import cached_label_changed

from ishtar_common.models import GeneralType, \
    LightHistorizedItem, OwnPerms, Address, Person, post_save_cache


class WarehouseType(GeneralType):
    class Meta:
        verbose_name = _(u"Warehouse type")
        verbose_name_plural = _(u"Warehouse types")
        ordering = ('label',)
post_save.connect(post_save_cache, sender=WarehouseType)
post_delete.connect(post_save_cache, sender=WarehouseType)


class Warehouse(Address, OwnPerms):
    SHOW_URL = 'show-warehouse'
    name = models.CharField(_(u"Name"), max_length=200)
    warehouse_type = models.ForeignKey(WarehouseType,
                                       verbose_name=_(u"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)
    comment = models.TextField(_(u"Comment"), null=True, blank=True)
    associated_divisions = models.ManyToManyField(
        'WarehouseDivision', verbose_name=_("Divisions"), blank=True,
        through='WarehouseDivisionLink'
    )

    TABLE_COLS = ['name', 'warehouse_type']

    class Meta:
        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")),
        )

    def __unicode__(self):
        return u"%s (%s)" % (self.name, unicode(self.warehouse_type))

    @property
    def associated_filename(self):
        return datetime.date.today().strftime('%Y-%m-%d') + '-' + \
               slugify(unicode(self))

    def save(self, *args, **kwargs):
        super(Warehouse, self).save(*args, **kwargs)
        for container in self.containers.all():
            cached_label_changed(Container, instance=container)


class Collection(LightHistorizedItem):
    name = models.CharField(_(u"Name"), max_length=200,
                            null=True, blank=True)
    description = models.TextField(_(u"Description"), null=True, blank=True)
    warehouse = models.ForeignKey(Warehouse, verbose_name=_(u"Warehouse"),
                                  related_name='collections')

    class Meta:
        verbose_name = _(u"Collection")
        verbose_name_plural = _(u"Collection")
        ordering = ('name',)

    def __unicode__(self):
        return self.name


class WarehouseDivision(GeneralType):
    class Meta:
        verbose_name = _(u"Warehouse division")
        verbose_name_plural = _(u"Warehouse divisions")
post_save.connect(post_save_cache, sender=WarehouseDivision)
post_delete.connect(post_save_cache, sender=WarehouseDivision)


class WarehouseDivisionLink(models.Model):
    RELATIVE_MODELS = {Warehouse: 'warehouse'}
    warehouse = models.ForeignKey(Warehouse)
    division = models.ForeignKey(WarehouseDivision)
    order = models.IntegerField(_("Order"), default=10)

    class Meta:
        ordering = ('warehouse', 'order')
        unique_together = ('warehouse', 'division')

    def __unicode__(self):
        return u"{} - {}".format(self.warehouse, self.division)


class ContainerType(GeneralType):
    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.IntegerField(_(u"Volume (l)"), blank=True, null=True)
    reference = models.CharField(_(u"Ref."), max_length=30)

    class Meta:
        verbose_name = _(u"Container type")
        verbose_name_plural = _(u"Container types")
        ordering = ('label',)
post_save.connect(post_save_cache, sender=ContainerType)
post_delete.connect(post_save_cache, sender=ContainerType)


class Container(LightHistorizedItem):
    TABLE_COLS = ['reference', 'container_type__label', 'cached_location',
                  'divisions_lbl']

    # search parameters
    EXTRA_REQUEST_KEYS = {
        'location': 'location__pk',
        'container_type': 'container_type__pk',
        'reference': 'reference__icontains',
    }
    SHOW_URL = 'show-container'
    COL_LABELS = {
        'cached_location': _(u"Location - index"),
        'divisions_lbl': _(u"Precise localisation"),
    }
    CACHED_LABELS = ['cached_label', 'cached_location']

    # fields
    location = models.ForeignKey(Warehouse, verbose_name=_(u"Warehouse"),
                                 related_name='containers')
    container_type = models.ForeignKey(ContainerType,
                                       verbose_name=_("Container type"))
    reference = models.CharField(_(u"Container ref."), max_length=40)
    comment = models.TextField(_(u"Comment"), null=True, blank=True)
    cached_label = models.CharField(_(u"Cached name"), max_length=500,
                                    null=True, blank=True)
    cached_location = models.CharField(_(u"Cached location"), max_length=500,
                                       null=True, blank=True)
    index = models.IntegerField(u"Index", default=0)

    class Meta:
        verbose_name = _(u"Container")
        verbose_name_plural = _(u"Containers")
        ordering = ('cached_label',)
        unique_together = ('index', 'location')

    def __unicode__(self):
        lbl = u"{} ({})".format(self.reference, self.container_type)
        return lbl

    def _generate_cached_label(self):
        items = [self.reference, self.precise_location]
        cached_label = u" | ".join(items)
        return cached_label

    def _generate_cached_location(self):
        items = [self.location.name, unicode(self.index)]
        cached_label = u" - ".join(items)
        return cached_label

    @property
    def associated_filename(self):
        filename = datetime.date.today().strftime('%Y-%m-%d')
        filename += u'-' + self.reference
        filename += u"-" + self.location.name
        filename += u"-" + unicode(self.index)
        filename += u"-" + self.divisions_lbl
        return slugify(filename)

    @property
    def precise_location(self):
        return self.location.name + u" - " + self.divisions_lbl

    @property
    def divisions_lbl(self):
        locas = [
            u"{} {}".format(loca.division.division, loca.reference)
            for loca in ContainerLocalisation.objects.filter(
                container=self)
            ]
        return u" | ".join(locas)

    def save(self, *args, **kwargs):
        super(Container, self).save(*args, **kwargs)

        if not self.index:
            self.skip_history_when_saving = True
            q = Container.objects.filter(location=self.location).order_by(
                '-index')
            if q.count():
                self.index = q.all()[0].index + 1
            else:
                self.index = 1
            self._cached_label_checked = False
            self.save()

post_save.connect(cached_label_changed, sender=Container)


class ContainerLocalisation(models.Model):
    container = models.ForeignKey(Container, verbose_name=_(u"Container"))
    division = models.ForeignKey(WarehouseDivisionLink,
                                 verbose_name=_(u"Division"))
    reference = models.CharField(_(u"Reference"), max_length=200, default='')

    class Meta:
        verbose_name = _(u"Container localisation")
        verbose_name_plural = _(u"Container localisations")
        unique_together = ('container', 'division')
        ordering = ('container', 'division__order')

    def __unicode__(self):
        lbl = u" - ".join((unicode(self.container),
                           unicode(self.division), self.reference))
        return lbl

    def save(self, *args, **kwargs):
        super(ContainerLocalisation, self).save(*args, **kwargs)
        cached_label_changed(Container, instance=self.container)