Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        params = request.query_params if len(
            request.data) == 0 else request.data
        contact_serializer = CreateContactSerializer(
            data=params, request_obj=request
        )
        address_serializer = BillingAddressSerializer(data=params)

        data = {}
        if not contact_serializer.is_valid():
            data["contact_errors"] = contact_serializer.errors
        if not address_serializer.is_valid():
            data["address_errors"] = (address_serializer.errors,)
        if data:
            return Response(
                {"error": True, "errors": data},
                status=status.HTTP_400_BAD_REQUEST,
            )
        # if contact_serializer.is_valid() and address_serializer.is_valid():
        address_obj = address_serializer.save()
        contact_obj = contact_serializer.save(
            date_of_birth=params.get("date_of_birth")
        )
        contact_obj.address = address_obj
        contact_obj.created_by = self.request.profile
        contact_obj.org = request.org
        contact_obj.save()

        if params.get("teams"):
            teams_list = json.loads(params.get("teams"))
            teams = Teams.objects.filter(
                id__in=teams_list, org=request.org)
            contact_obj.teams.add(*teams)

        if params.get("assigned_to"):
            assinged_to_list = json.loads(
                params.get("assigned_to"))
            profiles = Profile.objects.filter(
                id__in=assinged_to_list, org=request.org)
            contact_obj.assigned_to.add(*profiles)

        recipients = list(
            contact_obj.assigned_to.all().values_list("id", flat=True)
        )
        send_email_to_assigned_user.delay(
            recipients,
            contact_obj.id,
        )

        if request.FILES.get("contact_attachment"):
            attachment = Attachments()
            attachment.created_by = request.profile
            attachment.file_name = request.FILES.get("contact_attachment").name
            attachment.contact = contact_obj
            attachment.attachment = request.FILES.get("contact_attachment")
            attachment.save()
        return Response(
            {"error": False, "message": "Contact created Successfuly"},
            status=status.HTTP_200_OK,
        )
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        self.object = None
        params = request.query_params if len(
            request.data) == 0 else request.data
        contact_serializer = CreateContctForm(data=params)
        address_serializer = BillingAddressSerializer(data=params)
        data = {}
        if not contact_serializer.is_valid():
            data['contact_errors'] = contact_serializer.errors
        if not address_serializer.is_valid():
            data["address_errors"] = address_serializer.errors,
        if data:
            data['error'] = True
            return Response(data)
        address_obj = address_serializer.save()
        contact_obj = contact_serializer.save()
        contact_obj.address = address_obj
        contact_obj.created_by = self.request.user
        contact_obj.company = self.request.company
        contact_obj.save()
        if self.request.GET.get("view_account", None):
            if Account.objects.filter(
                    id=int(self.request.GET.get("view_account"))).exists():
                Account.objects.get(id=int(self.request.GET.get(
                    "view_account"))).contacts.add(contact_obj)
        if params.getlist("assigned_to", []):
            contact_obj.assigned_to.add(*params.getlist("assigned_to"))
        if params.getlist("teams", []):
            user_ids = Teams.objects.filter(
                id__in=params.getlist("teams")).values_list("users", flat=True)
            assinged_to_users_ids = contact_obj.assigned_to.all().values_list(
                "id", flat=True)
            for user_id in user_ids:
                if user_id not in assinged_to_users_ids:
                    contact_obj.assigned_to.add(user_id)

        if params.getlist("teams", []):
            contact_obj.teams.add(*params.getlist("teams"))

        assigned_to_list = list(contact_obj.assigned_to.all().values_list(
            "id", flat=True))
        current_site = get_current_site(self.request)
        recipients = assigned_to_list
        send_email_to_assigned_user.delay(
            recipients,
            contact_obj.id,
            domain=current_site.domain,
            protocol=self.request.scheme,
        )

        if request.FILES.get("contact_attachment"):
            attachment = Attachments()
            attachment.created_by = request.user
            attachment.file_name = request.FILES.get("contact_attachment").name
            attachment.contact = contact_obj
            attachment.attachment = request.FILES.get("contact_attachment")
            attachment.save()

        return Response({'error': False})
