Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/backoffice/templates/facility_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h3 class="card-title">{% if request.resolver_match.url_name == "facility_update
<form method="POST">
{% csrf_token %}
{% bootstrap_form form %}
<button id="use_location" class="btn btn-primary hide-for-no-js-users"><i class="fas fa-location"></i> Use my location</button>
<button type="submit" class="btn btn-success"><i class="fas fa-check"></i> Save</button>
<a href="{% url 'backoffice:facility_list' camp_slug=camp.slug %}" class="btn btn-secondary"><i class="fas fa-undo"></i> Cancel</a>
</form>
Expand Down
2 changes: 2 additions & 0 deletions src/backoffice/views/facilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def get_form(self, *args, **kwargs):
attrs={
"display_raw": "true",
"class": "form-control",
"geom_type": "Point",
},
)
return form
Expand Down Expand Up @@ -351,6 +352,7 @@ def get_form(self, *args, **kwargs):
"display_raw": "true",
"map_height": "500px",
"class": "form-control",
"geom_type": "Point",
},
)
return form
Expand Down
8 changes: 4 additions & 4 deletions src/maps/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/maps/templates/user_location_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<form method="POST">
{% csrf_token %}
{% bootstrap_form form %}
<button id="use_location" class="btn btn-primary hide-for-no-js-users"><i class="fas fa-location"></i> Use my location</button>
<button type="submit" class="btn btn-success">
<i class="fas fa-check"></i>
{% if request.resolver_match.url_name == "maps_user_location_update" %}Update{% else %}Create{% endif %} Location</button>
Expand Down
6 changes: 4 additions & 2 deletions src/maps/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
19 changes: 12 additions & 7 deletions src/maps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -126,15 +128,14 @@ 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,
).values_list("team__name", flat=True)
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:
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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"])
Expand Down
33 changes: 33 additions & 0 deletions src/static_src/js/maps/generic/init_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});
}
});
5 changes: 3 additions & 2 deletions src/utils/bootstrap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
23 changes: 16 additions & 7 deletions src/utils/color.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Loading