Skip to content
Snippets Groups Projects
Commit 86e39a98 authored by Tore.Brede's avatar Tore.Brede
Browse files

GREG-91: Fixing formatting

parent de45c9c7
No related branches found
No related tags found
1 merge request!126GREG-91: Expand sponsor api
Pipeline #98515 failed
......@@ -11,7 +11,11 @@ from greg.api.views.person import (
IdentityViewSet,
)
from greg.api.views.role_type import RoleTypeViewSet
from greg.api.views.sponsor import SponsorViewSet, SponsorGuestsViewSet, SponsorOrgunitLinkView
from greg.api.views.sponsor import (
SponsorViewSet,
SponsorGuestsViewSet,
SponsorOrgunitLinkView,
)
router = DefaultRouter(trailing_slash=False)
router.register(r"persons", PersonViewSet, basename="person")
......@@ -52,8 +56,11 @@ urlpatterns += [
SponsorGuestsViewSet.as_view({"get": "list"}),
name="sponsor_guests-list",
),
re_path(r"^sponsors/(?P<sponsor_id>[0-9]+)/orgunit/(?P<orgunit_id>[0-9]+)$",
SponsorOrgunitLinkView.as_view(
{"get": "retrieve", "post": "create", "delete": "destroy"}),
name="sponsor_orgunit-detail")
re_path(
r"^sponsors/(?P<sponsor_id>[0-9]+)/orgunit/(?P<orgunit_id>[0-9]+)$",
SponsorOrgunitLinkView.as_view(
{"get": "retrieve", "post": "create", "delete": "destroy"}
),
name="sponsor_orgunit-detail",
),
]
......@@ -25,7 +25,7 @@ class SponsorViewSet(ModelViewSet):
lookup_field = "id"
def destroy(self, request, *args, **kwargs):
""" Overridden method to handle exception """
"""Overridden method to handle exception"""
instance = self.get_object()
try:
self.perform_destroy(instance)
......@@ -63,11 +63,14 @@ class SponsorGuestsViewSet(mixins.ListModelMixin, GenericViewSet):
return qs
class SponsorOrgunitLinkView(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
GenericViewSet):
""" Endpoint that allows manipulation of links between a sponsor and the units he is attached to """
class SponsorOrgunitLinkView(
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
GenericViewSet,
):
"""Endpoint that allows manipulation of links between a sponsor and the units he is attached to"""
queryset = OrganizationalUnit.objects.all().order_by("id")
serializer_class = OrganizationalUnitSerializer
pagination_class = PrimaryKeyCursorPagination
......@@ -78,12 +81,14 @@ class SponsorOrgunitLinkView(mixins.CreateModelMixin,
(sponsor_id, orgunit_id) = self._extract_sponsor_and_orgunit(kwargs)
hierarchical_access = request.data.get("hierarchical_access")
sponsor = Sponsor.objects.get(id=sponsor_id)
sponsor.units.add(orgunit_id, through_defaults={'hierarchical_access': hierarchical_access})
sponsor.units.add(
orgunit_id, through_defaults={"hierarchical_access": hierarchical_access}
)
return Response(status=status.HTTP_204_NO_CONTENT)
def destroy(self, request, *args, **kwargs):
""" Overridden because a delete at this endpoint should not attempt to delete the organizational unit,
"""Overridden because a delete at this endpoint should not attempt to delete the organizational unit,
but the link between the sponsor and the unit"""
(sponsor_id, orgunit_id) = self._extract_sponsor_and_orgunit(kwargs)
sponsor = Sponsor.objects.filter(id=sponsor_id).get()
......
......@@ -66,12 +66,12 @@ def test_add_sponsor_with_unit(client, unit_foo: OrganizationalUnit):
assert len(sponsor_lookup_response_body["results"]) == 1
assert len(sponsor_lookup_response_body["results"][0]["units"]) == 0
data = {
"hierarchical_access": True
}
data = {"hierarchical_access": True}
create_sponsor_link_url = reverse("v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_id, "orgunit_id": unit_foo.id})
create_sponsor_link_url = reverse(
"v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_id, "orgunit_id": unit_foo.id},
)
response = client.post(create_sponsor_link_url, data=data)
assert response.status_code == status.HTTP_200_OK
......@@ -84,17 +84,19 @@ def test_add_sponsor_with_unit(client, unit_foo: OrganizationalUnit):
@pytest.mark.django_db
def test_remove_sponsor_orgunit_link(client, sponsor_guy: Sponsor, unit_foo: OrganizationalUnit):
def test_remove_sponsor_orgunit_link(
client, sponsor_guy: Sponsor, unit_foo: OrganizationalUnit
):
sponsor_detail_url = reverse("v1:sponsor-detail", kwargs={"id": sponsor_guy.id})
response_get = client.get(sponsor_detail_url).json()
assert len(response_get["units"]) == 0
data = {
"hierarchical_access": True
}
data = {"hierarchical_access": True}
sponsor_orgunit_url = reverse("v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_guy.id, "orgunit_id": unit_foo.id})
sponsor_orgunit_url = reverse(
"v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_guy.id, "orgunit_id": unit_foo.id},
)
response = client.post(sponsor_orgunit_url, data=data)
assert response.status_code == status.HTTP_204_NO_CONTENT
......@@ -111,8 +113,13 @@ def test_remove_sponsor_orgunit_link(client, sponsor_guy: Sponsor, unit_foo: Org
@pytest.mark.django_db
def test_delete_sponsor_connected_to_identity_not_allowed(client, sponsor_guy: Sponsor, person_foo: Person,
person_foo_verified: Identity, unit_foo: OrganizationalUnit):
def test_delete_sponsor_connected_to_identity_not_allowed(
client,
sponsor_guy: Sponsor,
person_foo: Person,
person_foo_verified: Identity,
unit_foo: OrganizationalUnit,
):
sponsor_detail_url = reverse("v1:sponsor-detail", kwargs={"id": sponsor_guy.id})
response_get = client.get(sponsor_detail_url).json()
assert len(response_get["units"]) == 0
......@@ -125,7 +132,10 @@ def test_delete_sponsor_connected_to_identity_not_allowed(client, sponsor_guy: S
assert response_get["id"] == sponsor_guy.id
# Remove the identity and try to delete the sponsor again
url_delete = reverse("v1:person_identity-detail", kwargs={"person_id": person_foo.id, "id": person_foo_verified.id})
url_delete = reverse(
"v1:person_identity-detail",
kwargs={"person_id": person_foo.id, "id": person_foo_verified.id},
)
client.delete(url_delete)
response_delete = client.delete(sponsor_detail_url)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment