Example #1
0
    def post(self, request, *args, **kwargs):
        params = request.query_params if len(
            request.data) == 0 else request.data
        serializer = CaseCreateSerializer(data=params, request_obj=request)
        if serializer.is_valid():
            cases_obj = serializer.save(
                created_by=request.profile,
                org=request.org,
                closed_on=params.get("closed_on"),
                case_type=params.get("type_of_case"),
            )

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

            if params.get("teams"):
                teams_list = json.loads(params.get("teams"))
                teams = Teams.objects.filter(
                    id__in=teams_list, org=request.org)
                if teams.exists():
                    cases_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, is_active=True)
                if profiles:
                    cases_obj.assigned_to.add(*profiles)

            if self.request.FILES.get("case_attachment"):
                attachment = Attachments()
                attachment.created_by = self.request.profile
                attachment.file_name = self.request.FILES.get(
                    "case_attachment").name
                attachment.cases = cases_obj
                attachment.attachment = self.request.FILES.get(
                    "case_attachment")
                attachment.save()

            recipients = list(
                cases_obj.assigned_to.all().values_list("id", flat=True)
            )
            send_email_to_assigned_user.delay(
                recipients,
                cases_obj.id,
            )
            return Response(
                {"error": False, "message": "Case Created Successfully"},
                status=status.HTTP_200_OK,
            )

        return Response(
            {"error": True, "errors": serializer.errors},
            status=status.HTTP_400_BAD_REQUEST,
        )
Example #2
0
    def put(self, request, pk, format=None):
        params = request.query_params if len(
            request.data) == 0 else request.data
        cases_object = self.get_object(pk=pk)
        if cases_object.org != request.org:
            return Response(
                {"error": True, "errors": "User company doesnot match with header...."},
                status=status.HTTP_403_FORBIDDEN,
            )
        if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin:
            if not (
                (self.request.profile == cases_object.created_by)
                or (self.request.profile in cases_object.assigned_to.all())
            ):
                return Response(
                    {
                        "error": True,
                        "errors": "You do not have Permission to perform this action",
                    },
                    status=status.HTTP_403_FORBIDDEN,
                )

        serializer = CaseCreateSerializer(
            cases_object,
            data=params,
            request_obj=request,
        )

        if serializer.is_valid():
            cases_object = serializer.save(
                closed_on=params.get("closed_on"), case_type=params.get("type_of_case")
            )
            previous_assigned_to_users = list(
                cases_object.assigned_to.all().values_list("id", flat=True)
            )
            cases_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:
                    cases_object.contacts.add(*contacts)

            cases_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.exists():
                    cases_object.teams.add(*teams)

            cases_object.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, is_active=True)
                if profiles:
                    cases_object.assigned_to.add(*profiles)

            if self.request.FILES.get("case_attachment"):
                attachment = Attachments()
                attachment.created_by = self.request.profile
                attachment.file_name = self.request.FILES.get(
                    "case_attachment").name
                attachment.case = cases_object
                attachment.attachment = self.request.FILES.get(
                    "case_attachment")
                attachment.save()

            assigned_to_list = list(
                cases_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,
                cases_object.id,
            )
            return Response(
                {"error": False, "message": "Case Updated Successfully"},
                status=status.HTTP_200_OK,
            )
        return Response(
            {"error": True, "errors": serializer.errors},
            status=status.HTTP_400_BAD_REQUEST,
        )
Example #3
0
    def put(self, request, pk, format=None):
        params = request.query_params if len(
            request.data) == 0 else request.data
        cases_object = self.get_object(pk=pk)
        data = {}
        if cases_object.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 == cases_object.created_by) or
                    (self.request.user in cases_object.assigned_to.all())):
                return Response(
                    {
                        "error":
                        True,
                        "errors":
                        "You do not have Permission to perform this action",
                    },
                    status=status.HTTP_401_UNAUTHORIZED,
                )

        serializer = CaseCreateSerializer(
            cases_object,
            data=params,
            request_obj=request,
            case=True,
        )

        if serializer.is_valid():
            cases_object = serializer.save(
                closed_on=params.get("closed_on"),
                case_type=params.get("type_of_case"))
            previous_assigned_to_users = list(
                cases_object.assigned_to.all().values_list("id", flat=True))
            cases_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:
                        cases_object.contacts.add(contact)
                    else:
                        data["contacts"] = "Please enter valid Contact"
                        return Response({"error": True, "errors": data})

            if self.request.user.role == "ADMIN":
                cases_object.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:
                            cases_object.teams.add(team)
                        else:
                            data["team"] = "Please enter valid Team"
                            return Response({"error": True, "errors": data})

                cases_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:
                            cases_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("case_attachment"):
                attachment = Attachments()
                attachment.created_by = self.request.user
                attachment.file_name = self.request.FILES.get(
                    "case_attachment").name
                attachment.cases = cases_object
                attachment.attachment = self.request.FILES.get(
                    "case_attachment")
                attachment.save()

            assigned_to_list = list(cases_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,
                cases_object.id,
                domain=current_site.domain,
                protocol=self.request.scheme,
            )
            return Response(
                {
                    "error": False,
                    "message": "Case Updated Successfully"
                },
                status=status.HTTP_200_OK,
            )
        return Response(
            {
                "error": True,
                "errors": serializer.errors
            },
            status=status.HTTP_400_BAD_REQUEST,
        )
