Skip to content

Commit

Permalink
Merge "fix: small fix for notification templates."
Browse files Browse the repository at this point in the history
  • Loading branch information
sadsfae authored and gerritforge-ltd committed Feb 7, 2022
2 parents cdd9a35 + 95bf7eb commit f4cf118
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
2 changes: 1 addition & 1 deletion conf/quads.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ircbot_port: 5050
ircbot_channel: #yourchannel

# wordpress wiki
wp_wiki: https://wiki.example.com/xmlrpc.php
wp_wiki: https://wiki.example.com
wp_username: wikiadmin
wp_password: wikipassword
# you will have to know your wordpress page ID for the main and assignment pages
Expand Down
9 changes: 6 additions & 3 deletions quads/tools/regenerate_vlans_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging

from xmlrpc.client import ProtocolError
from quads.tools.racks_wiki import update_wiki
from quads.tools.wiki import Wiki
from quads.config import Config
from quads.model import Vlan, Cloud, Schedule
from tempfile import NamedTemporaryFile
Expand Down Expand Up @@ -78,8 +78,11 @@ def regenerate_vlans_wiki():
render_vlans(_markdown)
_markdown.seek(0)
try:
update_wiki(
wp_url, wp_username, wp_password, page_title, page_id, _markdown.name
wiki = Wiki(
wp_url, wp_username, wp_password
)
wiki.update(
page_title, page_id, _markdown.name
)
except ProtocolError as ex:
logger.error(ex.errmsg)
Expand Down
14 changes: 7 additions & 7 deletions quads/tools/regenerate_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from xmlrpc.client import ProtocolError
from git import Repo, InvalidGitRepositoryError
from quads.config import Config
from quads.tools import create_input, create_input_assignments, racks_wiki
from quads.tools import create_input, create_input_assignments
from quads.tools.wiki import Wiki
from quads.tools.regenerate_vlans_wiki import regenerate_vlans_wiki

wp_wiki = Config["wp_wiki"]
Expand All @@ -19,7 +20,6 @@
wp_wiki_git_manage = Config["wp_wiki_git_manage"]
wp_wiki_git_repo_path = Config["wp_wiki_git_repo_path"]


logger = logging.getLogger(__name__)

if __name__ == "__main__":
Expand All @@ -40,10 +40,12 @@
repo.remotes.origin.push()

try:
racks_wiki.update_wiki(
wiki = Wiki(
url=wp_wiki,
username=wp_username,
password=wp_password,
)
wiki.update(
_page_title=wp_wiki_main_title,
_page_id=wp_wiki_main_page_id,
_markdown=main_md,
Expand All @@ -68,10 +70,8 @@
repo.remotes.origin.push()

try:
racks_wiki.update_wiki(
url=wp_wiki,
username=wp_username,
password=wp_password,
wiki = Wiki(url=wp_wiki, username=wp_username, password=wp_password)
wiki.update(
_page_title=wp_wiki_assignments_title,
_page_id=wp_wiki_assignments_page_id,
_markdown=assignments_md,
Expand Down
71 changes: 42 additions & 29 deletions quads/tools/racks_wiki.py → quads/tools/wiki.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python3
import os

from wordpress_xmlrpc import Client, WordPressPage
from wordpress_xmlrpc.methods.posts import EditPost
Expand All @@ -8,38 +9,49 @@
import ssl


def update_wiki(url, username, password, _page_title, _page_id, _markdown):
"""
Update an existing wordpress page with generated markdown.
Assumes you have a markdown file with content you want published
to an existing wordpress page.
requires: python-wordpress-xmlrpc or python3-wordpress-xmlrpc
:param url: wordpress xmlrpc endpoint
:param username: wordpress username
:param password: wordpress password
:param _page_title: post page title
:param _page_id: post page id
:param _markdown: path to markdown file for upload
"""
scheme = urlparse(url)[0]
if scheme == "https":
ssl._create_default_https_context = ssl._create_unverified_context
class Wiki:
def __init__(self, url, username, password):
"""
Wiki object initialization
wp = Client(url, username, password)
:param url: wordpress url
:param username: wordpress username
:param password: wordpress password
"""
self.url = url
self.username = username
self.password = password
self.endpoint = os.path.join(self.url, "xmlrpc.php")

# define pages variable
page = WordPressPage()
page.title = _page_title
scheme = urlparse(self.url)[0]
if scheme == "https":
ssl._create_default_https_context = ssl._create_unverified_context

# page id can be found by viewing via wp-admin dashboard in URL
page.id = _page_id
def update(self, _page_title, _page_id, _markdown):
"""
Update an existing wordpress page with generated markdown.
Assumes you have a markdown file with content you want published
to an existing wordpress page.
:param _page_title: post page title
:param _page_id: post page id
:param _markdown: path to markdown file for upload
"""

# set local content file to read handle info into a string
with open(_markdown, "r") as _file:
page.content = _file.read()
wp = Client(self.endpoint, self.username, self.password)

# post new content to the page
wp.call(EditPost(page.id, page))
# define pages variable
page = WordPressPage()
page.title = _page_title

# page id can be found by viewing via wp-admin dashboard in URL
page.id = _page_id

# set local content file to read handle info into a string
with open(_markdown, "r") as _file:
page.content = _file.read()

# post new content to the page
wp.call(EditPost(page.id, page))


if __name__ == "__main__":
Expand All @@ -66,7 +78,7 @@ def update_wiki(url, username, password, _page_title, _page_id, _markdown):
dest="wpurl",
type=str,
default=None,
help="Specify wordpress URL. e.g. http://wiki.example.com/xmlrpc.php",
help="Specify wordpress URL. e.g. http://wiki.example.com",
required=True,
)
parser.add_argument(
Expand Down Expand Up @@ -103,4 +115,5 @@ def update_wiki(url, username, password, _page_title, _page_id, _markdown):
page_title = args.pagetitle
page_id = args.pageid

update_wiki(wp_url, wp_username, wp_password, page_title, page_id)
wiki = Wiki(wp_url, wp_username, wp_password)
wiki.update(page_title, page_id, markdown)

0 comments on commit f4cf118

Please sign in to comment.