From 218abab16f98be79a5c4f16c6011cd08424ad635 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 3 Mar 2019 18:54:16 +0100 Subject: [PATCH] Display locations of relatives only --- map/models.py | 18 ++++++++++++++++++ map/templates/map/map.html | 8 +++++--- map/views.py | 9 ++++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/map/models.py b/map/models.py index 94993d0..54037ae 100644 --- a/map/models.py +++ b/map/models.py @@ -20,6 +20,24 @@ class Friend(AbstractUser): return display_name + @property + def _relatives_set(self): + relative_set = set() + for group in self.location_sharing_groups.all(): + for relative in group.friends.all(): + relative_set.add(relative) + return relative_set + + @property + def relatives(self): + return list(self._relatives_set) + + @property + def relatives_with_self(self): + relatives = self._relatives_set + relatives.add(self) + return list(relatives) + class FriendLocation(BaseModel): latitude = CoordinateField() diff --git a/map/templates/map/map.html b/map/templates/map/map.html index 5c9b8ed..e939842 100644 --- a/map/templates/map/map.html +++ b/map/templates/map/map.html @@ -24,9 +24,11 @@ attribution: '© OpenStreetMap contributors' }).addTo(map); let marker = null; - {% for location in locations %} - marker = L.marker([{{ location.latitude_str }}, {{ location.longitude_str }}]).addTo(map); - marker.bindPopup("{{ location.safe_html|safe }}"); + {% for relative in friend.relatives_with_self %} + {% with relative.location as location %} + marker = L.marker([{{ location.latitude_str }}, {{ location.longitude_str }}]).addTo(map); + marker.bindPopup("{{ location.safe_html|safe }}"); + {% endwith %} {% endfor %} {% endblock %} diff --git a/map/views.py b/map/views.py index e6627a1..ccf731b 100644 --- a/map/views.py +++ b/map/views.py @@ -8,11 +8,14 @@ from map.forms import LocationForm from map.mixins import QuickActionsMixin -class MapView(LoginRequiredMixin, QuickActionsMixin, generic.ListView): - model = models.FriendLocation - context_object_name = 'locations' +class MapView(LoginRequiredMixin, QuickActionsMixin, generic.DetailView): + model = models.Friend + context_object_name = 'friend' template_name = 'map/map.html' + def get_object(self, queryset=None): + return self.request.user + def get_quick_actions(self): if hasattr(self.request.user, 'location'): actions = [{