def submission_fav_api(request): token = getToken(request) if token is None: return Response({"authentication": ["This field is required."]}, status=status.HTTP_400_BAD_REQUEST) try: auth = Token.objects.get(key=token) except Token.DoesNotExist: return Response({"authentication": ["This key is invalid."]}, status=status.HTTP_401_UNAUTHORIZED) id = request.GET.get("id", None) if id is None: return Response({"id": ["This field is required."]}, status=status.HTTP_400_BAD_REQUEST) try: user = User.objects.get(username=id) except User.DoesNotExist: return Response({"id": ["User does not exist."]}, status=status.HTTP_404_NOT_FOUND) votedcontributions = ContributionVote.objects.filter(user=User.objects.get( username=request.GET.get('id'))) dto = [] for c in votedcontributions: contribution = Contribution.objects.get(id=c.contribution.id) fathers = Comment.objects.filter(contribution=contribution).filter( level=0).order_by('-votes') comments = [] for com in fathers: if com.father is None: f = None else: f = com.father.id comment_dto = CommentDTO(com.id, com.level, com.author, com.text, com.votes, com.date, com.contribution.id, f, com.contribution.title) comment_dto.replies = order(com.level + 1, com, com.contribution.id) comments.append(comment_dto) contribution_dto = ContributionDTO(contribution.id, contribution.title, contribution.type, contribution.points, contribution.author.username, contribution.url, contribution.text, contribution.date) contribution_dto.comments = comments dto.append(contribution_dto) serializer = ContributionDTOSerializer(dto, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def vote_comment_api(request, id): token = getToken(request) if token is None: return Response({"authentication": ["This field is required."]}, status=status.HTTP_400_BAD_REQUEST) try: auth = Token.objects.get(key=token) except Token.DoesNotExist: return Response({"authentication": ["This key is invalid."]}, status=status.HTTP_401_UNAUTHORIZED) try: c = Comment.objects.get(id=id) voted = CommentVote.objects.filter(user=auth.user, comment=c) if request.method == 'POST': if not voted: c.votes = c.votes + 1 c.save() commentvote = CommentVote() commentvote.user = auth.user commentvote.comment = c commentvote.save() if c.father is None: f = None else: f = c.father.id comment_dto = CommentDTO(c.id, c.level, c.author, c.text, c.votes, c.date, c.contribution.id, f, c.contribution.title) serializer = CommentDTOSerializer(comment_dto) return Response(serializer.data, status=status.HTTP_200_OK) else: if voted: c.votes = c.votes - 1 c.save() voted.delete() if c.father is None: f = None else: f = c.father.id comment_dto = CommentDTO(c.id, c.level, c.author, c.text, c.votes, c.date, c.contribution.id, f, c.contribution.title) serializer = CommentDTOSerializer(comment_dto) return Response(serializer.data, status=status.HTTP_200_OK) except Comment.DoesNotExist: return Response({"id": ["This id is not found."]}, status=status.HTTP_404_NOT_FOUND)
def order(i, father, id): children = Comment.objects.filter(contribution=Contribution.objects.get( id=id)).filter(level=i).filter(father=father) if len(children) == 0: return None else: replies = [] for child in children: c_dto = CommentDTO(child.id, child.level, child.author, child.text, child.votes, child.date, id, father.id, child.contribution.title) replies.append(c_dto) c_dto.replies = order(i + 1, child, id) return replies
def comments_api(request): token = getToken(request) if token is None: return Response({"authentication": ["This field is required."]}, status=status.HTTP_401_UNAUTHORIZED) try: auth = Token.objects.get(key=token) except Token.DoesNotExist: return Response({"authentication": ["This key is invalid."]}, status=status.HTTP_401_UNAUTHORIZED) id = request.GET.get("id", None) if id is None: return Response({"id": ["This field is required."]}, status=status.HTTP_400_BAD_REQUEST) try: user = User.objects.get(username=id) except User.DoesNotExist: return Response({"id": ["User does not exists."]}, status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': author = User.objects.get(username=id) comments = Comment.objects.filter(author=author).order_by( 'level', '-date') dto = [] for c in comments: if c.father is None: f = None else: f = c.father.id dto.append( CommentDTO(c.id, c.level, c.author, c.text, c.votes, c.date, c.contribution.id, f, c.contribution.title)) serializer = CommentDTOSerializer(dto, many=True) return Response(serializer.data)
def comments_fav_api(request): token = getToken(request) if token is None: return Response({"authentication": ["This field is required."]}, status=status.HTTP_400_BAD_REQUEST) try: auth = Token.objects.get(key=token) except Token.DoesNotExist: return Response({"authentication": ["This key is invalid."]}, status=status.HTTP_401_UNAUTHORIZED) id = request.GET.get("id", None) if id is None: return Response({"id": ["This field is required."]}, status=status.HTTP_400_BAD_REQUEST) try: user = User.objects.get(username=id) except User.DoesNotExist: return Response({"id": ["User does not exist."]}, status=status.HTTP_404_NOT_FOUND) coments_dto = [] votedcomments = CommentVote.objects.filter(user=user) for c in votedcomments: com = c.comment if com.father is None: f = None else: f = com.father.id coments_dto.append( CommentDTO(com.id, com.level, com.author, com.text, com.votes, com.date, com.contribution.id, f, com.contribution.title)) serializer = CommentDTOSerializer(coments_dto, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def submissions_api(request): token = getToken(request) if token is None: return Response({"authentication": ["This field is required."]}, status=status.HTTP_401_UNAUTHORIZED) try: auth = Token.objects.get(key=token) except Token.DoesNotExist: return Response({"authentication": ["This key is invalid."]}, status=status.HTTP_401_UNAUTHORIZED) if request.method == 'GET': id = request.GET.get("id", None) filter = request.GET.get("filter", None) type = request.GET.get("type", None) # obtenir les submissions d'un usuari concret if id is not None and filter is None and type is None: contributions = Contribution.objects.filter( author=User.objects.get( username=request.GET.get('id'))).order_by('-date') dto = [] for c in contributions: contribution = Contribution.objects.get(id=c.id) fathers = Comment.objects.filter( contribution=contribution).filter( level=0).order_by('-votes') comments = [] for com in fathers: if com.father is None: f = None else: f = com.father.id comment_dto = CommentDTO(com.id, com.level, com.author, com.text, com.votes, com.date, com.contribution.id, f, com.contribution.title) comment_dto.replies = order(com.level + 1, com, com.contribution.id) comments.append(comment_dto) contribution_dto = ContributionDTO( contribution.id, contribution.title, contribution.type, contribution.points, contribution.author.username, contribution.url, contribution.text, contribution.date) contribution_dto.comments = comments dto.append(contribution_dto) serializer = ContributionDTOSerializer(dto, many=True) return Response(serializer.data, status=status.HTTP_200_OK) # obtenir les submissions tipus ask elif id is None and filter is None and type is not None: if type == 'ask': contributions = Contribution.objects.filter( type="ask").order_by('-points') dto = [] for c in contributions: contribution = Contribution.objects.get(id=c.id) fathers = Comment.objects.filter( contribution=contribution).filter( level=0).order_by('-votes') comments = [] for com in fathers: if com.father is None: f = None else: f = com.father.id comment_dto = CommentDTO(com.id, com.level, com.author, com.text, com.votes, com.date, com.contribution.id, f, com.contribution.title) comment_dto.replies = order(com.level + 1, com, com.contribution.id) comments.append(comment_dto) contribution_dto = ContributionDTO( contribution.id, contribution.title, contribution.type, contribution.points, contribution.author.username, contribution.url, contribution.text, contribution.date) contribution_dto.comments = comments dto.append(contribution_dto) serializer = ContributionDTOSerializer(dto, many=True) return Response(serializer.data, status=status.HTTP_200_OK) else: return Response({"type": ["This field value must be ask."]}, status=status.HTTP_400_BAD_REQUEST) # obtenir totes les submissions ordenades per punts o data elif id is None and filter is not None and type is None: if filter == 'points': contributions = Contribution.objects.order_by('-points') dto = [] for c in contributions: contribution = Contribution.objects.get(id=c.id) fathers = Comment.objects.filter( contribution=contribution).filter( level=0).order_by('-votes') comments = [] for com in fathers: if com.father is None: f = None else: f = com.father.id comment_dto = CommentDTO(com.id, com.level, com.author, com.text, com.votes, com.date, com.contribution.id, f, com.contribution.title) comment_dto.replies = order(com.level + 1, com, com.contribution.id) comments.append(comment_dto) contribution_dto = ContributionDTO( contribution.id, contribution.title, contribution.type, contribution.points, contribution.author.username, contribution.url, contribution.text, contribution.date) contribution_dto.comments = comments dto.append(contribution_dto) serializer = ContributionDTOSerializer(dto, many=True) return Response(serializer.data, status=status.HTTP_200_OK) elif filter == 'news': contributions = Contribution.objects.all().order_by('-date') dto = [] for c in contributions: contribution = Contribution.objects.get(id=c.id) fathers = Comment.objects.filter( contribution=contribution).filter( level=0).order_by('-votes') comments = [] for com in fathers: if com.father is None: f = None else: f = com.father.id comment_dto = CommentDTO(com.id, com.level, com.author, com.text, com.votes, com.date, com.contribution.id, f, com.contribution.title) comment_dto.replies = order(com.level + 1, com, com.contribution.id) comments.append(comment_dto) contribution_dto = ContributionDTO( contribution.id, contribution.title, contribution.type, contribution.points, contribution.author.username, contribution.url, contribution.text, contribution.date) contribution_dto.comments = comments dto.append(contribution_dto) serializer = ContributionDTOSerializer(dto, many=True) return Response(serializer.data, status=status.HTTP_200_OK) else: return Response({"filter": ["This field value must be ask."]}, status=status.HTTP_400_BAD_REQUEST) # obtenir totes les submissions elif id is None and filter is None and type is None: contributions = Contribution.objects.all().order_by('-points') dto = [] for c in contributions: contribution = Contribution.objects.get(id=c.id) fathers = Comment.objects.filter( contribution=contribution).filter( level=0).order_by('-votes') comments = [] for com in fathers: if com.father is None: f = None else: f = com.father.id comment_dto = CommentDTO(com.id, com.level, com.author, com.text, com.votes, com.date, com.contribution.id, f, com.contribution.title) comment_dto.replies = order(com.level + 1, com, com.contribution.id) comments.append(comment_dto) contribution_dto = ContributionDTO( contribution.id, contribution.title, contribution.type, contribution.points, contribution.author.username, contribution.url, contribution.text, contribution.date) contribution_dto.comments = comments dto.append(contribution_dto) serializer = ContributionDTOSerializer(dto, many=True) return Response(serializer.data, status=status.HTTP_200_OK) else: return Response( { "query": ["You must only provide one optional query parameter."] }, status=status.HTTP_400_BAD_REQUEST) else: data = JSONParser().parse(request) serializer = ContributionCreationDTOSerializer(data=data) if serializer.is_valid(): c = Contribution() c.title = serializer.data.get('title') if not c.title: return Response({"body": ["You must provide a title."]}, status=status.HTTP_400_BAD_REQUEST) c.url = serializer.data.get('url') c.author = auth.user if not c.url: c.text = serializer.data.get('text') if not c.text: return Response( { "body": ["You must provide an url or text attribute."] }, status=status.HTTP_400_BAD_REQUEST) else: c.type = 'ask' else: match = Contribution.objects.filter(url=c.url).exists() if match: return Response( { "query": [ "Is already defined in this url: /api/submissions/" + str(Contribution.objects.get(url=c.url).id) ] }, status=status.HTTP_302_FOUND) c.save() if serializer.data.get('url') and serializer.data.get('text'): com = Comment() com.author = auth.user com.text = serializer.data.get('text') com.contribution = Contribution.objects.get(url=c.url) com.save() ud = UserDetail.objects.get(user=auth.user) ud.karma = ud.karma + 1 ud.save() contribution_dto = ContributionDTO(c.id, c.title, c.type, c.points, c.author.username, c.url, c.text, c.date) serializers = ContributionDTOSerializerNoComments(contribution_dto) return Response(serializers.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def submissions_id_api(request, id): token = getToken(request) if token is None: return Response({"authentication": ["This field is required."]}, status=status.HTTP_400_BAD_REQUEST) try: auth = Token.objects.get(key=token) except Token.DoesNotExist: return Response({"authentication": ["This key is invalid."]}, status=status.HTTP_401_UNAUTHORIZED) if request.method == 'GET': try: contribution = Contribution.objects.get(id=id) fathers = Comment.objects.filter( contribution=Contribution.objects.get(id=id)).filter( level=0).order_by('-votes') comments = [] for c in fathers: if c.father is None: f = None else: f = c.father.id comment_dto = CommentDTO(c.id, c.level, c.author, c.text, c.votes, c.date, c.contribution.id, f, c.contribution.title) comment_dto.replies = order(c.level + 1, c, c.contribution.id) comments.append(comment_dto) contribution_dto = ContributionDTO( contribution.id, contribution.title, contribution.type, contribution.points, contribution.author.username, contribution.url, contribution.text, contribution.date) contribution_dto.comments = comments serializer = ContributionDTOSerializer(contribution_dto) return Response(serializer.data, status=status.HTTP_200_OK) except Contribution.DoesNotExist: return Response({"id": ["This id is not found."]}, status=status.HTTP_404_NOT_FOUND) elif request.method == 'POST': try: contribution = Contribution.objects.get(id=id) data = JSONParser().parse(request) serializer = CommentCreationDTOSerializer(data=data) if serializer.is_valid(): comment = Comment() comment.text = serializer.data.get('text') comment.father = None comment.level = 0 comment.contribution = contribution comment.author = auth.user comment.save() comment_dto = CommentDTO(comment.id, comment.level, comment.author, comment.text, comment.votes, comment.date, comment.contribution.id, None, comment.contribution.title) serializer = CommentDTOSerializer(comment_dto) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) except Contribution.DoesNotExist: return Response({"id": ["This id is not found."]}, status=status.HTTP_404_NOT_FOUND)
def comments_id_api(request, id): token = getToken(request) if token is None: return Response({"authentication": ["This field is required."]}, status=status.HTTP_401_UNAUTHORIZED) try: auth = Token.objects.get(key=token) except Token.DoesNotExist: return Response({"authentication": ["This key is invalid."]}, status=status.HTTP_401_UNAUTHORIZED) if request.method == 'GET': try: c = Comment.objects.get(id=id) if c.father is None: f = None else: f = c.father.id comment_dto = CommentDTO(c.id, c.level, c.author, c.text, c.votes, c.date, c.contribution.id, f, c.contribution.title) comment_dto.replies = order(c.level + 1, c, c.contribution.id) serializer = CommentDTOSerializer(comment_dto) return Response(serializer.data, status=status.HTTP_200_OK) except Comment.DoesNotExist: return Response({"id": ["This id is not found."]}, status=status.HTTP_404_NOT_FOUND) elif request.method == 'POST': try: data = JSONParser().parse(request) serializer = CommentCreationDTOSerializer(data=data) if serializer.is_valid(): father = Comment.objects.get(id=id) comment = Comment() comment.text = serializer.data.get('text') comment.father = father comment.level = father.level + 1 comment.contribution = father.contribution comment.author = auth.user comment.save() comment_dto = CommentDTO(comment.id, comment.level, comment.author, comment.text, comment.votes, comment.date, comment.contribution.id, comment.father.id, comment.contribution.title) serializer = CommentDTOSerializer(comment_dto) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) except Comment.DoesNotExist: return Response({"id": ["This id is not found."]}, status=status.HTTP_404_NOT_FOUND)