summaryrefslogtreecommitdiff
path: root/app/birds/models.py
blob: dc2aa19b597da71dbd45bd6f9f43b7c09e6261cc (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
import datetime
from django.core.urlresolvers 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


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 = (
    (1, 'regular occurring - common'),
    (2, 'regular occurring - less common'),
    (3, 'rare'),
    (4, 'casual'),
    (5, 'accidental'),
    (6, 'Cannot be found'),
)


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)
    image = models.FileField(upload_to=get_upload_path, null=True, blank=True)

    def __str__(self):
        return self.common_name

    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, 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)
    point = models.PointField()
    location = models.ForeignKey(Location, blank=True)
    date = models.DateTimeField('Date', default=timezone.now)
    image = models.FileField(upload_to=get_upload_path, null=True, blank=True)
    seen_by = models.ManyToManyField(User)

    class Meta:
        verbose_name_plural = 'Bird Sighting'

    @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 __str__(self):
        return self.bird.common_name

    def save(self):
        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()