summaryrefslogtreecommitdiff
path: root/app/birds/models.py
blob: 5a14786f045a3e0b096dafd8ebea73b3cad92963 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import datetime
from django.urls import reverse
from django.template.defaultfilters import slugify
from django.contrib.gis.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from locations.models import Location
from django import forms
from django.conf import settings

from photos.models import LuxImage


def get_upload_path(self, filename):
    return "images/bird-images/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)


# from http://aba.org/checklist/codes.html
ABA_CODES = (
    (0, 'unknown'),
    (1, 'regular occurring - common'),
    (2, 'regular occurring - less common'),
    (3, 'rare'),
    (4, 'casual'),
    (5, 'accidental'),
    (6, 'Cannot be found'),
)

KIND_LIST = (
    (1, 'Bird'),
    (2, 'Mammal'),
    (3, 'Reptile'),
    (4, 'Amphibian'),
    (5, 'Plant'),
)


class APClass(models.Model):
    common_name = models.CharField(max_length=200)
    scientific_name = models.CharField(max_length=200)
    kind = models.IntegerField(choices=KIND_LIST, default=1)

    class Meta:
        verbose_name_plural = 'Animal/Plant Class'
        ordering = ["kind", "common_name"]

    def __str__(self):
        return self.common_name

class AP(models.Model):
    common_name = models.CharField(max_length=200)
    slug = models.SlugField()
    scientific_name = models.CharField(max_length=200)
    code = models.IntegerField(choices=ABA_CODES, default=0)
    apclass = models.ForeignKey(APClass, on_delete=models.CASCADE)
    image = models.FileField(upload_to=get_upload_path, null=True, blank=True, help_text="width of high res is 1360px")
    image_credit = models.CharField(max_length=200, blank=True, null=True)

    def __str__(self):
        return self.common_name

    def get_image_url(self):
        return "%s%s" % (settings.IMAGES_URL, self.image.url.split("media")[1][8:])

    def get_absolute_url(self):
        return reverse("sightings:detail", kwargs={"slug": self.slug})

    def kind(self):
        return self.apclass.kind

    class Meta:
        verbose_name_plural = 'Animal/Plant'
        verbose_name = 'Animal/Plant'
        ordering = ["common_name", ]

    def save(self, *args, **kwargs):
        self.slug = slugify(self.common_name[:50])
        super(AP, self).save(*args, **kwargs)


class Sighting(models.Model):
    ap = models.ForeignKey(AP, on_delete=models.CASCADE)
    point = models.PointField(blank=True)
    location = models.ForeignKey(Location, on_delete=models.CASCADE, blank=True, related_name="location_old")
    date = models.DateTimeField('Date', default=timezone.now)
    seen_by = models.ManyToManyField(User, related_name="seenby_old")
    images = models.ManyToManyField(LuxImage, blank=True, related_name="images_old")
    #audio = models.ManyToManyField(BirdAudio, blank=True)

    class Meta:
        ordering = ["-date", ]

    @property
    def state(self):
        return self.location.state

    @property
    def country(self):
        return self.location.state.country

    @property
    def region(self):
        return self.location.state.country.lux_region

    @property
    def longitude(self):
        '''Get the site's longitude.'''
        return self.point.x

    @property
    def latitude(self):
        '''Get the site's latitude.'''
        return self.point.y

    def get_small_image(self):
        for img in self.images.all():
            for size in img.sizes.all():
                if size.width > 360 and size.width < 700:
                    return img.get_image_by_size(size)

    def get_absolute_url(self):
        return reverse("birds:detail", kwargs={"slug": self.bird.slug})

    def __str__(self):
        return self.ap.common_name

    def save(self):
        if not self.point:
            self.point = Sighting.objects.latest().point
        try:
            self.location = Location.objects.filter(
                geometry__contains=self.point
            ).get()
        except Location.DoesNotExist:
            raise forms.ValidationError("There is no location associated with that point, add it:  %sadmin/locations/location/add/" % (settings.BASE_URL))
        super(Sighting, self).save()

