jenkins-bot has submitted this change and it was merged.
Change subject: Create new topics on a Flow board ......................................................................
Create new topics on a Flow board
This patch adds functionality that allows bots to create new topics on existing Flow boards.
Bug: T105129 Change-Id: I3b35082dde30f35df7663e265c89754c73f9ecb8 --- M pywikibot/flow.py M pywikibot/site.py M tests/flow_edit_tests.py 3 files changed, 81 insertions(+), 1 deletion(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/flow.py b/pywikibot/flow.py index 9c032b4..1580edb 100644 --- a/pywikibot/flow.py +++ b/pywikibot/flow.py @@ -138,6 +138,20 @@ cont_args = self._parse_url(data['links']['pagination']) data = self.site.load_topiclist(self, **cont_args)
+ def new_topic(self, title, content, format='wikitext'): + """Create and return a Topic object for a new topic on this Board. + + @param title: The title of the new topic (must be in plaintext) + @type title: unicode + @param content: The content of the topic's initial post + @type content: unicode + @param format: The content format of the value supplied for content + @type format: unicode (either 'wikitext' or 'html') + @return: The new topic + @rtype: Topic + """ + return Topic.create_topic(self, title, content, format) +
class Topic(FlowPage):
@@ -150,6 +164,24 @@ return self._data
@classmethod + def create_topic(cls, board, title, content, format='wikitext'): + """Create and return a Topic object for a new topic on a Board. + + @param board: The topic's parent board + @type board: Board + @param title: The title of the new topic (must be in plaintext) + @type title: unicode + @param content: The content of the topic's initial post + @type content: unicode + @param format: The content format of the value supplied for content + @type format: unicode (either 'wikitext' or 'html') + @return: The new topic + @rtype: Topic + """ + data = board.site.create_new_topic(board, title, content, format) + return cls(board.site, data['topic-page']) + + @classmethod def from_topiclist_data(cls, board, root_uuid, topiclist_data): """Create a Topic object from API data.
diff --git a/pywikibot/site.py b/pywikibot/site.py index ccc11c6..70ed67b 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -5873,6 +5873,30 @@
@must_be('user') @need_extension('Flow') + def create_new_topic(self, page, title, content, format): + """Create a new topic on a Flow board. + + @param page: A Flow board + @type page: Board + @param title: The title of the new topic (must be in plaintext) + @type title: unicode + @param content: The content of the topic's initial post + @type content: unicode + @param format: The content format of the value supplied for content + @type format: unicode (either 'wikitext' or 'html') + @return: The metadata of the new topic + @rtype: dict + """ + token = self.tokens['csrf'] + params = {'action': 'flow', 'page': page, 'token': token, + 'submodule': 'new-topic', 'ntformat': format, + 'nttopic': title, 'ntcontent': content} + req = self._request(parameters=params, use_get=False) + data = req.submit() + return data['flow']['new-topic']['committed']['topiclist'] + + @must_be('user') + @need_extension('Flow') def reply_to_post(self, page, reply_to_uuid, content, format): """Reply to a post on a Flow topic.
diff --git a/tests/flow_edit_tests.py b/tests/flow_edit_tests.py index 863fb71..9844f5d 100644 --- a/tests/flow_edit_tests.py +++ b/tests/flow_edit_tests.py @@ -9,7 +9,7 @@
__version__ = '$Id$'
-from pywikibot.flow import Topic, Post +from pywikibot.flow import Board, Topic, Post from pywikibot.tools import PY2
from tests.aspects import TestCase @@ -18,6 +18,30 @@ unicode = str
+class TestFlowCreateTopic(TestCase): + + """Test the creation of Flow topics.""" + + family = 'test' + code = 'test' + + user = True + write = True + + def test_create_topic(self): + """Test creation of topic.""" + site = self.get_site() + content = 'If you can read this, the Flow code in Pywikibot works!' + board = Board(site, 'Talk:Pywikibot test') + topic = board.new_topic('Pywikibot test', content, 'wikitext') + first_post = topic.replies()[0] + wikitext = first_post.get(format='wikitext') + self.assertIn('wikitext', first_post._content) + self.assertNotIn('html', first_post._content) + self.assertIsInstance(wikitext, unicode) + self.assertEqual(wikitext, content) + + class TestFlowReply(TestCase):
"""Test replying to existing posts."""
pywikibot-commits@lists.wikimedia.org