Exemple #1
0
def subscriber_list(request):
    """Subscriber list for the logged in user

    **Attributes**:

        * ``template`` - frontend/subscriber/list.html
        * ``form`` - SubscriberSearchForm

    **Logic Description**:

        * List all subscribers belonging to the logged in user & their campaign
    """
    sort_col_field_list = [
        "contact",
        "updated_date",
        "count_attempt",
        "completion_count_attempt",
        "status",
        "disposition",
        "collected_data",
        "agent",
    ]
    default_sort_field = "id"
    pagination_data = get_pagination_vars(request, sort_col_field_list, default_sort_field)

    PAGE_SIZE = pagination_data["PAGE_SIZE"]
    sort_order = pagination_data["sort_order"]
    start_page = pagination_data["start_page"]
    end_page = pagination_data["end_page"]

    form = SubscriberSearchForm(request.user)

    search_tag = 1
    campaign_id = ""
    agent_id = ""
    status = "all"

    if request.method == "POST":
        form = SubscriberSearchForm(request.user, request.POST)

        if form.is_valid():
            field_list = ["start_date", "end_date", "status", "campaign_id", "agent_id"]
            unset_session_var(request, field_list)
            campaign_id = getvar(request, "campaign_id", setsession=True)
            agent_id = getvar(request, "agent_id", setsession=True)

            if request.POST.get("from_date"):
                # From
                from_date = request.POST["from_date"]
                start_date = ceil_strdate(from_date, "start")
                request.session["session_start_date"] = start_date

            if request.POST.get("to_date"):
                # To
                to_date = request.POST["to_date"]
                end_date = ceil_strdate(to_date, "end")
                request.session["session_end_date"] = end_date

            status = request.POST.get("status")
            if status != "all":
                request.session["session_status"] = status

    post_var_with_page = 0
    try:
        if request.GET.get("page") or request.GET.get("sort_by"):
            post_var_with_page = 1
            start_date = request.session.get("session_start_date")
            end_date = request.session.get("session_end_date")
            campaign_id = request.session.get("session_campaign_id")
            agent_id = request.session.get("session_agent_id")
            status = request.session.get("session_status")
            form = SubscriberSearchForm(
                request.user,
                initial={
                    "from_date": start_date.strftime("%Y-%m-%d"),
                    "to_date": end_date.strftime("%Y-%m-%d"),
                    "campaign_id": campaign_id,
                    "agent_id": agent_id,
                    "status": status,
                },
            )
        else:
            post_var_with_page = 1
            if request.method == "GET":
                post_var_with_page = 0
    except:
        pass

    if post_var_with_page == 0:
        # default
        tday = datetime.utcnow().replace(tzinfo=utc)
        from_date = tday.strftime("%Y-%m-%d")
        to_date = tday.strftime("%Y-%m-%d")
        start_date = datetime(tday.year, tday.month, tday.day, 0, 0, 0, 0).replace(tzinfo=utc)
        end_date = datetime(tday.year, tday.month, tday.day, 23, 59, 59, 999999).replace(tzinfo=utc)

        form = SubscriberSearchForm(request.user, initial={"from_date": from_date, "to_date": to_date})
        # unset session var
        request.session["session_start_date"] = start_date
        request.session["session_end_date"] = end_date
        request.session["session_status"] = ""
        request.session["session_campaign_id"] = ""
        request.session["session_agent_id"] = ""

    kwargs = {}
    # updated_date might be replaced with last_attempt
    if start_date and end_date:
        kwargs["updated_date__range"] = (start_date, end_date)
    if start_date and end_date == "":
        kwargs["updated_date__gte"] = start_date
    if start_date == "" and end_date:
        kwargs["updated_date__lte"] = end_date

    if campaign_id and campaign_id != "0":
        kwargs["campaign_id"] = campaign_id

    if agent_id and agent_id != "0":
        kwargs["agent_id"] = agent_id

    if status and status != "all":
        kwargs["status"] = status

    subscriber_list = []
    all_subscriber_list = []
    subscriber_count = 0

    if request.user.is_superuser:
        subscriber_list = Subscriber.objects.all()
    else:
        subscriber_list = Subscriber.objects.filter(campaign__user=request.user)

    if kwargs:
        subscriber_list = subscriber_list.filter(**kwargs)
        request.session["subscriber_list_kwargs"] = kwargs
    # if contact_name:
    #    # Search on contact name
    #    q = (Q(last_name__icontains=contact_name) |
    #         Q(first_name__icontains=contact_name))
    #    if q:
    #        contact_list = contact_list.filter(q)

    all_subscriber_list = subscriber_list.order_by(sort_order)
    subscriber_list = all_subscriber_list[start_page:end_page]
    subscriber_count = all_subscriber_list.count()

    template = "frontend/subscriber/list.html"
    data = {
        "subscriber_list": subscriber_list,
        "all_subscriber_list": all_subscriber_list,
        "total_subscribers": subscriber_count,
        "PAGE_SIZE": PAGE_SIZE,
        "SUBSCRIBER_COLUMN_NAME": SUBSCRIBER_COLUMN_NAME,
        "col_name_with_order": pagination_data["col_name_with_order"],
        "msg": request.session.get("msg"),
        "error_msg": request.session.get("error_msg"),
        "form": form,
        "dialer_setting_msg": user_dialer_setting_msg(request.user),
        "search_tag": search_tag,
    }
    request.session["msg"] = ""
    request.session["error_msg"] = ""
    return render_to_response(template, data, context_instance=RequestContext(request))
