Skip to content

Commit

Permalink
Merge 3a326ba into 4de22f2
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonCoderAS committed Nov 20, 2020
2 parents 4de22f2 + 3a326ba commit 16b0a85
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 145 deletions.
11 changes: 11 additions & 0 deletions CHANGES.rst
Expand Up @@ -62,6 +62,12 @@ Unreleased
* :attr:`.WebSocketException.original_exception` is deprecated and slated for removal in
PRAW 8.0.

**Deprecated**

* :class:`.SubredditStylesheet`\ 's ``__call__`` method
(``subreddit.stylesheet()``) is deprecated and slotted for removal in PRAW
8.0.

**Fixed**

* An issue where certain subreddit settings could not be set through
Expand All @@ -74,6 +80,11 @@ Unreleased
when Reddit fails to post an image or video post.


**Removed**

* Class ``Stylesheet`` no longer exists. It has been merged with
:class:`.SubredditStylesheet`.

7.0.0 (2020/04/24)
------------------

Expand Down
1 change: 0 additions & 1 deletion praw/models/__init__.py
Expand Up @@ -52,7 +52,6 @@
)
from .reddit.wikipage import WikiPage
from .redditors import Redditors
from .stylesheet import Stylesheet
from .subreddits import Subreddits
from .trophy import Trophy
from .user import User
54 changes: 48 additions & 6 deletions praw/models/reddit/subreddit.py
Expand Up @@ -9,6 +9,7 @@
from os.path import basename, dirname, isfile, join
from typing import List
from urllib.parse import urljoin
from warnings import warn
from xml.etree.ElementTree import XML

import websocket
Expand Down Expand Up @@ -481,7 +482,7 @@ def stylesheet(self):
.. code-block:: python
subreddit = reddit.subreddit("SUBREDDIT")
stylesheet = subreddit.stylesheet()
stylesheet = subreddit.stylesheet.stylesheet
stylesheet += ".test{color:blue}"
subreddit.stylesheet.update(stylesheet)
Expand Down Expand Up @@ -3197,22 +3198,50 @@ def submissions(self, **stream_options):
return stream_generator(self.subreddit.new, **stream_options)


class SubredditStylesheet:
class SubredditStylesheet(RedditBase):
"""Provides a set of stylesheet functions to a Subreddit.
**Typical Attributes**
This table describes attributes that typically belong to objects of this
class. Since attributes are dynamically provided (see
:ref:`determine-available-attributes-of-an-object`), there is not a
guarantee that these attributes will always be present, nor is this list
necessarily comprehensive.
======================= ===================================================
Attribute Description
======================= ===================================================
``images`` A :class:`list` of images used by the stylesheet.
``stylesheet`` The contents of the stylesheet, as CSS.
======================= ===================================================
For example, to add the css data ``.test{color:blue}`` to the existing stylesheet:
.. code-block:: python
subreddit = reddit.subreddit("SUBREDDIT")
stylesheet = subreddit.stylesheet()
stylesheet = subreddit.stylesheet.contents
stylesheet += ".test{color:blue}"
subreddit.stylesheet.update(stylesheet)
"""

STR_FIELD = "subreddit_id"

@property
def contents(self) -> str:
"""Obtain the contents of the subreddit's stylesheet."""
return self.stylesheet

