summaryrefslogtreecommitdiff
path: root/app/locations/models.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2018-07-07 20:41:09 -0400
committerluxagraf <sng@luxagraf.net>2018-07-07 20:41:09 -0400
commit6a2393e6819ea09aeb559354a69746750aa8cbdf (patch)
tree0ac9890740f9afcd9720ea9b550a3895d95ecb50 /app/locations/models.py
parentfe13c3830c6b36fb8f78009a788650a992cb3070 (diff)
added campsite model, refactored some code to avoid circular imports,
reorganized some auxilary functions and fixed broken build JS.
Diffstat (limited to 'app/locations/models.py')
-rw-r--r--app/locations/models.py65
1 files changed, 64 insertions, 1 deletions
diff --git a/app/locations/models.py b/app/locations/models.py
index b4ee6f7..5efab23 100644
--- a/app/locations/models.py
+++ b/app/locations/models.py
@@ -1,5 +1,6 @@
import json
import requests
+from django import forms
from django.urls import reverse
from django.apps import apps
from django.contrib.gis.geos import GEOSGeometry, fromstr, MultiPolygon
@@ -7,6 +8,9 @@ from django.contrib.gis.db import models
from django.contrib.sitemaps import Sitemap
from django.utils.safestring import mark_safe
from django.utils import timezone
+from django.conf import settings
+
+from utils.util import render_images, extract_main_image, markdown_to_html
"http://staticmap.openstreetmap.de/staticmap.php?center=object.location.geometry.centroid.y,object.location.geometry.centroid.x&zoom=14&size=1140x300&maptype=osmarenderer&markers=40.702147,-74.015794,lightblue1"
@@ -115,7 +119,9 @@ class State(models.Model):
class Location(models.Model):
- """Model to hold location shapes as arbitrarily defined by me"""
+ """
+ Model to hold location shapes as arbitrarily defined by me
+ """
state = models.ForeignKey(State, on_delete=models.CASCADE)
name = models.CharField(max_length=50, )
slug = models.SlugField()
@@ -198,6 +204,63 @@ class CheckIn(models.Model):
super(CheckIn, self).save()
+class Campsite(models.Model):
+ name = models.CharField(max_length=200)
+ point = models.PointField(blank=True)
+ location = models.ForeignKey(Location, on_delete=models.CASCADE, blank=True, null=True)
+ date_arrived = models.DateField(default=timezone.now)
+ date_left = models.DateField(default=timezone.now, null=True, blank=True)
+ CAMPSITE_TYPE = (
+ (0, 'National Park'),
+ (1, 'National Forest'),
+ (2, 'National Other'),
+ (3, 'State Park'),
+ (4, 'State Forest'),
+ (5, 'County Park'),
+ (6, 'City Park'),
+ (7, 'Boondocking'),
+ (8, 'Other'),
+ )
+ campsite_type = models.IntegerField(choices=CAMPSITE_TYPE, default=0)
+ campsite_number = models.IntegerField(blank=True, null=True)
+ campsite_we_wish_we_had = models.IntegerField(blank=True, null=True)
+ body_markdown = models.TextField(blank=True, null=True)
+ body_html = models.TextField(blank=True, null=True)
+ image = models.ForeignKey("photos.LuxImage", on_delete=models.CASCADE, null=True, blank=True)
+
+ class Meta:
+ ordering = ('-date_arrived',)
+ get_latest_by = 'date_arrived'
+
+ def __str__(self):
+ return "%s - %s" % (self.name, self.location)
+
+ @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, *args, **kwargs):
+ created = self.pk is None
+ if not created:
+ md = render_images(self.body_markdown)
+ self.body_html = markdown_to_html(md)
+ 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))
+
+ main_image = extract_main_image(self.body_markdown)
+ if main_image:
+ self.image = main_image
+ super(Campsite, self).save(*args, **kwargs)
+
+
class WritingbyCountrySitemap(Sitemap):
changefreq = "weekly"
priority = 0.6