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)
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})
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 test_inactive_topics_are_returned_correctly(self): request = self.factory.get("api/topics/inactive") force_authenticate(request, user=self.user) response = self.inactive_view(request) topics = TopicSerializer(data=response.data['results'], many=True) topics.is_valid(raise_exception=True) topics = topics.create(topics.validated_data) self.assertEqual(self.inactive_topics_count, len(topics)) for topic in topics: self.assertFalse(topic.is_active())
def post(self, request, action, format=None): if action == 'create': if request.user.is_authenticated: form = TopicForm(request.data) if form.is_valid(): topic = form.save(commit=False) topic.url = make_url(topic.title) topic.creator = request.user topic.save() serializer = TopicSerializer(topic) return Response(serializer.data, status=status.HTTP_200_OK) return Response(form.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_401_UNAUTHORIZED) elif action == 'like': if request.user.is_authenticated: topic_id = request.data.get('topic_id') try: topic = Topic.objects.get(id=topic_id) if topic is not None: like = check_like(user=request.user, topic=topic) if like: like.save() else: return Response({'errors': 'Can not remove like'}, status=status.HTTP_403_FORBIDDEN) serializer = LikeSerializer(like) return Response(serializer.data, status=status.HTTP_200_OK) except ObjectDoesNotExist: return Response({"errors": "Invalid input"}, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(status=status.HTTP_204_NO_CONTENT)
def get(self, request, action, format=None): if action == 'list': limit, offset = make_limit_and_offset(request.GET) topics = Topic.objects.order_by('-created')[offset:limit] serializer = TopicSerializer(topics, many=True) return Response(serializer.data, status=status.HTTP_200_OK) return Response(status=status.HTTP_204_NO_CONTENT)
def topic_detail(request, topic_id): try: topic = Topic.objects.get(id=topic_id) except Topic.DoesNotExist as e: return Response({'error': str(e)}) if request.method == 'GET': serializer = TopicWithCommentsSerializer(topic) return Response(serializer.data) elif request.method == 'PUT': serializer = TopicSerializer(instance=topic, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'error': serializer.errors}) elif request.method == 'DELETE': topic.delete() return Response({'deleted': True}) elif request.method == 'POST': serializer = CommentMSerializer(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 test_can_get_empty_topic_list(self): response = self.client.get(reverse('topic-list')) topics = Topic.objects.all() serializer = TopicSerializer(topics, many=True) self.assertEqual(response.data, serializer.data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 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})
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)
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)
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 GeneralData(request): """!@brief Function which returns general data useful during the signup process while filling the form @details the function returns the list of all topics in the db currently along with a list of department choices, degree choices and year choices while filling up the signup form @return a dictionary of department choices with key being the value to be added while the value being the description for the key, similarly for degree as well as year """ depchoices = {} for x in department_choices: depchoices[x[0]] = x[1] degchoices = {} for x in degree_choices: degchoices[x[0]] = x[1] topics = Topic.objects.all() tserializer = TopicSerializer(topics, many=True) return Response({ "department_choices": depchoices, "degree_choices": degchoices, "topics": tserializer.data })
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)
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)
def get(self, request, topic_id): topic = self.get_object(topic_id) serializer = TopicSerializer(topic) return Response(serializer.data)
def get(self, request, pk): topic = get_object_or_404(Topic, pk=pk) data = TopicSerializer(topic).data return Response(data)
def TopicList(request): topics = Topic.objects.all() serializer = TopicSerializer(topics, many=True) return Response(serializer.data)
def get(self, request): topics = Topic.objects.all()[:20] data = TopicSerializer(topics, many=True).data return Response(data)
def get(self, request, category_id, discussion_id): # category = Category.objects.get(id=category_id) # discussion = Discussion.objects.get(id=discussion_id) topics = Topic.objects.filter(discussion_id=discussion_id) serializer = TopicSerializer(topics, many=True) return Response(serializer.data)
def topic_list(request): if request.method == "GET": topics = Topic.objects.all() serializer = TopicSerializer(topics, many=True) return Response(serializer.data)
def get(self, request, *args, **kwargs): # noqa queryset = Topic.get_most_popular_topics() serializer = TopicSerializer(queryset, many=True, context={'request': request}) return Response(serializer.data)
def topics_count(request): if request.method == 'GET': topics = Topic.all_topics.all() serializer = TopicSerializer(topics, many=True) return Response(serializer.data)
def get(self, request, pk, format=None): serializer = TopicSerializer(self.get_object(pk)) return Response(serializer.data)
def get(self, request, format=None): topics = Topic.objects.all() serializer = TopicSerializer(topics, many=True) return Response(serializer.data)