def user_posts_detail(request, pk, pk_post): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': print(pk) print(pk_post) snippets = Post.objects.get(id=pk_post, profile=pk) serializer = PostSerializer(snippets, many=False) return JsonResponse(serializer.data, safe=False) elif request.method == 'PUT': data = JSONParser().parse(request) original = Post.objects.get(id=pk_post, profile=pk) serializer = PostSerializer(original, data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE': Post.objects.get(id=pk_post, profile=pk).delete() return JsonResponse({"Deleted": "True"}, status=201, safe=False) return JsonResponse({"Method Not Allowed": "True"}, status=405)
def update(self, request, *args, **kwargs): pk = kwargs['pk'] snippet = self.get_object(pk) if snippet.author != request.user.id: return Response({"error": "only author can update post"}, status=status.HTTP_400_BAD_REQUEST) data = request.data.dict() if not data.get('title'): data['title'] = snippet.title if not data.get('text'): data['text'] = snippet.title data['author'] = snippet.author.id data['liked'] = snippet.liked data['liked'] = snippet.unliked data['liked'] = snippet.total_liked serializer = PostSerializer(snippet, data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def new_post(request): # data is passed as a string and needs to be converted to a dict data = json.loads(request.data['data']) client = get_object_or_404(Client, user=request.user) if request.data.get('file'): data['image'] = request.data['file'] if 'blog' in data: blog = get_object_or_404(Blog, id=data['blog']) else: topic = Topic.objects.get(name="Personal") blog = Blog.objects.get(topic=topic, owner__in=[client]) data.update({'blog': blog.id}) # check permissions to create new post on this blog if not blog.isPublic and client not in blog.subs.all(): data = {'error': 'not enough permissions'} else: data.update({'client': client.id}) post_serializer = PostSerializer(data=data) if post_serializer.is_valid(): post_serializer.save() data = { 'success': 'successfully created a new post', 'post': post_serializer.data } else: data = post_serializer.errors return Response(data)
def post(self, request, format=None): print 'In post hander' user = request.user serializer = PostSerializer(data=request.data, context={'user':user}) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.error, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, pk): post = get_object_or_404(Post, pk=pk) data = {"amount_of_upvotes": post.amount_of_upvotes + 1} serializer = PostSerializer(post, data=data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_post_serializer_is_invalid(self): post_obj_data = { u'title': u'title', u'code': u'code', u'ttl_option': u'minutes=10', u'syntax': "1" } post_serializer = PostSerializer(data=post_obj_data) self.assertFalse(post_serializer.is_valid())
def test_post_serializer_is_invalid(self): post_obj_data = { u'title': u'title', u'code': u'code', u'ttl_option': u'minutes=10', u'syntax': "1" } post_serializer = PostSerializer(data=post_obj_data) self.assertFalse(post_serializer.is_valid())
def test_post_serializer_is_valid(self): Syntax.objects.create(syntax_name="syntax_name1") post_obj_data = { u'title': u'title', u'code': u'code', u'ttl_option': u'minutes=10', u'syntax': "1" } post_serializer = PostSerializer(data=post_obj_data) self.assertTrue(post_serializer.is_valid())
def test_post_serializer_is_valid(self): Syntax.objects.create(syntax_name="syntax_name1") post_obj_data = { u'title': u'title', u'code': u'code', u'ttl_option': u'minutes=10', u'syntax': "1" } post_serializer = PostSerializer(data=post_obj_data) self.assertTrue(post_serializer.is_valid())
def put(self, request, pk, format=None): post = self.get_object(pk) self.check_object_permissions(self.request, post) if post.category != request.data.get('category'): return Response(status=status.HTTP_400_BAD_REQUEST) serializer = PostSerializer(post, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def user_posts(request, pk): """ List all code snippets, or create a new snippet. """ if request.method == 'GET': snippets = Post.objects.filter(profile=pk) serializer = PostSerializer(snippets, many=True) return JsonResponse(serializer.data, safe=False) elif request.method == 'POST': data = JSONParser().parse(request) serializer = PostSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400) return JsonResponse({"Method Not Allowed": "True"}, status=405)
def post(self, request, *args, **kwargs): serializer = PostSerializer(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 post(self, request, format=None): serializer = PostSerializer(data=request.data) if serializer.is_valid(): serializer.save(owner=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)