def wrapped(cls, *args, **kwargs): def is_mod_of_all(user, subreddit): mod_subs = user.get_cached_moderated_reddits() subs = six.text_type(subreddit).lower().split('+') return all(sub in mod_subs for sub in subs) login_req = login and function.__name__ not in login_exceptions mod_req = mod and function.__name__ not in moderator_exceptions # This segment of code uses hasattr to determine what instance type # the function was called on. We could use isinstance if we wanted # to import the types at runtime (decorators is used by all the # types). if cls is None: # Occurs with (un)friend assert login_req raise errors.LoginRequired(function.__name__) elif hasattr(cls, 'reddit_session'): obj = cls.reddit_session if mod: if hasattr(cls, 'display_name'): # Subreddit object subreddit = cls else: # Defer access until necessary for RedditContentObject. # This is because scoped sessions may not require this # attribute to exist, thus it might not be set. subreddit = False else: subreddit = None else: obj = cls subreddit = args[0] if mod else None # This function sets _use_oauth for one time use only. # Verify that statement is actually true. assert not obj._use_oauth # pylint: disable-msg=W0212 if scope and obj.has_scope(scope): obj._use_oauth = True # pylint: disable-msg=W0212 elif oauth_only: raise errors.OAuthScopeRequired(function.__name__, scope) elif login_req and obj.is_logged_in(): if subreddit is False: # Now fetch the subreddit attribute. There is no good # reason for it to not be set during a logged in session. subreddit = cls.subreddit if mod_req and not is_mod_of_all(obj.user, subreddit): if scope: raise errors.ModeratorOrScopeRequired( function.__name__, scope) raise errors.ModeratorRequired(function.__name__) elif login_req: if scope: raise errors.LoginOrScopeRequired(function.__name__, scope) raise errors.LoginRequired(function.__name__) try: return function(cls, *args, **kwargs) finally: obj._use_oauth = False # pylint: disable-msg=W0212
def wrap(function, *args, **kwargs): if args[0] is None: # Occurs with (un)friend assert login raise errors.LoginRequired(function.__name__) # This segment of code uses hasattr to determine what instance type # the function was called on. We could use isinstance if we wanted # to import the types at runtime (decorators is used by all the # types). if mod: if hasattr(args[0], 'reddit_session'): # Defer access until necessary for RedditContentObject. # This is because scoped sessions may not require this # attribute to exist, thus it might not be set. from praw.objects import Subreddit subreddit = args[0] if isinstance(args[0], Subreddit) \ else False else: subreddit = kwargs.get('subreddit', args[1] if len(args) > 1 else None) if subreddit is None: # Try the default value defaults = six.get_function_defaults(function) subreddit = defaults[0] if defaults else None else: subreddit = None obj = getattr(args[0], 'reddit_session', args[0]) # This function sets _use_oauth for one time use only. # Verify that statement is actually true. assert not obj._use_oauth # pylint: disable=W0212 if scope and obj.has_scope(scope): obj._use_oauth = True # pylint: disable=W0212 elif oauth_only: raise errors.OAuthScopeRequired(function.__name__, scope) elif login and obj.is_logged_in(): if subreddit is False: # Now fetch the subreddit attribute. There is no good # reason for it to not be set during a logged in session. subreddit = args[0].subreddit if mod and not _is_mod_of_all(obj.user, subreddit): if scope: raise errors.ModeratorOrScopeRequired( function.__name__, scope) raise errors.ModeratorRequired(function.__name__) elif login: if scope: raise errors.LoginOrScopeRequired(function.__name__, scope) raise errors.LoginRequired(function.__name__) try: return function(*args, **kwargs) finally: obj._use_oauth = False # pylint: disable=W0212
def login_required_function(self, *args, **kwargs): if isinstance(self, RedditContentObject): user = self.reddit_session.user modhash = self.reddit_session.modhash else: user = self.user modhash = self.modhash if user is None or modhash is None: raise errors.LoginRequired('%r requires login' % function.__name__) else: return function(self, *args, **kwargs)