http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10422
Revision: 10422 Author: valhallasw Date: 2012-06-26 19:10:02 +0000 (Tue, 26 Jun 2012) Log Message: ----------- Added must_be decorator to use with site methods. This decorator will make sure the user is logged in (as 'user' or as 'sysop', currently) before the method is run.
+ tests + applied to APISite.editpage
Modified Paths: -------------- branches/rewrite/pywikibot/site.py branches/rewrite/tests/dry_site_tests.py
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2012-06-26 19:05:50 UTC (rev 10421) +++ branches/rewrite/pywikibot/site.py 2012-06-26 19:10:02 UTC (rev 10422) @@ -579,6 +579,36 @@ raise NotImplementedError
+def must_be(group=None,right=None): + """ Decorator to require a certain user status. For now, only the values + group = 'user' and group = 'sysop' are supported. The right property + will be ignored for now. + + @param group: the group the logged in user should belong to + legal values: 'user' and 'sysop' + @param right: the rights the logged in user hsould have + not supported yet and thus ignored. + @returns: a decorator to make sure the requirement is statisfied when + the decorated function is called. + """ + if group == 'user': + run = lambda self: self.login(False) + elif group == 'sysop': + run = lambda self: self.login(True) + else: + raise Exception("Not implemented") + + def decorator(fn): + def callee(self, *args, **kwargs): + run(self) + return fn(self, *args, **kwargs) + callee.__name__ = fn.__name__ + callee.__doc__ = fn.__doc__ + return callee + + return decorator + + class APISite(BaseSite): """API interface to MediaWiki site.
@@ -2363,6 +2393,7 @@ "editconflict": "Page %(title)s not saved due to edit conflict.", }
+ @must_be(group='user') def editpage(self, page, summary, minor=True, notminor=False, bot=True, recreate=True, createonly=False, watch=None): """Submit an edited Page object to be saved to the wiki.
Modified: branches/rewrite/tests/dry_site_tests.py =================================================================== --- branches/rewrite/tests/dry_site_tests.py 2012-06-26 19:05:50 UTC (rev 10421) +++ branches/rewrite/tests/dry_site_tests.py 2012-06-26 19:10:02 UTC (rev 10422) @@ -22,3 +22,30 @@ x._userinfo['groups'] = ['sysop'] assert x.logged_in(True) assert not x.logged_in(False) + +class SiteMock(object): + last_login = None + last_fn_called = False + + def login(self, as_sysop): + self.last_login = 'sysop' if as_sysop else 'user' + + def inner_fn(self, *args, **kwargs): + self.last_fn_called = (args, kwargs) + return (args, kwargs) + +def test_must_be_user(): + x = SiteMock() + wrapped_inner = pywikibot.site.must_be(group='user')(x.inner_fn) + assert(wrapped_inner(x,1,2,3,a='a', b='b') == ((x,1,2,3), {'a': 'a', 'b': 'b'})) + assert(x.last_fn_called == ((x,1,2,3), {'a': 'a', 'b': 'b'})) + assert(x.last_login == 'user') + +def test_must_be_sysop(): + x = SiteMock() + wrapped_inner = pywikibot.site.must_be(group='sysop')(x.inner_fn) + assert(wrapped_inner(x,1,2,3,a='a', b='b') == ((x,1,2,3), {'a': 'a', 'b': 'b'})) + assert(x.last_fn_called == ((x,1,2,3), {'a': 'a', 'b': 'b'})) + assert(x.last_login == 'sysop') + +