Ejemplo n.º 3
0
    def put(self, request, pk, format=None):
        params = request.query_params if len(
            request.data) == 0 else request.data
        invoice_obj = self.get_object(pk=pk)
        from_address_obj = invoice_obj.from_address
        to_address_obj = invoice_obj.to_address
        data = {}
        if invoice_obj.company != request.company:
            return Response(
                {
                    "error": True,
                    "errors": "User company doesnot match with header...."
                },
                status=status.HTTP_404_NOT_FOUND,
            )
        if self.request.user.role != "ADMIN" and not self.request.user.is_superuser:
            if not ((self.request.user == invoice_obj.created_by) or
                    (self.request.user in invoice_obj.assigned_to.all())):
                return Response(
                    {
                        "error":
                        True,
                        "errors":
                        "You don't have Permission to perform this action",
                    },
                    status=status.HTTP_401_UNAUTHORIZED,
                )

        serializer = InvoiceCreateSerializer(
            invoice_obj,
            data=params,
            request_obj=request,
            invoice=True,
        )
        from_address_serializer = BillingAddressSerializer(
            data=params, instance=from_address_obj)
        to_address_serializer = BillingAddressSerializer(
            data=params, instance=to_address_obj)
        if not from_address_serializer.is_valid():
            data["from_address_errors"] = from_address_serializer.errors
        if not to_address_serializer.is_valid():
            data["to_address_errors"] = to_address_serializer.errors
        if data:
            return Response({"error": True}, data)
        if serializer.is_valid():
            invoice_obj = serializer.save()
            previous_assigned_to_users = list(
                invoice_obj.assigned_to.all().values_list("id", flat=True))
            from_address_obj = from_address_serializer.save(
                address_line=params.get("from_address_line"),
                street=params.get("from_street"),
                city=params.get("from_city"),
                state=params.get("from_state"),
                postcode=params.get("from_postcode"),
                country=params.get("from_country"),
            )
            to_address_obj = to_address_serializer.save(
                address_line=params.get("to_address_line"),
                street=params.get("to_street"),
                city=params.get("to_city"),
                state=params.get("to_state"),
                postcode=params.get("to_postcode"),
                country=params.get("to_country"),
            )
            invoice_obj.from_address = from_address_obj
            invoice_obj.to_address = to_address_obj

            quality_hours = int(params.get("quality_hours"))
            rate = float(params.get("rate"))
            quantity = quality_hours * rate
            tax = quantity * float(params.get("tax")) / 100
            invoice_obj.total_amount = quantity + tax
            invoice_obj.save()

            invoice_obj.accounts.clear()
            if params.get("accounts"):
                accounts = json.loads(params.get("accounts"))
                for account in accounts:
                    obj_account = Account.objects.filter(
                        id=account, company=request.company)
                    if obj_account.exists():
                        invoice_obj.accounts.add(account)
                    else:
                        data["accounts"] = "Please enter valid account"
                        return Response({"error": True}, data)

            if self.request.user.role == "ADMIN":
                invoice_obj.teams.clear()
                if params.get("teams"):
                    teams = json.loads(params.get("teams"))
                    for team in teams:
                        obj_team = Teams.objects.filter(
                            id=team, company=request.company)
                        if obj_team.exists():
                            invoice_obj.teams.add(team)
                        else:
                            data["team"] = "Please enter valid Team"
                            return Response({"error": True}, data)

                invoice_obj.assigned_to.clear()
                if params.get("assigned_to"):
                    assinged_to_users_ids = json.loads(
                        params.get("assigned_to"))
                    for user_id in assinged_to_users_ids:
                        user = User.objects.filter(id=user_id,
                                                   company=request.company)
                        if user.exists():
                            invoice_obj.assigned_to.add(user_id)
                        else:
                            data["assigned_to"] = "Please enter valid User"
                            return Response({"error": True}, data)

            assigned_to_list = list(invoice_obj.assigned_to.all().values_list(
                "id", flat=True))
            recipients = list(
                set(assigned_to_list) - set(previous_assigned_to_users))
            send_email.delay(
                recipients,
                invoice_obj.id,
                domain=settings.DOMAIN_NAME,
                protocol=self.request.scheme,
            )
            return Response(
                {
                    "error": False,
                    "message": "Invoice Updated Successfully"
                },
                status=status.HTTP_200_OK,
            )
        return Response(
            {
                "error": True,
                "errors": serializer.errors
            },
            status=status.HTTP_400_BAD_REQUEST,
        )
