def test_get_newsletter_clicked_link_statistics(self):
        stats = get_newsletter_clicked_link_statistics(self.status, self.recipients, 0)
        self.assertEquals(stats['total_clicked_links'], 0)
        self.assertEquals(stats['total_clicked_links_percent'], 0)
        self.assertEquals(stats['double_clicked_links'], 0)
        self.assertEquals(stats['double_clicked_links_percent'], 0.0)
        self.assertEquals(stats['unique_clicked_links'], 0)
        self.assertEquals(stats['unique_clicked_links_percent'], 0)
        self.assertEquals(stats['clicked_links_by_openings'], 0.0)
        self.assertEquals(stats['clicked_links_average'], 0.0)

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

        stats = get_newsletter_clicked_link_statistics(status, self.recipients, 3)
        self.assertEquals(stats['total_clicked_links'], 5)
        self.assertEquals(stats['total_clicked_links_percent'], 125.0)
        self.assertEquals(stats['double_clicked_links'], 2)
        self.assertEquals(stats['double_clicked_links_percent'], 50.0)
        self.assertEquals(stats['unique_clicked_links'], 3)
        self.assertEquals(stats['unique_clicked_links_percent'], 75.0)
        self.assertEquals(stats['clicked_links_by_openings'], 166.66666666666669)
        self.assertEquals(stats['clicked_links_average'], 1.6666666666666667)
    def test_get_newsletter_clicked_link_statistics(self):
        stats = get_newsletter_clicked_link_statistics(self.status,
                                                       self.recipients, 0)
        self.assertEquals(stats['total_clicked_links'], 0)
        self.assertEquals(stats['total_clicked_links_percent'], 0)
        self.assertEquals(stats['double_clicked_links'], 0)
        self.assertEquals(stats['double_clicked_links_percent'], 0.0)
        self.assertEquals(stats['unique_clicked_links'], 0)
        self.assertEquals(stats['unique_clicked_links_percent'], 0)
        self.assertEquals(stats['clicked_links_by_openings'], 0.0)
        self.assertEquals(stats['clicked_links_average'], 0.0)

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

        stats = get_newsletter_clicked_link_statistics(status, self.recipients,
                                                       3)
        self.assertEquals(stats['total_clicked_links'], 5)
        self.assertEquals(stats['total_clicked_links_percent'], 125.0)
        self.assertEquals(stats['double_clicked_links'], 2)
        self.assertEquals(stats['double_clicked_links_percent'], 50.0)
        self.assertEquals(stats['unique_clicked_links'], 3)
        self.assertEquals(stats['unique_clicked_links_percent'], 75.0)
        self.assertEquals(stats['clicked_links_by_openings'],
                          166.66666666666669)
        self.assertEquals(stats['clicked_links_average'], 1.6666666666666667)
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())
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())
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())