Exemplo n.º 1
0
 def post(self, request):
     serializer = TopicSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response({'error': serializer.errors},
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 def put(self, request, pk, format=None):
     topic = self.get_object(pk)
     serializer = TopicSerializer(topic, data=request.data, partial=True)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
def discussion_topics(request, discussion_id):
    try:
        discussion = Discussion.objects.get(id=discussion_id)
        # topics = Topic.objects.filter(discussion_id=discussion_id) # default
        topics = Topic.sorted_objects.sort_by_title(request).filter(
            discussion_id=discussion_id)  # sort by title
        # topics = Topic.sorted_objects.sort_by_id(request).filter(discussion_id=discussion_id) # sort by id
    except (Category.DoesNotExist or Discussion.DoesNotExist
            or Topic.DoesNotExist) as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        paginator = PageNumberPagination()
        paginator.page_size = 3
        result_page = paginator.paginate_queryset(topics, request)
        serializer = TopicSerializer(result_page, many=True)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = TopicSerializer(instance=topics, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response({'error': serializer.errors})

    elif request.method == 'DELETE':
        topics.delete()
        return Response({'deleted': True})
Exemplo n.º 4
0
 def put(self, request, topic_id):
     topic = self.get_object(topic_id)
     serializer = TopicSerializer(instance=topic, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response({'error': serializer.errors})
Exemplo n.º 5
0
def topic_list(request):
  if request.method == "GET":
    topics = Topic.objects.all()
    ser = TopicSerializer(topics, many=True)
    return JsonResponse(ser.data, safe=False)

  elif request.method == "POST":
    data = JSONParser().parse(request)
    ser = TopicSerializer(data=data)
    if ser.is_valid():
      ser.save()
      return JsonResponse(ser.data, status=201)
      
  return JsonResponse(ser.errors, status=400)
Exemplo n.º 6
0
def topic_list(request):
    if request.method == 'GET':
        paginator = PageNumberPagination()
        paginator.page_size = 4
        topics = Topic.objects.all()
        result_page = paginator.paginate_queryset(topics, request)
        serializer = TopicSerializer(result_page, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = TopicSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response({'error': serializer.errors},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    def post(self, request, format=None):
        if 'board_id' not in request.data.keys():
            board = Board.objects.create(
                name=request.data['board']['name'],
                description=request.data['board']['description'],
                creater=request.user)
            board.save()
            request.data['board_id'] = board.pk

        request.data['starter'] = request.user
        topic_serializer = TopicSerializer(data=request.data)
        if topic_serializer.is_valid():
            topic_serializer.save(board_id=request.data['board_id'],
                                  starter=request.user)
            return Response(topic_serializer.data,
                            status=status.HTTP_201_CREATED)
        return Response(topic_serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 8
0
def topics_list(request, section_id):
    try:
        section = Section.objects.get(id=section_id)
    except Section.DoesNotExist as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        topics = Topic.objects.filter(section=section)
        serializer = TopicSerializer(topics, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = TopicSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response({'error': serializer.errors},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 9
0
def AddTopic(request):
    serializer = TopicSerializer(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)