"""
Migration from Birds to abstract:
birdclass = BirdClass.objects.all()
for b in birdclass:
    APClass.objects.create(
        common_name = b.common_name,
        scientific_name = b.scientific_name,
        kind = 1
    )
 
birds = Bird.objects.all()
for b in birds:
    ap = APClass.objects.get(scientific_name=b.bird_class.scientific_name)
    print(ap)
    AP.objects.create(
        common_name = b.common_name,
        scientific_name = b.scientific_name,
        code = b.code,
        apclass = ap,
        image = b.image,
        image_credit = b.image_credit,
    )
    print(t)

birdsighting = BirdSighting.objects.all()
for bird in birdsighting:
    ap = AP.objects.get(scientific_name=bird.bird.scientific_name)
    s = Sighting.objects.create(
        ap = ap,
        point = bird.point,
        location = bird.location,
        date = bird.date,
    )
    for t in bird.images.all():
        s.images.add(t)
    for t in bird.seen_by.all():
        s.seen_by.add(t)
"""


class BirdClass(models.Model):
    common_name = models.CharField(max_length=200)
    scientific_name = models.CharField(max_length=200)

    class Meta:
        verbose_name_plural = 'Bird Class'
        ordering = ["common_name", ]

    def __str__(self):
        return self.common_name


class Bird(models.Model):
    common_name = models.CharField(max_length=200)
    slug = models.SlugField()
    scientific_name = models.CharField(max_length=200)
    code = models.IntegerField(choices=ABA_CODES, default=0)
    bird_class = models.ForeignKey(BirdClass, on_delete=models.CASCADE)
    image = models.FileField(upload_to=get_upload_path, null=True, blank=True, help_text="width of high res is 1360px")
    image_credit = models.CharField(max_length=200, blank=True, null=True)

    def __str__(self):
        return self.common_name

    # function to resize large image to 680px wide and use as normal image
    # the question is, should this happen here, or with some universale image 
    # model that can be attached to other models, loaded in entries and 
    # displayed in galleries. I suppose the answer is yes then isn't it?
    # the problem is that I still can't see exactly what that looks like...

    def get_image_url(self):
        return "%s%s" % (settings.IMAGES_URL, self.image.url.split("media")[1][8:])

    def get_absolute_url(self):
        return reverse("birds:detail", kwargs={"slug": self.slug})

    class Meta:
        ordering = ["common_name", ]

    def save(self):
        self.slug = slugify(self.common_name[:50])
        super(Bird, self).save()


class BirdAudio(models.Model):
    bird = models.ForeignKey(Bird, on_delete=models.CASCADE, related_name='recordings')
    audio = models.FileField(upload_to='audio/birds/')
    recorder = models.CharField(max_length=200, null=True, blank=True)
    pub_date = models.DateTimeField()
    location = models.CharField(max_length=200, null=True, blank=True)
    link = models.CharField(max_length=450, null=True, blank=True)
    copyright = models.CharField(max_length=250, null=True, blank=True)

    class Meta:
        verbose_name_plural = 'Bird Audio'
        ordering = ["bird", ]

    def __str__(self):
        return self.bird.common_name


class BirdSighting(models.Model):
    bird = models.ForeignKey(Bird, on_delete=models.CASCADE)
    point = models.PointField(blank=True)
    location = models.ForeignKey(Location, on_delete=models.CASCADE, blank=True)
    date = models.DateTimeField('Date', default=timezone.now)
    seen_by = models.ManyToManyField(User)
    images = models.ManyToManyField(LuxImage, blank=True)
    audio = models.ManyToManyField(BirdAudio, blank=True)

    class Meta:
        verbose_name_plural = 'Bird Sighting'
        get_latest_by = 'date'

    @property
    def state(self):
        return self.location.state

    @property
    def country(self):
        return self.location.state.country

    @property
    def region(self):
        return self.location.state.country.lux_region

    @property
    def longitude(self):
        '''Get the site's longitude.'''
        return self.point.x

    @property
    def latitude(self):
        '''Get the site's latitude.'''
        return self.point.y

    def get_small_image(self):
        for img in self.images.all():
            for size in img.sizes.all():
                if size.width > 360 and size.width < 700:
                    return img.get_image_by_size(size)

    def get_absolute_url(self):
        return reverse("birds:detail", kwargs={"slug": self.bird.slug})

    def __str__(self):
        return self.bird.common_name

    def save(self):
        if not self.point:
            self.point = BirdSighting.objects.latest().point
        try:
            self.location = Location.objects.filter(
                geometry__contains=self.point
            ).get()
        except Location.DoesNotExist:
            raise forms.ValidationError("There is no location associated with that point, add it:  %sadmin/locations/location/add/" % (settings.BASE_URL))
        super(BirdSighting, self).save()