From 8e52f779ee339c78b043220eb6dbd9377423c740 Mon Sep 17 00:00:00 2001 From: James Graham Date: Tue, 15 Dec 2020 14:23:51 +0000 Subject: [PATCH] feat: add location fields to person answer sets --- people/forms.py | 2 ++ .../migrations/0028_person_location_fields.py | 23 +++++++++++++++++++ people/models/person.py | 6 +++++ people/static/js/map.js | 6 +---- people/templates/people/person/detail.html | 16 +++++++++---- .../people/person/includes/answer_set.html | 7 ++++++ people/views/person.py | 10 ++++++++ 7 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 people/migrations/0028_person_location_fields.py diff --git a/people/forms.py b/people/forms.py index 80c6fad..199d6b2 100644 --- a/people/forms.py +++ b/people/forms.py @@ -78,6 +78,8 @@ class PersonAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase): 'job_title', 'disciplines', 'themes', + 'latitude', + 'longitude', ] widgets = { 'nationality': Select2Widget(), diff --git a/people/migrations/0028_person_location_fields.py b/people/migrations/0028_person_location_fields.py new file mode 100644 index 0000000..860c34a --- /dev/null +++ b/people/migrations/0028_person_location_fields.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.10 on 2020-12-15 13:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('people', '0027_multiple_choice_questions'), + ] + + operations = [ + migrations.AddField( + model_name='personanswerset', + name='latitude', + field=models.FloatField(blank=True, null=True), + ), + migrations.AddField( + model_name='personanswerset', + name='longitude', + field=models.FloatField(blank=True, null=True), + ), + ] diff --git a/people/models/person.py b/people/models/person.py index e8fff6c..3009a29 100644 --- a/people/models/person.py +++ b/people/models/person.py @@ -177,5 +177,11 @@ class PersonAnswerSet(AnswerSet): #: Project themes within this person works themes = models.ManyToManyField(Theme, related_name='people', blank=True) + #: Latitude for displaying locaiton on a map + latitude = models.FloatField(blank=True, null=True) + + #: Longitude for displaying locaiton on a map + longitude = models.FloatField(blank=True, null=True) + def get_absolute_url(self): return self.person.get_absolute_url() diff --git a/people/static/js/map.js b/people/static/js/map.js index 78eda6a..36550e9 100644 --- a/people/static/js/map.js +++ b/people/static/js/map.js @@ -10,13 +10,9 @@ const marker_label_offset = 0.27 * marker_scale; const marker_edge_alpha = 1.0; const marker_edge_width = 1.0; -const settings = { - zoom: 5 -} - // The function called when Google Maps starts up function initMap() { - const centre_latlng = new google.maps.LatLng(0, 0); + const centre_latlng = new google.maps.LatLng(settings.centre_lat, settings.centre_lng); // The map, centered at Soton const map = new google.maps.Map( document.getElementById('map'), { zoom: settings.zoom, center: centre_latlng }); diff --git a/people/templates/people/person/detail.html b/people/templates/people/person/detail.html index b2e337e..520043a 100644 --- a/people/templates/people/person/detail.html +++ b/people/templates/people/person/detail.html @@ -4,8 +4,18 @@ {% load staticfiles %} @@ -37,9 +47,7 @@ {% endif %} - {% with person.current_answers as answer_set %} - {% include 'people/person/includes/answer_set.html' %} - {% endwith %} + {% include 'people/person/includes/answer_set.html' %} Update diff --git a/people/templates/people/person/includes/answer_set.html b/people/templates/people/person/includes/answer_set.html index 8bcb346..0712879 100644 --- a/people/templates/people/person/includes/answer_set.html +++ b/people/templates/people/person/includes/answer_set.html @@ -42,6 +42,13 @@ {% endif %} + {% if answer_set.latitude and answer_set.longitude %} + + Location + {{ answer_set.latitude }}, {{ answer_set.longitude }} + + {% endif %} + {% for answer in answer_set.question_answers.all %} {{ answer.question }} diff --git a/people/views/person.py b/people/views/person.py index c495a86..ba4d4ed 100644 --- a/people/views/person.py +++ b/people/views/person.py @@ -2,6 +2,8 @@ Views for displaying or manipulating instances of :class:`Person`. """ +import typing + from django.contrib.auth.mixins import LoginRequiredMixin from django.utils import timezone from django.views.generic import CreateView, DetailView, ListView, UpdateView @@ -54,6 +56,14 @@ class ProfileView(permissions.UserIsLinkedPersonMixin, DetailView): # pk was not provided in URL return self.request.user.person + def get_context_data(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]: + """Add current :class:`PersonAnswerSet` to context.""" + context = super().get_context_data(**kwargs) + + context['answer_set'] = self.object.current_answers + + return context + class PersonUpdateView(permissions.UserIsLinkedPersonMixin, UpdateView): """View for updating a :class:`Person` record."""