summaryrefslogtreecommitdiff
path: root/archaeological_context_records/models.py
blob: c47fd3354f9f5e57f2cbe431369dceb7d1f2fde5 (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
#!/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.

from django.conf import settings
from django.contrib.gis.db import models
from django.utils.translation import ugettext_lazy as _, ugettext

from ishtar_common.models import GeneralType, BaseHistorizedItem, \
          LightHistorizedItem, HistoricalRecords, OwnPerms, Town, Person, Source
FILES_AVAILABLE = 'archaeological_files' in settings.INSTALLED_APPS
if FILES_AVAILABLE:
    from archaeological_files.models import File
from archaeological_operations.models import Operation, Period

class Parcel(LightHistorizedItem):
    if FILES_AVAILABLE:
        associated_file = models.ForeignKey(File, related_name='parcels',
                                blank=True, null=True, verbose_name=_(u"File"))
    operation = models.ForeignKey(Operation, related_name='parcels', blank=True,
                                  null=True, verbose_name=_(u"Operation"))
    year = models.IntegerField(_(u"Year"), blank=True, null=True)
    town = models.ForeignKey(Town, related_name='parcels',
                             verbose_name=_(u"Town"))
    section = models.CharField(_(u"Section"), max_length=4)
    parcel_number = models.CharField(_(u"Parcel number"), max_length=6)

    class Meta:
        verbose_name = _(u"Parcel")
        verbose_name_plural = _(u"Parcels")

    def short_label(self):
        return JOINT.join([unicode(item) for item in [self.section,
                                                   self.parcel_number] if item])

    def __unicode__(self):
        return self.short_label()

    def long_label(self):
        items = [unicode(self.operation or self.associated_file)]
        items += [unicode(item) for item in [self.section, self.parcel_number]
                                          if item]
        return JOINT.join(items)

class ParcelOwner(LightHistorizedItem):
    owner = models.ForeignKey(Person, verbose_name=_(u"Owner"))
    parcel = models.ForeignKey(Parcel, verbose_name=_(u"Parcel"))
    start_date = models.DateField(_(u"Start date"))
    end_date = models.DateField(_(u"End date"))

    class Meta:
        verbose_name = _(u"Parcel owner")
        verbose_name_plural = _(u"Parcel owners")

    def __unicode__(self):
        return self.owner + JOINT + self.parcel

class DatingType(GeneralType):
    class Meta:
        verbose_name = _(u"Dating type")
        verbose_name_plural = _(u"Dating types")

class DatingQuality(GeneralType):
    class Meta:
        verbose_name = _(u"Dating quality")
        verbose_name_plural = _(u"Dating qualities")

class Dating(models.Model):
    period = models.ForeignKey(Period, verbose_name=_(u"Period"))
    start_date = models.IntegerField(_(u"Start date"), blank=True, null=True)
    end_date = models.IntegerField(_(u"End date"), blank=True, null=True)
    dating_type = models.ForeignKey(DatingType, verbose_name=_(u"Dating type"),
                                    blank=True, null=True)
    quality = models.ForeignKey(DatingQuality, verbose_name=_(u"Quality"),
                                blank=True, null=True)

    class Meta:
        verbose_name = _(u"Dating")
        verbose_name_plural = _(u"Datings")

    def __unicode__(self):
        start_date = self.start_date and unicode(self.start_date) or u""
        end_date = self.end_date and unicode(self.end_date) or u""
        if not start_date and not end_date:
            return unicode(self.period)
        return u"%s (%s-%s)" % (self.period, start_date, end_date)

class Unit(GeneralType):
    order = models.IntegerField(_(u"Order"))
    parent = models.ForeignKey("Unit", verbose_name=_(u"Parent unit"),
                               blank=True, null=True)

    class Meta:
        verbose_name = _(u"Type Unit")
        verbose_name_plural = _(u"Types Unit")

    def __unicode__(self):
        return self.label

class ActivityType(GeneralType):
    order = models.IntegerField(_(u"Order"))

    class Meta:
        verbose_name = _(u"Type Activity")
        verbose_name_plural = _(u"Types Activity")

    def __unicode__(self):
        return self.label

class IdentificationType(GeneralType):
    order = models.IntegerField(_(u"Order"))
    class Meta:
        verbose_name = _(u"Type Identification")
        verbose_name_plural = _(u"Types Identification")

    def __unicode__(self):
        return self.label

class ContextRecord(BaseHistorizedItem, OwnPerms):
    TABLE_COLS = ['parcel.town', 'operation.year',
                  'operation.operation_code',
                  'label', 'unit']
    if settings.COUNTRY == 'fr':
        TABLE_COLS.insert(1, 'parcel.operation.code_patriarche')
    parcel = models.ForeignKey(Parcel, verbose_name=_(u"Parcel"),
                               related_name='context_record')
    operation = models.ForeignKey(Operation, verbose_name=_(u"Operation"),
                                  related_name='context_record')
    label = models.CharField(_(u"ID"), max_length=200)
    description = models.TextField(_(u"Description"), blank=True, null=True)
    length = models.IntegerField(_(u"Length (cm)"), blank=True, null=True)
    width = models.IntegerField(_(u"Width (cm)"), blank=True, null=True)
    thickness = models.IntegerField(_(u"Thickness (cm)"), blank=True, null=True)
    depth = models.IntegerField(_(u"Depth (cm)"), blank=True, null=True)
    location = models.CharField(_(u"Location"), blank=True, null=True,
     max_length=200,
     help_text=_(u"A short description of the location of the context record"))
    datings = models.ManyToManyField(Dating)
    unit = models.ForeignKey(Unit, verbose_name=_(u"Unit"), related_name='+',
                             blank=True, null=True)
    has_furniture = models.NullBooleanField(u"Has furniture?", blank=True,
                                            null=True)
    filling = models.TextField(_(u"Filling"), blank=True, null=True)
    interpretation = models.TextField(_(u"Interpretation"), blank=True,
                                      null=True)
    taq = models.IntegerField(_(u"TAQ"), blank=True, null=True,
     help_text=_(u"\"Terminus Ante Quem\" the context record can't have been "
                 "created after this date"))
    taq_estimated = models.IntegerField(_(u"Estimated TAQ"), blank=True,
     null=True, help_text=_(u"Estimation of a \"Terminus Ante Quem\""))
    tpq = models.IntegerField(_(u"TPQ"), blank=True, null=True,
     help_text=_(u"\"Terminus Post Quem\" the context record can't have been "
                 " created before this date"))
    tpq_estimated = models.IntegerField(_(u"Estimated TPQ"), blank=True,
     null=True, help_text=_(u"Estimation of a \"Terminus Post Quem\""))
    identification = models.ForeignKey(IdentificationType, blank=True,
                            null=True, verbose_name=_(u"Identification"),)
    activity = models.ForeignKey(ActivityType,blank=True, null=True,
                                 verbose_name=_(u"Activity"),)
    history = HistoricalRecords()

    class Meta:
        verbose_name = _(u"Context Record")
        verbose_name_plural = _(u"Context Record")
        permissions = (
 ("view_own_contextrecord", ugettext(u"Can view own Context Record")),
 ("add_own_contextrecord", ugettext(u"Can add own Context Record")),
 ("change_own_contextrecord", ugettext(u"Can change own Context Record")),
 ("delete_own_contextrecord", ugettext(u"Can delete own Context Record")),
        )

    def __unicode__(self):
        return self.short_label()

    def short_label(self):
        return JOINT.join([unicode(item) for item in [self.parcel,
                                                   self.label] if item])

    def full_label(self):
        if not self.parcel.operation:
            return unicode(self)
        return self._real_label() or self._temp_label()

    def _real_label(self):
        if not self.parcel.operation.code_patriarche:
            return
        return JOINT.join((self.parcel.operation.code_patriarche,
                           self.label))

    def _temp_label(self):
        if self.parcel.operation.code_patriarche:
            return
        return JOINT.join([unicode(lbl) for lbl in [self.parcel.operation.year,
                                           self.parcel.operation.operation_code,
                                           self.label] if lbl])

    @classmethod
    def get_years(cls):
        years = set()
        for res in list(cls.objects.values('operation__start_date')):
            yr = res['operation__start_date'].year
            years.add(yr)
        return list(years)

    @classmethod
    def get_by_year(cls, year):
        return cls.objects.filter(operation__start_date__year=year)

    @classmethod
    def get_operations(cls):
        return [dct['operation__pk']
                for dct in cls.objects.values('operation__pk').distinct()]

    @classmethod
    def get_by_operation(cls, operation_id):
        return cls.objects.filter(operation__pk=operation_id)

    @classmethod
    def get_total_number(cls):
        return cls.objects.filter(operation__start_date__isnull=False).count()

class ContextRecordSource(Source):
    class Meta:
        verbose_name = _(u"Context record documentation")
        verbose_name_plural = _(u"Context record documentations")
    context_record = models.ForeignKey(ContextRecord,
                       verbose_name=_(u"Context record"), related_name="source")