From caf1316d3c6e01583550f992cd1532fb0b562dff Mon Sep 17 00:00:00 2001 From: bsimpson63 Date: Thu, 14 Feb 2013 11:39:43 -0500 Subject: [PATCH] Add next/prev buttons to promoted link traffic listings. --- r2/r2/controllers/front.py | 13 +++++-- r2/r2/lib/pages/trafficpages.py | 49 +++++++++++++++++++----- r2/r2/templates/promotedlinktraffic.html | 20 ++++++++++ 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/r2/r2/controllers/front.py b/r2/r2/controllers/front.py index fed865504..73677868d 100755 --- a/r2/r2/controllers/front.py +++ b/r2/r2/controllers/front.py @@ -994,9 +994,16 @@ class FrontController(RedditController, OAuth2ResourceController): @require_oauth2_scope("modtraffic") @validate(VTrafficViewer('article'), - article = VLink('article')) - def GET_traffic(self, article): - content = trafficpages.PromotedLinkTraffic(article) + article = VLink('article'), + before = VDate('before', format='%Y%m%d%H'), + after = VDate('after', format='%Y%m%d%H')) + def GET_traffic(self, article, before, after): + if before: + before = before.replace(tzinfo=None) + if after: + after = after.replace(tzinfo=None) + + content = trafficpages.PromotedLinkTraffic(article, before, after) if c.render_style == 'csv': return content.as_csv() diff --git a/r2/r2/lib/pages/trafficpages.py b/r2/r2/lib/pages/trafficpages.py index fe951c8b2..d07de5d72 100644 --- a/r2/r2/lib/pages/trafficpages.py +++ b/r2/r2/lib/pages/trafficpages.py @@ -23,9 +23,10 @@ import collections import datetime +import urllib from pylons.i18n import _ -from pylons import g, c +from pylons import g, c, request import babel.core from r2.lib import promote @@ -440,8 +441,13 @@ def _is_promo_preliminary(end_date): class PromotedLinkTraffic(RedditTraffic): - def __init__(self, thing): + def __init__(self, thing, before=None, after=None): self.thing = thing + self.before = before + self.after = after + self.period = datetime.timedelta(days=31) + self.prev = None + self.next = None editable = c.user_is_sponsor or c.user._id == thing.author_id self.viewer_list = TrafficViewerList(thing, editable) @@ -449,17 +455,42 @@ class PromotedLinkTraffic(RedditTraffic): RedditTraffic.__init__(self, None) def make_tables(self): - start, end = promote.get_total_run(self.thing) + now = datetime.datetime.utcnow().replace(minute=0, second=0, + microsecond=0) - if not start or not end: + promo_start, promo_end = promote.get_total_run(self.thing) + promo_end = min(now, promo_end) + + if not promo_start or not promo_end: self.history = [] return - now = datetime.datetime.utcnow().replace(minute=0, second=0, - microsecond=0) - end = min(end, now) - cutoff = end - datetime.timedelta(days=31) - start = max(start, cutoff) + start = self.after + end = self.before + + if not start and not end: + end = promo_end + start = end - self.period + + elif not end: + end = start + self.period + + elif not start: + start = end - self.period + + if start > promo_start: + p = request.get.copy() + p.update({'after':None, 'before':start.strftime('%Y%m%d%H')}) + self.prev = '%s?%s' % (request.path, urllib.urlencode(p)) + else: + start = promo_start + + if end < promo_end: + p = request.get.copy() + p.update({'after':end.strftime('%Y%m%d%H'), 'before':None}) + self.next = '%s?%s' % (request.path, urllib.urlencode(p)) + else: + end = promo_end fullname = self.thing._fullname imps = traffic.AdImpressionsByCodename.promotion_history(fullname, diff --git a/r2/r2/templates/promotedlinktraffic.html b/r2/r2/templates/promotedlinktraffic.html index c97b84c0e..dfa344310 100644 --- a/r2/r2/templates/promotedlinktraffic.html +++ b/r2/r2/templates/promotedlinktraffic.html @@ -22,6 +22,7 @@ <%inherit file="reddittraffic.html"/> <%namespace file="reddittraffic.html" import="load_timeseries_js"/> +<%namespace file="utils.html" import="plain_link" /> <%! from r2.lib.strings import strings @@ -100,4 +101,23 @@ % if thing.is_preliminary:

* ${_("totals are preliminary until 24 hours after the end of the promotion.")}

% endif + + ${nextprev()} + + + +<%def name="nextprev()"> + %if thing.prev or thing.next: +

${_("view more:")} + %if thing.prev: + ${plain_link(unsafe("‹ " + _("prev")), thing.prev, _sr_path=False, rel="nofollow prev")} + %endif + %if thing.prev and thing.next: + + %endif + %if thing.next: + ${plain_link(unsafe(_("next") + " ›"), thing.next, _sr_path=False, rel="nofollow next")} + %endif +

+ %endif