Add missing constraint.
[wolnelektury.git] / src / catalogue / api / views.py
index 1af1577..0f95f0a 100644 (file)
@@ -313,18 +313,38 @@ class TagView(RetrieveAPIView):
     queryset = Tag.objects.all()
     
     def get_object(self):
     queryset = Tag.objects.all()
     
     def get_object(self):
-        return get_object_or_404(
-            Tag,
-            category=self.kwargs['category'],
-            slug=self.kwargs['slug']
-        )
+        try:
+            return get_object_or_404(
+                Tag,
+                category=self.kwargs['category'],
+                slug=self.kwargs['slug']
+            )
+        except Http404:
+            if self.request.method == 'POST':
+                return Tag(
+                    category=self.kwargs['category'],
+                    slug=self.kwargs['slug']
+                )
+            else:
+                raise
 
     def post(self, request, **kwargs):
         data = json.loads(request.POST.get('data'))
 
     def post(self, request, **kwargs):
         data = json.loads(request.POST.get('data'))
-        desc = data['description_pl']
+        fields = {
+            "description_pl": "description_pl",
+            "plural": "plural",
+            "is_epoch_specific": "genre_epoch_specific",
+            "collective_noun": "collective_noun",
+            "adjective_feminine_singular": "adjective_feminine_singular",
+            "adjective_nonmasculine_plural": "adjective_nonmasculine_plural",
+        }
         obj = self.get_object()
         obj = self.get_object()
-        obj.description_pl = desc
-        obj.save(update_fields=['description_pl'], quick=True)
+        for data_field, model_field in fields.items():
+            setattr(obj, model_field, data.get(data_field, getattr(obj, model_field)))
+        if obj.pk:
+            obj.save(update_fields=fields.values(), quick=True)
+        else:
+            obj.save()
         return Response({})
 
 
         return Response({})