def get_object(self): try: return Client.objects.get(owner=self.request.user, id=self.kwargs["id"]) except Client.DoesNotExist: raise CustomException(404, "The client does not exist.") except ValidationError: raise CustomException(400, "The UUID is not correct.")
def get_object(self): try: return TimelineItemLead.objects.get(owner=self.request.user, id=self.kwargs["id"]) except TimelineItemLead.DoesNotExist: raise CustomException(404, "The timeline item does not exist.") except TimelineItemLead: raise CustomException(400, "The UUID is not correct.")
def perform_create(self, serializer): try: project = Project.objects.get( Q(owner=self.request.user) | Q(users=self.request.user), id=self.kwargs["project_id"], ) return serializer.save(project=project, owner=self.request.user) except Project.DoesNotExist: raise CustomException(404, "The project does not exist.") except ValidationError: raise CustomException(400, "The UUID is not correct.")
def create(self, validated_data): validated_data["owner"] = self.context.get("request").user try: lead_id = (self.context.get("request").parser_context.get( "kwargs").get("lead_id")) lead = Lead.objects.get(owner=self.context.get("request").user, id=lead_id) validated_data["lead"] = lead except Lead.DoesNotExist: raise CustomException(404, "The lead was not found.") except ValidationError: raise CustomException(400, "The UUID is not valid.") return super(TimelineItemLeadsSerializer, self).create(validated_data)
def get_object(self): try: if self.request.method == "GET": return Project.objects.get( Q(owner=self.request.user) | Q(users=self.request.user), id=self.kwargs["id"], ) else: return Project.objects.get( owner=self.request.user, id=self.kwargs["id"] ) except Project.DoesNotExist: raise CustomException(404, "The project does not exist.") except ValidationError: raise CustomException(400, "The UUID is not correct.")
def create(self, validated_data): validated_data["owner"] = self.context.get("request").user try: return super(ClientCreateSerializer, self).create(validated_data) except IntegrityError: raise CustomException( 400, "A client with the specified email already exists.")
def create(self, validated_data): validated_data["owner"] = self.context.get("request").user try: return super(InvoiceCreateUpdateSerializer, self).create(validated_data) except (Project.DoesNotExist, Client.DoesNotExist): raise CustomException(400, "The client or project does not exist.")
def create(self, validated_data): clients = None emails = None validated_data["owner"] = self.context.get("request").user if "clients" in validated_data: clients = Client.objects.filter( email__in=validated_data.pop("clients"), owner=self.context.get("request").user, ) if "users" in validated_data: emails = validated_data.pop( "users") # Notify users that have been added to the project try: instance = super(ProjectCreateUpdateSerializer, self).create(validated_data) if clients: instance.clients.add(*clients) if emails: send_invite_project(emails, instance, self.context.get("request").user) instance.save() return instance except IntegrityError: raise CustomException( 400, "A project with the specified name already exists.")
def update(self, instance, validated_data): try: return super(ClientRetrieveUpdateSerializer, self).update(instance, validated_data) except IntegrityError: raise CustomException( 400, "A client with the specified email already exists.")
def upload_file(data, file_path): file = data.pop("file") if file.size > 2000000: return 400, { "error": "The maximum file size is 2 MB. The current file is " + "{:.0f}".format(file.size / 1000000) + " MB" } if check_file_if_exists(file_path): # avoid overwriting existing file buffer = NonCloseableBufferedReader(file) try: s3 = boto3.resource( "s3", aws_access_key_id=settings.AWS_ACCESS_KEY_ID, aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, ) bucket = s3.Bucket(settings.AWS_STORAGE_BUCKET_NAME) bucket.upload_fileobj(buffer, file_path) except ValueError as e: raise CustomException( 400, "There was an error with the request. Please try again later.", e ) buffer.detach() assert not file.closed data["size"] = file.size data["path"] = file_path file_obj = Files.objects.create(**data) return 200, FileSerializer(file_obj).data return 400, {"error": "A file with that name already exists for this project."}
def get_queryset(self): try: return TimelineItemClient.objects.filter( owner=self.request.user, client=self.kwargs["client_id"] ) except ValidationError: raise CustomException(400, "The UUID is not correct.")
def create(self, validated_data): validated_data["owner"] = self.context.get("request").user try: project_id = ( self.context.get("request") .parser_context.get("kwargs") .get("project_id") ) user = self.context.get("request").user project = Project.objects.get( Q(project__owner=user) | Q(project__owner=user), id=project_id ) validated_data["project"] = project except Project.DoesNotExist: raise CustomException(404, "The client was not found.") except ValidationError: raise CustomException(400, "The UUID is not valid.") return super(ProjectTaskSerializer, self).create(validated_data)
def get_queryset(self): try: return Files.objects.filter( Q(project__owner=self.request.user) | Q(project__users=self.request.user), project__id=self.kwargs.get("project_id"), ) except ValidationError: raise CustomException(400, "The UUID is not correct.")
def generate_response(self, query_set, serializer_obj, request): try: page_data = self.paginate_queryset(query_set, request) except NotFoundError: raise CustomException( 400, "No results were found for the requested page.") serialized_page = serializer_obj(page_data, many=True) return self.get_paginated_response(serialized_page.data)
def create(self, validated_data): validated_data["owner"] = self.context.get("request").user try: client_id = (self.context.get("request").parser_context.get( "kwargs").get("client_id")) client = Client.objects.get(id=client_id, owner=self.context.get("request").user) except Client.DoesNotExist: raise CustomException(404, "The client was not found.") validated_data["client"] = client return super(TimelineItemClientSerializer, self).create(validated_data)
def create(self, request, *args, **kwargs): serializer = FilePostSerializer(data=request.data) if serializer.is_valid(): serializer.validated_data["owner"] = self.request.user try: serializer.validated_data["project"] = Project.objects.get( Q(owner=self.request.user) | Q(users=self.request.user), id=self.kwargs.get("project_id"), ) except Project.DoesNotExist: raise CustomException(400, "The project does not exist") except ValidationError: raise CustomException(400, "The UUID is not valid.") file_directory = "projects/" + self.kwargs.get("project_id") + "/" file_path_within_bucket = file_directory + serializer.validated_data.get( "file").name.replace(" ", "_") status, data = upload_file(serializer.validated_data, file_path_within_bucket) return JsonResponse(data=data, status=status) return JsonResponse(data=serializer.errors, status=400)
def get(self, request, *args, **kwargs): try: file = Files.objects.get(id=self.kwargs.get("id"), owner=self.request.user) url = download_file(file.path) response = HttpResponse(content_type="application/force-download") response[ "Content-Disposition"] = 'attachment; filename="{}"'.format( str(file.name)) response["X-Accel-Redirect"] = url return response except (Files.DoesNotExist, Project.DoesNotExist) as e: raise CustomException(400, "The file does not exist.", e)
def accept_project_invite(user, invite_id=None): """ Add the users with invites to the projects :param user: The user that accepted the invite - Account Object :param invite_id: The ID of the invite. If it's None, this function was called from the Sign Up View :return: list of objects with a response for each invite """ # If invite_id is not None, add the user if invite_id: try: invite = ProjectInvite.objects.get(id=invite_id, email=user.email) invite.add_user(user) return { "message": "You have succesfully been added to the project {}.".format( invite.project.name) } except ProjectInvite.DoesNotExist: raise CustomException(404, "No invite was found.") # If the invite_id is None, this function has been called for Sign Up else: # Find all of the invites and add the user invites = ProjectInvite.objects.filter(email=user.email) response = [] for invite in invites: if invite.add_user(user): response.append({ "message": "You have been added to project {}".format( invite.project.name) }) else: response.append({ "message": "There was a problem with adding you to the project {}. " "Please contact the project owner and try again.".format( invite.project.name) }) return response
def create(self, validated_data): try: validated_data["owner"] = self.context.get("request").user return super(ResumeSerializer, self).create(validated_data) except IntegrityError: raise CustomException(400, "You already have an existing resume.")
def get_object(self): try: return Files.objects.get(id=self.kwargs.get("id"), owner=self.request.user) except Files.DoesNotExist: raise CustomException(404, "The file does not exist.")
def get(self, request, *args, **kwargs): try: response = accept_project_invite(self.request.user, self.kwargs["id"]) return JsonResponse(data=response, status=status.HTTP_200_OK) except ValidationError: raise CustomException(400, "The UUID is not correct.")