示例#1
0
 def get_context_data(self, **kwargs):
     params = self.request.query_params if len(
         self.request.data) == 0 else request.data
     context = {}
     context["contact_obj"] = ContactSerializer(self.contact).data
     user_assgn_list = [
         assigned_to.id for assigned_to in self.contact.assigned_to.all()
     ]
     if self.request.user.role == "ADMIN" or self.request.user.is_superuser:
         self.users = User.objects.filter(
             is_active=True, company=self.request.company).order_by("email")
     else:
         self.users = User.objects.filter(
             role="ADMIN", company=self.request.company).order_by("email")
     if self.request.user == self.contact.created_by:
         user_assgn_list.append(self.request.user.id)
     if self.request.user.role != "ADMIN" and not self.request.user.is_superuser:
         if self.request.user.id not in user_assgn_list:
             raise PermissionDenied
     context["address_obj"] = BillingAddressSerializer(
         self.contact.address).data
     context["users"] = UserSerializer(self.users, many=True).data
     context["countries"] = COUNTRIES
     context["teams"] = TeamsSerializer(
         Teams.objects.filter(company=self.request.company), many=True).data
     context["assignedto_list"] = [
         int(i) for i in params.getlist("assigned_to", []) if i
     ]
     return context
示例#2
0
class ContactSerializer(serializers.ModelSerializer):
    created_by = UserSerializer()
    company = CompanySerializer()
    teams = TeamsSerializer(read_only=True, many=True)
    assigned_to = UserSerializer(read_only=True, many=True)
    address = BillingAddressSerializer(read_only=True)
    get_team_users = UserSerializer(read_only=True, many=True)
    get_team_and_assigned_users = UserSerializer(read_only=True, many=True)
    get_assigned_users_not_in_teams = UserSerializer(read_only=True, many=True)

    class Meta:
        model = Contact
        fields = (
            'id',
            "first_name",
            "last_name",
            "email",
            "phone",
            "address",
            "description",
            "assigned_to",
            "created_by",
            "created_on",
            "is_active",
            "teams",
            "company",
            "created_on_arrow",
            "get_team_users",
            "get_team_and_assigned_users",
            "get_assigned_users_not_in_teams",
        )
示例#3
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,
        )
示例#4
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})
示例#5
0
class InvoiceSerailizer(serializers.ModelSerializer):
    from_address = BillingAddressSerializer()
    to_address = BillingAddressSerializer()
    created_by = UserSerializer()
    org = OrganizationSerializer()
    teams = TeamsSerializer(read_only=True, many=True)
    assigned_to = UserSerializer(read_only=True, many=True)

    class Meta:
        model = Invoice
        fields = (
            "id",
            "invoice_title",
            "invoice_number",
            "status",
            "due_date",
            "name",
            "email",
            "phone",
            "from_address",
            "to_address",
            "created_on",
            "created_by",
            "currency",
            "quantity",
            "rate",
            "tax",
            "total_amount",
            "amount_due",
            "amount_paid",
            "is_email_sent",
            "details",
            "teams",
            "assigned_to",
            "org",
        )
示例#6
0
class ContactSerializer(serializers.ModelSerializer):
    created_by = ProfileSerializer()
    teams = TeamsSerializer(read_only=True, many=True)
    assigned_to = ProfileSerializer(read_only=True, many=True)
    address = BillingAddressSerializer(read_only=True)
    get_team_users = ProfileSerializer(read_only=True, many=True)
    get_team_and_assigned_users = ProfileSerializer(read_only=True, many=True)
    get_assigned_users_not_in_teams = ProfileSerializer(read_only=True, many=True)
    contact_attachment = AttachmentsSerializer(read_only=True, many=True)
    date_of_birth = serializers.DateField()
    org = OrganizationSerializer()

    class Meta:
        model = Contact
        fields = (
            "id",
            "salutation",
            "first_name",
            "last_name",
            "date_of_birth",
            "organization",
            "title",
            "primary_email",
            "secondary_email",
            "mobile_number",
            "secondary_number",
            "department",
            "language",
            "do_not_call",
            "address",
            "description",
            "linked_in_url",
            "facebook_url",
            "twitter_username",
            "contact_attachment",
            "assigned_to",
            "created_by",
            "created_on",
            "is_active",
            "teams",
            "created_on_arrow",
            "get_team_users",
            "get_team_and_assigned_users",
            "get_assigned_users_not_in_teams",
            "org"
        )
示例#7
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,
        )
示例#8
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,
        )
