def put(self, request, pk, format=None):
        params = request.query_params if len(
            request.data) == 0 else request.data
        context = {}
        serializer = AccountCreateSerializer(data=params,
                                             instance=self.account,
                                             request_obj=request,
                                             account=True)
        if serializer.is_valid():
            account_object = serializer.save()
            previous_assigned_to_users = list(
                account_object.assigned_to.all().values_list("id", flat=True))
            account_object.tags.clear()
            if params.get("tags", ""):
                tags = params.get("tags")
                splitted_tags = tags.split(",")
                for t in splitted_tags:
                    tag = Tags.objects.filter(name=t.lower())
                    if tag:
                        tag = tag[0]
                    else:
                        tag = Tags.objects.create(name=t.lower())
                    account_object.tags.add(tag)
            if params.getlist("contacts", []):
                account_object.contacts.clear()
                account_object.contacts.add(*params.getlist("contacts"))
            if params.getlist("assigned_to", []):
                account_object.assigned_to.clear()
                account_object.assigned_to.add(*params.getlist("assigned_to"))
            else:
                account_object.assigned_to.clear()
            if self.request.FILES.get("account_attachment"):
                attachment = Attachments()
                attachment.created_by = self.request.user
                attachment.file_name = self.request.FILES.get(
                    "account_attachment").name
                attachment.account = account_object
                attachment.attachment = self.request.FILES.get(
                    "account_attachment")
                attachment.save()

            if params.getlist("teams", []):
                account_object.teams.clear()
                account_object.teams.add(*params.getlist("teams"))
            else:
                account_object.teams.clear()
            if params.getlist("teams", []):
                user_ids = Teams.objects.filter(
                    id__in=self.request.POST.getlist("teams")).values_list(
                        "users", flat=True)
                assinged_to_users_ids = account_object.assigned_to.all(
                ).values_list("id", flat=True)
                for user_id in user_ids:
                    if user_id not in assinged_to_users_ids:
                        account_object.assigned_to.add(user_id)

            assigned_to_list = list(
                account_object.assigned_to.all().values_list("id", flat=True))
            current_site = get_current_site(self.request)
            recipients = list(
                set(assigned_to_list) - set(previous_assigned_to_users))
            send_email_to_assigned_user.delay(
                recipients,
                account_object.id,
                domain=current_site.domain,
                protocol=self.request.scheme,
            )
            return Response({
                "error": False,
                "message": "Account Updated Successfully"
            })
        context["errors"] = serializer.errors
        return Response(context, status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
    def put(self, request, pk, format=None):
        params = request.query_params if len(
            request.data) == 0 else request.data
        account_object = self.get_object(pk=pk)
        if account_object.org != request.org:
            return Response(
                {
                    "error": True,
                    "errors": "User company doesnot match with header...."
                },
                status=status.HTTP_403_FORBIDDEN,
            )
        serializer = AccountCreateSerializer(account_object,
                                             data=params,
                                             request_obj=request,
                                             account=True)

        if serializer.is_valid():
            if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin:
                if not ((self.request.profile == account_object.created_by) or
                        (self.request.profile
                         in account_object.assigned_to.all())):
                    return Response(
                        {
                            "error":
                            True,
                            "errors":
                            "You do not have Permission to perform this action",
                        },
                        status=status.HTTP_403_FORBIDDEN,
                    )
            account_object = serializer.save()
            previous_assigned_to_users = list(
                account_object.assigned_to.all().values_list("id", flat=True))

            account_object.contacts.clear()
            if params.get("contacts"):
                contacts_list = json.loads(params.get("contacts"))
                contacts = Contact.objects.filter(id__in=contacts_list,
                                                  org=request.org)
                if contacts:
                    account_object.contacts.add(*contacts)

            account_object.tags.clear()
            if params.get("tags"):
                tags = json.loads(params.get("tags"))
                for tag in tags:
                    tag_obj = Tags.objects.filter(slug=tag.lower())
                    if tag_obj.exists():
                        tag_obj = tag_obj[0]
                    else:
                        tag_obj = Tags.objects.create(name=tag)
                    account_object.tags.add(tag_obj)

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

            account_object.assigned_to.clear()
            if params.get("assigned_to"):
                assigned_to_list = json.loads(params.get("assigned_to"))
                profiles = Profile.objects.filter(id__in=assigned_to_list,
                                                  org=request.org,
                                                  is_active=True)
                if profiles:
                    account_object.assigned_to.add(*profiles)

            if self.request.FILES.get("account_attachment"):
                attachment = Attachments()
                attachment.created_by = self.request.profile
                attachment.file_name = self.request.FILES.get(
                    "account_attachment").name
                attachment.account = account_object
                attachment.attachment = self.request.FILES.get(
                    "account_attachment")
                attachment.save()

            assigned_to_list = list(
                account_object.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,
                account_object.id,
            )
            return Response(
                {
                    "error": False,
                    "message": "Account Updated Successfully"
                },
                status=status.HTTP_200_OK,
            )
        return Response(
            {
                "error": True,
                "errors": serializer.errors
            },
            status=status.HTTP_400_BAD_REQUEST,
        )
Exemple #3
0
    def post(self, request, *args, **kwargs):
        params = request.query_params if len(
            request.data) == 0 else request.data
        serializer = AccountCreateSerializer(data=params,
                                             request_obj=request,
                                             account=True)
        # Save Account
        if serializer.is_valid():
            account_object = serializer.save(created_by=request.profile,
                                             org=request.org)
            if params.get("contacts"):
                contacts_list = json.loads(params.get("contacts"))
                contacts = Contact.objects.filter(id__in=contacts_list,
                                                  org=request.org)
                if contacts:
                    account_object.contacts.add(*contacts)
            if params.get("tags"):
                tags = json.loads(params.get("tags"))
                for tag in tags:
                    tag_obj = Tags.objects.filter(slug=tag.lower())
                    if tag_obj.exists():
                        tag_obj = tag_obj[0]
                    else:
                        tag_obj = Tags.objects.create(name=tag)
                    account_object.tags.add(tag_obj)
            if params.get("teams"):
                teams_list = json.loads(params.get("teams"))
                teams = Teams.objects.filter(id__in=teams_list,
                                             org=request.org)
                if teams:
                    account_object.teams.add(*teams)
                if params.get("assigned_to"):
                    assigned_to_list = json.loads(params.get("assigned_to"))
                    profiles = Profile.objects.filter(id__in=assigned_to_list,
                                                      org=request.org,
                                                      is_active=True)
                    if profiles:
                        account_object.assigned_to.add(*profiles)

            if self.request.FILES.get("account_attachment"):
                attachment = Attachments()
                attachment.created_by = request.profile
                attachment.file_name = request.FILES.get(
                    "account_attachment").name
                attachment.account = account_object
                attachment.attachment = request.FILES.get("account_attachment")
                attachment.save()

            recipients = list(account_object.assigned_to.all().values_list(
                "id", flat=True))
            send_email_to_assigned_user.delay(
                recipients,
                account_object.id,
            )
            return Response(
                {
                    "error": False,
                    "message": "Account Created Successfully"
                },
                status=status.HTTP_200_OK,
            )
        return Response(
            {
                "error": True,
                "errors": serializer.errors
            },
            status=status.HTTP_400_BAD_REQUEST,
        )
Exemple #4
0
    def put(self, request, pk, format=None):
        params = request.query_params if len(
            request.data) == 0 else request.data
        account_object = self.get_object(pk=pk)
        data = {}
        if account_object.company != request.company:
            return Response({
                "error":True,
                "errors":"User company doesnot match with header...."},
                status=status.HTTP_404_NOT_FOUND
            )
        serializer = AccountCreateSerializer(
            account_object, data=params, request_obj=request, account=True
        )

        if serializer.is_valid():
            if self.request.user.role != "ADMIN" and not self.request.user.is_superuser:
                if not (
                    (self.request.user == account_object.created_by)
                    or (self.request.user in account_object.assigned_to.all())
                ):
                    return Response(
                        {"error": True,
                         "errors": "You do not have Permission to perform this action"},
                         status=status.HTTP_401_UNAUTHORIZED,
                    )
            account_object = serializer.save()
            previous_assigned_to_users = list(
                account_object.assigned_to.all().values_list("id", flat=True)
            )
            account_object.contacts.clear()
            if params.get("contacts"):
                contacts = json.loads(params.get("contacts"))               
                for contact in contacts:
                    obj_contact = Contact.objects.filter(id=contact, company=request.company)
                    if obj_contact:
                        account_object.contacts.add(contact)
                    else:
                        data["contacts"] = "Please enter valid Contact"
                        return Response({"error":True, "errors":data})
            account_object.tags.clear()
            if params.get("tags"):
                tags = json.loads(params.get("tags"))
                for tag in tags:
                    tag_obj = Tags.objects.filter(slug=tag.lower())
                    if tag_obj:
                        tag_obj = tag_obj[0]
                    else:
                        tag_obj = Tags.objects.create(name=tag)
                    account_object.tags.add(tag_obj)
                        
            if self.request.user.role == "ADMIN":
                account_object.teams.clear()
                if params.get("teams"):
                    teams = json.loads(params.get("teams"))
                    for team in teams:
                        teams_ids = Teams.objects.filter(id=team,company=request.company)
                        if teams_ids:
                            account_object.teams.add(team)
                        else:
                            data["team"] = "Please enter valid Team"
                            return Response({"error": True, "errors":data})

                account_object.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:                            
                            account_object.assigned_to.add(user_id)
                        else:
                            data["assigned_to"] = "Please enter valid User"
                            return Response({"error": True, "errors":data})
                
            if self.request.FILES.get("account_attachment"):
                attachment = Attachments()
                attachment.created_by = self.request.user
                attachment.file_name = self.request.FILES.get("account_attachment").name
                attachment.account = account_object
                attachment.attachment = self.request.FILES.get("account_attachment")
                attachment.save()

            assigned_to_list = list(
                account_object.assigned_to.all().values_list("id", flat=True)
            )
            current_site = get_current_site(self.request)
            recipients = list(set(assigned_to_list) - set(previous_assigned_to_users))
            send_email_to_assigned_user.delay(
                recipients,
                account_object.id,
                domain=current_site.domain,
                protocol=self.request.scheme,
            )
            return Response(
                {"error": False, "message": "Account Updated Successfully"},
                status=status.HTTP_200_OK,
            )
        return Response(
            {"error": True, "errors": serializer.errors},
             status=status.HTTP_400_BAD_REQUEST
        )
Exemple #5
0
    def post(self, request, *args, **kwargs):
        params = request.query_params if len(request.data) == 0 else request.data
        data = {}
        serializer = AccountCreateSerializer(
            data=params, request_obj=request, account=True
        )
        # Save Account
        if serializer.is_valid():
            account_object = serializer.save(
                created_by=request.user, company=request.company
            )
            if params.get("contacts"):
                contacts = json.loads(params.get("contacts"))                
                for contact in contacts:
                    obj_contact = Contact.objects.filter(id=contact, company=request.company)
                    if obj_contact:
                        account_object.contacts.add(contact)
                    else:
                        account_object.delete()
                        data["contacts"] = "Please enter valid contact"
                        return Response({"error": True, "errors":data})
            if params.get("tags"):
                tags = json.loads(params.get("tags"))
                for tag in tags:
                    tag_obj = Tags.objects.filter(slug=tag.lower())
                    if tag_obj:
                        tag_obj = tag_obj[0]
                    else:
                        tag_obj = Tags.objects.create(name=tag)
                    account_object.tags.add(tag_obj)
            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,company=request.company)
                        if teams_ids:
                            account_object.teams.add(team)
                        else:
                            account_object.delete()
                            data["team"] = "Please enter valid Team"
                            return Response({"error": True, "errors":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:                            
                            account_object.assigned_to.add(user_id)
                        else:
                            account_object.delete()
                            data["assigned_to"] = "Please enter valid user"
                            return Response({"error": True, "errors":data})
           
            if self.request.FILES.get("account_attachment"):
                attachment = Attachments()
                attachment.created_by = request.user
                attachment.file_name = request.FILES.get("account_attachment").name
                attachment.account = account_object
                attachment.attachment = request.FILES.get("account_attachment")
                attachment.save()

            assigned_to_list = list(
                account_object.assigned_to.all().values_list("id", flat=True)
            )

            current_site = get_current_site(request)
            recipients = assigned_to_list
            send_email_to_assigned_user.delay(
                recipients,
                account_object.id,
                domain=current_site.domain,
                protocol=self.request.scheme,
            )
            return Response({"error": False, "message": "Account Created Successfully"})
        return Response(
            {"error": True, "errors": serializer.errors},
             status=status.HTTP_400_BAD_REQUEST
        )
Exemple #6
0
    def post(self, request, *args, **kwargs):
        params = request.query_params if len(
            request.data) == 0 else request.data
        context = {}
        serializer = AccountCreateSerializer(
            data=params, request_obj=request, account=True
        )
        # Save Account
        if serializer.is_valid():
            account_object = serializer.save(
                created_by=request.user, company=request.company)

            if self.request.POST.get("tags", ""):
                tags = params.get("tags")
                splitted_tags = tags.split(",")
                for t in splitted_tags:
                    tag = Tags.objects.filter(name=t)
                    if tag:
                        tag = tag[0]
                    else:
                        tag = Tags.objects.create(name=t)
                    account_object.tags.add(tag)
            if params.getlist("contacts", []):
                account_object.contacts.add(
                    *params.getlist("contacts"))
            if params.getlist("assigned_to", []):
                account_object.assigned_to.add(
                    *params.getlist("assigned_to"))
            if self.request.FILES.get("account_attachment"):
                attachment = Attachments()
                attachment.created_by = request.user
                attachment.file_name = request.FILES.get(
                    "account_attachment").name
                attachment.account = account_object
                attachment.attachment = request.FILES.get(
                    "account_attachment")
                attachment.save()
            if params.getlist("teams", []):
                user_ids = Teams.objects.filter(
                    id__in=params.getlist("teams")
                ).values_list("users", flat=True)
                assinged_to_users_ids = account_object.assigned_to.all().values_list(
                    "id", flat=True
                )
                for user_id in user_ids:
                    if user_id not in assinged_to_users_ids:
                        account_object.assigned_to.add(user_id)
            if params.getlist("teams", []):
                account_object.teams.add(*params.getlist("teams"))

            assigned_to_list = list(
                account_object.assigned_to.all().values_list("id", flat=True)
            )
            current_site = get_current_site(request)
            recipients = assigned_to_list
            send_email_to_assigned_user.delay(
                recipients,
                account_object.id,
                domain=current_site.domain,
                protocol=self.request.scheme,
            )
            return Response({'error': False,
                             'message': 'Account Created Successfully'})
        context["errors"] = serializer.errors
        return Response(context, status=status.HTTP_400_BAD_REQUEST)