def post(self, request, url_slug): idea = verify_idea(url_slug=url_slug) if not idea: return Response({'message': 'Idea not found.'}, status=status.HTTP_404_NOT_FOUND) if 'rating' not in request.data: return Response({'message': 'Missing parameter'}, status=status.HTTP_400_BAD_REQUEST) if int(request.data['rating']) > 100 or int( request.data['rating']) < 0: return Response( {'message': 'Unsupported value for parameter rating'}, status=status.HTTP_400_BAD_REQUEST) idea_rating, created = IdeaRating.objects.get_or_create( idea=idea, user=request.user) idea_rating.rating = int(request.data['rating']) idea_rating.save() average_rating = sum(idea_rating.rating for idea_rating in idea. rating_idea.all()) // idea.rating_idea.count() return Response( { "average_rating": { "rating_score": average_rating, "number_of_ratings": idea.rating_idea.count() } }, status=status.HTTP_201_CREATED)
def patch(self, request, url_slug, comment_id): idea = verify_idea(url_slug=url_slug) if not idea: raise NotFound(detail="Idea not found.") idea_comment = self.get_comment(comment_id=comment_id) self.verfiy_author_permission(idea_comment, request.user) parent_comment = None if 'parent_comment_id' in request.data: try: parent_comment = IdeaComment.objects.get( id=request.data['parent_comment_id']) except IdeaComment.DoesNotExist as e: logger.error( f"UDIdeaComment: idea comment {request.data['parent_comment_id']} does not exist: {e}" ) idea_comment.content = request.data['content'] if parent_comment: idea_comment.parent_comment = parent_comment idea_comment.save() return Response({'message': 'Your comment is updated.'}, status=status.HTTP_200_OK)
def post(self, request, url_slug): idea = verify_idea(url_slug=url_slug) if not idea: raise NotFound(detail="Idea not found.") # Check for required params required_params = ['content'] for param in required_params: if param not in request.data: raise ParseError(detail="Missing required parameter") parent_comment = None if request.data['parent_comment_id']: try: parent_comment = IdeaComment.objects.get( id=request.data['parent_comment_id']) except IdeaComment.DoesNotExist as e: logger.info( f"IdeaComment: Parent comment for {request.data['parent_comment_id']} does not exist: {e}" ) idea_comment = IdeaComment.objects.create( author_user=request.user, content=request.data['content'], idea=idea, parent_comment=parent_comment) if idea_comment.parent_comment: create_idea_comment_reply_notification(idea, idea_comment, request.user) else: create_idea_comment_notification(idea, idea_comment, request.user) serializer = IdeaCommentSerializer(idea_comment) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request, url_slug): idea = verify_idea(url_slug=url_slug) if not idea: return Response({'message': 'Idea not found.'}, status=status.HTTP_404_NOT_FOUND) IdeaSupporter.objects.get_or_create(idea=idea, user=request.user) return Response(None, status=status.HTTP_201_CREATED)
def get(self, request, url_slug, format=None): idea = verify_idea(url_slug) if not idea: return Response({'message': 'Idea not found'}, status=status.HTTP_404_NOT_FOUND) serializer = IdeaSerializer(idea) return Response(serializer.data, status=status.HTTP_200_OK)
def get_queryset(self): idea = verify_idea(url_slug=self.kwargs['url_slug']) if not idea: raise NotFound(detail="Idea not found.") return IdeaComment.objects.filter( idea__url_slug=self.kwargs['url_slug'], is_abusive=False, deleted_at__isnull=True, parent_comment=None).order_by('-id', 'updated_at', 'created_at')
def has_permission(self, request, view): if request.method in SAFE_METHODS: return True idea = verify_idea(view.kwargs.get('url_slug')) if not idea: return False if idea.user == request.user: return True return False
def delete(self, request, url_slug, comment_id): idea = verify_idea(url_slug=url_slug) if not idea: raise NotFound(detail="Idea not found.") idea_comment = self.get_comment(comment_id=comment_id) self.verfiy_author_permission(idea_comment, request.user) idea_comment.deleted_at = timezone.now() idea_comment.deleted_by_user = request.user idea_comment.content = 'DELETED' idea_comment.save() return Response({"message": "Comment deleted."}, status=status.HTTP_200_OK)
def get(self, request, url_slug): idea = verify_idea(url_slug=url_slug) if not idea: raise NotFound(detail="Idea not found:" + url_slug, code=status.HTTP_404_NOT_FOUND) user_rating = IdeaRating.objects.filter(user=request.user, idea=idea) if user_rating.exists(): return Response( { 'user_rating': user_rating[0].rating, 'has_user_rated': True }, status=status.HTTP_200_OK) else: return Response({ 'user_rating': 0, 'has_user_rated': False }, status=status.HTTP_200_OK)
def get(self, request, url_slug): print("checking whether I joined") idea = verify_idea(url_slug) if not idea: print("There is no idea with the url slug " + url_slug) return Response({'message': 'Idea not found'}, status=status.HTTP_404_NOT_FOUND) try: chat = MessageParticipants.objects.get(related_idea=idea.id) participant = Participant.objects.filter(user=request.user, chat=chat) if participant.exists(): return Response( { 'chat_uuid': chat.chat_uuid, 'has_joined': True }, status=status.HTTP_200_OK) else: return Response({'has_joined': False}, status=status.HTTP_200_OK) except MessageParticipants.DoesNotExist: return Response({'message': 'Group chat not found'}, status=status.HTTP_404_NOT_FOUND)
def post(self, request, url_slug): idea = verify_idea(url_slug) if not idea: return Response({'message': 'Idea not found'}, status=status.HTTP_404_NOT_FOUND) try: chat = MessageParticipants.objects.get(related_idea=idea.id) member_role = Role.objects.get(role_type=Role.READ_ONLY_TYPE) idea_supporter = IdeaSupporter.objects.get_or_create( idea=idea, user=request.user) try: Participant.objects.get(user=request.user, chat=chat) except Participant.DoesNotExist: Participant.objects.create(user=request.user, chat=chat, role=member_role) create_idea_join_notification(idea, idea_supporter[0], chat.chat_uuid) except MessageParticipants.DoesNotExist: return Response({'message': 'Group chat not found'}, status=status.HTTP_404_NOT_FOUND) # Participant.objects.get_or_create(user=request.user, chat=idea.related_idea_message_participant) return Response({'chat_uuid': chat.chat_uuid}, status=status.HTTP_200_OK)
def patch(self, request, url_slug, format=None): idea = verify_idea(url_slug) if not idea: return Response({'message': 'Idea not found'}, status=status.HTTP_404_NOT_FOUND) if 'name' in request.data and idea.name != request.data['name']: idea.name = request.data['name'] if 'short_description' in request.data and\ idea.short_description != request.data['short_description']: idea.short_description = request.data['short_description'] if 'image' in request.data and request.data['image'] is not None: image = get_image_from_data_url(request.data['image'])[0] idea.image = image if 'thumbnail_image' in request.data and request.data[ 'thumbnail_image']: thumbnail_image = get_image_from_data_url( request.data['thumbnail_image'])[0] idea.thumbnail_image = thumbnail_image if 'location' in request.data and request.data['location']: idea.location = get_location(request.data['location']) if 'hub' in request.data: try: hub = Hub.objects.get(url_slug=request.data['hub']) except Hub.DoesNotExist: hub = None if hub: idea.hub = hub if 'parent_organization' in request.data: try: organization = Organization.objects.get( id=request.data['parent_organization']) except Organization.DoesNotExist: organization = None if organization: idea.organization = organization idea.save() # TODO (Dip 8-6-2021): Transfer this logic to celery task. texts = { "name": idea.name, "short_description": idea.short_description } try: translations = get_translations(texts, {}, idea.language.language_code) except ValueError as ve: translations = None logger.error( "TranslationFailed: Error translating texts, {}".format(ve)) if translations: idea_translations(idea=idea, translations=translations['translations'], source_language=idea.language) serializer = IdeaSerializer(idea) return Response(serializer.data, status=status.HTTP_200_OK)