Exemple #2
0
def subscriber_list(request):
    """
    Subscriber list for the logged in user

    **Attributes**:

        * ``template`` - dialer_campaign/subscriber/list.html
        * ``form`` - SubscriberSearchForm

    **Logic Description**:

        * List all subscribers belonging to the logged in user & their campaign
    """
    sort_col_field_list = ['contact', 'updated_date', 'count_attempt', 'completion_count_attempt',
                           'status', 'disposition', 'collected_data', 'agent']
    pag_vars = get_pagination_vars(request, sort_col_field_list, default_sort_field='id')
    form = SubscriberSearchForm(request.user, request.POST or None)
    campaign_id = ''
    agent_id = ''
    status = 'all'
    start_date = end_date = None
    post_var_with_page = 0
    if form.is_valid():
        post_var_with_page = 1
        field_list = ['start_date', 'end_date', 'status', 'campaign_id', 'agent_id']
        unset_session_var(request, field_list)
        campaign_id = getvar(request, 'campaign_id', setsession=True)
        agent_id = getvar(request, 'agent_id', setsession=True)

        from_date = getvar(request, 'from_date')
        to_date = getvar(request, 'to_date')
        start_date = ceil_strdate(str(from_date), 'start')
        end_date = ceil_strdate(str(to_date), 'end')

        converted_start_date = start_date.strftime('%Y-%m-%d')
        converted_end_date = end_date.strftime('%Y-%m-%d')
        request.session['session_start_date'] = converted_start_date
        request.session['session_end_date'] = converted_end_date

        status = getvar(request, 'status', setsession=True)

    if request.GET.get('page') or request.GET.get('sort_by'):
        post_var_with_page = 1
        start_date = request.session.get('session_start_date')
        end_date = request.session.get('session_end_date')

        start_date = ceil_strdate(start_date, 'start')
        end_date = ceil_strdate(end_date, 'end')

        campaign_id = request.session.get('session_campaign_id')
        agent_id = request.session.get('session_agent_id')
        status = request.session.get('session_status')
        form = SubscriberSearchForm(
            request.user,
            initial={'from_date': start_date.strftime('%Y-%m-%d'),
                     'to_date': end_date.strftime('%Y-%m-%d'),
                     'campaign_id': campaign_id,
                     'agent_id': agent_id,
                     'status': status})

    if post_var_with_page == 0:
        # default
        tday = datetime.utcnow().replace(tzinfo=utc)
        from_date = tday.strftime('%Y-%m-%d')
        to_date = tday.strftime('%Y-%m-%d')
        start_date = datetime(tday.year, tday.month, tday.day, 0, 0, 0, 0).replace(tzinfo=utc)
        end_date = datetime(tday.year, tday.month, tday.day, 23, 59, 59, 999999).replace(tzinfo=utc)

        form = SubscriberSearchForm(request.user, initial={'from_date': from_date, 'to_date': to_date})
        # unset session var
        request.session['session_start_date'] = start_date
        request.session['session_end_date'] = end_date
        request.session['session_status'] = ''
        request.session['session_campaign_id'] = ''
        request.session['session_agent_id'] = ''

    kwargs = {}
    # updated_date might be replaced with last_attempt
    if start_date and end_date:
        kwargs['updated_date__range'] = (start_date, end_date)
    if start_date and end_date == '':
        kwargs['updated_date__gte'] = start_date
    if start_date == '' and end_date:
        kwargs['updated_date__lte'] = end_date

    if campaign_id and campaign_id != '0':
        kwargs['campaign_id'] = campaign_id

    #if agent_id and agent_id != '0':
    #    kwargs['agent_id'] = agent_id

    if status and status != 'all':
        kwargs['status'] = status

    subscriber_list = []
    all_subscriber_list = []
    subscriber_count = 0

    if request.user.is_superuser:
        subscriber_list = Subscriber.objects.all()
    else:
        subscriber_list = Subscriber.objects.filter(campaign__user=request.user)

    if kwargs:
        subscriber_list = subscriber_list.filter(**kwargs)
        request.session['subscriber_list_kwargs'] = kwargs

    all_subscriber_list = subscriber_list.order_by(pag_vars['sort_order'])
    subscriber_list = all_subscriber_list[pag_vars['start_page']:pag_vars['end_page']]
    subscriber_count = all_subscriber_list.count()

    data = {
        'subscriber_list': subscriber_list,
        'all_subscriber_list': all_subscriber_list,
        'total_subscribers': subscriber_count,
        'SUBSCRIBER_COLUMN_NAME': SUBSCRIBER_COLUMN_NAME,
        'col_name_with_order': pag_vars['col_name_with_order'],
        'msg': request.session.get('msg'),
        'error_msg': request.session.get('error_msg'),
        'form': form,
    }
    request.session['msg'] = ''
    request.session['error_msg'] = ''
    return render_to_response('dialer_campaign/subscriber/list.html', data, context_instance=RequestContext(request))