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()