Ejemplo n.º 1
0
    def as_csv(cls, thing):
        """Return the traffic data in CSV format for reports."""

        import csv
        import cStringIO

        start, end = promote.get_traffic_dates(thing)
        history = cls.get_traffic(thing, start, end)
        out = cStringIO.StringIO()
        writer = csv.writer(out)

        headers = [
            _("date and time (UTC)"),
            _("impressions"),
            _("clicks"),
            _("click-through rate (%)"),
        ]

        # peek at the first row's values tuple to see if we have spent
        first_row = history and history[0]
        has_spent = first_row and len(first_row[2]) == 4
        if has_spent:
            headers.insert(1, _("spent"))

        writer.writerow(headers)
        for date, datestr, values in history:
            # flatten (date, datestr, value-tuple) to (date, value1, value2...)
            writer.writerow((date, ) + tuple(value
                                             for value, formated in values))

        return out.getvalue()
Ejemplo n.º 2
0
    def as_csv(cls, thing):
        """Return the traffic data in CSV format for reports."""

        import csv
        import cStringIO

        start, end = promote.get_traffic_dates(thing)
        history = cls.get_traffic(thing, start, end)
        out = cStringIO.StringIO()
        writer = csv.writer(out)

        headers = [
            _("date and time (UTC)"),
            _("impressions"),
            _("clicks"),
            _("click-through rate (%)"),
        ]

        # peek at the first row's values tuple to see if we have spent
        first_row = history and history[0]
        has_spent = first_row and len(first_row[2]) == 4
        if has_spent:
            headers.insert(1, _("spent"))

        writer.writerow(headers)
        for date, datestr, values in history:
            # flatten (date, datestr, value-tuple) to (date, value1, value2...)
            writer.writerow((date,) + tuple(value for value, formated in values))

        return out.getvalue()
Ejemplo n.º 3
0
    def check_dates(self, thing):
        """Shorten range for display and add next/prev buttons."""
        start, end = promote.get_traffic_dates(thing)

        if self.period:
            display_start = self.after
            display_end = self.before

            if not display_start and not display_end:
                display_end = end
                display_start = end - self.period
            elif not display_end:
                display_end = display_start + self.period
            elif not display_start:
                display_start = display_end - self.period

            if display_start > start:
                p = request.GET.copy()
                p.update({"after": None, "before": display_start.strftime("%Y%m%d%H")})
                self.prev = "%s?%s" % (request.path, urllib.urlencode(p))
            else:
                display_start = start

            if display_end < end:
                p = request.GET.copy()
                p.update({"after": display_end.strftime("%Y%m%d%H"), "before": None})
                self.next = "%s?%s" % (request.path, urllib.urlencode(p))
            else:
                display_end = end
        else:
            display_start, display_end = start, end

        return display_start, display_end
Ejemplo n.º 4
0
    def check_dates(self, thing):
        """Shorten range for display and add next/prev buttons."""
        start, end = promote.get_traffic_dates(thing)

        # Check date of latest traffic (campaigns can end early).
        history = list(get_promo_traffic(thing, start, end))
        if history:
            end = max(date for date, data in history)
            end = end.replace(
                tzinfo=g.tz)  # get_promo_traffic returns tz naive
            # datetimes but is actually g.tz

        if self.period:
            display_start = self.after
            display_end = self.before

            if not display_start and not display_end:
                display_end = end
                display_start = end - self.period
            elif not display_end:
                display_end = display_start + self.period
            elif not display_start:
                display_start = display_end - self.period

            if display_start > start:
                p = request.GET.copy()
                p.update({
                    'after': None,
                    'before': display_start.strftime('%Y%m%d%H'),
                })
                self.prev = '%s?%s' % (request.path, urllib.urlencode(p))
            else:
                display_start = start

            if display_end < end:
                p = request.GET.copy()
                p.update({
                    'after': display_end.strftime('%Y%m%d%H'),
                    'before': None,
                })
                self.next = '%s?%s' % (request.path, urllib.urlencode(p))
            else:
                display_end = end
        else:
            display_start, display_end = start, end

        return display_start, display_end
