Happy5214 has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/215392

Change subject: Create classes (with UUIDs) for Flow boards, topics, and posts
......................................................................

Create classes (with UUIDs) for Flow boards, topics, and posts

This change will create four new classes representing Flow entities:

* FlowPage: a superclass for page-like Flow objects
* Board: Flow boards
* Topic: Flow topics
* Post: Flow posts

These classes implement UUID-related functionality, including properties
and private methods to load object data. This entails creating new API
hooks to perform calls for loading data from boards, topics, and posts.
Three tests have been added to test the proper working of this UUID
functionality.

Bug: T101146
Change-Id: I37691cd209b4ee9cc6aa03358bdc5d13fa9f1362
---
M pywikibot/page.py
M pywikibot/site.py
M tests/page_tests.py
3 files changed, 211 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/92/215392/1

diff --git a/pywikibot/page.py b/pywikibot/page.py
index 90efda1..555f1a7 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -76,7 +76,7 @@
     reading from or writing to the wiki.  All other methods are delegated
     to the Site object.
 
-    Will be subclassed by Page and WikibasePage.
+    Will be subclassed by Page, WikibasePage, and FlowPage.
 
     """
 
@@ -4333,6 +4333,168 @@
                 'type': self.value_types.get(self.type, self.type)
                 }
 
+class FlowPage(BasePage):
+
+    """
+    The base page for the Flow extension.
+
+    There should be no need to instantiate this directly.
+    """
+
+    def __init__(self, site, title=u"", **kwargs):
+        """Constructor.
+
+        @param site: Flow-enabled site
+        @type site: Site
+        @param title: normalized title of the page
+        @type title: unicode
+
+        @raise TypeError: incorrect use of parameters
+        @raise ValueError: use of non-Flow-enabled Site
+        """
+        if not isinstance(site, pywikibot.site.BaseSite):
+            raise TypeError("site must be a Site object")
+
+        if not site.has_extension('Flow'):
+            raise ValueError("site is not Flow-enabled")
+
+        super(FlowPage, self).__init__(site, title, **kwargs)
+
+    @property
+    def uuid(self):
+        """Return the UUID of the page.
+
+        @return: UUID of the page
+        @rtype: unicode
+        """
+        if not hasattr(self, '_uuid'):
+            self._load_uuid()
+        return self._uuid
+
+class Board(FlowPage):
+
+    """A Flow discussion board."""
+
+    def __init__(self, site, title=u"", **kwargs):
+        """Constructor.
+
+        @param site: Flow-enabled site
+        @type site: Site
+        @param title: normalized title of the page
+        @type title: unicode
+
+        @raise TypeError: incorrect use of parameters
+        @raise ValueError: use of non-Flow-enabled Site
+        """
+        super(Board, self).__init__(site, title, **kwargs)
+
+    def _load(self):
+        """Load and cache the Board's data, derived from its topic list."""
+        if not hasattr(self, '_data'):
+            self._data = self.site.topiclist(self)
+
+    def _load_uuid(self):
+        """Load and save the UUID of the page.
+
+        The UUID of the Board is derived from the workflowId field in the
+        data returned by view-topiclist.
+        """
+        if not hasattr(self, '_data'):
+            self._load()
+        self._uuid = self._data['workflowId']
+
+class Topic(FlowPage):
+
+    """A Flow discussion topic."""
+
+    def __init__(self, site, title=u"", **kwargs):
+        """Constructor.
+
+        @param site: Flow-enabled site
+        @type site: Site
+        @param title: normalized title of the page
+        @type title: unicode
+
+        @raise TypeError: incorrect use of parameters
+        @raise ValueError: use of non-Flow-enabled Site
+        """
+        super(Topic, self).__init__(site, title, **kwargs)
+
+    def _load(self):
+        """Load and cache the Topic's data."""
+        if not hasattr(self, '_data'):
+            self._data = self.site.topic(self)
+
+    def _load_uuid(self):
+        """Load and save the UUID of the page.
+
+        The UUID of the Topic is derived from the workflowId field in the
+        data returned by view-topic.
+        """
+        if not hasattr(self, '_data'):
+            self._load()
+        self._uuid = self._data['workflowId']
+
+class Post():
+
+    """A post to a Flow discussion topic."""
+
+    def __init__(self, site, page, uuid=u"", **kwargs):
+        """
+        Constructor
+
+        @param site: Flow-enabled site
+        @type site: Site
+        @param page: Flow topic or board
+        @type page: FlowPage
+        @param uuid: UUID of a Flow post
+        @type uuid: unicode
+
+        @raise TypeError: incorrect types of parameters
+        @raise ValueError: use of non-Flow-enabled Site or invalid UUID
+        """
+        if not isinstance(site, pywikibot.site.BaseSite):
+            raise TypeError("site must be a Site object")
+
+        if not isinstance(page, FlowPage):
+            raise TypeError("page must be a FlowPage object")
+
+        if not site.has_extension('Flow'):
+            raise ValueError("site is not Flow-enabled")
+
+        if not uuid:
+            raise ValueError("post UUID must be provided")
+
+        self._site = site
+        self._page = page
+        self._uuid = uuid
+
+    @property
+    def uuid(self):
+        """Return the UUID of the post.
+
+        @return: UUID of the post
+        @rtype: unicode
+        """
+        return self._uuid
+
+    @property
+    def site(self):
+        """Return the site associated with the post.
+
+        @return: Site associated with the post
+        @rtype: Site
+        """
+        return self._site
+
+    @property
+    def page(self):
+        """Return the page associated with the post.
+
+        @return: Page associated with the post
+        @rtype: FlowPage
+        """
+        return self._page
 
 class Revision(DotReadableDict):
 
