Add coordinates autocomplete widget & data (#32)

Just tweaks based on what Carl had already written in dce53630 and b97d421a
This commit is contained in:
Anna Sidwell 2018-04-16 14:27:13 +10:00
parent 31a9ca316f
commit 677f6a85a9
9 changed files with 125 additions and 24 deletions

View File

@ -1,14 +1,30 @@
from django.contrib import admin from django.contrib import admin
from django import forms
from dal import autocomplete
from leaflet.admin import LeafletGeoAdmin from leaflet.admin import LeafletGeoAdmin
from .models import CaseStudy, CaseStudyDraft from .models import CaseStudy, CaseStudyDraft, SpatialRefSys
class CaseStudyDraftAdmin(admin.ModelAdmin): class CaseStudyDraftAdmin(admin.ModelAdmin):
model = CaseStudyDraft model = CaseStudyDraft
class CaseStudyAdminForm(forms.ModelForm):
class Meta:
model = CaseStudy
widgets = {
'coordinate_reference_system': autocomplete.ModelSelect2(
url='srs-autocomplete'
)
}
fields = '__all__'
class CaseStudyAdmin(LeafletGeoAdmin): class CaseStudyAdmin(LeafletGeoAdmin):
list_display = ('id', 'date_created', 'entry_name', 'approved') list_display = ('id', 'date_created', 'entry_name', 'approved')
actions = ['approve', 'unapprove'] actions = ['approve', 'unapprove']
form = CaseStudyAdminForm
def approve(self, request, queryset): def approve(self, request, queryset):
updated = queryset.update(approved=True) updated = queryset.update(approved=True)
@ -33,5 +49,5 @@ class CaseStudyAdmin(LeafletGeoAdmin):
unapprove.short_description = "Un-approve selected case studies" unapprove.short_description = "Un-approve selected case studies"
admin.site.register(CaseStudy, CaseStudyAdmin) admin.site.register(CaseStudy, CaseStudyAdmin)
admin.site.register(SpatialRefSys)
admin.site.register(CaseStudyDraft, CaseStudyDraftAdmin) admin.site.register(CaseStudyDraft, CaseStudyDraftAdmin)

View File

@ -2,12 +2,14 @@ from django.urls import reverse
from django import forms from django import forms
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, HTML, Fieldset, Div from crispy_forms.layout import Submit, Layout, HTML, Fieldset, Div
from crispy_forms.bootstrap import Tab, TabHolder, PrependedText, FormActions from crispy_forms.bootstrap import Tab, TabHolder, PrependedText, FormActions
from dal import autocomplete
from leaflet.forms.widgets import LeafletWidget from leaflet.forms.widgets import LeafletWidget
from .models import CaseStudy from .models import CaseStudy, SpatialRefSys
class MinimumZoomWidget(LeafletWidget): class MinimumZoomWidget(LeafletWidget):
@ -80,6 +82,12 @@ class ShortCaseStudyForm(BaseCaseStudyForm):
class LongCaseStudyForm(BaseCaseStudyForm): class LongCaseStudyForm(BaseCaseStudyForm):
"""Long version of the CaseStudy form.""" """Long version of the CaseStudy form."""
coordinate_reference_system = forms.ModelChoiceField(
queryset=SpatialRefSys.objects.all(),
widget=autocomplete.ModelSelect2(url='srs-autocomplete'),
initial=4326,
)
POSITIVE_CASE_TYPE_HELP = { POSITIVE_CASE_TYPE_HELP = {
'CREP': _("We are using the World Wind Energy Association's Community Power definition, \ 'CREP': _("We are using the World Wind Energy Association's Community Power definition, \
which is that a community project is one where at least \ which is that a community project is one where at least \

View File

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-04-16 03:55
from __future__ import unicode_literals
from django.conf import settings
import django.contrib.gis.db.models.fields
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('gis', '__first__'),
('map', '0053_casestudydraft'),
]
operations = [
migrations.CreateModel(
name='SpatialRefSys',
fields=[
],
options={
'verbose_name': 'spatial reference system',
'proxy': True,
'indexes': [],
},
bases=('gis.postgisspatialrefsys',),
),
migrations.AlterField(
model_name='casestudy',
name='coordinate_reference_system',
field=models.ForeignKey(blank=True, default=4326, null=True, on_delete=django.db.models.deletion.CASCADE, to='map.SpatialRefSys'),
),
migrations.AlterField(
model_name='casestudy',
name='location',
field=django.contrib.gis.db.models.fields.PointField(srid=4326, verbose_name='Project location'),
),
migrations.AlterField(
model_name='casestudydraft',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]

View File

@ -1,11 +1,14 @@
import datetime import datetime
from urllib import parse from urllib import parse
from django.contrib.gis.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.gis.db import models
from django.db import connection
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext as _
from django_extensions.db.fields import AutoSlugField from django_extensions.db.fields import AutoSlugField
from django_countries.fields import CountryField from django_countries.fields import CountryField
from django.utils.translation import ugettext as _
from django.template.defaultfilters import slugify
from multiselectfield import MultiSelectField from multiselectfield import MultiSelectField
from phonenumber_field.modelfields import PhoneNumberField from phonenumber_field.modelfields import PhoneNumberField
@ -21,6 +24,18 @@ class CaseStudyDraft(models.Model):
data = models.TextField() data = models.TextField()
class SpatialRefSys(connection.ops.spatial_ref_sys()):
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return '{0.auth_name}:{0.auth_srid} {0.name}'.format(self)
class Meta:
proxy = True
verbose_name = "spatial reference system"
class Shapefile(models.Model): class Shapefile(models.Model):
file = models.FileField( file = models.FileField(
upload_to='shapefiles/', upload_to='shapefiles/',
@ -1021,13 +1036,11 @@ class CaseStudy(models.Model):
) )
# 4.3.2 # 4.3.2
coordinate_reference_system = models.CharField( coordinate_reference_system = models.ForeignKey(
verbose_name=_("Coordinate reference system"), SpatialRefSys,
help_text=_("Enter the coordinate reference system of the shapefiles."),
max_length=12,
default=None,
null=True, null=True,
blank=True blank=True,
default=4326
) )
# 4.3.3 # 4.3.3

View File

@ -1,9 +1,5 @@
{% extends "base_page.html" %} {% extends "base_page.html" %}
{% load compress %} {% load compress crispy_forms_tags i18n leaflet_tags static %}
{% load crispy_forms_tags %}
{% load i18n %}
{% load leaflet_tags %}
{% load static %}
{% block stylesheets %} {% block stylesheets %}
{{ block.super }} {{ block.super }}

View File

@ -8,12 +8,15 @@ from . import views
urlpatterns = [ urlpatterns = [
url(r'^$', RedirectView.as_view(url=reverse_lazy('map')), name='index'), url(r'^$', RedirectView.as_view(url=reverse_lazy('map')), name='index'),
url(r'^data.geojson$', GeoJSONLayerView.as_view(model=CaseStudy, geometry_field='location'), name='data'),
url(r'^case-study/create/?$', views.Create.as_view(), name="create"), url(r'^case-study/create/?$', views.Create.as_view(), name="create"),
url(r'^case-study/create/short/?$', views.ShortForm.as_view(), name='short-form'), url(r'^case-study/create/short/?$', views.ShortForm.as_view(), name='short-form'),
url(r'^case-study/create/long/?$', views.LongForm.as_view(), name='long-form'), url(r'^case-study/create/long/?$', views.LongForm.as_view(), name='long-form'),
url(r'^case-study/create/success/?$', views.FormSuccess.as_view(), name='form-success'), url(r'^case-study/create/success/?$', views.FormSuccess.as_view(), name='form-success'),
url(r'^case-study/draft/?$', views.Drafts.as_view(), name='drafts'), url(r'^case-study/draft/?$', views.Drafts.as_view(), name='drafts'),
url(r'^case-study/(?P<slug>[-\w]+)/?$', views.CaseStudyDetail.as_view(), name='detail'), url(r'^case-study/(?P<slug>[-\w]+)/?$', views.CaseStudyDetail.as_view(), name='detail'),
url(r'^map/?$', views.Map.as_view(), name='map') url(r'^map/?$', views.Map.as_view(), name='map'),
# API
url(r'^data.geojson$', GeoJSONLayerView.as_view(model=CaseStudy, geometry_field='location'), name='data'),
url(r'^srs-autocomplete/$', views.SpatialRefSysAutocomplete.as_view(), name='srs-autocomplete'),
] ]

View File

@ -1,13 +1,17 @@
from django.core.mail import send_mail
from django.conf import settings from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.mail import send_mail
from django.db.models import Q
from django.http import Http404, HttpResponse
from django.urls import reverse
from django.views import View from django.views import View
from django.views.generic import DetailView from django.views.generic import DetailView
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView
from django.http import Http404, HttpResponse
from django.contrib.auth.mixins import LoginRequiredMixin from dal import autocomplete
from django.urls import reverse
from .models import CaseStudy, CaseStudyDraft from .models import CaseStudy, CaseStudyDraft, SpatialRefSys
from .forms import ShortCaseStudyForm, LongCaseStudyForm from .forms import ShortCaseStudyForm, LongCaseStudyForm
@ -83,6 +87,19 @@ class CaseStudyDetail(DetailView):
context_object_name = "case_study" context_object_name = "case_study"
class SpatialRefSysAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = SpatialRefSys.objects.all()
if self.q:
qs = qs.filter(
Q(auth_name__icontains=self.q)
| Q(auth_srid__icontains=self.q)
)
return qs
class Drafts(View): class Drafts(View):
"""Retrieve or save a draft.""" """Retrieve or save a draft."""

View File

@ -47,6 +47,8 @@ INSTALLED_APPS = [
'cas_server', 'cas_server',
'compressor', 'compressor',
'crispy_forms', 'crispy_forms',
'dal',
'dal_select2',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',

View File

@ -3,6 +3,7 @@ brotlipy==0.7.0
boto==2.48.0 boto==2.48.0
boto3==1.4.7 boto3==1.4.7
Django==1.11.6 Django==1.11.6
django-autocomplete-light==3.2.10
django-appconf==1.0.2 django-appconf==1.0.2
django-anymail==2.0 django-anymail==2.0
django-avatar==4.0.1 django-avatar==4.0.1