示例#1
0
def mailinglog_pdf(request, mailinglog_id):
    contacts = list(
            Contact.objects.filter(mailing__mailinglog_id=mailinglog_id)
    )
    if len(contacts) == 0:
        raise Http404
    return contact_pdf_response(contacts)
示例#2
0
def download_sharingbatch(request, sharingbatch_id, filetype):
    batch = get_object_or_404(SharingBatch, pk=sharingbatch_id)
    if filetype == "csv":
        return contact_csv_response(batch.contacts.all())
    elif filetype == "pdf":
        return contact_pdf_response(batch.contacts.all())
    else:
        raise Http404
示例#3
0
def download_sharingbatch(request, sharingbatch_id, filetype):
    batch = get_object_or_404(SharingBatch, pk=sharingbatch_id)
    if filetype == "csv":
        return contact_csv_response(batch.contacts.all())
    elif filetype == "pdf":
        return contact_pdf_response(batch.contacts.all())
    else:
        raise Http404
示例#4
0
def contacts(request):
    form_params = {}
    form_params.update(CONTACT_SEARCH_DEFAULTS)
    for key in request.GET:
        form_params[key] = request.GET.get(key)
    form = forms.ContactSearch(form_params)
    contacts = Contact.objects.all().order_by('last_name').select_related('address')
    if form.is_valid():
        contacts = form.constrain(contacts)
        if request.GET.get('csv', None):
            return contact_csv_response(contacts)
        if request.GET.get('pdf', None):
            return contact_pdf_response(contacts)
    return render(request, "contacts/list.html", {
        'form': form,
        'contacts': _paginate(request, contacts.select_related("address").distinct()),
    })
示例#5
0
def show_mailinglog(request, mailinglog_id):
    log = get_object_or_404(MailingLog, id=mailinglog_id)
    contact_id_to_mailings = defaultdict(list)
    for mailing in log.mailings.all():
        contact_id_to_mailings[mailing.contact_id].append(mailing)
    recipients = Contact.objects.filter(mailing__mailinglog=log)
    search_form = forms.ContactSearch(request.GET or CONTACT_SEARCH_DEFAULTS)
    if search_form.is_valid():
        recipients = search_form.constrain(recipients)
    if request.GET.get("csv", False):
        return contact_csv_response(recipients)
    elif request.GET.get("pdf", False):
        return contact_pdf_response(recipients)
    return render(request, "mailings/show_mailinglog.html", {
        'log': log,
        'form': search_form,
        'contact_id_to_mailings': contact_id_to_mailings,
        'page': _paginate(request, recipients),
    })
示例#6
0
def show_mailinglog(request, mailinglog_id):
    log = get_object_or_404(MailingLog, id=mailinglog_id)
    contact_id_to_mailings = defaultdict(list)
    for mailing in log.mailings.all():
        contact_id_to_mailings[mailing.contact_id].append(mailing)
    recipients = Contact.objects.filter(mailing__mailinglog=log)
    search_form = forms.ContactSearch(request.GET or CONTACT_SEARCH_DEFAULTS)
    if search_form.is_valid():
        recipients = search_form.constrain(recipients)
    if request.GET.get("csv", False):
        return contact_csv_response(recipients)
    elif request.GET.get("pdf", False):
        return contact_pdf_response(recipients)
    return render(
        request, "mailings/show_mailinglog.html", {
            'log': log,
            'form': search_form,
            'contact_id_to_mailings': contact_id_to_mailings,
            'page': _paginate(request, recipients),
        })
示例#7
0
def contacts(request):
    form_params = {}
    form_params.update(CONTACT_SEARCH_DEFAULTS)
    for key in request.GET:
        form_params[key] = request.GET.get(key)
    form = forms.ContactSearch(form_params)
    contacts = Contact.objects.all().order_by('last_name').select_related(
        'address')
    if form.is_valid():
        contacts = form.constrain(contacts)
        if request.GET.get('csv', None):
            return contact_csv_response(contacts)
        if request.GET.get('pdf', None):
            return contact_pdf_response(contacts)
    return render(
        request, "contacts/list.html", {
            'form':
            form,
            'contacts':
            _paginate(request,
                      contacts.select_related("address").distinct()),
        })
