Skip to content

Commit 06c6b90

Browse files
committed
feat: #141 delete organization API
1 parent 1ae373b commit 06c6b90

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

django_email_learning/platform/api/views.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unt
402402

403403

404404
@method_decorator(is_platform_admin(), name="post")
405+
@method_decorator(is_platform_admin(), name="delete")
405406
class SingleOrganizationView(View):
406407
def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-untyped-def]
407408
try:
@@ -431,6 +432,20 @@ def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unt
431432
except IntegrityError as e:
432433
return JsonResponse({"error": str(e)}, status=409)
433434

435+
def delete(self, request, *args, **kwargs): # type: ignore[no-untyped-def]
436+
try:
437+
organization = Organization.objects.get(id=kwargs["organization_id"])
438+
organization.delete()
439+
return JsonResponse(
440+
{"message": "Organization deleted successfully"}, status=200
441+
)
442+
except Organization.DoesNotExist:
443+
return JsonResponse({"error": "Organization not found"}, status=404)
444+
except ValidationError as e:
445+
return JsonResponse({"error": e.json()}, status=400)
446+
except IntegrityError as e:
447+
return JsonResponse({"error": str(e)}, status=409)
448+
434449

435450
@method_decorator(accessible_for(roles={"admin", "editor"}), name="post")
436451
class FileView(View):

tests/platform/api/test_views/test_organizations_view.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,17 @@ def test_edit_organization_requires_platform_admin_or_superadmin(client):
126126
payload = {"name": "Updated Org", "description": "Updated description"}
127127
response = client.post(update_url(1), data=payload, content_type="application/json")
128128
assert response.status_code == 403
129+
130+
131+
def test_delete_organization(superadmin_client):
132+
organization = Organization.objects.first()
133+
response = superadmin_client.delete(update_url(organization.id))
134+
assert response.status_code == 200
135+
assert not Organization.objects.filter(id=organization.id).exists()
136+
137+
138+
@pytest.mark.parametrize("client", ["viewer", "editor"], indirect=True)
139+
def test_delete_organization_requires_platform_admin_or_superadmin(client):
140+
organization = Organization.objects.first()
141+
response = client.delete(update_url(organization.id))
142+
assert response.status_code == 403

0 commit comments

Comments
 (0)