summaryrefslogtreecommitdiff
path: root/app/daily/models.py
blob: a0a6d5bd51bbe20db840f2c74b15e82cca339e3c (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
from django.contrib.gis.db import models
from django.conf import settings
from django import forms
from django.utils import timezone

from locations.models import Location

from utils.widgets import markdown_to_html


class CheckIn(models.Model):
    point = models.PointField(blank=True)
    location = models.ForeignKey(Location, blank=True, null=True)
    date = models.DateField(default=timezone.now)

    class Meta:
        ordering = ('-date',)
        get_latest_by = 'date'

    def __str__(self):
        return str(self.date)

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

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

    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(CheckIn, self).save()


class Weather(models.Model):
    point = models.PointField(null=True, blank=True)
    date = models.DateField()
    temp_max = models.CharField(max_length=8)
    temp_min = models.CharField(max_length=8)
    temp_mean = models.CharField(max_length=8)
    wind_mean = models.CharField(max_length=10)
    wind_max = models.CharField(max_length=10)
    humidity = models.CharField(max_length=10)
    snow_amount = models.CharField(max_length=10)
    rain_amount = models.CharField(max_length=10)
    fog = models.NullBooleanField()
    rain = models.NullBooleanField()
    snow = models.NullBooleanField()
    hail = models.NullBooleanField()
    thunder = models.NullBooleanField()

    class Meta:
        ordering = ('-date',)
        get_latest_by = 'date'
        verbose_name_plural = "Weather"

    def __str__(self):
        return str(self.date)


class Daily(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    location = models.ForeignKey(Location, blank=True, null=True)
    weather = models.ForeignKey(Weather, blank=True, null=True)
    weather_human = models.TextField(blank=True, null=True)
    body_html = models.TextField(blank=True, null=True)
    body_markdown = models.TextField(blank=True, null=True)
    date = models.DateField()

    class Meta:
        ordering = ('date',)
        get_latest_by = 'date'

    def __str__(self):
        return str(self.date)

    @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

    @property
    def get_previous_published(self):
        return self.get_previous_by_pub_date(status__exact=1)

    @property
    def get_next_published(self):
        return self.get_next_by_pub_date(status__exact=1)

    def save(self, **kwargs):
        if self.body_markdown:
            self.body_html = markdown_to_html(self.body_markdown)
        self.location = CheckIn.objects.latest().location
        super(Daily, self).save()