Skip to content

Commit 7ec2852

Browse files
committed
change problem edit URI
api/problem -> api/problem/<int:id>
1 parent 484eba6 commit 7ec2852

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

problem/tests.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from rest_framework import status
44
from rest_framework.test import APIRequestFactory, force_authenticate
55

6-
6+
from .models import Problem
77
from .views import ProblemView, TagView
88
from account.models import User
99

@@ -61,6 +61,31 @@ def testX_post_new_problem(self):
6161
response = self.view(request)
6262
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
6363

64+
def testW_change_problem(self):
65+
request_data = {
66+
"title": "Not Hard Problem",
67+
"description": "This is description",
68+
"pid": 8,
69+
"allow_html": True,
70+
"tags": [4, 3, 1]
71+
}
72+
73+
ac_data = {
74+
"enabled": True
75+
}
76+
77+
request = self.factory.patch(self.base_url, data=request_data)
78+
force_authenticate(request, User.objects.get(username="admin"))
79+
response = self.view(request, pid=5)
80+
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
81+
82+
target = Problem.objects.get(pid=8)
83+
self.assertEqual(target.pid, request_data["pid"])
84+
self.assertEqual(target.description, request_data["description"])
85+
self.assertEqual(target.title, request_data["title"])
86+
self.assertEqual(target.allow_html, request_data["allow_html"])
87+
self.assertEqual(target.enabled, ac_data["enabled"])
88+
6489

6590
class TagViewTest(TestCase):
6691
fixtures = ["testdatabase.yaml"]

problem/views.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,22 @@ def post(self, request):
4040
ps.save()
4141
return Response(status=status.HTTP_201_CREATED)
4242

43-
@method_decorator(syllable_required("pid", int))
44-
@method_decorator(
45-
permission_required("problem.change_problem", raise_exception=True)
46-
)
47-
def patch(slef, request):
43+
@method_decorator(parameter_required("pid"))
44+
@method_decorator(permission_required("problem.change_problem", raise_exception=True))
45+
def patch(self, request, pid):
4846
data = request.data
49-
id = data.get("pid")
5047

51-
problem = get_object_or_404(Problem, pid=id)
48+
problem = get_object_or_404(Problem, pid=pid)
5249
ps = ProblemSerializer(problem, data=data, partial=True)
5350
ps.is_valid(raise_exception=True)
5451
ps.save()
5552
return Response(status=status.HTTP_204_NO_CONTENT)
5653

57-
@method_decorator(syllable_required("pid", int))
5854
@method_decorator(permission_required("problem.delete_problem"))
59-
def delete(self, request):
55+
def delete(self, request, pid):
6056
data = request.data
61-
id = data.get("pid")
6257

63-
problem = get_object_or_404(Problem, pid=id)
58+
problem = get_object_or_404(Problem, pid=pid)
6459
problem.delete()
6560
return Response(status=status.HTTP_204_NO_CONTENT)
6661

0 commit comments

Comments
 (0)