diff --git a/src/backoffice/templates/facility_form.html b/src/backoffice/templates/facility_form.html index fab85fce8..81c4f45cb 100644 --- a/src/backoffice/templates/facility_form.html +++ b/src/backoffice/templates/facility_form.html @@ -21,6 +21,7 @@

{% if request.resolver_match.url_name == "facility_update
{% csrf_token %} {% bootstrap_form form %} + Cancel
diff --git a/src/backoffice/views/facilities.py b/src/backoffice/views/facilities.py index 98c7b8fc1..0d6ad8705 100644 --- a/src/backoffice/views/facilities.py +++ b/src/backoffice/views/facilities.py @@ -289,6 +289,7 @@ def get_form(self, *args, **kwargs): attrs={ "display_raw": "true", "class": "form-control", + "geom_type": "Point", }, ) return form @@ -351,6 +352,7 @@ def get_form(self, *args, **kwargs): "display_raw": "true", "map_height": "500px", "class": "form-control", + "geom_type": "Point", }, ) return form diff --git a/src/maps/mixins.py b/src/maps/mixins.py index 54cd12b82..f37ce7014 100644 --- a/src/maps/mixins.py +++ b/src/maps/mixins.py @@ -25,13 +25,13 @@ def setup(self, *args, **kwargs) -> None: super().setup(*args, **kwargs) self.layer = get_object_or_404(Layer, slug=self.kwargs["layer_slug"]) if not self.layer.public: - if ((self.layer.responsible_team and - self.layer.responsible_team.member_permission_set in self.request.user.get_all_permissions()) or - self.request.user.has_perm("camps.gis_team_member")): + if ( + self.layer.responsible_team + and self.layer.responsible_team.member_permission_set in self.request.user.get_all_permissions() + ) or self.request.user.has_perm("camps.gis_team_member"): return raise PermissionDenied - def get_context_data(self, *args, **kwargs) -> dict: """Add self.layer to context.""" context = super().get_context_data(*args, **kwargs) diff --git a/src/maps/templates/user_location_form.html b/src/maps/templates/user_location_form.html index e11e0393a..efb178b3a 100644 --- a/src/maps/templates/user_location_form.html +++ b/src/maps/templates/user_location_form.html @@ -23,6 +23,7 @@
{% csrf_token %} {% bootstrap_form form %} + diff --git a/src/maps/tests/test_views.py b/src/maps/tests/test_views.py index 291a2b666..38137e6d9 100644 --- a/src/maps/tests/test_views.py +++ b/src/maps/tests/test_views.py @@ -135,12 +135,14 @@ def setUpTestData(cls) -> None: ) cls.hidden_layer.save() - TeamMember.objects.create( + cls.users[0].save() + teammember = TeamMember.objects.create( team=cls.teams["noc"], user=cls.users[0], approved=True, lead=True, - ).save() + ) + teammember.save() def test_geojson_layer_views(self) -> None: """Test the geojson view.""" diff --git a/src/maps/views.py b/src/maps/views.py index f0772e815..3af4af861 100644 --- a/src/maps/views.py +++ b/src/maps/views.py @@ -36,9 +36,11 @@ from oauth2_provider.views.generic import ScopedProtectedResourceView if TYPE_CHECKING: + from uuid import UUID + from django.db.models import QuerySet from django.forms import BaseForm - from django.http import TemplateResponse + from django.template.response import TemplateResponse from typing import ClassVar @@ -78,7 +80,7 @@ class MapMarkerView(TemplateView): template_name = "marker.svg" @property - def color(self) -> tuple: + def color(self) -> tuple[int, int, int] | tuple[int, int, int, int]: """Return the color values as ints.""" hex_color = self.kwargs["color"] length = len(hex_color) @@ -97,7 +99,7 @@ def color(self) -> tuple: return (r, g, b, a) raise MarkerColorError - def get_context_data(self, **kwargs) -> dict: + def get_context_data(self, **kwargs) -> dict[str, tuple[int, int, int] | tuple[int, int, int, int]]: """Get the context data.""" context = super().get_context_data(**kwargs) try: @@ -126,7 +128,7 @@ class MapView(CampViewMixin, TemplateView): def get_layers(self) -> QuerySet: """Method to get the layers the user has access to.""" - user_teams=[] + user_teams = [] if not self.request.user.is_anonymous: user_teams = self.request.user.teammember_set.filter( team__camp=self.camp, @@ -134,7 +136,6 @@ def get_layers(self) -> QuerySet: return Layer.objects.filter( ((Q(responsible_team__camp=self.camp) | Q(responsible_team=None)) & Q(public=True)) | (Q(responsible_team__name__in=user_teams) & Q(public=False)), - ) def get_context_data(self, **kwargs) -> dict: @@ -550,7 +551,11 @@ def post(self, request: HttpRequest, **kwargs) -> dict: "data": location.data, } - def patch(self, request: HttpRequest, **kwargs) -> dict: + def patch( + self, + request: HttpRequest, + **kwargs, + ) -> dict[str, str | int | float | UUID] | HttpResponseNotAllowed | tuple[dict[str, str], int]: """HTTP Method for updating a user location.""" if "user_location" not in kwargs and "camp_slug" not in kwargs: return HttpResponseNotAllowed(permitted_methods=["POST"]) @@ -583,7 +588,7 @@ def patch(self, request: HttpRequest, **kwargs) -> dict: "data": location.data, } - def delete(self, request: HttpRequest, **kwargs) -> dict: + def delete(self, request: HttpRequest, **kwargs) -> HttpResponse | HttpResponseNotAllowed: """HTTP Method for deleting a user location.""" if "user_location" not in kwargs and "camp_slug" not in kwargs: return HttpResponseNotAllowed(permitted_methods=["POST"]) diff --git a/src/static_src/js/maps/generic/init_loader.js b/src/static_src/js/maps/generic/init_loader.js index a61f8f646..ed1d0e6bd 100644 --- a/src/static_src/js/maps/generic/init_loader.js +++ b/src/static_src/js/maps/generic/init_loader.js @@ -17,3 +17,36 @@ window.addEventListener("map:init", function (event) { }); } }); + + +document.addEventListener("DOMContentLoaded", function(){ + /* Method run when location lookup was successfull */ + function location_place_success(pos) { + const crd = pos.coords; + const geomInput = document.getElementsByClassName('django-leaflet-raw-textarea')[0]; + if (geomInput) { + geomInput.value = `{"type":"Point","coordinates":[${crd.longitude},${crd.latitude}]}`; + document.querySelector("form").submit(); + } else { + console.log("Could not find input field"); + } + } + + /* Method run when location lookup failed */ + function location_error(err) { + alert(`ERROR(${err.code}): ${err.message}`); + } + + const location_options = { + enableHighAccuracy: true, + timeout: 3000, + maximumAge: 0, //Always retrieve position dont use cached value + }; + var use_location_button = document.getElementById("use_location"); + if (use_location_button) { + use_location_button.addEventListener("click", function(event){ + navigator.geolocation.getCurrentPosition(location_place_success, location_error, location_options); + event.preventDefault() + }); + } +}); diff --git a/src/utils/bootstrap/base.py b/src/utils/bootstrap/base.py index 1ef43a35d..13ff78f4c 100644 --- a/src/utils/bootstrap/base.py +++ b/src/utils/bootstrap/base.py @@ -1691,7 +1691,7 @@ def create_camp_info_items(self, camp: Camp, categories: dict) -> None: category=categories["noc"], headline="Switches", anchor="switches", - body="We have places for you to get your cable plugged in to a switch" + body="We have places for you to get your cable plugged in to a switch", ) def create_camp_feedback(self, camp: Camp, users: dict[User]) -> None: @@ -2176,6 +2176,7 @@ def bootstrap_tests(self) -> None: camp.save() self.camp = self.camps[1][0] + self.add_team_permissions(self.camp) self.teams = teams[self.camp.camp.lower.year] for member in TeamMember.objects.filter(team__camp=self.camp): member.save() @@ -2297,7 +2298,7 @@ def bootstrap_camp(self, options: dict) -> None: camp.call_for_participation_open = not read_only camp.call_for_sponsors_open = not read_only camp.save() - + # Update team permissions. if camp.camp.lower.year == settings.UPCOMING_CAMP_YEAR: for member in TeamMember.objects.filter(team__camp=camp): diff --git a/src/utils/color.py b/src/utils/color.py index 589fe55dd..d240026ae 100644 --- a/src/utils/color.py +++ b/src/utils/color.py @@ -1,9 +1,16 @@ -# TODO: docstrings because what the hell is going on here +"""Utilities for adjusting color.""" + from __future__ import annotations +DARK = 150 + -def adjust_color(color, factor=0.4): - if len(color) == 3: +def adjust_color( + color: tuple[int, int, int] | tuple[int, int, int, int], + factor: float = 0.4, +) -> tuple[int, int, int, int]: + """Adjust the color by a factor.""" + if len(color) == 3: # noqa: PLR2004 color = (*color, 1) r, g, b, a = color if factor > 0: @@ -19,9 +26,11 @@ def adjust_color(color, factor=0.4): return (new_r, new_g, new_b, a) -def is_dark(color): - return 0.2126 * color[0] + 0.7152 * color[1] + 0.0722 * color[2] < 150 +def is_dark(color: tuple[int, int, int] | tuple[int, int, int, int]) -> bool: + """Method to test if color is on the darker side.""" + return 0.2126 * color[0] + 0.7152 * color[1] + 0.0722 * color[2] < DARK -def is_light(color): - return 0.2126 * color[0] + 0.7152 * color[1] + 0.0722 * color[2] > 150 +def is_light(color: tuple[int, int, int] | tuple[int, int, int, int]) -> bool: + """Method to test if color is on the lighter side.""" + return 0.2126 * color[0] + 0.7152 * color[1] + 0.0722 * color[2] > DARK