jenkins-bot has submitted this change and it was merged.

Change subject: Finish implementation of must_be decorator
......................................................................


Finish implementation of must_be decorator

Converted a few uses to use the proper userright, which eventually
all uses should use instead of a specific group

Change-Id: Ic7196732b0841a5c3a956fa0c7a2b558eba16371
---
M pywikibot/site.py
1 file changed, 38 insertions(+), 18 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/site.py b/pywikibot/site.py
index bca3feb..943e82b 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -637,28 +637,43 @@
     def globalusers_address(self, target='', limit=500, offset='', group=''):
         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.
+def must_be(group=None,right=None):
     """
-    if group == 'user':
-        run = lambda self: self.login(False)
-    elif group == 'sysop':
-        run = lambda self: self.login(True)
+    Decorator to require a certain user status.
+    You can use the group and right independently or together.
+    Example:
+        @must_be(group='user', right='edit)
+        def edit_page(...):
+
+    @param group: any arbitrary group the user should belong to
+    @param right: the rights the logged in user should have.
+    @return: a decorator to make sure the requirement is statisfied when
+             the decorated function is called.
+    """
+
+    if group:
+        if group == 'user':
+            grp = lambda self: self.login(False)
+        elif group == 'sysop':
+            grp = lambda self: self.login(True)
+        else:
+            grp = lambda self: self.has_group(group)
     else:
-        raise Exception("Not implemented")
+        grp = lambda self: True  # No group provided
+
+    if right:
+        rht = lambda self: self.has_right(group)
+    else:
+        rht = lambda self: True  # No right provided
+
+    run = lambda self: grp(self) and rht(self)
 
     def decorator(fn):
         def callee(self, *args, **kwargs):
-            run(self)
+            ok = run(self)
+            if not ok:
+                raise NoUsername('')  # FIXME: Pick a better error
             return fn(self, *args, **kwargs)
         callee.__name__ = fn.__name__
         callee.__doc__ = fn.__doc__
@@ -833,6 +848,11 @@
             self._loginstatus = LoginStatus.NOT_LOGGED_IN # failure
         if not hasattr(self, "_siteinfo"):
             self._getsiteinfo()
+
+        if sysop:
+            return self._loginstatus == LoginStatus.AS_SYSOP
+        else:
+            return self._loginstatus == LoginStatus.AS_USER
 
     forceLogin = login  # alias for backward-compatibility
 
@@ -2543,7 +2563,7 @@
         "editconflict": "Page %(title)s not saved due to edit conflict.",
     }
 
-    @must_be(group='user')
+    @must_be(group='user', right='edit')
     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.
@@ -2901,7 +2921,7 @@
 
     #TODO: implement patrol
 
-    @must_be(group='sysop')
+    @must_be(right='block')
     def blockuser(self, user, expiry, reason, anononly=True, nocreate=True, 
autoblock=True,
                   noemail=False, reblock=False):
 

-- 
To view, visit https://gerrit.wikimedia.org/r/79557
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic7196732b0841a5c3a956fa0c7a2b558eba16371
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to