Esempio n. 1
0
    def test_get_newsletter_opening_statistics(self):
        stats = get_newsletter_opening_statistics(self.status, self.recipients)
        self.assertEquals(stats['total_openings'], 0)
        self.assertEquals(stats['unique_openings'], 0)
        self.assertEquals(stats['double_openings'], 0)
        self.assertEquals(stats['unique_openings_percent'], 0)
        self.assertEquals(stats['unknow_openings'], 0)
        self.assertEquals(stats['unknow_openings_percent'], 0)
        self.assertEquals(stats['opening_average'], 0)

        ContactMailingStatus.objects.create(newsletter=self.newsletter,
                                            contact=self.contacts[0],
                                            status=ContactMailingStatus.OPENED)
        ContactMailingStatus.objects.create(newsletter=self.newsletter,
                                            contact=self.contacts[0],
                                            status=ContactMailingStatus.OPENED)
        ContactMailingStatus.objects.create(newsletter=self.newsletter,
                                            contact=self.contacts[1],
                                            status=ContactMailingStatus.OPENED)
        ContactMailingStatus.objects.create(
            newsletter=self.newsletter,
            contact=self.contacts[2],
            status=ContactMailingStatus.OPENED_ON_SITE)
        ContactMailingStatus.objects.create(
            newsletter=self.newsletter,
            contact=self.contacts[2],
            status=ContactMailingStatus.LINK_OPENED)
        status = ContactMailingStatus.objects.filter(
            newsletter=self.newsletter)

        stats = get_newsletter_opening_statistics(status, self.recipients)
        self.assertEquals(stats['total_openings'], 4)
        self.assertEquals(stats['unique_openings'], 3)
        self.assertEquals(stats['double_openings'], 1)
        self.assertEquals(stats['unique_openings_percent'], 75.0)
        self.assertEquals(stats['unknow_openings'], 1)
        self.assertEquals(stats['unknow_openings_percent'], 25.0)
        self.assertEquals(stats['opening_average'], 1.3333333333333333)
        self.assertEquals(stats['opening_deducted'], 0)

        ContactMailingStatus.objects.create(
            newsletter=self.newsletter,
            contact=self.contacts[3],
            status=ContactMailingStatus.LINK_OPENED)
        ContactMailingStatus.objects.create(
            newsletter=self.newsletter,
            contact=self.contacts[3],
            status=ContactMailingStatus.LINK_OPENED)
        status = ContactMailingStatus.objects.filter(
            newsletter=self.newsletter)

        stats = get_newsletter_opening_statistics(status, self.recipients)
        self.assertEquals(stats['total_openings'], 5)
        self.assertEquals(stats['unique_openings'], 4)
        self.assertEquals(stats['double_openings'], 1)
        self.assertEquals(stats['unique_openings_percent'], 100.0)
        self.assertEquals(stats['unknow_openings'], 0)
        self.assertEquals(stats['unknow_openings_percent'], 0.0)
        self.assertEquals(stats['opening_average'], 1.25)
        self.assertEquals(stats['opening_deducted'], 1)
    def test_get_newsletter_opening_statistics(self):
        stats = get_newsletter_opening_statistics(self.status, self.recipients)
        self.assertEquals(stats['total_openings'], 0)
        self.assertEquals(stats['unique_openings'], 0)
        self.assertEquals(stats['double_openings'], 0)
        self.assertEquals(stats['unique_openings_percent'], 0)
        self.assertEquals(stats['unknow_openings'], 0)
        self.assertEquals(stats['unknow_openings_percent'], 0)
        self.assertEquals(stats['opening_average'], 0)

        ContactMailingStatus.objects.create(newsletter=self.newsletter,
                                            contact=self.contacts[0],
                                            status=ContactMailingStatus.OPENED)
        ContactMailingStatus.objects.create(newsletter=self.newsletter,
                                            contact=self.contacts[0],
                                            status=ContactMailingStatus.OPENED)
        ContactMailingStatus.objects.create(newsletter=self.newsletter,
                                            contact=self.contacts[1],
                                            status=ContactMailingStatus.OPENED)
        ContactMailingStatus.objects.create(newsletter=self.newsletter,
                                            contact=self.contacts[2],
                                            status=ContactMailingStatus.OPENED_ON_SITE)
        status = ContactMailingStatus.objects.filter(newsletter=self.newsletter)

        stats = get_newsletter_opening_statistics(status, self.recipients)
        self.assertEquals(stats['total_openings'], 4)
        self.assertEquals(stats['unique_openings'], 3)
        self.assertEquals(stats['double_openings'], 1)
        self.assertEquals(stats['unique_openings_percent'], 75.0)
        self.assertEquals(stats['unknow_openings'], 1)
        self.assertEquals(stats['unknow_openings_percent'], 25.0)
        self.assertEquals(stats['opening_average'], 1.3333333333333333)
