Skip to content
This repository has been archived by the owner on May 22, 2022. It is now read-only.

Commit

Permalink
Stop gating trophies to OAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
kemitche committed Apr 17, 2015
1 parent 66fce65 commit 6203bac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions r2/r2/config/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def make_map():

mc('/user/:username/about', controller='user', action='about',
where='overview')
mc('/user/:username/trophies', controller='user', action='trophies')
mc('/user/:username/:where', controller='user', action='listing',
where='overview')
mc('/user/:username/saved/:category', controller='user', action='listing',
Expand Down
14 changes: 3 additions & 11 deletions r2/r2/controllers/apiv1/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
from r2.controllers.reddit_base import OAuth2OnlyController
from r2.lib.jsontemplates import (
FriendTableItemJsonTemplate,
get_usertrophies,
IdentityJsonTemplate,
KarmaListJsonTemplate,
PrefsJsonTemplate,
TrophyListJsonTemplate,
)
from r2.lib.pages import FriendTableItem
from r2.lib.validator import (
Expand Down Expand Up @@ -78,14 +78,6 @@ def GET_prefs(self, fields):
resp = PrefsJsonTemplate(fields).data(c.oauth_user)
return self.api_wrapper(resp)

def _get_usertrophies(self, user):
trophies = Trophy.by_account(user)
def visible_trophy(trophy):
return trophy._thing2.awardtype != 'invisible'
trophies = filter(visible_trophy, trophies)
resp = TrophyListJsonTemplate().render(trophies)
return self.api_wrapper(resp.finalize())

@require_oauth2_scope("read")
@validate(
user=VAccountByName('username'),
Expand All @@ -96,7 +88,7 @@ def visible_trophy(trophy):
)
def GET_usertrophies(self, user):
"""Return a list of trophies for the a given user."""
return self._get_usertrophies(user)
return self.api_wrapper(get_usertrophies(user))

@require_oauth2_scope("identity")
@validate(
Expand All @@ -108,7 +100,7 @@ def GET_usertrophies(self, user):
)
def GET_trophies(self):
"""Return a list of trophies for the current user."""
return self._get_usertrophies(c.oauth_user)
return self.api_wrapper(get_usertrophies(c.oauth_user))

@require_oauth2_scope("mysubreddits")
@validate(
Expand Down
10 changes: 10 additions & 0 deletions r2/r2/controllers/listingcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from r2.models.query_cache import CachedQuery, MergedCachedQuery
from r2.config.extensions import is_api
from r2.lib.filters import _force_unicode
from r2.lib.jsontemplates import get_usertrophies
from r2.lib.pages import *
from r2.lib.pages.things import wrap_links
from r2.lib.menus import TimeMenu, SortMenu, RecSortMenu, ProfileSortMenu
Expand Down Expand Up @@ -944,6 +945,15 @@ def GET_rel_user_redirect(self, rest=""):
url += "?" + request.query_string
return self.redirect(url, code=302)

@validate(
user=VAccountByName('username'),
)
def GET_trophies(self, user):
"""Return a list of trophies for the a given user."""
if not is_api():
return self.abort404()
return self.api_wrapper(get_usertrophies(user))


class MessageController(ListingController):
show_nums = False
Expand Down
14 changes: 12 additions & 2 deletions r2/r2/lib/jsontemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
from mako.template import Template
from r2.config import feature
from r2.config.extensions import get_api_subtype
from r2.lib.filters import spaceCompress, safemarkdown
from r2.models import Account, Report
from r2.lib.filters import spaceCompress, safemarkdown, _force_unicode
from r2.models import Account, Report, Trophy
from r2.models.subreddit import SubSR
from r2.models.token import OAuth2Scope, extra_oauth2_scope
import time, pytz
Expand Down Expand Up @@ -1255,6 +1255,16 @@ def data(self, karmas):
def kind(self, wrapped):
return "KarmaList"


def get_usertrophies(user):
trophies = Trophy.by_account(user)
def visible_trophy(trophy):
return trophy._thing2.awardtype != 'invisible'
trophies = filter(visible_trophy, trophies)
resp = TrophyListJsonTemplate().render(trophies)
return resp.finalize()


class TrophyJsonTemplate(ThingJsonTemplate):
_data_attrs_ = dict(
award_id="award._id36",
Expand Down

0 comments on commit 6203bac

Please sign in to comment.