Ejemplo n.º 4
0
    def post(self, request, *args, **kwargs):
        params = request.query_params if len(
            request.data) == 0 else request.data
        data = {}
        serializer = InvoiceCreateSerializer(data=params, request_obj=request)
        from_address_serializer = BillingAddressSerializer(data=params)
        to_address_serializer = BillingAddressSerializer(data=params)
        if not from_address_serializer.is_valid():
            data["from_address_errors"] = from_address_serializer.errors
        if not to_address_serializer.is_valid():
            data["to_address_errors"] = to_address_serializer.errors
        if data:
            return Response({"error": True}, data)

        if serializer.is_valid():
            quality_hours = int(params.get("quality_hours"))
            rate = float(params.get("rate"))
            quantity = quality_hours * rate
            tax = quantity * float(params.get("tax")) / 100
            total_amount = quantity + tax

            from_address_obj = from_address_serializer.save(
                address_line=params.get("from_address_line"),
                street=params.get("from_street"),
                city=params.get("from_city"),
                state=params.get("from_state"),
                postcode=params.get("from_postcode"),
                country=params.get("from_country"),
            )
            to_address_obj = to_address_serializer.save(
                address_line=params.get("to_address_line"),
                street=params.get("to_street"),
                city=params.get("to_city"),
                state=params.get("to_state"),
                postcode=params.get("to_postcode"),
                country=params.get("to_country"),
            )

            invoice_obj = serializer.save(
                created_by=request.user,
                company=request.company,
                quantity=params.get("quality_hours"),
                total_amount=total_amount,
                from_address_id=from_address_obj.id,
                to_address_id=to_address_obj.id,
            )

            if params.get("accounts"):
                accounts = json.loads(params.get("accounts"))
                for account in accounts:
                    obj_account = Account.objects.filter(
                        id=account, company=request.company)
                    if obj_account.exists():
                        invoice_obj.accounts.add(account)
                    else:
                        invoice_obj.delete()
                        data["accounts"] = "Please enter valid account"
                        return Response({"error": True}, data)

            if self.request.user.role == "ADMIN":
                if params.get("teams"):
                    teams = json.loads(params.get("teams"))
                    for team in teams:
                        obj_team = Teams.objects.filter(
                            id=team, company=request.company)
                        if obj_team.exists():
                            invoice_obj.teams.add(team)
                        else:
                            invoice_obj.delete()
                            data["team"] = "Please enter valid Team"
                            return Response({"error": True}, data)
                if params.get("assigned_to"):
                    assinged_to_users_ids = json.loads(
                        params.get("assigned_to"))

                    for user_id in assinged_to_users_ids:
                        user = User.objects.filter(id=user_id,
                                                   company=request.company)
                        if user.exists():
                            invoice_obj.assigned_to.add(user_id)
                        else:
                            invoice_obj.delete()
                            data["assigned_to"] = "Please enter valid user"
                            return Response({"error": True}, data)
            create_invoice_history(invoice_obj.id, request.user.id, [])
            assigned_to_list = list(invoice_obj.assigned_to.all().values_list(
                "id", flat=True))

            recipients = assigned_to_list
            send_email.delay(
                recipients,
                invoice_obj.id,
                domain=settings.DOMAIN_NAME,
                protocol=self.request.scheme,
            )
            return Response({
                "error": False,
                "message": "Invoice Created Successfully"
            })
        return Response(
            {
                "error": True,
                "errors": serializer.errors
            },
            status=status.HTTP_400_BAD_REQUEST,
        )
