From 50d5a0d9c61b6294870f7cebd8e504da7e401146 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Fri, 8 Nov 2024 16:50:47 -0500 Subject: [PATCH] categories crud (#2613) --- peterbecom/api/forms.py | 5 ++++ peterbecom/api/tests/test_categories.py | 35 +++++++++++++++++++++++++ peterbecom/api/views.py | 28 +++++++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/peterbecom/api/forms.py b/peterbecom/api/forms.py index b88cb5c83..518d6a8ef 100644 --- a/peterbecom/api/forms.py +++ b/peterbecom/api/forms.py @@ -252,3 +252,8 @@ class Meta: def clean_classification(self): value = self.cleaned_data["classification"] return value.lower() + + +class CategoryForm(forms.Form): + name = forms.CharField(required=True) + category = forms.ModelChoiceField(queryset=Category.objects.all(), required=False) diff --git a/peterbecom/api/tests/test_categories.py b/peterbecom/api/tests/test_categories.py index ae187e605..c0e9bd520 100644 --- a/peterbecom/api/tests/test_categories.py +++ b/peterbecom/api/tests/test_categories.py @@ -1,3 +1,5 @@ +import json + from django.urls import reverse from django.utils import timezone @@ -42,3 +44,36 @@ def test_happy_path(admin_client): assert second["count"] == 0 assert second["id"] assert second["name"] == "Foodware" + + +def test_add_and_edit_category(admin_client): + url = reverse("api:categories") + response = admin_client.post( + url, + json.dumps({"name": "Newnewss", "category": ""}), + content_type="application/json", + ) + assert response.status_code == 201 + response = admin_client.get(url) + assert response.status_code == 200 + (first,) = response.json()["categories"] + assert first["name"] == "Newnewss" + + response = admin_client.post( + url, + json.dumps({"name": "Newnewss2", "category": str(first["id"])}), + content_type="application/json", + ) + response = admin_client.get(url) + assert response.status_code == 200 + (first,) = response.json()["categories"] + assert first["name"] == "Newnewss2" + + response = admin_client.delete(f"{url}?id=999") + assert response.status_code == 404 + response = admin_client.delete(f"{url}?id={first['id']}") + assert response.status_code == 200 + + response = admin_client.get(url) + assert response.status_code == 200 + assert not response.json()["categories"] diff --git a/peterbecom/api/views.py b/peterbecom/api/views.py index 1d10bbc4d..6fc655ca6 100644 --- a/peterbecom/api/views.py +++ b/peterbecom/api/views.py @@ -60,6 +60,7 @@ BlogFileForm, BlogFileUpload, BlogitemRealtimeHitsForm, + CategoryForm, CommentCountsIntervalForm, EditBlogCommentForm, EditBlogForm, @@ -271,10 +272,35 @@ def blogitem(request, oid): def categories(request): + if request.method == "DELETE": + id = request.GET["id"] + category = get_object_or_404(Category, id=id) + category.delete() + return json_response({"ok": True}, status=200) + + if request.method == "POST": + data = json.loads(request.body.decode("utf-8")) + if "category" in data and not data["category"]: + data.pop("category") + form = CategoryForm(data) + if form.is_valid(): + if form.cleaned_data["category"]: + form.cleaned_data["category"].name = form.cleaned_data["name"] + form.cleaned_data["category"].save() + else: + Category.objects.create(name=form.cleaned_data["name"]) + return json_response({"ok": True}, status=201) + else: + return json_response({"errors": form.errors}, status=400) + # Prepare all the names and IDs all_categories = {} for id, name in Category.objects.all().values_list("id", "name"): - all_categories[id] = {"name": name, "count": 0, "id": id} + all_categories[id] = { + "name": name, + "count": 0, + "id": id, + } # Gather each categories usage count qs = (