Skip to content

Commit 94a0c57

Browse files
authored
Merge branch 'main' into shift-selling
2 parents ce8985b + e74a82b commit 94a0c57

3 files changed

Lines changed: 73 additions & 11 deletions

File tree

src/maps/tests/test_views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ def test_map_views(self) -> None:
176176
response = self.client.get(url)
177177
assert response.status_code == 200
178178

179+
def test_map_layer_json_view(self) -> None:
180+
"""Test the map layers json view."""
181+
url = reverse("maps:map_layers_json")
182+
response = self.client.get(url)
183+
assert response.status_code == 200
184+
179185
def test_marker_views(self) -> None:
180186
"""Test the marker view."""
181187
good = ["ffffff", "ffffff00"]

src/maps/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
from django.urls import re_path
88

99
from .views import LayerGeoJSONView
10+
from .views import LayerJsonView
1011
from .views import MapMarkerView
1112
from .views import MapProxyView
1213

1314
app_name = "maps"
1415

1516
urlpatterns = [
1617
path("marker/<color>/", MapMarkerView.as_view(), name="marker"),
18+
path("layers/", LayerJsonView.as_view(), name="map_layers_json"),
1719
path(
1820
"<slug:layer_slug>/",
1921
include(

src/maps/views.py

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
from django.contrib import messages
1313
from django.contrib.auth.mixins import LoginRequiredMixin
1414
from django.contrib.gis.geos import Point
15-
from django.utils.decorators import method_decorator
16-
from django.views.decorators.cache import cache_page
17-
from django.views.decorators.cache import cache_control
1815
from django.core.exceptions import BadRequest
1916
from django.core.exceptions import PermissionDenied
2017
from django.core.serializers import serialize
18+
from django.db.models import Count
2119
from django.db.models import Q
2220
from django.http import HttpRequest
2321
from django.http import HttpResponse
@@ -27,6 +25,8 @@
2725
from django.templatetags.static import static
2826
from django.urls import reverse
2927
from django.utils.decorators import method_decorator
28+
from django.views.decorators.cache import cache_control
29+
from django.views.decorators.cache import cache_page
3030
from django.views.decorators.csrf import csrf_exempt
3131
from django.views.generic import CreateView
3232
from django.views.generic import DeleteView
@@ -123,6 +123,47 @@ def render_to_response(self, context: dict, **kwargs) -> TemplateResponse:
123123
)
124124

125125

126+
class LayerJsonView(JsonView):
127+
"""View for returning all available layers in json."""
128+
129+
def get_context_data(self, **kwargs) -> list:
130+
"""Return the GeoJSON Data to the client."""
131+
layers = []
132+
for layer in Layer.objects.filter(public=True):
133+
url = reverse(
134+
"maps:map_layer_geojson",
135+
kwargs={"layer_slug": layer.slug},
136+
)
137+
layers.append(
138+
{
139+
"name": layer.name,
140+
"team": layer.responsible_team.name if layer.responsible_team else "None",
141+
"camp": layer.responsible_team.camp.slug if layer.responsible_team else "all",
142+
"url": self.request.build_absolute_uri(url),
143+
"type": "layer",
144+
},
145+
)
146+
for facility_type in FacilityType.objects.all():
147+
url = reverse(
148+
"facilities:facility_list_geojson",
149+
kwargs={
150+
"camp_slug": facility_type.responsible_team.camp.slug,
151+
"facility_type_slug": facility_type.slug,
152+
},
153+
)
154+
layers.append(
155+
{
156+
"name": facility_type.name,
157+
"team": facility_type.responsible_team.name,
158+
"camp": facility_type.responsible_team.camp.slug,
159+
"url": self.request.build_absolute_uri(url),
160+
"type": "facility",
161+
},
162+
)
163+
164+
return layers
165+
166+
126167
class MapView(CampViewMixin, TemplateView):
127168
"""Global map view."""
128169

@@ -136,21 +177,34 @@ def get_layers(self) -> QuerySet:
136177
user_teams = self.request.user.teammember_set.filter(
137178
team__camp=self.camp,
138179
).values_list("team__name", flat=True)
139-
return Layer.objects.filter(
140-
((Q(responsible_team__camp=self.camp) | Q(responsible_team=None)) & Q(public=True))
141-
| (Q(responsible_team__name__in=user_teams) & Q(public=False)),
180+
return (
181+
Layer.objects.filter(
182+
((Q(responsible_team__camp=self.camp) | Q(responsible_team=None)) & Q(public=True))
183+
| (Q(responsible_team__name__in=user_teams) & Q(public=False)),
184+
)
185+
.annotate(num_features=Count("features"))
186+
.filter(num_features__gt=0)
142187
)
143188

144189
def get_context_data(self, **kwargs) -> dict:
145190
"""Get the context data."""
146191
context = super().get_context_data(**kwargs)
147-
context["facilitytype_list"] = FacilityType.objects.filter(
148-
responsible_team__camp=self.camp,
192+
context["facilitytype_list"] = (
193+
FacilityType.objects.filter(
194+
responsible_team__camp=self.camp,
195+
)
196+
.annotate(num_facilities=Count("facilities"))
197+
.filter(num_facilities__gt=0)
149198
)
150199
context["layers"] = self.get_layers()
151-
context["user_location_types"] = UserLocationType.objects.filter(
152-
user_locations__isnull=False,
153-
).distinct()
200+
context["user_location_types"] = (
201+
UserLocationType.objects.filter(
202+
user_locations__isnull=False,
203+
)
204+
.annotate(num_features=Count("user_locations"))
205+
.filter(num_features__gt=0)
206+
.distinct()
207+
)
154208
context["externalLayers"] = ExternalLayer.objects.filter(
155209
Q(responsible_team__camp=self.camp) | Q(responsible_team=None),
156210
)

0 commit comments

Comments
 (0)