def __call__(self):
"""Return the subreddit's stylesheet.
"""Return the subreddit's stylesheet (Deprecated).
.. note:: As of PRAW 7.1, the model containing the stylesheet data and
the class holding the methods have been combined into one.
.. deprecated:: 7.1
This method will be removed in the next major release of PRAW (8.0)
To be used as:
Expand All @@ -3221,8 +3250,14 @@ def __call__(self):
stylesheet = reddit.subreddit("SUBREDDIT").stylesheet()
"""
url = API_PATH["about_stylesheet"].format(subreddit=self.subreddit)
return self.subreddit._reddit.get(url)
warn(
"This method is deprecated and will be removed in PRAW 8.0. "
"Removing the call will still result in the same functionality. "
"To do so, change `subreddit.stylesheet()` to `subreddit.stylesheet`.",
category=DeprecationWarning,
stacklevel=2,
)
return self

def __init__(self, subreddit):
"""Create a SubredditStylesheet instance.
Expand All @@ -3237,6 +3272,13 @@ def __init__(self, subreddit):
"""
self.subreddit = subreddit
super().__init__(subreddit._reddit, None)

def _fetch(self):
url = API_PATH["about_stylesheet"].format(subreddit=self.subreddit)
data = self._reddit.get(url)
data = data["data"]
self.__dict__.update(data)

def _update_structured_styles(self, style_data):
url = API_PATH["structured_styles"].format(subreddit=self.subreddit)
Expand Down
23 changes: 0 additions & 23 deletions praw/models/stylesheet.py

This file was deleted.

1 change: 0 additions & 1 deletion praw/reddit.py
Expand Up @@ -390,7 +390,6 @@ def _prepare_objector(self):
"more": models.MoreComments,
"post-flair": models.PostFlairWidget,
"rule": models.Rule,
"stylesheet": models.Stylesheet,
"subreddit-rules": models.RulesWidget,
"textarea": models.TextArea,
"widget": models.Widget,
Expand Down
221 changes: 221 additions & 0 deletions tests/integration/cassettes/TestSubredditStylesheet.test_attrs.json
@@ -0,0 +1,221 @@
{
"http_interactions": [
{
"recorded_at": "2020-03-21T23:53:51",
"request": {
"body": {
"encoding": "utf-8",
"string": "grant_type=password&password=<PASSWORD>&username=<USERNAME>"
},
"headers": {
"Accept": [
"*/*"
],
"Accept-Encoding": [
"identity"
],
"Authorization": [
"Basic <BASIC_AUTH>"
],
"Connection": [
"keep-alive"
],
"Content-Length": [
"61"
],
"Content-Type": [
"application/x-www-form-urlencoded"
],
"User-Agent": [
"<USER_AGENT> PRAW/7.0.0.dev0 prawcore/1.0.1"
]
},
"method": "POST",
"uri": "https://www.reddit.com/api/v1/access_token"
},
"response": {
"body": {
"encoding": "UTF-8",
"string": "{\"access_token\": \"<ACCESS_TOKEN>\", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"*\"}"
},
"headers": {
"Accept-Ranges": [
"bytes"
],
"Connection": [
"keep-alive"
],
"Content-Length": [
"118"
],
"Content-Type": [
"application/json; charset=UTF-8"
],
"Date": [
"Sat, 21 Mar 2020 23:53:51 GMT"
],
"Server": [
"snooserv"
],
"Set-Cookie": [
"edgebucket=b9GRcPdORfNO8naijI; Domain=reddit.com; Max-Age=63071999; Path=/; secure"
],
"Strict-Transport-Security": [
"max-age=15552000; includeSubDomains; preload"
],
"Via": [
"1.1 varnish"
],
"X-Cache": [
"MISS"
],
"X-Cache-Hits": [
"0"
],
"X-Moose": [
"majestic"
],
"X-Served-By": [
"cache-lga21941-LGA"
],
"X-Timer": [
"S1584834832.507552,VS0,VE314"
],
"cache-control": [
"max-age=0, must-revalidate"
],
"x-content-type-options": [
"nosniff"
],
"x-frame-options": [
"SAMEORIGIN"
],
"x-xss-protection": [
"1; mode=block"
]
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://www.reddit.com/api/v1/access_token"
}
},
{
"recorded_at": "2020-03-21T23:53:52",
"request": {
"body": {
"encoding": "utf-8",
"string": ""
},
"headers": {
"Accept": [
"*/*"
],
"Accept-Encoding": [
"identity"
],
"Authorization": [
"bearer <ACCESS_TOKEN>"
],
"Connection": [
"keep-alive"
],
"Cookie": [
"edgebucket=b9GRcPdORfNO8naijI"
],
"User-Agent": [
"<USER_AGENT> PRAW/7.0.0.dev0 prawcore/1.0.1"
]
},
"method": "GET",
"uri": "https://oauth.reddit.com/r/<TEST_SUBREDDIT>/about/stylesheet/?raw_json=1"
},
"response": {
"body": {
"encoding": "UTF-8",
"string": "{\"kind\": \"stylesheet\", \"data\": {\"images\": [{\"url\": \"https://b.thumbs.redditmedia.com/pFZFx9GVBHDtoJp_7We5AG3aWT1ldcLl6n55wYL2kro.png\", \"link\": \"url(%%Watermark%%)\", \"name\": \"Watermark\"}], \"subreddit_id\": \"t5_2bhxu0\", \"stylesheet\": \"/* test */\"}}"
},
"headers": {
"Accept-Ranges": [
"bytes"
],
"Connection": [
"keep-alive"
],
"Content-Length": [
"245"
],
"Content-Type": [
"application/json; charset=UTF-8"
],
"Date": [
"Sat, 21 Mar 2020 23:53:52 GMT"
],
"Server": [
"snooserv"
],
"Set-Cookie": [
"loid=00000000005bn5wlvy.2.1577584524387.Z0FBQUFBQmVkcWtRRnFVcTJ1VC11Qnd3dUpDcVpRLXNPb0U2d1VvV2pxZFNCRmJMZW00ODRzcURTZXFtYlpkWlpfWXdkZllzZ0MyM0FQVFo0SXJURFdfVEhnVlBPNVpHTkFhaEdyMmhLRFVsT2dIMUhKYlZRaFQ0Ynh1blpvV2hoTWx0R0NQTFF1U3Y; Domain=reddit.com; Max-Age=63071999; Path=/; expires=Mon, 21-Mar-2022 23:53:52 GMT; secure; SameSite=None; Secure",
"session_tracker=fIz54mlWmWeZTqgtox.0.1584834832000.Z0FBQUFBQmVkcWtRMnBxRS1WU1lvNERSTkV2SnNuSWI4eVpwWkphTEVyalRjSjNNSDM1d3ptOEFIbWlxZHMzbmR4M0NPWEtUTmRGNVV6cTVJQkJ4MXpLUGhEX082R01VNHk2QmYxQ0NSZ1Y5UjUwNVJrZFFNNWZFT3ZBQTM4XzVYY1NJdC14VzBJVWI; Domain=reddit.com; Max-Age=7199; Path=/; expires=Sun, 22-Mar-2020 01:53:52 GMT; secure; SameSite=None; Secure",
"csv=1; Max-Age=63072000; Domain=.reddit.com; Path=/; Secure; SameSite=None"
],
"Strict-Transport-Security": [
"max-age=15552000; includeSubDomains; preload"
],
"Via": [
"1.1 varnish"
],
"X-Cache": [
"MISS"
],
"X-Cache-Hits": [
"0"
],
"X-Moose": [
"majestic"
],
"X-Served-By": [
"cache-lga21925-LGA"
],
"X-Timer": [
"S1584834832.963531,VS0,VE121"
],
"cache-control": [
"private, s-maxage=0, max-age=0, must-revalidate, no-store, max-age=0, must-revalidate"
],
"expires": [
"-1"
],
"x-content-type-options": [
"nosniff"
],
"x-frame-options": [
"SAMEORIGIN"
],
"x-ratelimit-remaining": [
"599.0"
],
"x-ratelimit-reset": [
"368"
],
"x-ratelimit-used": [
"1"
],
"x-ua-compatible": [
"IE=edge"
],
"x-xss-protection": [
"1; mode=block"
]
},
"status": {
"code": 200,
"message": "OK"
},
"url": "https://oauth.reddit.com/r/<TEST_SUBREDDIT>/about/stylesheet/?raw_json=1"
}
}
],
"recorded_with": "betamax/0.8.1"
}

0 comments on commit 16b0a85

Please sign in to comment.