diff options
author | luxagraf <sng@luxagraf.net> | 2018-07-07 20:41:09 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2018-07-07 20:41:09 -0400 |
commit | 6a2393e6819ea09aeb559354a69746750aa8cbdf (patch) | |
tree | 0ac9890740f9afcd9720ea9b550a3895d95ecb50 /app/locations | |
parent | fe13c3830c6b36fb8f78009a788650a992cb3070 (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')
-rw-r--r-- | app/locations/admin.py | 56 | ||||
-rw-r--r-- | app/locations/migrations/0007_campsite.py | 37 | ||||
-rw-r--r-- | app/locations/migrations/0008_campsite_name.py | 19 | ||||
-rw-r--r-- | app/locations/migrations/0009_auto_20180707_1403.py | 19 | ||||
-rw-r--r-- | app/locations/models.py | 65 |
5 files changed, 182 insertions, 14 deletions
diff --git a/app/locations/admin.py b/app/locations/admin.py index 4adb836..f970130 100644 --- a/app/locations/admin.py +++ b/app/locations/admin.py @@ -1,10 +1,14 @@ from django.contrib import admin from django.contrib.gis.admin import OSMGeoAdmin -from locations.models import Region, Country, Location, State, Route, CheckIn -from utils.widgets import OLAdminBase +from .models import Region, Country, Location, State, Route, CheckIn, Campsite + +from utils.widgets import OLAdminBase from utils.util import get_latlon +from utils.widgets import LGEntryForm + +@admin.register(Region) class RegionAdmin(OSMGeoAdmin): list_display = ('name', 'slug') prepopulated_fields = {'slug': ('name',)} @@ -45,9 +49,8 @@ class RegionAdmin(OSMGeoAdmin): map_template = 'gis/admin/osm.html' openlayers_url = '/static/admin/js/OpenLayers.js' -admin.site.register(Region, RegionAdmin) - +@admin.register(Country) class CountryAdmin(OSMGeoAdmin): list_display = ('name', 'pop2005', 'region', 'subregion') search_fields = ('name',) @@ -112,9 +115,7 @@ class CountryAdmin(OSMGeoAdmin): openlayers_url = '/static/admin/js/OpenLayers.js' -admin.site.register(Country, CountryAdmin) - - +@admin.register(State) class StateAdmin(OSMGeoAdmin): list_display = ('name', 'code', 'slug', 'country') prepopulated_fields = {'slug': ('name',)} @@ -157,9 +158,8 @@ class StateAdmin(OSMGeoAdmin): map_template = 'gis/admin/osm.html' openlayers_url = '/static/admin/js/OpenLayers.js' -admin.site.register(State, StateAdmin) - +@admin.register(Location) class LocationAdmin(OSMGeoAdmin): list_display = ('name', 'pub_date', 'parent', 'state', 'slug') prepopulated_fields = {'slug': ('name',)} @@ -198,9 +198,8 @@ class LocationAdmin(OSMGeoAdmin): class Media: js = ('next-prev-links.js',) -admin.site.register(Location, LocationAdmin) - +@admin.register(Route) class RouteAdmin(OSMGeoAdmin): list_display = ('name', 'slug') prepopulated_fields = {'slug': ('name',)} @@ -237,8 +236,6 @@ class RouteAdmin(OSMGeoAdmin): map_template = 'gis/admin/osm.html' openlayers_url = '/static/admin/js/OpenLayers.js' -admin.site.register(Route, RouteAdmin) - @admin.register(CheckIn) class CheckInAdmin(OLAdminBase): @@ -246,3 +243,36 @@ class CheckInAdmin(OLAdminBase): # options for OSM map Using custom ESRI topo map default_lat, default_lon = get_latlon() default_zoom = 9 + + +@admin.register(Campsite) +class CampsiteAdmin(OLAdminBase): + form = LGEntryForm + list_display = ('name', 'location', 'date_arrived', 'campsite_type',) + list_filter = ('date_arrived', 'campsite_type') + search_fields = ['location__name', ] + fieldsets = ( + ('Campsite', { + 'fields': ( + 'name', + ('date_arrived', 'date_left'), + 'point', + 'campsite_type', + 'campsite_number', + 'campsite_we_wish_we_had', + 'body_markdown' + ), + 'classes': ( + 'show', + 'extrapretty', + 'wide' + ) + } + ), + ) + + default_lat, default_lon = get_latlon() + default_zoom = 9 + + class Media: + js = ('image-loader.js', 'next-prev-links.js') diff --git a/app/locations/migrations/0007_campsite.py b/app/locations/migrations/0007_campsite.py new file mode 100644 index 0000000..116b5ed --- /dev/null +++ b/app/locations/migrations/0007_campsite.py @@ -0,0 +1,37 @@ +# Generated by Django 2.0.1 on 2018-07-07 09:58 + +import django.contrib.gis.db.models.fields +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('photos', '0018_auto_20161130_1218'), + ('locations', '0006_auto_20180629_1739'), + ] + + operations = [ + migrations.CreateModel( + name='Campsite', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('point', django.contrib.gis.db.models.fields.PointField(blank=True, srid=4326)), + ('date_arrived', models.DateField(default=django.utils.timezone.now)), + ('date_left', models.DateField(default=django.utils.timezone.now)), + ('campsite_type', models.IntegerField(choices=[(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')], 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(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='photos.LuxImage')), + ('location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='locations.Location')), + ], + options={ + 'ordering': ('-date_arrived',), + 'get_latest_by': 'date_arrived', + }, + ), + ] diff --git a/app/locations/migrations/0008_campsite_name.py b/app/locations/migrations/0008_campsite_name.py new file mode 100644 index 0000000..7b5c513 --- /dev/null +++ b/app/locations/migrations/0008_campsite_name.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.1 on 2018-07-07 13:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('locations', '0007_campsite'), + ] + + operations = [ + migrations.AddField( + model_name='campsite', + name='name', + field=models.CharField(default='st louis', max_length=200), + preserve_default=False, + ), + ] diff --git a/app/locations/migrations/0009_auto_20180707_1403.py b/app/locations/migrations/0009_auto_20180707_1403.py new file mode 100644 index 0000000..3561e4e --- /dev/null +++ b/app/locations/migrations/0009_auto_20180707_1403.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.1 on 2018-07-07 14:03 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('locations', '0008_campsite_name'), + ] + + operations = [ + migrations.AlterField( + model_name='campsite', + name='date_left', + field=models.DateField(blank=True, default=django.utils.timezone.now, null=True), + ), + ] 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 |