Ejemplo n.º 5
0
    def put(self, request, pk, format=None):
        params = request.query_params if len(
            request.data) == 0 else request.data
        contact_obj = self.get_object(pk=pk)
        address_obj = contact_obj.address
        if contact_obj.org != request.org:
            return Response(
                {
                    "error": True,
                    "errors": "User company doesnot match with header...."
                },
                status=status.HTTP_403_FORBIDDEN)
        contact_serializer = CreateContactSerializer(data=params,
                                                     instance=contact_obj,
                                                     request_obj=request,
                                                     contact=True)
        address_serializer = BillingAddressSerializer(data=params,
                                                      instance=address_obj)
        data = {}
        if not contact_serializer.is_valid():
            data["contact_errors"] = contact_serializer.errors
        if not address_serializer.is_valid():
            data["address_errors"] = (address_serializer.errors, )
        if data:
            data["error"] = True
            return Response(
                data,
                status=status.HTTP_400_BAD_REQUEST,
            )

        if contact_serializer.is_valid():
            if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin:
                if not (
                    (self.request.profile == contact_obj.created_by) or
                    (self.request.profile in contact_obj.assigned_to.all())):
                    return Response(
                        {
                            "error":
                            True,
                            "errors":
                            "You do not have Permission to perform this action",
                        },
                        status=status.HTTP_403_FORBIDDEN,
                    )
            address_obj = address_serializer.save()
            contact_obj = contact_serializer.save(
                date_of_birth=params.get("date_of_birth"))
            contact_obj.address = address_obj
            contact_obj.save()
            contact_obj = contact_serializer.save()
            contact_obj.teams.clear()
            if params.get("teams"):
                teams_list = json.loads(params.get("teams"))
                teams = Teams.objects.filter(id__in=teams_list,
                                             org=request.org)
                contact_obj.teams.add(*teams)

            contact_obj.assigned_to.clear()
            if params.get("assigned_to"):
                assinged_to_list = json.loads(params.get("assigned_to"))
                profiles = Profile.objects.filter(id__in=assinged_to_list,
                                                  org=request.org)
                contact_obj.assigned_to.add(*profiles)

            previous_assigned_to_users = list(
                contact_obj.assigned_to.all().values_list("id", flat=True))

            assigned_to_list = list(contact_obj.assigned_to.all().values_list(
                "id", flat=True))
            recipients = list(
                set(assigned_to_list) - set(previous_assigned_to_users))
            send_email_to_assigned_user.delay(
                recipients,
                contact_obj.id,
            )
            if request.FILES.get("contact_attachment"):
                attachment = Attachments()
                attachment.created_by = request.profile
                attachment.file_name = request.FILES.get(
                    "contact_attachment").name
                attachment.contact = contact_obj
                attachment.attachment = request.FILES.get("contact_attachment")
                attachment.save()
            return Response(
                {
                    "error": False,
                    "message": "Contact Updated Successfully"
                },
                status=status.HTTP_200_OK,
            )
