def approve_space(request, id): reviewer = RequestUtils.get_request_profile(request=request) try: space = IdeaSpace.objects.get(id=id) if space.approved: return Response({ 'error': 'The space had been approved' }, status=status.HTTP_400_BAD_REQUEST) space.reviewer = reviewer space.approved = True space.enabled = True space.rejected = False space.save() MailService.send_space_approval_message(space=space) serializer = IdeaSpaceSerializer(space) return Response(data=serializer.data, status=status.HTTP_200_OK) except (ObjectDoesNotExist, ValidationError): return Response({ 'error': 'invalid space id' }, status=status.HTTP_400_BAD_REQUEST)
def propose_idea_space(request): owner = RequestUtils.get_request_profile(request=request) serializer = IdeaSpaceSerializer(data=request.data) serializer.is_valid(raise_exception=True) idea_space_data = serializer.data idea_space_data.update({ 'owner': owner }) idea_space = IdeaSpace.objects.create(**idea_space_data) idea_space_serializer = IdeaSpaceSerializer(idea_space) return Response(data=idea_space_serializer.data, status=status.HTTP_201_CREATED)
def update_my_profile(request): profile = RequestUtils.get_request_profile(request=request) request_data = filter_out_empty_keys(request.data) serializer = UpdateProfileSerializer(data=request_data) serializer.is_valid(raise_exception=True) Profile.objects.filter(id=profile.id).update(**serializer.data) updated_profile = Profile.objects.get(id=profile.id) profile_serializer = UserProfileSerializer(updated_profile) return Response(data=profile_serializer.data, status=status.HTTP_200_OK)
def update_my_location(request): profile = RequestUtils.get_request_profile(request=request) city_id = request.data.get('city_id') try: city = City.objects.get(id=city_id) profile.city = city profile.save() serializer = UserProfileSerializer(profile) return Response(data=serializer.data, status=status.HTTP_200_OK) except (ObjectDoesNotExist, ValidationError): return Response({ 'error': 'Invalid city id' }, status=status.HTTP_400_BAD_REQUEST)
def create_idea(request, id): owner = RequestUtils.get_request_profile(request=request) serializer = IdeaSerializer(data=request.data) serializer.is_valid(raise_exception=True) try: data = serializer.data space = IdeaSpace.objects.get(id=id, enabled=True) data.update({'owner': owner, 'idea_space': space}) idea = Idea.objects.create(**data) idea_serializer = IdeaSerializer(idea) return Response(data=idea_serializer.data, status=status.HTTP_201_CREATED) except (ObjectDoesNotExist, ValidationError): return Response({'error': 'the space id is invalid'}, status=status.HTTP_400_BAD_REQUEST)
def reject_space(request, id): reviewer = RequestUtils.get_request_profile(request=request) serializer = RejectionSerializer(data=request.data) serializer.is_valid(raise_exception=True) data = request.data try: rejection_reason = SPACE_REJECTION_REASON_CATEGORIES.get( data.get('rejection_category') ) provided_reason = data.get('reason') if not rejection_reason and not provided_reason: return Response({ 'error': 'Please provide a reason for rejecting the space.' }, status=status.HTTP_400_BAD_REQUEST) reason = rejection_reason if rejection_reason else provided_reason space = IdeaSpace.objects.get(id=id) if space.rejected: return Response({ 'error': 'The space had been rejected' }, status=status.HTTP_400_BAD_REQUEST) space.rejected = True space.approved = False space.enabled = False space.reason_for_rejection = reason space.reviewer = reviewer space.save() MailService.send_space_rejection_message(space=space, reason=reason) space_serializer = IdeaSpaceSerializer(space) return Response(data=space_serializer.data, status=status.HTTP_200_OK) except(ObjectDoesNotExist, ValidationError): return Response({ 'error': 'invalid space id' }, status=status.HTTP_400_BAD_REQUEST)
def fetch_my_profile(request): profile = RequestUtils.get_request_profile(request=request) serializer = UserProfileSerializer(profile) return Response(data=serializer.data, status=status.HTTP_200_OK)