Ejemplo n.º 1
0
def create_case(request):
    users = []
    if request.user.role == "ADMIN" or request.user.is_superuser:
        users = User.objects.filter(is_active=True,
                                    company=request.company).order_by("email")
    elif request.user.google.all():
        users = []
    else:
        users = User.objects.filter(role="ADMIN",
                                    company=request.company).order_by("email")
    accounts = Account.objects.filter(status="open", company=request.company)
    contacts = Contact.objects.filter(company=request.company)
    if request.user.role != "ADMIN" and not request.user.is_superuser:
        accounts = Account.objects.filter(created_by=request.user).filter(
            company=request.company)
        contacts = Contact.objects.filter(
            Q(assigned_to__in=[request.user])
            | Q(created_by=request.user)).filter(company=request.company)
    kwargs_data = {
        "assigned_to": users,
        "account": accounts,
        "contacts": contacts,
        "request_obj": request,
    }
    form = CaseForm(**kwargs_data)
    template_name = "create_cases.html"

    if request.POST:
        form = CaseForm(request.POST, request.FILES, **kwargs_data)
        if form.is_valid():
            case = form.save(commit=False)
            case.created_by = request.user
            case.company = request.company
            case.save()
            if request.POST.getlist("assigned_to", []):
                case.assigned_to.add(*request.POST.getlist("assigned_to"))
                assigned_to_list = request.POST.getlist("assigned_to")
                # current_site = get_current_site(request)
                # recipients = assigned_to_list
                # send_email_to_assigned_user.delay(recipients, case.id, domain=current_site.domain,
                #     protocol=request.scheme)
                # for assigned_to_user in assigned_to_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
                #         })
                #     email = EmailMessage(
                #         mail_subject, message, to=[user.email])
                #     email.content_subtype = "html"
                #     email.send()

            if request.POST.getlist("contacts", []):
                case.contacts.add(*request.POST.getlist("contacts"))
Ejemplo n.º 2
0
def add_case(request):
    accounts = Account.objects.all()
    contacts = Contact.objects.all()
    users = User.objects.filter(is_active=True).order_by('email')
    form = CaseForm(assigned_to=users, account=accounts, contacts=contacts)
    teams = Team.objects.all()
    teams_list = request.POST.getlist("teams")
    assignedto_list = request.POST.getlist("assigned_to")
    contacts_list = request.POST.getlist("contacts")
    if request.method == 'POST':
        form = CaseForm(request.POST,
                        assigned_to=users,
                        account=accounts,
                        contacts=contacts)
        if form.is_valid():
            case = form.save(commit=False)
            case.created_by = request.user
            case.save()
            case.assigned_to.add(*assignedto_list)
            case.teams.add(*teams_list)
            case.contacts.add(*contacts_list)
            if request.is_ajax():
                return JsonResponse({'error': False})
            if request.POST.get("savenewform"):
                return redirect("cases:case_add")
            else:
                return HttpResponseRedirect(reverse('cases:list'))
        else:
            if request.is_ajax():
                return JsonResponse({
                    'error': True,
                    'case_errors': form.errors
                })
            return render(
                request, "create_cases.html", {
                    'case_form':
                    form,
                    'accounts':
                    accounts,
                    'users':
                    users,
                    'teams':
                    teams,
                    'case_types':
                    CASE_TYPE,
                    'case_priority':
                    PRIORITY_CHOICE,
                    'case_status':
                    STATUS_CHOICE,
                    'teams_list':
                    teams_list,
                    'assignedto_list':
                    [int(user_id) for user_id in assignedto_list],
                    'contacts_list':
                    contacts_list
                })
Ejemplo n.º 3
0
def edit_case(request, case_id):
    case_object = get_object_or_404(Case, id=case_id)
    users = User.objects.filter(is_active=True).order_by('email')
    accounts = Account.objects.all()
    contacts = Contact.objects.all()
    form = CaseForm(instance=case_object, assigned_to=users, account=accounts, contacts=contacts)
    teams = Team.objects.all()
    teams_list = request.POST.getlist("teams")
    assignedto_list = request.POST.getlist("assigned_to")
    contacts_list = request.POST.getlist("contacts")
    if request.method == 'POST':
        form = CaseForm(
            request.POST, instance=case_object, assigned_to=users, account=accounts,
            contacts=contacts
        )
        if form.is_valid():
            case_obj = form.save(commit=False)
            if request.POST.get("account"):
                case_obj.account = Account.objects.get(id=request.POST.get("account"))
            case_obj.created_by = request.user
            case_obj.save()
            case_obj.assigned_to.clear()
            case_obj.assigned_to.add(*assignedto_list)
            case_obj.teams.clear()
            case_obj.teams.add(*teams_list)
            case_obj.contacts.clear()
            case_obj.contacts.add(*contacts_list)
            if request.is_ajax():
                return JsonResponse({'error': False})
            return HttpResponseRedirect(reverse('cases:list'))
        else:
            if request.is_ajax():
                return JsonResponse({'error': True, 'case_errors': form.errors})
            return render(request, "cases/create_cases.html", {
                'case_obj': case_object,
                'case_form': form,
                'accounts': accounts,
                'users': users,
                'teams': teams,
                'case_types': CASE_TYPE,
                'case_priority': PRIORITY_CHOICE,
                'case_status': STATUS_CHOICE,
                'teams_list': teams_list,
                'assignedto_list': assignedto_list,
                'contacts_list': contacts_list
            })

    return render(request, "cases/create_cases.html", {
        'case_form': form,
        'case_obj': case_object,
        'accounts': accounts,
        'users': users,
        'teams': teams,
        'case_types': CASE_TYPE,
        'case_priority': PRIORITY_CHOICE,
        'case_status': STATUS_CHOICE,
        'assignedto_list': assignedto_list,
        'teams_list': teams_list,
        'contacts_list': contacts_list
    })
