Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #VIEWS.PY
- @api_view(['GET'])
- @permission_classes([AllowAny])
- def topics_view(request):
- topics = Topic.objects.all().prefetch_related('author')
- serializer = TopicSerializer(topics, many=True)
- return Response(serializer.data, status=status.HTTP_200_OK)
- @api_view(['GET'])
- @permission_classes([AllowAny])
- def topic_detail_view(request, id):
- try:
- topic = Topic.objects.get(id=id)
- except Topic.DoesNotExist:
- message = {'error': 'Topic not found'}
- return Response(message, status=status.HTTP_400_BAD_REQUEST)
- serializer = TopicSerializer(topic)
- return Response(serializer.data, status=status.HTTP_200_OK)
- @api_view(['POST'])
- @permission_classes([IsAuthenticated])
- @authentication_classes([TokenAuthentication])
- @parser_classes([MultiPartParser, FormParser])
- def create_topic_view(request, format=None):
- serializer = TopicSerializer(data=request.data)
- if serializer.is_valid():
- new_topic = serializer.save()
- new_topic.image_url = f'{fetch_host(request)}{new_topic.image.url}'
- new_topic.user_created_topic = True
- new_topic.save()
- message = {**serializer.data, 'message':'Successfully created'}
- return Response(message, status=status.HTTP_201_CREATED)
- return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
- @api_view(['PUT'])
- @permission_classes([IsAuthenticated])
- @authentication_classes([TokenAuthentication])
- @parser_classes([MultiPartParser, FormParser])
- def update_topic_view(request, id, format=None):
- try:
- topic = Topic.objects.get(id=id, author=request.user)
- except Topic.DoesNotExist:
- message = {'error': 'Topic not found.'}
- return Response(message, status=status.HTTP_404_NOT_FOUND)
- serializer = TopicSerializer(topic, data=request.data)
- if serializer.is_valid():
- serializer.save()
- message = {**serializer.data, 'message':'Successfully created'}
- return Response(message, status=status.HTTP_202_ACCEPTED)
- return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
- @api_view(['POST'])
- @permission_classes([IsAuthenticated])
- @authentication_classes([TokenAuthentication])
- def delete_topic_view(request, id):
- try:
- topic = Topic.objects.get(id=id)
- except Topic.DoesNotExist:
- message = {'error': 'Topic not found.'}
- return Response(message, status=status.HTTP_404_NOT_FOUND)
- topic.delete()
- message = {'message': f'{topic.name} has been deleted successfully.'}
- return Response(message, status=status.HTTP_204_NO_CONTENT)
- @api_view(['GET'])
- @permission_classes([AllowAny])
- def post_list_view(request):
- posts = Post.objects.all().prefetch_related('like', 'topic', 'author')
- serializer = PostSerializer(posts, many=True)
- return Response(serializer.data, status=status.HTTP_200_OK)
- @api_view(['GET'])
- @permission_classes([AllowAny])
- def post_detail_view(request, id):
- try:
- post = Post.objects.prefetch_related('topic', 'author', 'like').get(id=id)
- except Post.DoesNotExist:
- error = {'message':'Post not found.'}
- return Response(error, status=status.HTTP_404_NOT_FOUND)
- serializer = PostSerializer(post)
- author_profile_image_url = post.author.profile.image_url
- usernames = [user.username for user in post.like.all()]
- data = {
- **serializer.data,
- 'author_profile_image_url': author_profile_image_url,
- 'like':usernames,
- }
- return Response(data, status=status.HTTP_200_OK)
- @api_view(['POST'])
- @permission_classes([IsAuthenticated])
- @authentication_classes([TokenAuthentication])
- @parser_classes([MultiPartParser, FormParser])
- def create_post_view(request, format=None):
- try:
- topic = Topic.objects.get(name__iexact=request.data.get('topic'))
- except Topic.DoesNotExist:
- message = {'error': 'Topic not found.'}
- return Response(message, status=status.HTTP_404_NOT_FOUND)
- serializer = PostSerializer(data=request.data)
- if serializer.is_valid():
- new_post = serializer.save()
- new_post.author = request.user
- new_post.topic = topic
- new_post.image_url = f'{fetch_host(request)}{new_post.image.url}'
- new_post.save()
- new_post.topic.update_total_post()
- message = {**serializer.data, 'message':'Successfully created'}
- return Response(message, status=status.HTTP_201_CREATED)
- return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
- @api_view(['PUT'])
- @permission_classes([IsAuthenticated])
- @authentication_classes([TokenAuthentication])
- @parser_classes([MultiPartParser, FormParser])
- def update_post_view(request, id, format=None):
- user = request.user
- post = Post.objects.filter(id=id, author=user).first()
- if post == None:
- message = {'error': 'Post not found.'}
- return Response(message, status=status.HTTP_404_NOT_FOUND)
- else:
- serializer = PostSerializer(post, data=request.data)
- if serializer.is_valid():
- updated_post = serializer.save()
- if serializer.validated_data.get('image'):
- updated_post.image_url = f'{fetch_host(request)}{updated_post.image.url}'
- updated_post.save()
- message = {**serializer.data, 'message':'Successfully updated'}
- return Response(message, status=status.HTTP_202_ACCEPTED)
- return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
- @api_view(['DELETE'])
- @permission_classes([IsAuthenticated])
- @authentication_classes([TokenAuthentication])
- def delete_post_view(request, id):
- user = request.user
- post = Post.objects.filter(id=id, author=user).first()
- if post == None:
- message = {'error':"Post not found"}
- return Response(message, status=status.HTTP_404_NOT_FOUND)
- else:
- post.delete()
- post.topic.update_total_post()
- message = {'message': f'Post has been deleted successfully.'}
- return Response(message, status=status.HTTP_200_OK)
- @api_view(['POST'])
- @permission_classes([IsAuthenticated])
- @authentication_classes([TokenAuthentication])
- def update_post_like_view(request, id):
- user = request.user
- try:
- post = Post.objects.prefetch_related('like').get(id=id)
- except Post.DoesNotExist:
- message = {'error':'Post not found'}
- return Response(message, status=status.HTTP_404_NOT_FOUND)
- if user not in post.like.all():
- post.like.add(user)
- post.save()
- message = {'message': 'Submmision was successfull.'}
- return Response(message, status=status.HTTP_200_OK)
- message = {'error': 'You have already submited.'}
- return Response(message, status=status.HTTP_400_BAD_REQUEST)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement