feat: add location fields to person answer sets

This commit is contained in:
James Graham
2020-12-15 14:23:51 +00:00
parent 82a235e6ff
commit 8e52f779ee
7 changed files with 61 additions and 9 deletions

View File

@@ -78,6 +78,8 @@ class PersonAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
'job_title', 'job_title',
'disciplines', 'disciplines',
'themes', 'themes',
'latitude',
'longitude',
] ]
widgets = { widgets = {
'nationality': Select2Widget(), 'nationality': Select2Widget(),

View File

@@ -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),
),
]

View File

@@ -177,5 +177,11 @@ class PersonAnswerSet(AnswerSet):
#: Project themes within this person works #: Project themes within this person works
themes = models.ManyToManyField(Theme, related_name='people', blank=True) 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): def get_absolute_url(self):
return self.person.get_absolute_url() return self.person.get_absolute_url()

View File

@@ -10,13 +10,9 @@ const marker_label_offset = 0.27 * marker_scale;
const marker_edge_alpha = 1.0; const marker_edge_alpha = 1.0;
const marker_edge_width = 1.0; const marker_edge_width = 1.0;
const settings = {
zoom: 5
}
// The function called when Google Maps starts up // The function called when Google Maps starts up
function initMap() { 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 // The map, centered at Soton
const map = new google.maps.Map( const map = new google.maps.Map(
document.getElementById('map'), { zoom: settings.zoom, center: centre_latlng }); document.getElementById('map'), { zoom: settings.zoom, center: centre_latlng });

View File

@@ -4,8 +4,18 @@
{% load staticfiles %} {% load staticfiles %}
<script type="application/javascript"> <script type="application/javascript">
const data = [ const data = [
{name: 'Test Person', lat: 0, lng: 0}, {
name: '{{ person.name }}',
lat: '{{ answer_set.latitude }}',
lng: '{{ answer_set.longitude }}'
},
] ]
const settings = {
zoom: 8,
centre_lat: '{{ answer_set.latitude }}',
centre_lng: '{{ answer_set.longitude }}',
}
</script> </script>
<script src="{% static 'js/map.js' %}"></script> <script src="{% static 'js/map.js' %}"></script>
@@ -37,9 +47,7 @@
{% endif %} {% endif %}
{% with person.current_answers as answer_set %}
{% include 'people/person/includes/answer_set.html' %} {% include 'people/person/includes/answer_set.html' %}
{% endwith %}
<a class="btn btn-success" <a class="btn btn-success"
href="{% url 'people:person.update' pk=person.pk %}">Update</a> href="{% url 'people:person.update' pk=person.pk %}">Update</a>

View File

@@ -42,6 +42,13 @@
</tr> </tr>
{% endif %} {% endif %}
{% if answer_set.latitude and answer_set.longitude %}
<tr>
<td>Location</td>
<td>{{ answer_set.latitude }}, {{ answer_set.longitude }}</td>
</tr>
{% endif %}
{% for answer in answer_set.question_answers.all %} {% for answer in answer_set.question_answers.all %}
<tr> <tr>
<td>{{ answer.question }}</td> <td>{{ answer.question }}</td>

View File

@@ -2,6 +2,8 @@
Views for displaying or manipulating instances of :class:`Person`. Views for displaying or manipulating instances of :class:`Person`.
""" """
import typing
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils import timezone from django.utils import timezone
from django.views.generic import CreateView, DetailView, ListView, UpdateView from django.views.generic import CreateView, DetailView, ListView, UpdateView
@@ -54,6 +56,14 @@ class ProfileView(permissions.UserIsLinkedPersonMixin, DetailView):
# pk was not provided in URL # pk was not provided in URL
return self.request.user.person 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): class PersonUpdateView(permissions.UserIsLinkedPersonMixin, UpdateView):
"""View for updating a :class:`Person` record.""" """View for updating a :class:`Person` record."""