Ejemplo n.º 6
0
    def put(self, request, pk, format=None):
        params = request.query_params if len(
            request.data) == 0 else request.data
        self.object = self.get_object(pk)
        address_obj = self.object.address
        contact_serializer = CreateContctForm(
            data=params, instance=self.object)
        address_serializer = BillingAddressSerializer(
            data=params, instance=address_obj)
        data = {}
        if not contact_serializer.is_valid():
            data['contact_errors'] = contact_serializer.errors
        if not address_serializer.is_valid():
            data["address_errors"] = address_serializer.errors,
        if data:
            data['error'] = True
            return Response(data)
        addres_obj = address_serializer.save()
        contact_obj = contact_serializer.save()
        contact_obj.address = addres_obj
        contact_obj.save()

        previous_assigned_to_users = list(
            contact_obj.assigned_to.all().values_list("id", flat=True)
        )
        if params.getlist("assigned_to", []):
            current_site = get_current_site(request)

            contact_obj.assigned_to.clear()
            contact_obj.assigned_to.add(
                *params.getlist("assigned_to"))
        else:
            contact_obj.assigned_to.clear()

        if params.getlist("teams", []):
            user_ids = Teams.objects.filter(
                id__in=params.getlist("teams")
            ).values_list("users", flat=True)
            assinged_to_users_ids = contact_obj.assigned_to.all().values_list(
                "id", flat=True
            )
            for user_id in user_ids:
                if user_id not in assinged_to_users_ids:
                    contact_obj.assigned_to.add(user_id)

        if params.getlist("teams", []):
            contact_obj.teams.clear()
            contact_obj.teams.add(*params.getlist("teams"))
        else:
            contact_obj.teams.clear()

        current_site = get_current_site(request)
        assigned_to_list = list(
            contact_obj.assigned_to.all().values_list("id", flat=True)
        )
        recipients = list(set(assigned_to_list) -
                          set(previous_assigned_to_users))
        send_email_to_assigned_user.delay(
            recipients,
            contact_obj.id,
            domain=current_site.domain,
            protocol=request.scheme,
        )
        if request.FILES.get("contact_attachment"):
            attachment = Attachments()
            attachment.created_by = request.user
            attachment.file_name = request.FILES.get(
                "contact_attachment").name
            attachment.contact = contact_obj
            attachment.attachment = request.FILES.get(
                "contact_attachment")
            attachment.save()
        return Response({'error': False})
Ejemplo n.º 7
0
    def put(self, request, pk, format=None):
        params = request.query_params if len(request.data) == 0 else request.data
        contact_obj = self.get_object(pk=pk)
        address_obj = contact_obj.address

        contact_serializer = CreateContactSerializer(
            data=params, instance=contact_obj, request_obj=request, contact=True
        )
        address_serializer = BillingAddressSerializer(data=params, instance=address_obj)
        data = {}
        if not contact_serializer.is_valid():
            data["contact_errors"] = contact_serializer.errors
        if not address_serializer.is_valid():
            data["address_errors"] = (address_serializer.errors,)
        if data:
            data["error"] = True
            return Response(
                data,
                status=status.HTTP_400_BAD_REQUEST,
            )

        if contact_serializer.is_valid():
            if self.request.user.role != "ADMIN" and not self.request.user.is_superuser:
                if not (
                    (self.request.user == contact_obj.created_by)
                    or (self.request.user in contact_obj.assigned_to.all())
                ):
                    return Response(
                        {
                            "error": True,
                            "errors": "You do not have Permission to perform this action",
                        },
                        status=status.HTTP_403_FORBIDDEN,
                    )
            address_obj = address_serializer.save()
            contact_obj = contact_serializer.save()
            contact_obj.address = address_obj
            contact_obj.save()
            contact_obj = contact_serializer.save()
            if self.request.user.role == "ADMIN":
                contact_obj.teams.clear()
                if params.get("teams"):
                    teams = json.loads(params.get("teams"))
                    for team in teams:
                        teams_ids = Teams.objects.filter(id=team)
                        if teams_ids.exists():
                            contact_obj.teams.add(team)
                        else:
                            data["team"] = "Please enter valid Team"
                            return Response(
                                {"error": True, "errors": data},
                                status=status.HTTP_400_BAD_REQUEST,
                            )

                contact_obj.assigned_to.clear()
                if params.get("assigned_to"):
                    assinged_to_users_ids = json.loads(params.get("assigned_to"))
                    for user_id in assinged_to_users_ids:
                        user = User.objects.filter(id=user_id)
                        if user.exists():
                            contact_obj.assigned_to.add(user_id)
                        else:
                            data["assigned_to"] = "Please enter valid user"
                            return Response(
                                {"error": True, "errors": data},
                                status=status.HTTP_400_BAD_REQUEST,
                            )

            previous_assigned_to_users = list(
                contact_obj.assigned_to.all().values_list("id", flat=True)
            )

            assigned_to_list = list(
                contact_obj.assigned_to.all().values_list("id", flat=True)
            )
            recipients = list(set(assigned_to_list) - set(previous_assigned_to_users))
            send_email_to_assigned_user.delay(
                recipients,
                contact_obj.id,
                domain=settings.Domain,
                protocol=request.scheme,
            )
            if request.FILES.get("contact_attachment"):
                attachment = Attachments()
                attachment.created_by = request.user
                attachment.file_name = request.FILES.get("contact_attachment").name
                attachment.contact = contact_obj
                attachment.attachment = request.FILES.get("contact_attachment")
                attachment.save()
            return Response(
                {"error": False, "message": "Contact Updated Successfully"},
                status=status.HTTP_200_OK,
            )