def view_newsletter_charts(request, slug, mailing_list_pk=None):
    newsletter = get_object_or_404(Newsletter, slug=slug)

    start = int(request.POST.get('start', 0))
    end = int(request.POST.get('end', 6))

    recipients_list = get_recipients_list(newsletter, mailing_list_pk)
    recipients = len(recipients_list)

    sending_date = newsletter.sending_date.date()
    labels, clicks_by_day, openings_by_day = [], [], []

    for i in range(start, end + 1):
        day = sending_date + timedelta(days=i)
        day_status = ContactMailingStatus.objects.filter(newsletter=newsletter,
                                                         creation_date__day=day.day,
                                                         creation_date__month=day.month,
                                                         creation_date__year=day.year,
                                                         contact__in=recipients_list)

        opening_stats = get_newsletter_opening_statistics(day_status, recipients)
        click_stats = get_newsletter_clicked_link_statistics(day_status, recipients, 0)
        # Labels
        labels.append(date(day, 'D d M y').capitalize())
        # Values
        openings_by_day.append(opening_stats['total_openings'])
        clicks_by_day.append(click_stats['total_clicked_links'])


    b1 = Chart(type='bar_3d', colour=BAR_COLOR_1,
               text=_('Total openings'), tip=_('#val# openings'),
               on_show={'type': 'grow-up'}, values=openings_by_day)

    b2 = Chart(type='bar_3d', colour=BAR_COLOR_2,
               text=_('Total clicked links'), tip=_('#val# clicks'),
               on_show={'type': 'grow-up'}, values=clicks_by_day)

    chart = Chart(bg_colour=BG_COLOR)
    chart.title.text = _('Consultation histogram')
    chart.title.style = '{font-size: 16px; color: #666666; text-align: center; font-weight: bold;}'

    chart.y_axis = {'colour': AXIS_COLOR, 'grid-colour': GRID_COLOR,
                    'min': 0, 'max': max(openings_by_day + clicks_by_day) + 2,
                    'steps': max(openings_by_day) / 5}
    chart.x_axis = {'colour': AXIS_COLOR, 'grid-colour': GRID_COLOR,
                    '3d': 5, 'labels': {'labels': labels, 'rotate': 60}}
    chart.elements = [b1, b2]

    return HttpResponse(chart.render())