示例#9
0
    def get(self, request, pk, format=None):
        context = {}
        contact_obj = self.get_object(pk)
        context["contact_obj"] = ContactSerializer(contact_obj).data
        user_assgn_list = [
            assigned_to.id for assigned_to in contact_obj.assigned_to.all()
        ]
        user_assigned_accounts = set(
            self.request.profile.account_assigned_users.values_list("id",
                                                                    flat=True))
        contact_accounts = set(
            contact_obj.account_contacts.values_list("id", flat=True))
        if user_assigned_accounts.intersection(contact_accounts):
            user_assgn_list.append(self.request.profile.id)
        if self.request.profile == contact_obj.created_by:
            user_assgn_list.append(self.request.profile.id)
        if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin:
            if self.request.profile.id not in user_assgn_list:
                return Response(
                    {
                        "error":
                        True,
                        "errors":
                        "You do not have Permission to perform this action",
                    },
                    status=status.HTTP_403_FORBIDDEN,
                )
        assigned_data = []
        for each in contact_obj.assigned_to.all():
            assigned_dict = {}
            assigned_dict["id"] = each.id
            assigned_dict["name"] = each.email
            assigned_data.append(assigned_dict)

        if self.request.profile.is_admin or self.request.profile.role == "ADMIN":
            users_mention = list(
                Profile.objects.filter(
                    is_active=True, org=request.org).values("user__username"))
        elif self.request.profile != contact_obj.created_by:
            users_mention = [{
                "username": contact_obj.created_by.user.username
            }]
        else:
            users_mention = list(
                contact_obj.assigned_to.all().values("username"))

        if request.profile == contact_obj.created_by:
            user_assgn_list.append(self.request.profile.id)

        context["address_obj"] = BillingAddressSerializer(
            contact_obj.address).data
        context["countries"] = COUNTRIES
        context.update({
            "comments":
            CommentSerializer(contact_obj.contact_comments.all(),
                              many=True).data,
            "attachments":
            AttachmentsSerializer(contact_obj.contact_attachment.all(),
                                  many=True).data,
            "assigned_data":
            assigned_data,
            "tasks":
            TaskSerializer(contact_obj.contacts_tasks.all(), many=True).data,
            "users_mention":
            users_mention,
        })
        return Response(context)
示例#10
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,
            )
示例#11
0
    def get(self, request, pk, format=None):
        context = {}
        contact_obj = self.get_object(pk)
        context["contact_obj"] = ContactSerializer(contact_obj).data
        user_assgn_list = [
            assigned_to.id for assigned_to in contact_obj.assigned_to.all()
        ]
        user_assigned_accounts = set(
            self.request.user.account_assigned_users.values_list(
                "id", flat=True)
        )
        contact_accounts = set(
            contact_obj.account_contacts.values_list("id", flat=True)
        )
        if user_assigned_accounts.intersection(contact_accounts):
            user_assgn_list.append(self.request.user.id)
        if self.request.user == contact_obj.created_by:
            user_assgn_list.append(self.request.user.id)
        if self.request.user.role != "ADMIN" and not self.request.user.is_superuser:
            if self.request.user.id not in user_assgn_list:
                raise PermissionDenied
        assigned_data = []
        for each in contact_obj.assigned_to.all():
            assigned_dict = {}
            assigned_dict["id"] = each.id
            assigned_dict["name"] = each.email
            assigned_data.append(assigned_dict)

        if self.request.user.is_superuser or self.request.user.role == "ADMIN":
            users_mention = list(
                User.objects.filter(
                    is_active=True, company=self.request.company
                ).values("username")
            )
        elif self.request.user != contact_obj.created_by:
            users_mention = [
                {"username": contact_obj.created_by.username}]
        else:
            users_mention = list(
                contact_obj.assigned_to.all().values("username"))

        if self.request.user.role == "ADMIN" or self.request.user.is_superuser:
            self.users = User.objects.filter(
                is_active=True, company=self.request.company).order_by("email")
        else:
            self.users = User.objects.filter(
                role="ADMIN", company=self.request.company).order_by("email")
        if request.user == contact_obj.created_by:
            user_assgn_list.append(self.request.user.id)

        context["address_obj"] = BillingAddressSerializer(
            contact_obj.address).data
        context["users"] = UserSerializer(self.users, many=True).data
        context["countries"] = COUNTRIES
        context["teams"] = TeamsSerializer(
            Teams.objects.filter(company=request.company), many=True).data
        context.update(
            {
                "comments": CommentSerializer(
                    contact_obj.contact_comments.all(), many=True).data,
                "attachments": AttachmentsSerializer(
                    contact_obj.contact_attachment.all(), many=True).data,
                "assigned_data": assigned_data,
                "tasks": TaskSerializer(
                    contact_obj.contacts_tasks.all(), many=True).data,
                "users_mention": users_mention,
            }
        )
        return Response(context)
示例#12
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})
示例#13
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,
            )
示例#14
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,
        )