def put(self, request, pk): post = self.get_object(pk) 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 view_post(self, post): views = post.views + 1 post_serializer = PostSerializer(post, data={'views': views}, partial=True) if post_serializer.is_valid(): post_serializer.save()
def post(self, request): serializer = PostSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(response_data(True, serializer.data)) else: return Response(response_data(False, serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def unlike_post(self, pk): post = PostDetailAPIView().get_object(pk) like = post.like - 1 post_serializer = PostSerializer(post, data={'like': like}, partial=True) if post_serializer.is_valid(): post_serializer.save()
def post(self, request): print('post') print(request.data) serializer = PostSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.data, status=400)
def put(self, request, pk): post = self.get_object(pk) serializer = PostSerializer(post, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(response_data(True, serializer.data)) else: return Response(response_data(False, serializer.errors), status=status.HTTP_400_BAD_REQUEST)
def retrive(self, request, pk=None): boardId = request.QUERY_PARAMS['boardId'] print 'retrive boardId : %s' % (boardId) queryset = Posts.objects.filter(board__exact=boardId) serializer = PostSerializer(queryset) return Response(serializer.data)
def getObjectNoti(request, url_name): univ = request.user.univ content_type = request.GET.get('contentType') object_id = request.GET.get('objectId') if content_type == 10: content = Post.objects.get(pk=object_id) serializer = PostSerializer(content) else: content = Comment.objects.get(pk=object_id) serializer = CommentSerializer(content) data = {'content': serializer.data} return JsonResponse(data)
def detail(request, post_id): """ GET | post_id 에 해당하는 post 리스트를 보여줍니다. DELETE | post_id 에 해당하는 post 를 삭제합니다. """ post = get_object_or_404(Post, pk=post_id) if request.method == "GET": # 게시글 하나 보여주기 serializer = PostSerializer(post) return Response(serializer.data, status=status.HTTP_200_OK) if request.method == "DELETE": # 게시글 하나 삭제 post.delete() return Response({'message': '삭제 성공!'}, status=status.HTTP_204_NO_CONTENT)
def get(self, request, pk): post = self.get_object(pk) self.view_post(post) post_serializer = PostSerializer(post) post_data = post_serializer.data user = User.objects.get(pk=post_data['user']) user_serializer = UserSerializer(user) comment_count = Comment.objects.filter(post_id=post_data['id']).count() post_data['created_at'] = datetime_formatter(post_data['created_at'], '%Y-%m-%d %H:%M:%S') post_data['updated_at'] = datetime_formatter(post_data['updated_at'], '%Y-%m-%d %H:%M:%S') post_data['user'] = user_serializer.data post_data['comment_count'] = comment_count return Response(response_data(True, post_data))
def index(request, pageInfo_id): """ GET | category에 따라서 board 리스트를 보여줍니다. POST | 게시글을 등록합니다. """ if request.method == "GET": posts = get_list_or_404(Post, pageInfo=pageInfo_id) serializer = PostSerializer(posts, many=True) return Response(serializer.data, status=status.HTTP_200_OK) elif request.method == "POST": # 게시글 등록 serializer = PostSerializer(data=request.data) if serializer.is_valid(raise_exception=True): #is_valid()에서 비속어 처리도 진행 serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): limit = int(request.query_params.get('limit', 10)) now_page = int(request.query_params.get('page', 1)) max_page = math.ceil(len(Post.objects.all()) / limit) post_qs = paging_data(Post.objects.all()[::-1], limit, now_page) post_serializer = PostSerializer(post_qs, many=True) posts = post_serializer.data for index, post in enumerate(posts): user = User.objects.get(pk=post['user']) user_serializer = UserSerializer(user) comment_count = Comment.objects.filter(post_id=post['id']).count() posts[index]['created_at'] = datetime_formatter( post['created_at'], '%Y-%m-%d %H:%M:%S') posts[index]['updated_at'] = datetime_formatter( post['updated_at'], '%Y-%m-%d %H:%M:%S') posts[index]['user'] = user_serializer.data posts[index]['comment_count'] = comment_count return Response(response_data(True, posts, now_page, max_page))
def main(request, url_name): try: request.session['back_url'] = request.path # term, state 변경 했을 때 유저를 체크 해야 함 [univ, state, term, selected_category] = can_use(request, url_name, True, True, True) post_sets = make_posts_set(None, univ, state, term) is_post = False if post_sets else True notice_sets = Post.objects.select_related('ctgy', 'author') \ .prefetch_related('comments__author', 'ctgy__univ', 'likes', 'saved', 'viewed_by', 'comments', 'images')\ .filter(ctgy__univ=univ, is_notice=True) post_paginator = Paginator(post_sets, 15).page posts = post_paginator(1) if request.is_ajax(): # 무한스크롤 if not request.method == "POST": raise Exception("Not allowed request method") requested_page = request.POST.get('requestPage') next_posts = post_paginator(requested_page) serializer = PostSerializer(next_posts.object_list, many=True) has_next = next_posts.has_next() return Response(status=status.HTTP_200_OK, data={ "next_posts": serializer.data, "has_next": has_next }) else: url = reverse('core:board:main_board', args=[url_name]) return render( request, 'board/main_board.html', { 'univ': univ, "url_name": url_name, 'categories': univ.category.all(), 'use_category': False, 'posts': posts.object_list, 'notices': notice_sets, 'state': state, 'url': url, 'has_next': posts.has_next(), 'is_post': is_post, }) except Univ.DoesNotExist as e: raise Http404 except Exception as e: if str(e) == 'anon': return redirect_with_next("core:accounts:login", "core:board:main_board", params={ "to": [url_name], "next": [url_name] }) if str(e) == 'others': return redirect("core:board:main_board", url_name=request.user.univ.url_name) # return HttpResponseBadRequest(content="Bad Request: " + str(e)) raise SuspiciousOperation
def get(self, request): serializer = PostSerializer(Post.objects.all(), many=True) return Response(serializer.data)
def get(self, request, pk, format=None): post = self.get_object(pk) serializer = PostSerializer(post) return Response(serializer.data)