Example #4
0
def update_case(request, pk):
    case_object = Case.objects.filter(pk=pk).first()
    accounts = Account.objects.filter(status="open")
    contacts = Contact.objects.all()
    if request.user.role != "ADMIN" and not request.user.is_superuser:
        accounts = Account.objects.filter(created_by=request.user)
        contacts = Contact.objects.filter(
            Q(assigned_to__in=[request.user]) | Q(created_by=request.user))
    users = []
    if request.user.role == 'ADMIN' or request.user.is_superuser:
        users = User.objects.filter(is_active=True).order_by('email')
    elif request.user.google.all():
        users = []
    else:
        users = User.objects.filter(role='ADMIN').order_by('email')
    kwargs_data = {
        "assigned_to": users,
        "account": accounts,
        "contacts": contacts
    }
    form = CaseForm(instance=case_object, **kwargs_data)

    if request.POST:
        form = CaseForm(request.POST,
                        request.FILES,
                        instance=case_object,
                        **kwargs_data)
        if form.is_valid():
            assigned_to_ids = case_object.assigned_to.all().values_list(
                'id', flat=True)
            case_obj = form.save(commit=False)
            case_obj.contacts.clear()
            case_obj.save()
            all_members_list = []

            if request.POST.getlist('assigned_to', []):
                current_site = get_current_site(request)

                assigned_form_users = form.cleaned_data.get(
                    'assigned_to').values_list('id', flat=True)
                all_members_list = list(
                    set(list(assigned_form_users)) -
                    set(list(assigned_to_ids)))
                recipients = all_members_list
                send_email_to_assigned_user.delay(recipients,
                                                  case_obj.id,
                                                  domain=current_site.domain,
                                                  protocol=request.scheme)
                # if all_members_list:
                #     for assigned_to_user in all_members_list:
                #         user = get_object_or_404(User, pk=assigned_to_user)
                #         mail_subject = 'Assigned to case.'
                #         message = render_to_string(
                #             'assigned_to/cases_assigned.html', {
                #                 'user': user,
                #                 'domain': current_site.domain,
                #                 'protocol': request.scheme,
                #                 'case': case_obj
                #             })
                #         email = EmailMessage(
                #             mail_subject, message, to=[user.email])
                #         email.content_subtype = "html"
                #         email.send()

                case_obj.assigned_to.clear()
                case_obj.assigned_to.add(*request.POST.getlist('assigned_to'))
            else:
                case_obj.assigned_to.clear()

            if request.POST.getlist('contacts', []):
                case_obj.contacts.add(*request.POST.getlist('contacts'))

            success_url = reverse("cases:list")
            if request.POST.get('from_account'):
                from_account = request.POST.get('from_account')
                success_url = reverse("accounts:view_account",
                                      kwargs={'pk': from_account})
            if request.FILES.get('case_attachment'):
                attachment = Attachments()
                attachment.created_by = request.user
                attachment.file_name = request.FILES.get(
                    'case_attachment').name
                attachment.case = case_obj
                attachment.attachment = request.FILES.get('case_attachment')
                attachment.save()

            return JsonResponse({'error': False, 'success_url': success_url})
        return JsonResponse({'error': True, 'errors': form.errors})
    context = {}
    context["case_obj"] = case_object
    user_assgn_list = [
        assgined_to.id
        for assgined_to in context["case_obj"].assigned_to.all()
    ]

    if request.user == case_object.created_by:
        user_assgn_list.append(request.user.id)
    if request.user.role != "ADMIN" and not request.user.is_superuser:
        if request.user.id not in user_assgn_list:
            raise PermissionDenied
    context["case_form"] = form
    context["accounts"] = accounts
    if request.GET.get('view_account'):
        context['account'] = get_object_or_404(
            Account, id=request.GET.get('view_account'))
    context["contacts"] = contacts
    context["users"] = kwargs_data['assigned_to']
    context["case_types"] = CASE_TYPE
    context["case_priority"] = PRIORITY_CHOICE
    context["case_status"] = STATUS_CHOICE
    context["assignedto_list"] = [
        int(i) for i in request.POST.getlist('assigned_to', []) if i
    ]
    context["contacts_list"] = [
        int(i) for i in request.POST.getlist('contacts', []) if i
    ]

    return render(request, "create_cases.html", context)
Example #5
0
    def post(self, request, *args, **kwargs):
        params = request.query_params if len(
            request.data) == 0 else request.data
        data = {}
        serializer = CaseCreateSerializer(data=params, request_obj=request)
        if serializer.is_valid():
            cases_obj = serializer.save(
                created_by=request.user,
                company=request.company,
                closed_on=params.get("closed_on"),
                case_type=params.get("type_of_case"),
            )

            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:
                        cases_obj.contacts.add(contact)
                    else:
                        cases_obj.delete()
                        data["contacts"] = "Please enter valid contact"
                        return Response({"error": True, "errors": 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:
                            cases_obj.teams.add(team)
                        else:
                            cases_obj.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:
                            cases_obj.assigned_to.add(user_id)
                        else:
                            cases_obj.delete()
                            data["assigned_to"] = "Please enter valid user"
                            return Response({"error": True, "errors": data})

            if self.request.FILES.get("case_attachment"):
                attachment = Attachments()
                attachment.created_by = self.request.user
                attachment.file_name = self.request.FILES.get(
                    "case_attachment").name
                attachment.cases = cases_obj
                attachment.attachment = self.request.FILES.get(
                    "case_attachment")
                attachment.save()

            assigned_to_list = list(cases_obj.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,
                cases_obj.id,
                domain=current_site.domain,
                protocol=self.request.scheme,
            )
            return Response({
                "error": False,
                "message": "Case Created Successfully"
            })

        return Response(
            {
                "error": True,
                "errors": serializer.errors
            },
            status=status.HTTP_400_BAD_REQUEST,
        )