Ejemplo n.º 8
0
    def post(self, request, *args, **kwargs):
        params = request.query_params if len(request.data) == 0 else request.data
        contact_serializer = CreateContactSerializer(
            data=params, request_obj=request, contact=True
        )
        address_serializer = BillingAddressSerializer(data=params)

        data = {}
        if not contact_serializer.is_valid():
            data["contact_errors"] = contact_serializer.errors
        if not address_serializer.is_valid():
            data["address_errors"] = (address_serializer.errors,)
        if data:
            return Response(
                {"error": True, "errors": data},
                status=status.HTTP_400_BAD_REQUEST,
            )
        # if contact_serializer.is_valid() and address_serializer.is_valid():
        address_obj = address_serializer.save()
        contact_obj = contact_serializer.save()
        contact_obj.address = address_obj
        contact_obj.created_by = self.request.user
        contact_obj.save()

        if self.request.user.role == "ADMIN":
            if params.get("teams"):
                teams = json.loads(params.get("teams"))
                for team in teams:
                    teams_ids = Teams.objects.filter(id=team)
                    if teams_ids.exists():
                        contact_obj.teams.add(team)
                    else:
                        contact_obj.delete()
                        data["team"] = "Please enter valid Team"
                        return Response(
                            {"error": True, "errors": data},
                            status=status.HTTP_400_BAD_REQUEST,
                        )

                assinged_to_users_ids = json.loads(params.get("assigned_to"))
                for user_id in assinged_to_users_ids:
                    user = User.objects.filter(id=user_id)
                    if user.exists():
                        contact_obj.assigned_to.add(user_id)
                    else:
                        contact_obj.delete()
                        data["assigned_to"] = "Please enter valid user"
                        return Response(
                            {"error": True, "errors": data},
                            status=status.HTTP_400_BAD_REQUEST,
                        )

        assigned_to_list = list(
            contact_obj.assigned_to.all().values_list("id", flat=True)
        )
        recipients = assigned_to_list
        send_email_to_assigned_user.delay(
            recipients,
            contact_obj.id,
            domain=settings.Domain,
            protocol=self.request.scheme,
        )

        if request.FILES.get("contact_attachment"):
            attachment = Attachments()
            attachment.created_by = request.user
            attachment.file_name = request.FILES.get("contact_attachment").name
            attachment.contact = contact_obj
            attachment.attachment = request.FILES.get("contact_attachment")
            attachment.save()
        return Response(
            {"error": False, "message": "Contact created Successfuly"},
            status=status.HTTP_200_OK,
        )