示例#1
0
    def ensure_indices(self, leaderboard, date):
        year_key = date.strftime('value.y%Y')
        month_key = date.strftime('value.m%Y%m')
        week_key = 'value.w%s' % Leaderboard.weeknum(date)

        log.info('ensuring indices: %s %s %s', year_key, month_key, week_key)

        leaderboard.ensure_index([
            (year_key, DESCENDING),
            ('_id', ASCENDING)
        ], sparse=True)
        leaderboard.ensure_index([
            (month_key, DESCENDING),
            ('_id', ASCENDING)
        ], sparse=True)
        leaderboard.ensure_index([
            (week_key, DESCENDING),
            ('_id', ASCENDING)
        ], sparse=True)
        leaderboard.ensure_index([
            ('a', DESCENDING),
            ('_id', ASCENDING)
        ])
示例#2
0
文件: views.py 项目: dcrosta/nymwit
def leaderboard(request, window, key):
    url_window = window
    window_name = {
        'y': 'For',
        'm': 'For the month of',
        'w': 'For the week of',
        'a': 'For all time',
    }

    now = datetime.now(utc)

    if window == 'all':
        window = 'a'
        key = ''
        prev, next = None, None

    elif window == 'year':
        window = 'y'
        if key is None:
            key = now.strftime('%Y')

        if int(key) <= 0:
            raise Http404()

        prev, next = int(key) - 1, int(key) + 1

    elif window == 'month':
        window = 'm'
        if key is None:
            key = now.strftime('%Y%m')

        year, month = int(key[:4]), int(key[4:])
        if year <= 0 or month <= 0 or month > 12:
            raise Http404()

        if month == 1:
            prev = '%d%02d' % (year - 1, 12)
            next = '%d%02d' % (year, month + 1)
        elif month == 12:
            prev = '%d%02d' % (year, month - 1)
            next = '%d%02d' % (year + 1, 1)
        else:
            prev = '%d%02d' % (year, month - 1)
            next = '%d%02d' % (year, month + 1)

    elif window == 'week':
        window = 'w'
        if key is None:
            key = Leaderboard.weeknum(now)

        if int(key) <= 0:
            raise Http404()

        prev, next = int(key) - 1, int(key) + 1


    prev = Leaderboard.exists(window, prev) and prev or None
    next = Leaderboard.exists(window, next) and next or None

    if not Leaderboard.exists(window, key):
        return render(request, 'no_leaderboard.html',
                      window_name=window_name.get(window),
                      time_period=time_period(window, key),
                     )

    leaders = Leaderboard.leaders(window, key)

    rank = None
    if request.user.is_authenticated():
        rank = Leaderboard.rank(window, key, request.user.username)

    return render(request, 'leaderboard.html',
                  leaders=leaders,
                  rank=rank,
                  window_name=window_name.get(window),
                  time_period=time_period(window, key),
                  url_window=url_window,
                  prev=prev,
                  prev_time_period=time_period(window, prev),
                  next=next,
                  next_time_period=time_period(window, next),
                 )