def view_newsletter_charts(request, slug):
    newsletter = get_object_or_404(Newsletter, slug=slug)

    start = int(request.POST.get("start", 0))
    end = int(request.POST.get("end", 6))

    recipients = newsletter.mailing_list.expedition_set().count()

    sending_date = newsletter.sending_date.date()
    labels, clicks_by_day, openings_by_day = [], [], []

    for i in range(start, end + 1):
        day = sending_date + timedelta(days=i)
        day_status = ContactMailingStatus.objects.filter(
            newsletter=newsletter,
            creation_date__day=day.day,
            creation_date__month=day.month,
            creation_date__year=day.year,
        )

        opening_stats = get_newsletter_opening_statistics(day_status, recipients)
        click_stats = get_newsletter_clicked_link_statistics(day_status, recipients, 0)
        # Labels
        labels.append(date(day, "D d M y").capitalize())
        # Values
        openings_by_day.append(opening_stats["total_openings"])
        clicks_by_day.append(click_stats["total_clicked_links"])

    b1 = Chart(
        type="bar_3d",
        colour=BAR_COLOR_1,
        text=_("Total openings"),
        tip=_("#val# openings"),
        on_show={"type": "grow-up"},
        values=openings_by_day,
    )

    b2 = Chart(
        type="bar_3d",
        colour=BAR_COLOR_2,
        text=_("Total clicked links"),
        tip=_("#val# clicks"),
        on_show={"type": "grow-up"},
        values=clicks_by_day,
    )

    chart = Chart(bg_colour=BG_COLOR)
    chart.title.text = _("Consultation histogram")
    chart.title.style = "{font-size: 16px; color: #666666; text-align: center; font-weight: bold;}"

    chart.y_axis = {
        "colour": AXIS_COLOR,
        "grid-colour": GRID_COLOR,
        "min": 0,
        "max": max(openings_by_day + clicks_by_day) + 2,
        "steps": max(openings_by_day) / 5,
    }
    chart.x_axis = {
        "colour": AXIS_COLOR,
        "grid-colour": GRID_COLOR,
        "3d": 5,
        "labels": {"labels": labels, "rotate": 60},
    }
    chart.elements = [b1, b2]

    return HttpResponse(chart.render())
Esempio n. 5
0
def view_newsletter_charts(request, slug):
    newsletter = get_object_or_404(Newsletter, slug=slug)

    start = int(request.POST.get('start', 0))
    end = int(request.POST.get('end', 6))

    recipients = newsletter.mailing_list.expedition_set().count()

    sending_date = newsletter.sending_date.date()
    labels, clicks_by_day, openings_by_day = [], [], []

    for i in range(start, end + 1):
        day = sending_date + timedelta(days=i)
        day_status = ContactMailingStatus.objects.filter(
            newsletter=newsletter,
            creation_date__day=day.day,
            creation_date__month=day.month,
            creation_date__year=day.year)

        opening_stats = get_newsletter_opening_statistics(
            day_status, recipients)
        click_stats = get_newsletter_clicked_link_statistics(
            day_status, recipients, 0)
        # Labels
        labels.append(date(day, 'D d M y').capitalize())
        # Values
        openings_by_day.append(opening_stats['total_openings'])
        clicks_by_day.append(click_stats['total_clicked_links'])

    b1 = Chart(type='bar_3d',
               colour=BAR_COLOR_1,
               text=_('Total openings'),
               tip=_('#val# openings'),
               on_show={'type': 'grow-up'},
               values=openings_by_day)

    b2 = Chart(type='bar_3d',
               colour=BAR_COLOR_2,
               text=_('Total clicked links'),
               tip=_('#val# clicks'),
               on_show={'type': 'grow-up'},
               values=clicks_by_day)

    chart = Chart(bg_colour=BG_COLOR)
    chart.title.text = _('Consultation histogram')
    chart.title.style = '{font-size: 16px; color: #666666; text-align: center; font-weight: bold;}'

    chart.y_axis = {
        'colour': AXIS_COLOR,
        'grid-colour': GRID_COLOR,
        'min': 0,
        'max': max(openings_by_day + clicks_by_day) + 2,
        'steps': max(openings_by_day) / 5
    }
    chart.x_axis = {
        'colour': AXIS_COLOR,
        'grid-colour': GRID_COLOR,
        '3d': 5,
        'labels': {
            'labels': labels,
            'rotate': 60
        }
    }
    chart.elements = [b1, b2]

    return HttpResponse(chart.render())