From 2de5f2d3ec56fdd7609e253db54a4b9fca41d92c Mon Sep 17 00:00:00 2001 From: Keith Mitchell Date: Wed, 12 Feb 2014 11:43:24 -0800 Subject: [PATCH] OAuth2: Add personal "flair" scope Allows token bearer to manipulate user flair, and the flair of his submitted links Give /api/setflairenabled "flair" scope Give /api/flairselector "flair" scope --- r2/r2/config/templates.py | 1 + r2/r2/controllers/api.py | 3 ++ r2/r2/lib/jsontemplates.py | 57 ++++++++++++++++++++++++++++++++++++++ r2/r2/lib/pages/pages.py | 3 ++ r2/r2/models/token.py | 6 ++++ 5 files changed, 70 insertions(+) diff --git a/r2/r2/config/templates.py b/r2/r2/config/templates.py index 1fd0d3b92..8ba768a4a 100644 --- a/r2/r2/config/templates.py +++ b/r2/r2/config/templates.py @@ -69,6 +69,7 @@ api('wikipagesettings', WikiSettingsJsonTemplate) api('flairlist', FlairListJsonTemplate) api('flaircsv', FlairCsvJsonTemplate) +api('flairselector', FlairSelectorJsonTemplate) api('subredditstylesheet', StylesheetTemplate) api('subredditstylesheetsource', StylesheetTemplate) diff --git a/r2/r2/controllers/api.py b/r2/r2/controllers/api.py index 30e97d75e..7f800f152 100755 --- a/r2/r2/controllers/api.py +++ b/r2/r2/controllers/api.py @@ -3051,6 +3051,7 @@ class ApiController(RedditController, OAuth2ResourceController): return BoringPage(_("API"), content = results).render() + @require_oauth2_scope("flair") @validatedForm(VUser(), VModhash(), flair_enabled = VBoolean("flair_enabled")) @@ -3204,6 +3205,7 @@ class ApiController(RedditController, OAuth2ResourceController): ModAction.create(c.site, c.user, action='editflair', details='flair_clear_template') + @require_oauth2_scope("flair") @validate(VUser(), user = VFlairAccount('name'), link = VFlairLink('link')) @@ -3220,6 +3222,7 @@ class ApiController(RedditController, OAuth2ResourceController): user = None return FlairSelector(user=user).render() + @require_oauth2_scope("flair") @validatedForm(VUser(), VModhash(), user = VFlairAccount('name'), diff --git a/r2/r2/lib/jsontemplates.py b/r2/r2/lib/jsontemplates.py index d8cac6ed5..7772de86e 100755 --- a/r2/r2/lib/jsontemplates.py +++ b/r2/r2/lib/jsontemplates.py @@ -819,6 +819,63 @@ class FlairCsvJsonTemplate(JsonTemplate): def render(self, thing, *a, **kw): return ObjectTemplate([l.__dict__ for l in thing.results_by_line]) + +class FlairSelectorJsonTemplate(JsonTemplate): + def _template_dict(self, flair): + return {"flair_template_id": flair.flair_template_id, + "flair_position": flair.flair_position, + "flair_text": flair.flair_text, + "flair_css_class": flair.flair_css_class, + "flair_text_editable": flair.flair_text_editable} + + def render(self, thing, *a, **kw): + """Render a list of flair choices into JSON + + Sample output: + { + "choices": [ + { + "flair_css_class": "flair-444", + "flair_position": "right", + "flair_template_id": "5668d204-9388-11e3-8109-080027a38559", + "flair_text": "444", + "flair_text_editable": true + }, + { + "flair_css_class": "flair-nouser", + "flair_position": "right", + "flair_template_id": "58e34d7a-9388-11e3-ab01-080027a38559", + "flair_text": "nouser", + "flair_text_editable": true + }, + { + "flair_css_class": "flair-bar", + "flair_position": "right", + "flair_template_id": "fb01cc04-9391-11e3-b1d6-080027a38559", + "flair_text": "foooooo", + "flair_text_editable": true + } + ], + "current": { + "flair_css_class": "444", + "flair_position": "right", + "flair_template_id": "5668d204-9388-11e3-8109-080027a38559", + "flair_text": "444" + } + } + + """ + choices = [self._template_dict(choice) for choice in thing.choices] + + current_flair = { + "flair_text": thing.text, + "flair_css_class": thing.css_class, + "flair_position": thing.position, + "flair_template_id": thing.matching_template, + } + return ObjectTemplate({"current": current_flair, "choices": choices}) + + class StylesheetTemplate(ThingJsonTemplate): _data_attrs_ = dict( images='_images', diff --git a/r2/r2/lib/pages/pages.py b/r2/r2/lib/pages/pages.py index 281ea8448..855216111 100644 --- a/r2/r2/lib/pages/pages.py +++ b/r2/r2/lib/pages/pages.py @@ -3165,6 +3165,9 @@ class FlairSelector(CachedTemplate): matching_template=matching_template, target_name=target_name) + def render(self, *a, **kw): + return responsive(CachedTemplate.render(self, *a, **kw), True) + def _get_templates(self, site, flair_type, text, css_class): ids = FlairTemplateBySubredditIndex.get_template_ids( site._id, flair_type) diff --git a/r2/r2/models/token.py b/r2/r2/models/token.py index 1397fbf93..6061965f4 100644 --- a/r2/r2/models/token.py +++ b/r2/r2/models/token.py @@ -105,6 +105,12 @@ class OAuth2Scope: "name": _("Edit Posts"), "description": _("Edit and delete my comments and submissions."), }, + "flair": { + "id": "flair", + "name": _("Manage My Flair"), + "description": _("Select my subreddit flair. " + "Change link flair on my submissions."), + }, "history": { "id": "history", "name": _("History"),