Example #1
0
 def __init__(self, page_id, obj, focus_id=None):
     self.title = "Comments"
     log.debug(
         "Build CommentContextView for comments of page with id '%s'" %
         page_id)
     comments = {
         '0': {
             'title': obj.title,
             'id': obj.id
         },
         'children': get_comments_of_page(page_id),
     }
     help_string = cs.COMMENT_CONTEXT_VIEW_HELP
     super().__init__(comments, CommentWidget, help_string=help_string)
     # set focus
     if not focus_id:
         return
     node = self._body.focus
     while True:
         node = self._body.get_next(node)[1]
         if not node:
             break
         if list(node.get_value().keys())[0] == focus_id:
             break
     if node:
         self.set_focus(node)
Example #2
0
 def __init__(self, data):
     super().__init__(data)
     self.type = '?'
     log.debug(json.dumps(data, indent=2))
     self.id = None
     try:
         self.title = self._data['title']
     except KeyError:
         self.title = "Generic object"
Example #3
0
    def push_view(self, view):
        """Open a new view and keep track of the old one"""

        title = getattr(view, "title", "untitled")
        log.debug("Pushing view '%s'" % title)
        self._title_stack.append(title)
        self._view_stack.append(self.loop.widget.body)
        self.loop.widget.body = view
        self.header.set_text(('head', self.get_full_title()))
        self.footer.update_keylegend(view.key_actions)
Example #4
0
def get_comments_of_page(id):
    """Retrieve comments of a page from the Confluence API

    :id: the id of the page
    """
    def get_by_id(children, cid):
        for c in children:
            if cid in list(c.keys()):
                return c

    #  id = re.search('/([^/]*)$', url).groups()[0]
    log.debug("Get comment tree of page %s" % id)

    url = f'rest/api/content/{id}/child/comment'
    params = {
        'expand': 'body.view,content,history.lastUpdated,version,ancestors,'
        'extensions.inlineProperties,version',
        'depth': 'all',
        'limit': 9999,
    }

    items = []
    while True:
        r = make_request(url, params=params)
        parsed = r.json()
        items += parsed['results']
        links = parsed['_links']
        if 'next' in links:
            url = links['next']
        else:
            break

    result = []

    # Build the structure returned by Confluence into something more useful.
    # Most importantly, it's a flat list of all items with each item
    # possessing a list of its ancestors. We want a nested list.
    for c in items:
        parent = result
        # Step down the ancestor list
        # ATTENTION: Apparently the order is arbitrary... can break
        for a in reversed(c['ancestors']):
            parent = get_by_id(parent, a['id'])['children']

        parent.append({
            c['id']: Comment(c),
            'children': [],
        })

    #  log.debug(result)
    return result
Example #5
0
 def get_next_view(self):
     log.debug(self.obj.type)
     if self.obj.type in ["page", "blogpost"]:
         return PageView(self.obj)
     elif self.obj.type == "comment":
         parent = self.obj._data['resultParentContainer']
         page_id = parent['displayUrl']
         page_id = page_id.split('=')[-1]
         #  title = parent['title']
         return CommentContextView(
             page_id,
             self.obj.content,
             self.obj.content.id,
         )
Example #6
0
    def unlike(self):
        id = self.id
        log.debug("Unliking %s" % id)
        r = make_request(
            f'rest/likes/1.0/content/{id}/likes',
            method='DELETE',
            #  headers=headers,
            data="")

        if r.status_code == 200:
            self.liked = False
            return True
        log.error("Unlike failed")
        return False
Example #7
0
def open_content_in_cli_browser(app, id):
    log.debug("Build HTML view for page with id '%s'" % id)
    if not id:
        app.alert("Object has no ID", 'error')
        return
    rest_url = f"rest/api/content/{id}?expand=body.view"
    r = make_request(rest_url)
    if not r.ok:
        app.alert("Request failed (%d)" % r.status_code, 'error')
        return
    content = r.json()
    content = content["body"]["view"]["value"]

    content = f"<html><head></head><body>{content}</body></html>"
    open_doc_in_cli_browser(content.encode(), app)
Example #8
0
 def like(self):
     id = self.id
     log.debug("Liking %s" % id)
     headers = {
         'Content-Type': 'application/json',
     }
     r = make_request(f'rest/likes/1.0/content/{id}/likes',
                      method='POST',
                      headers=headers,
                      data='')
     if r.status_code == 200:
         self.liked = True
         return True
     if r.status_code == 400:
         # already liked
         self.liked = True
     log.error("Like failed")
     return False
Example #9
0
 def unhandled_input(self, key):
     if key not in KEY_ACTIONS:
         return
     if KEY_ACTIONS[key] == 'show help':
         widget = self.get_current_widget()
         if isinstance(widget, HelpView):
             # HelpViews don't need help
             return
         view = HelpView(widget, self.key_actions)
         self.push_view(view)
     elif KEY_ACTIONS[key] == 'back':
         self.pop_view()
     elif KEY_ACTIONS[key] == 'exit':
         self.exit()
     elif KEY_ACTIONS[key] == 'show log':
         log_text = log_stream.getvalue()
         log.debug(log_text)
         view = CongruenceTextBox(log_text)
         view.title = "Log"
         self.push_view(view)
Example #10
0
    def send_inline_reply(self, text):
        page_id = self._data['_expandable']['container']
        page_id = re.search(r'/([^/]*$)', page_id).groups()[0]

        try:
            root_id = self._data['ancestors'][0]['_links']['self']
            root_id = re.search(r'/([^/]*$)', root_id).groups()[0]
        except IndexError:
            # It's the root element already
            root_id = self.id

        url = f"rest/inlinecomments/1.0/comments/{root_id}/replies"
        params = {
            'containerId': page_id,
        }
        data = {
            "body": md_to_html(text),
            "commentId": int(root_id),
        }
        headers = {
            'Content-Type': 'application/json',
            'X-Requested-With': 'XMLHttpRequest',
        }
        r = make_request(url,
                         params,
                         method='POST',
                         data=json.dumps(data),
                         headers=headers)
        if r.status_code == 200:
            return True
        log.debug(r.request.headers)
        log.debug(r.request.body)
        log.debug(r.text)
        return False
Example #11
0
 def __init__(self, data):
     self._data = data
     log.debug(json.dumps(data, indent=2))