def put(self, request, pk): article = self.get_object(pk) serializer = ArticleSerializer(article) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def article_list_create_api_view(request): if request.method == "GET": articles = Article.objects.filter(active=True) serializer = ArticleSerializer(articles, many=True) return Response(serializer.data) elif request.method == "POST": serializer = ArticleSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def Create(author, data): serialized = ArticleSerializer(data=data, partial=True) if serialized.is_valid(): instance = serialized.save(author) if instance: serialized = ArticleSerializer(instance) return (True, serialized.data) return ( False, serialized.errors, )
def save_article_with_serializer(article): themes = article.get("themes") if themes: article.pop("themes") serializer = ArticleSerializer(data=article) if serializer.is_valid(): article_obj = serializer.save() print(f"id:{article_obj.id}, title:{article_obj.title}") return save_article_theme(article_obj, themes) return None
def Update(id, data, serialize=True): article = ArticleManager.Find(id, False) if article is not None: serialized = ArticleSerializer(article, data=data, partial=True) if serialized.is_valid(): instance = serialized.save() if instance: if serialize: return serialized.data else: return instance return None
def article_detail_api_view(request, pk): try: article = Article.objects.get(pk=pk) except Article.DoesNotExist: return Response( {"error": { "code": 404, "message": "Article Not Found!" }}) if request.method == "GET": serializer = ArticleSerializer(article) return Response(serializer.data) elif request.method == "PUT": serializer = ArticleSerializer(article, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == "DELETE": article.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request): serializer = ArticleSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)