示例#8
0
def mailinglog_pdf(request, mailinglog_id):
    contacts = list(
        Contact.objects.filter(mailing__mailinglog_id=mailinglog_id))
    if len(contacts) == 0:
        raise Http404
    return contact_pdf_response(contacts)
示例#9
0
def mailings_needed(request, issue_id=None):
    """
    Handle listing and creation of 3 types of things:
     - Issues
     - Last issue notices
     - Info requests
    Each of these things generate a Mailing object, and this Mailing object
    gets attached to a MailingLog object when sent.

    Each item has 3 states:
     - needed: policy dictates it should exist, but there's no Mailing object.
     - enqueued: a Mailing object exists, but it's not sent.
     - sent: the Mailing object is marked sent.

    MailingLog objects act as a convenient interface by which to group Mailing
    objects for printing labels en masse.  However, it's not considered "sent"
    until the Mailing object has been marked "sent", regardless of belonging
    to a MailingLog object or not.
    """
    if issue_id:
        issue = get_object_or_404(Issue, pk=issue_id)
    else:
        issue = Issue.objects.latest('date')

    mailings = [
        {
            'name': unicode("Issue: %s" % issue),
            'needed': mail_rules.IssuesNeeded(issue),
            'unreachable': mail_rules.IssuesUnreachable(issue),
            'enqueued': mail_rules.IssuesEnqueued(issue),
            'sent': mail_rules.IssuesSent(issue),
        },
        {
            'name': unicode("0 issues left, recipient"),
            'needed': mail_rules.NoticeNeeded(0, "recipient"),
            'unreachable': mail_rules.NoticeUnreachable(0, "recipient"),
            'enqueued': mail_rules.NoticeEnqueued(0, "recipient"),
            'sent': mail_rules.NoticeSent(0, "recipient"),
        },
        {
            'name': unicode("1 issues left, recipient"),
            'needed': mail_rules.NoticeNeeded(1, "recipient"),
            'unreachable': mail_rules.NoticeUnreachable(1, "recipient"),
            'enqueued': mail_rules.NoticeEnqueued(1, "recipient"),
            'sent': mail_rules.NoticeSent(1, "recipient"),
        },
        {
            'name': unicode("2 issues left, recipient"),
            'needed': mail_rules.NoticeNeeded(2, "recipient"),
            'unreachable': mail_rules.NoticeUnreachable(2, "recipient"),
            'enqueued': mail_rules.NoticeEnqueued(2, "recipient"),
            'sent': mail_rules.NoticeSent(2, "recipient"),
        },
        {
            'name': unicode("0 issues left, payer"),
            'needed': mail_rules.NoticeNeeded(0, "payer"),
            'unreachable': mail_rules.NoticeUnreachable(0, "payer"),
            'enqueued': mail_rules.NoticeEnqueued(0, "payer"),
            'sent': mail_rules.NoticeSent(0, "payer"),
        },
        {
            'name': unicode("1 issues left, payer"),
            'needed': mail_rules.NoticeNeeded(1, "payer"),
            'unreachable': mail_rules.NoticeUnreachable(1, "payer"),
            'enqueued': mail_rules.NoticeEnqueued(1, "payer"),
            'sent': mail_rules.NoticeSent(1, "payer"),
        },
        {
            'name': unicode("2 issues left, payer"),
            'needed': mail_rules.NoticeNeeded(2, "payer"),
            'unreachable': mail_rules.NoticeUnreachable(2, "payer"),
            'enqueued': mail_rules.NoticeEnqueued(2, "payer"),
            'sent': mail_rules.NoticeSent(2, "payer"),
        },
        {
            'name': unicode("Inquiry Responses"),
            'needed': mail_rules.InquiryResponseNeeded(),
            'unreachable': mail_rules.InquiryResponseUnreachable(),
            'enqueued': mail_rules.InquiryResponseEnqueued(),
            'sent': mail_rules.InquiryResponseSent(),
        },
        {
            'name': unicode("Other enqueued mail"),
            'needed': mail_rules.EmptyRule(),
            'unreachable': mail_rules.EmptyRule(),
            'enqueued': mail_rules.OtherEnqueuedMail(issue),
            'sent': mail_rules.EmptyRule(),
        },
    ]

    # Try to resolve the list of whom to show.
    path = request.GET.get("path")
    if path:
        matched_mailing = None
        for mailing_group in mailings:
            for key in ("needed", "unreachable", "enqueued", "sent"):
                if path == mailing_group[key].code:
                    matched_mailing = mailing_group[key]
                    break
            else:
                continue
            break
        else:
            raise Http404
        if request.method == "POST":
            if request.POST.get("action") == matched_mailing.action:
                matched_mailing.transition(request.user)
                return redirect(request.path)
            else:
                return HttpResponseBadRequest("Unmatched action")
        if path.startswith("inquiry"):
            recipients = None
            inquiries = _paginate(request,
                                  matched_mailing.qs.order_by('-date'))
        else:
            recipients = matched_mailing.qs
            inquiries = None
            if request.GET.get("csv"):
                return contact_csv_response(recipients)
            elif request.GET.get("pdf"):
                return contact_pdf_response(recipients)
    else:
        matched_mailing = None
        recipients = None
        inquiries = None

    if recipients is not None:
        search_form_vars = {}
        search_form_vars.update(CONTACT_SEARCH_DEFAULTS)
        for key in request.GET:
            search_form_vars[key] = request.GET.get(key)
        contact_search_form = forms.ContactSearch(search_form_vars)
        recipients = contact_search_form.constrain(recipients)
        recipients = recipients.select_related('address')
        page = _paginate(request, recipients)
    else:
        contact_search_form = None
        page = None

    return render(
        request, "mailings/needed.html", {
            'issue': issue,
            'path': path,
            'mailings': mailings,
            'matched_mailing': matched_mailing,
            'inquiries': inquiries,
            'contact_search_form': contact_search_form,
            'page': page,
            'logs': MailingLog.objects.all().order_by('-created')[0:10],
        })
