1212from django .contrib import messages
1313from django .contrib .auth .mixins import LoginRequiredMixin
1414from 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
1815from django .core .exceptions import BadRequest
1916from django .core .exceptions import PermissionDenied
2017from django .core .serializers import serialize
18+ from django .db .models import Count
2119from django .db .models import Q
2220from django .http import HttpRequest
2321from django .http import HttpResponse
2725from django .templatetags .static import static
2826from django .urls import reverse
2927from django .utils .decorators import method_decorator
28+ from django .views .decorators .cache import cache_control
29+ from django .views .decorators .cache import cache_page
3030from django .views .decorators .csrf import csrf_exempt
3131from django .views .generic import CreateView
3232from 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+
126167class 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