jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/362324 )
Change subject: Add ability for Pywikibot to thank Flow posts
......................................................................
Add ability for Pywikibot to thank Flow posts
This change adds a thank method to Post, which will allow bots to
thank the creators of Flow posts. A property was added to Post
which returns a User object corresponding to the creator of that
post. Two tests, adapted from the revision thanking tests, have
been added and pass locally.
Bug: T135411
Change-Id: Ie629aaa46aa007a39e6d5dce6400504bf73e5980
---
M pywikibot/data/api.py
M pywikibot/flow.py
M pywikibot/site.py
A tests/flow_thanks_tests.py
4 files changed, 96 insertions(+), 2 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 7f4c80b..9a19f8a 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -1442,7 +1442,7 @@
"wbcreateclaim", "wbremoveclaims",
"wbsetclaimvalue",
"wbsetreference", "wbremovereferences",
"wbsetclaim",
'wbcreateredirect',
- 'thank',
+ 'thank', 'flowthank'
)
# Client side verification that the request is being performed
# by a logged in user, and warn if it isn't a config username.
diff --git a/pywikibot/flow.py b/pywikibot/flow.py
index 5f8a006..e5920a3 100644
--- a/pywikibot/flow.py
+++ b/pywikibot/flow.py
@@ -12,7 +12,7 @@
import logging
from pywikibot.exceptions import NoPage, UnknownExtension, LockedPage
-from pywikibot.page import BasePage
+from pywikibot.page import BasePage, User
from pywikibot.tools import PY2
if not PY2:
@@ -428,6 +428,16 @@
self._load()
return self._current_revision['isModerated']
+ @property
+ def creator(self):
+ """The creator of this post."""
+ if not hasattr(self, '_current_revision'):
+ self._load()
+ if not hasattr(self, '_creator'):
+ self._creator = User(self.site,
+
self._current_revision['creator']['name'])
+ return self._creator
+
def get(self, format='wikitext', force=False, sysop=False):
"""Return the contents of the post in the given format.
@@ -535,3 +545,7 @@
"""
self.site.restore_post(self, reason)
self._load()
+
+ def thank(self):
+ """Thank the user who made this post."""
+ self.site.thank_post(self)
diff --git a/pywikibot/site.py b/pywikibot/site.py
index e2bc6cc..08b7b7a 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -6775,6 +6775,7 @@
comparison = data['compare']['*']
return comparison
+ # Thanks API calls
@need_extension('Thanks')
def thank_revision(self, revid, source=None):
"""Corresponding method to the 'action=thank' API action.
@@ -6794,6 +6795,25 @@
raise api.APIError('Thanking unsuccessful')
return data
+ @need_extension('Flow')
+ @need_extension('Thanks')
+ def thank_post(self, post):
+ """Corresponding method to the 'action=flowthank' API
action.
+
+ @param post: The post to be thanked for.
+ @type post: Post
+ @raise APIError: On thanking oneself or other API errors.
+ @return: The API response.
+ """
+ post_id = post.uuid
+ token = self.tokens['csrf']
+ req = self._simple_request(action='flowthank',
+ postid=post_id, token=token)
+ 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/flow_thanks_tests.py b/tests/flow_thanks_tests.py
new file mode 100644
index 0000000..00710c9
--- /dev/null
+++ b/tests/flow_thanks_tests.py
@@ -0,0 +1,60 @@
+# -*- 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.flow import Topic
+
+from tests.aspects import TestCase
+
+
+NO_THANKABLE_POSTS = 'There is no recent post which can be test thanked.'
+
+
+class TestThankFlowPost(TestCase):
+
+ """Test thanks for Flow posts."""
+
+ family = 'test'
+ code = 'test'
+
+ write = True
+
+ @classmethod
+ def setUpClass(cls):
+ """Set up class."""
+ super(TestThankFlowPost, cls).setUpClass()
+ cls._topic_title = 'Topic:Tvkityksg1ukyrrw'
+
+ def test_thank_post(self):
+ """Test thanks for Flow posts."""
+ found_log = False
+ site = self.get_site()
+ topic = Topic(site, self._topic_title)
+ for post in reversed(topic.replies()):
+ user = post.creator
+ if site.user() == user.username:
+ continue
+ if user.is_thankable:
+ break
+ else:
+ self.skipTest(NO_THANKABLE_POSTS)
+ before_time = site.getcurrenttimestamp()
+ post.thank()
+ log_entries = site.logevents(logtype='thanks', total=5, page=user,
+ start=before_time, reverse=True)
+ for __ in log_entries:
+ found_log = True
+ break
+ self.assertTrue(found_log)
+
+ def test_self_thank(self):
+ """Test that thanking one's own Flow post causes an
error."""
+ site = self.get_site()
+ topic = Topic(site, self._topic_title)
+ my_reply = topic.reply('My attempt to thank myself.')
+ self.assertAPIError('invalidrecipient', None, my_reply.thank)
--
To view, visit
https://gerrit.wikimedia.org/r/362324
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie629aaa46aa007a39e6d5dce6400504bf73e5980
Gerrit-PatchSet: 8
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Happy5214 <happy5214(a)gmail.com>
Gerrit-Reviewer: Framawiki <framawiki(a)tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>