diff --git a/pywikibot/site.py b/pywikibot/site.py
index ac0c761..48462cc 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -5499,6 +5499,35 @@
     isAllowed = redirect_func(has_right, old_name='isAllowed',
                               class_name='APISite')
 
+    def topiclist(self, page, **kwargs):
+        if self.has_extension('Flow'):
+            title = page.title(withSection=False)
+            req = api.Request(site=self, action='flow', page=title,
+                              submodule='view-topiclist')
+            data = req.submit()
+            topiclist = data['flow']['view-topiclist']['result']['topiclist']
+            return topiclist
+
+    def topic(self, page, **kwargs):
+        if self.has_extension('Flow'):
+            title = page.title(withSection=False)
+            req = api.Request(site=self, action='flow', page=title,
+                              submodule='view-topic')
+            data = req.submit()
+            topic = data['flow']['view-topic']['result']['topic']
+            return topic
+
+    def view_post(self, page, post_id, format='wikitext', **kwargs):
+        if self.has_extension('Flow'):
+            title = page.title(withSection=False)
+            req = api.Request(site=self, action='flow', page=title,
+                              submodule='view-post', vppostId=post_id,
+                              vpformat=format)
+            data = req.submit()
+            result = data['flow']['view-post']['result']['topic']
+            current_revision_id = result['posts'][post_id][0]
+            post = result['revisions'][current_revision_id]
+            return post
 
 class DataSite(APISite):
 
diff --git a/tests/page_tests.py b/tests/page_tests.py
index c031da4..141ccc1 100644
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -737,6 +737,25 @@
         page = pywikibot.Page(site, u'Talk:Sandbox')
         self.assertEqual(page.content_model, 'flow-board')
 
+    # Temporary location until new flow_tests.py file is created
+    def test_board_uuid(self):
+        """Test retrieval of Flow board UUID."""
+        site = self.get_site()
+        board = pywikibot.page.Board(site, u'Talk:Sandbox')
+        self.assertEqual(board.uuid, u'rl7iby6wgksbpfno')
+
+    def test_topic_uuid(self):
+        """Test retrieval of Flow topic UUID."""
+        site = self.get_site()
+        topic = pywikibot.page.Topic(site, u'Topic:Sh6wgo5tu3qui1w2')
+        self.assertEqual(topic.uuid, u'sh6wgo5tu3qui1w2')
+
+    def test_post_uuid(self):
+        """Test retrieval of Flow post UUID."""
+        site = self.get_site()
+        topic = pywikibot.page.Topic(site, u'Topic:Sh6wgo5tu3qui1w2')
+        post = pywikibot.page.Post(site, topic, u'sh6wgoagna97q0ia')
+        self.assertEqual(post.uuid, u'sh6wgoagna97q0ia')
 
 class TestPageUserAction(DefaultSiteTestCase):
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I37691cd209b4ee9cc6aa03358bdc5d13fa9f1362
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Happy5214 <happy5...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to