Commits

Konstantin Mochalov  committed 47aa12a

POST /tasks/id and some tests for it

  • Participants
  • Parent commits 0e6a841

Comments (0)

Files changed (2)

File tasks/tests.py

         self.assertEquals(r.status_code, 400)
 
     def test_post_task(self):
-        r = Client().post('/task/3', json.dumps({
+        r = Client().post('/tasks/3', json.dumps({
             'description': 'New description 111',
             'date_start': '2013-09-13T17:39:32',
-            }))
+            }), content_type='text/json')
         self.assertEquals(r.status_code, 200)
         task = Task.objects.get(pk=3)
         self.assertEquals(task.description, "New description 111")
-        self.assertEquals(task.date_start, datetime.datetime(2013, 9, 13, 17, 39, 22))
+        self.assertEquals(task.date_start, datetime.datetime(2013, 9, 13, 17, 39, 32))
+
+    def test_post_task_invalid(self):
+        r = Client().post('/tasks/3', json.dumps({
+            'description': '',
+            'date_start': '2013-09-13T17:39:32',
+            }), content_type='text/json')
+        self.assertEquals(r.status_code, 400)

File tasks/views.py

 def task(request, id):
     if request.method in ["POST", "DELETE"]:
         try:
-            json.loads(request.body)
+            data = json.loads(request.body)
         except ValueError:
             return HttpResponseBadRequest("Invalid json")
 
-        task = Task()
+        task = Task.objects.get(pk=id)
 
+        try:
+            task.update_from_api(data)
+            task.save()
+        except ValidationError as e:
+            return HttpResponseBadRequest(str(e))
 
+        return HttpResponse()
     else:
         return HttpResponseNotAllowed(['PUT', 'POST', 'DELETE'])