def post(self, request): name = request.data.get('name') posts = request.data.get('posts') if name and posts: try: board = Board.objects.create(user=request.user, name=name) for post in posts: try: board.posts.add(post) except Exception as e: pass return JsonResponse( BoardSerializer(board, requester=request.user).data) except Exception as e: print("status: 400", error_data(request="InternalError")) return JsonResponse(error_data(request="InternalError"), status=400) else: errors = error_data() if not name: errors = error_data(name="Required") if not posts: errors = error_data(posts="Required") print("status: 400", errors) return JsonResponse(errors, status=400)
def delete(self, request, post_id): try: Post.objects.get(pk=post_id, user=request.user).delete() print("status: 200", success_data("PostDeletedSuccessfully")) return JsonResponse(success_data("PostDeletedSuccessfully")) except ObjectDoesNotExist as e: print("status: 400", error_data(post="NotExist")) return JsonResponse(error_data(post="NotExist"), status=400)
def delete(self, request, board_id): try: board = Board.objects.get(pk=board_id, user=request.user) board.delete() return JsonResponse(success_data("BoardDeleted")) except Exception as e: print("status: 400", error_data(board="NotExist")) return JsonResponse(error_data(board="NotExist"), status=400)
def get(self, request, post_id): try: post = Post.objects.get(pk=post_id) data = get_paginated_data( data=CommentSerializer(post.comments.all(), many=True).data, page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/posts/{post_id}/comments/?") return JsonResponse(data) except ObjectDoesNotExist: print("status: 400", error_data(post="NotExist")) return JsonResponse(error_data(post="NotExist"), status=400)
def get(self, request, post_id): try: if not has_permission(request.user, Post.objects.get(pk=post_id).user): print("status: 400", error_data(profile="Private")) return JsonResponse(error_data(profile="Private"), status=400) return JsonResponse( PostSerializer(Post.objects.get(pk=post_id), requester=request.user).data) except ObjectDoesNotExist as e: print("status: 400", error_data(post="NotExist")) return JsonResponse(error_data(post="NotExist"), status=400)
def post(self, request, post_id): text = request.data.get('text') if text: try: post = Post.objects.get(pk=post_id) comment = post.comments.create(user=request.user, text=text) data = CommentSerializer(comment).data return JsonResponse(data) except ObjectDoesNotExist: print("status: 400", error_data(post="NotExist")) return JsonResponse(error_data(post="NotExist"), status=400) else: print("status: 400", error_data(text="Required")) return JsonResponse(error_data(text="Required"), status=400)
def get(self, request): user = get_user(request) if user: if not has_permission(request.user, user): print("status: 400", error_data(profile="Private")) return JsonResponse(error_data(profile="Private"), status=400) data = get_paginated_data( data=BoardSerializer(user.boards.all().order_by('-id'), requester=request.user, many=True).data, page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/boards/?username={user.username}") return JsonResponse(data) else: print("status: 400", error_data(profile="NotExist")) return JsonResponse(error_data(profile="NotExist"), status=400)
def get(self, request, board_id): try: if not has_permission(request.user, Board.objects.get(pk=board_id).user): print("status: 400", error_data(profile="Private")) return JsonResponse(error_data(profile="Private"), status=400) board = BoardSerializer(Board.objects.get(pk=board_id), requester=request.user).data data = get_paginated_data(data=board['posts'], page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/boards/{board_id}/?") data.update({ "id": board['id'], "name": board['name'], "posts_count": len(data['data']), }) return JsonResponse(data) except Exception as e: print("status: 400", error_data(board="NotExist")) return JsonResponse(error_data(board="NotExist"), status=400)
def get(self, request): tag = request.query_params.get("tag") if tag: users = list(Post.objects.all().filter( user__profile__is_private=False).values_list('user', flat=True)) users += list(request.user.profile.followings.all().values_list( 'user', flat=True)) users.append(request.user.pk) data = get_paginated_data(data=PostSerializer( Post.objects.filter(tags__name=tag, user_id__in=set(users)).order_by('-date'), many=True, requester=request.user).data, page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/posts/?tag={tag}") return JsonResponse(data) user = get_user(request) if user: if not has_permission(request.user, user): print("status: 400", error_data(profile="Private")) return JsonResponse(error_data(profile="Private"), status=400) data = get_paginated_data( data=PostSerializer( Post.objects.filter(user_id=user.pk).order_by('-date'), many=True, requester=request.user).data, page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/posts/?username={user.username}") return JsonResponse(data) else: print("status: 400", error_data(profile="NotExist")) return JsonResponse(error_data(profile="NotExist"), status=400)
def put(self, request, post_id): try: post = Post.objects.get(pk=post_id, user=request.user) if request.data.get("des"): post.des = request.data.get("des") if request.data.get("location"): post.location = request.data.get("location") if request.data.get("tags"): tags = request.data.get("tags").split() for tag in tags: try: Tag.objects.create(name=tag) except Exception as e: pass post.tags.clear() post.tags.add(*tags) post.save() return JsonResponse( PostSerializer(Post.objects.get(pk=post_id), requester=request.user).data) except ObjectDoesNotExist: print("status: 400", error_data(post="NotExist")) return JsonResponse(error_data(post="NotExist"), status=400)
def put(self, request, board_id): try: board = Board.objects.get(pk=board_id, user=request.user) name = request.data.get('name') add_posts = request.data.get('add_posts') remove_posts = request.data.get('remove_posts') if name: board.name = name if add_posts: for post in add_posts: try: board.posts.add(post) except Exception as e: pass if remove_posts: for post in remove_posts: try: board.posts.remove(post) except Exception as e: pass board.save() board = BoardSerializer(board, requester=request.user).data data = get_paginated_data(data=board['posts'], page=request.query_params.get('page'), limit=request.query_params.get('limit'), url=F"/social/boards/{board_id}/?") data.update({ "id": board['id'], "name": board['name'], "posts_count": len(data['data']), }) return JsonResponse(data) except Exception as e: print("status: 400", error_data(board="NotExist")) return JsonResponse(error_data(board="NotExist"), status=400)
def put(self, request, post_id): try: post = Post.objects.get(pk=post_id) if request.data.get('method'): method = request.data.get('method') if method == "like": post.likes.add(request.user) return JsonResponse( PostSerializer(post, requester=request.user).data) elif method == "dislike": post.likes.remove(request.user) return JsonResponse( PostSerializer(post, requester=request.user).data) else: print("status: 400", error_data(method="WrongData")) return JsonResponse(error_data(method="WrongData"), status=400) else: print("status: 400", error_data(method="Required")) return JsonResponse(error_data(method="Required"), status=400) except ObjectDoesNotExist: print("status: 400", error_data(post="NotExist")) return JsonResponse(error_data(post="NotExist"), status=400)