Ejemplo n.º 5
0
    def check_dates(self, thing):
        """Shorten range for display and add next/prev buttons."""
        start, end = promote.get_traffic_dates(thing)

        # Check date of latest traffic (campaigns can end early).
        history = list(get_promo_traffic(thing, start, end))
        if history:
            end = max(date for date, data in history)
            end = end.replace(tzinfo=g.tz)  # get_promo_traffic returns tz naive
                                            # datetimes but is actually g.tz

        if self.period:
            display_start = self.after
            display_end = self.before

            if not display_start and not display_end:
                display_end = end
                display_start = end - self.period
            elif not display_end:
                display_end = display_start + self.period
            elif not display_start:
                display_start = display_end - self.period

            if display_start > start:
                p = request.GET.copy()
                p.update({
                    'after': None,
                    'before': display_start.strftime('%Y%m%d%H'),
                })
                self.prev = '%s?%s' % (request.path, urllib.urlencode(p))
            else:
                display_start = start

            if display_end < end:
                p = request.GET.copy()
                p.update({
                    'after': display_end.strftime('%Y%m%d%H'),
                    'before': None,
                })
                self.next = '%s?%s' % (request.path, urllib.urlencode(p))
            else:
                display_end = end
        else:
            display_start, display_end = start, end

        return display_start, display_end
Ejemplo n.º 6
0
    def as_csv(cls, thing):
        """Return the traffic data in CSV format for reports."""

        import csv
        import cStringIO

        start, end = promote.get_traffic_dates(thing)
        history = cls.get_hourly_traffic(thing, start, end)

        out = cStringIO.StringIO()
        writer = csv.writer(out)

        writer.writerow((_("date and time (UTC)"), _("impressions"), _("clicks"), _("click-through rate (%)")))
        for date, datestr, values in history:
            # flatten (date, datestr, value-tuple) to (date, value1, value2...)
            writer.writerow((date,) + values)

        return out.getvalue()
Ejemplo n.º 7
0
    def as_csv(cls, thing):
        """Return the traffic data in CSV format for reports."""

        import csv
        import cStringIO

        start, end = promote.get_traffic_dates(thing)
        history = cls.get_hourly_traffic(thing, start, end)

        out = cStringIO.StringIO()
        writer = csv.writer(out)

        writer.writerow(
            (_("date and time (UTC)"), _("impressions"), _("clicks"),
             _("click-through rate (%)")))
        for date, datestr, values in history:
            # flatten (date, datestr, value-tuple) to (date, value1, value2...)
            writer.writerow((date, ) + values)

        return out.getvalue()
Ejemplo n.º 8
0
    def check_dates(self, thing):
        """Shorten range for display and add next/prev buttons."""
        start, end = promote.get_traffic_dates(thing)

        if self.period:
            display_start = self.after
            display_end = self.before

            if not display_start and not display_end:
                display_end = end
                display_start = end - self.period
            elif not display_end:
                display_end = display_start + self.period
            elif not display_start:
                display_start = display_end - self.period

            if display_start > start:
                p = request.GET.copy()
                p.update({
                    'after': None,
                    'before': display_start.strftime('%Y%m%d%H'),
                })
                self.prev = '%s?%s' % (request.path, urllib.urlencode(p))
            else:
                display_start = start

            if display_end < end:
                p = request.GET.copy()
                p.update({
                    'after': display_end.strftime('%Y%m%d%H'),
                    'before': None,
                })
                self.next = '%s?%s' % (request.path, urllib.urlencode(p))
            else:
                display_end = end
        else:
            display_start, display_end = start, end

        return display_start, display_end
Ejemplo n.º 9
0
def get_billable_traffic(campaign):
    """Get traffic for dates when PromoCampaign is active."""
    start, end = promote.get_traffic_dates(campaign)
    return get_promo_traffic(campaign, start, end)
Ejemplo n.º 10
0
def get_billable_traffic(campaign):
    """Get traffic for dates when PromoCampaign is active."""
    start, end = promote.get_traffic_dates(campaign)
    return get_promo_traffic(campaign, start, end)