Skip to content

Commit

Permalink
categories crud (#2613)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Nov 8, 2024
1 parent 8c6d7fd commit 50d5a0d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions peterbecom/api/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
35 changes: 35 additions & 0 deletions peterbecom/api/tests/test_categories.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from django.urls import reverse
from django.utils import timezone

Expand Down Expand Up @@ -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"]
28 changes: 27 additions & 1 deletion peterbecom/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
BlogFileForm,
BlogFileUpload,
BlogitemRealtimeHitsForm,
CategoryForm,
CommentCountsIntervalForm,
EditBlogCommentForm,
EditBlogForm,
Expand Down Expand Up @@ -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 = (
Expand Down

0 comments on commit 50d5a0d

Please sign in to comment.