jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/294901 )

Change subject: Implement pywikibot support for adding thanks to normal 
revisions
......................................................................


Implement pywikibot support for adding thanks to normal revisions

Add methods and tests which enable pywikibot support for thanking
normal revisions.

Bug: T135409
Change-Id: I087f669d7de4ce705c9a6947c82e8b2d51aea09c
---
M pywikibot/page.py
M pywikibot/site.py
A tests/thanks_tests.py
3 files changed, 96 insertions(+), 0 deletions(-)

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



diff --git a/pywikibot/page.py b/pywikibot/page.py
index 4c5d56f..0c8fb44 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -3462,6 +3462,23 @@
                    item.pageid() > 0
                    )
 
+    @property
+    def is_thankable(self):
+        """
+        Determine if the user has thanks notifications enabled.
+
+        NOTE: This doesn't accurately determine if thanks is enabled for user.
+              Privacy of thanks preferences is under discussion, please see
+              https://phabricator.wikimedia.org/T57401#2216861, and
+              https://phabricator.wikimedia.org/T120753#1863894
+
+        @rtype: bool
+        """
+        if self.isAnonymous():
+            return False
+
+        return True
+
 
 class WikibasePage(BasePage):
 
@@ -5167,6 +5184,17 @@
         return Revision.FullHistEntry(self.revid, self.timestamp, self.user,
                                       self.text, self.rollbacktoken)
 
+    @staticmethod
+    def _thank(revid, site, source='pywikibot'):
+        """Thank a user for this revision.
+
+        @param site: The Site object for this revision.
+        @type site: Site
+        @param source: An optional source to pass to the API.
+        @type source: str
+        """
+        site.thank_revision(revid, source)
+
 
 class FileInfo(DotReadableDict):
 
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 7c778c9..48a8877 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -6774,6 +6774,25 @@
         comparison = data['compare']['*']
         return comparison
 
+    @need_extension('Thanks')
+    def thank_revision(self, revid, source=None):
+        """Corresponding method to the 'action=thank' API action.
+
+        @param revid: Revision ID for the revision to be thanked.
+        @type revid: int
+        @param source: A source for the thanking operation.
+        @type source: str
+        @raise APIError: On thanking oneself or other API errors.
+        @return: The API response.
+        """
+        token = self.tokens['csrf']
+        req = self._simple_request(action='thank', rev=revid, token=token,
+                                   source=source)
+        data = req.submit()
+        if data['result']['success'] != 1:
+            raise api.APIError('Thanking unsuccessful')
+        return data
+
     # Flow API calls
     @need_extension('Flow')
     def load_board(self, page):
diff --git a/tests/thanks_tests.py b/tests/thanks_tests.py
new file mode 100644
index 0000000..cab6cb3
--- /dev/null
+++ b/tests/thanks_tests.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+"""Tests for thanks-related code."""
+#
+# (C) Pywikibot team, 2016-17
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import absolute_import, unicode_literals
+
+from pywikibot.page import Revision, User
+
+from tests.aspects import TestCase
+
+
+class TestThankRevision(TestCase):
+
+    """Test thanks for revisions."""
+
+    family = 'test'
+    code = 'test'
+
+    write = True
+
+    def test_thank_revision(self):
+        """Test thanks for normal revisions.
+
+        NOTE: This test relies on activity in recentchanges, and
+              there must make edits made before reruns of this test.
+              Please see https://phabricator.wikimedia.org/T137836.
+        """
+        found_log = can_thank = False
+        site = self.get_site()
+        data = site.recentchanges(total=50, reverse=True)
+        for i in data:
+            revid = i['revid']
+            username = i['user']
+            user = User(site, username)
+            if user.is_thankable:
+                can_thank = True
+                break
+        if not can_thank:
+            self.skipTest('There is no recent change which can be test 
thanked.')
+        before_time = site.getcurrenttimestamp()
+        Revision._thank(revid, site, source='pywikibot test')
+        log_entries = site.logevents(logtype='thanks', total=5, 
start=before_time, page=user)
+        for __ in log_entries:
+            found_log = True
+            break
+        self.assertTrue(found_log)

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I087f669d7de4ce705c9a6947c82e8b2d51aea09c
Gerrit-PatchSet: 13
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Darthbhyrava <[email protected]>
Gerrit-Reviewer: Darthbhyrava <[email protected]>
Gerrit-Reviewer: Happy5214 <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Magul <[email protected]>
Gerrit-Reviewer: Polybuildr <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to