Ejemplo n.º 4
0
def case(request):
    if request.method == "POST":
        form = CaseForm(request.POST)
        if form.is_valid():
            try:
                form.save()
                return redirect('/view.html')
            except:
                pass
    else:
        form = CaseForm()
    return render(request, 'addcase.html', {'form': form})
Ejemplo n.º 5
0
def addcase(request):
    if request.method == "POST":
        form = CaseForm(request.POST)
        if form.is_valid():
            p = form.save(commit=False)
            p.casemanager = request.user.username
            p.save()
            return redirect('/view')
    else:
        form = CaseForm()
    return render(request, 'addcase.html', {'form': form})
Ejemplo n.º 6
0
def create_case(request):
    users = User.objects.filter(is_active=True).order_by('email')
    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))
    kwargs_data = {
        "assigned_to": users, "account": accounts, "contacts": contacts}
    form = CaseForm(**kwargs_data)
    template_name = "create_cases.html"

    if request.POST:
        form = CaseForm(request.POST, request.FILES, **kwargs_data)
        if form.is_valid():
            case = form.save(commit=False)
            case.created_by = request.user
            case.save()
            if request.POST.getlist('assigned_to', []):
                case.assigned_to.add(*request.POST.getlist('assigned_to'))
                assigned_to_list = request.POST.getlist('assigned_to')
                current_site = get_current_site(request)
                for assigned_to_user in assigned_to_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
                    })
                    email = EmailMessage(mail_subject, message, to=[user.email])
                    email.content_subtype = "html"
                    email.send()

            if request.POST.getlist('contacts', []):
                case.contacts.add(*request.POST.getlist('contacts'))
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
def create_case(request):
    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')
    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))
    kwargs_data = {
        "assigned_to": users,
        "account": accounts,
        "contacts": contacts
    }
    form = CaseForm(**kwargs_data)
    template_name = "create_cases.html"

    if request.POST:
        form = CaseForm(request.POST, request.FILES, **kwargs_data)
        if form.is_valid():
            case = form.save(commit=False)
            case.created_by = request.user
            name = request.POST.get("name")
            print("???????", name)
            status = request.POST.get("status")
            print("status:", status)
            case.save()
            time = Case.objects.values_list("created_on",
                                            flat=True).filter(name=name)
            print(".....", time)
            # for t in time:
            #     dtime = t.created_on
            #     dt = dtime.strftime("%H:%M:%S")
            #     print("time:",dt)
            #     # a=tt
            #     time.append(dt)
            if request.POST.getlist('assigned_to', []):
                case.assigned_to.add(*request.POST.getlist('assigned_to'))
                assigned_to_list = request.POST.getlist('assigned_to')
                # current_site = get_current_site(request)
                # recipients = assigned_to_list
                # send_email_to_assigned_user.delay(recipients, case.id, domain=current_site.domain,
                #     protocol=request.scheme)
                # for assigned_to_user in assigned_to_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
                #         })
                #     email = EmailMessage(
                #         mail_subject, message, to=[user.email])
                #     email.content_subtype = "html"
                #     email.send()

            if request.POST.getlist('contacts', []):
                case.contacts.add(*request.POST.getlist('contacts'))
Ejemplo n.º 9
0
            p.save()
            return redirect('/viewtask/'+str(id1))
    else:
        form = TaskForm()
    return render(request, 'addtask.html', {'form': form})


def viewtask(request, id1):
    tasks = Tasks.objects.filter(caseid=id1)
    return render(request, 'viewtask.html', {'tasks': tasks})


def editcase(request, id1, queryset=None):
    case = Cases.objects.filter(caseid=id1).first()
    if request.method == "POST":
        form = CaseForm(request.POST, instance=case)
        if form.is_valid():
            form.save()
            return redirect('/view')
    
    form = CaseForm(instance=case)
    return render(request, 'editcase.html', {'form': form})


def edittask(request, name, queryset=None):
    task = Tasks.objects.filter(taskname=name).first()
    if request.method == "POST":
        form = TaskForm(request.POST, instance=task)
        if form.is_valid():
            form.save()
            return redirect('/view')