示例#10
0
def mailings_needed(request, issue_id=None):
    """
    Handle listing and creation of 3 types of things:
     - Issues
     - Last issue notices
     - Info requests
    Each of these things generate a Mailing object, and this Mailing object
    gets attached to a MailingLog object when sent.

    Each item has 3 states:
     - needed: policy dictates it should exist, but there's no Mailing object.
     - enqueued: a Mailing object exists, but it's not sent.
     - sent: the Mailing object is marked sent.

    MailingLog objects act as a convenient interface by which to group Mailing
    objects for printing labels en masse.  However, it's not considered "sent"
    until the Mailing object has been marked "sent", regardless of belonging
    to a MailingLog object or not.
    """
    if issue_id:
        issue = get_object_or_404(Issue, pk=issue_id)
    else:
        issue = Issue.objects.latest('date')


    mailings = [
            {
                'name': unicode("Issue: %s" % issue),
                'needed': mail_rules.IssuesNeeded(issue),
                'unreachable': mail_rules.IssuesUnreachable(issue),
                'enqueued': mail_rules.IssuesEnqueued(issue),
                'sent': mail_rules.IssuesSent(issue),
            },
            {
                'name': unicode("0 issues left, recipient"),
                'needed': mail_rules.NoticeNeeded(0, "recipient"),
                'unreachable': mail_rules.NoticeUnreachable(0, "recipient"),
                'enqueued': mail_rules.NoticeEnqueued(0, "recipient"),
                'sent': mail_rules.NoticeSent(0, "recipient"),
            },
            {
                'name': unicode("1 issues left, recipient"),
                'needed': mail_rules.NoticeNeeded(1, "recipient"),
                'unreachable': mail_rules.NoticeUnreachable(1, "recipient"),
                'enqueued': mail_rules.NoticeEnqueued(1, "recipient"),
                'sent': mail_rules.NoticeSent(1, "recipient"),
            },
            {
                'name': unicode("2 issues left, recipient"),
                'needed': mail_rules.NoticeNeeded(2, "recipient"),
                'unreachable': mail_rules.NoticeUnreachable(2, "recipient"),
                'enqueued': mail_rules.NoticeEnqueued(2, "recipient"),
                'sent': mail_rules.NoticeSent(2, "recipient"),
            },
            {
                'name': unicode("0 issues left, payer"),
                'needed': mail_rules.NoticeNeeded(0, "payer"),
                'unreachable': mail_rules.NoticeUnreachable(0, "payer"),
                'enqueued': mail_rules.NoticeEnqueued(0, "payer"),
                'sent': mail_rules.NoticeSent(0, "payer"),
            },
            {
                'name': unicode("1 issues left, payer"),
                'needed': mail_rules.NoticeNeeded(1, "payer"),
                'unreachable': mail_rules.NoticeUnreachable(1, "payer"),
                'enqueued': mail_rules.NoticeEnqueued(1, "payer"),
                'sent': mail_rules.NoticeSent(1, "payer"),
            },
            {
                'name': unicode("2 issues left, payer"),
                'needed': mail_rules.NoticeNeeded(2, "payer"),
                'unreachable': mail_rules.NoticeUnreachable(2, "payer"),
                'enqueued': mail_rules.NoticeEnqueued(2, "payer"),
                'sent': mail_rules.NoticeSent(2, "payer"),
            },
            {
                'name': unicode("Inquiry Responses"),
                'needed': mail_rules.InquiryResponseNeeded(),
                'unreachable': mail_rules.InquiryResponseUnreachable(),
                'enqueued': mail_rules.InquiryResponseEnqueued(),
                'sent': mail_rules.InquiryResponseSent(),
            },
            {
                'name': unicode("Other enqueued mail"),
                'needed': mail_rules.EmptyRule(),
                'unreachable': mail_rules.EmptyRule(),
                'enqueued': mail_rules.OtherEnqueuedMail(issue),
                'sent': mail_rules.EmptyRule(),
            },
    ]


    # Try to resolve the list of whom to show.
    path = request.GET.get("path")
    if path:
        matched_mailing = None
        for mailing_group in mailings:
            for key in ("needed", "unreachable", "enqueued", "sent"):
                if path == mailing_group[key].code:
                    matched_mailing = mailing_group[key]
                    break
            else:
                continue
            break
        else:
            raise Http404
        if request.method == "POST":
            if request.POST.get("action") == matched_mailing.action:
                matched_mailing.transition(request.user)
                return redirect(request.path)
            else:
                return HttpResponseBadRequest("Unmatched action")
        if path.startswith("inquiry"):
            recipients = None
            inquiries = _paginate(request, matched_mailing.qs.order_by('-date'))
        else:
            recipients = matched_mailing.qs
            inquiries = None
            if request.GET.get("csv"):
                return contact_csv_response(recipients)
            elif request.GET.get("pdf"):
                return contact_pdf_response(recipients)
    else:
        matched_mailing = None
        recipients = None
        inquiries = None

    if recipients is not None:
        search_form_vars = {}
        search_form_vars.update(CONTACT_SEARCH_DEFAULTS)
        for key in request.GET:
            search_form_vars[key] = request.GET.get(key)
        contact_search_form = forms.ContactSearch(search_form_vars)
        recipients = contact_search_form.constrain(recipients)
        recipients = recipients.select_related('address')
        page = _paginate(request, recipients)
    else:
        contact_search_form = None
        page = None

    return render(request, "mailings/needed.html", {
        'issue': issue,
        'path': path,
        'mailings': mailings,
        'matched_mailing': matched_mailing,
        'inquiries': inquiries,
        'contact_search_form': contact_search_form,
        'page': page,
        'logs': MailingLog.objects.all().order_by('-created')[0:10],
    })