|
|
|
@ -18,6 +18,9 @@ load_dotenv(dotenv_path=dotenv_path)
|
|
|
|
|
# load the configuration env
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
|
|
DEFAULT_PAGE = os.environ.get("DEFAULT_PAGE", '')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# prefix to add /soupboat/padliography to all the routes
|
|
|
|
|
class PrefixMiddleware(object):
|
|
|
|
|
def __init__(self, app, prefix=""):
|
|
|
|
@ -58,6 +61,9 @@ def add_pad(padliography, link, title, overview, categories, date):
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 3. Select the page and get the contents
|
|
|
|
|
# --> prefix the page title with Padliography/
|
|
|
|
|
# so we dont erase eventual pages with the same title
|
|
|
|
|
padliography = f'Padliography/{padliography}'
|
|
|
|
|
page = site.pages[padliography]
|
|
|
|
|
text = page.text()
|
|
|
|
|
|
|
|
|
@ -83,6 +89,8 @@ def get_pads(padliography):
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 3. Use the MediaWiki API to get the wikitext contents in HTML
|
|
|
|
|
# Pages in the padliography comes with the Padliography/ prefix
|
|
|
|
|
padliography = f'Padliography/{padliography}'
|
|
|
|
|
html = site.api('parse', prop='text', page=padliography)
|
|
|
|
|
|
|
|
|
|
# 4. Parse the HTML with BeautifulSoup to extract data from the table of pads
|
|
|
|
@ -111,13 +119,15 @@ def init_page(padliography, description):
|
|
|
|
|
site = mwclient.Site('pzwiki.wdka.nl', path='/mw-mediadesign/')
|
|
|
|
|
|
|
|
|
|
# 2. Authenticate using the credential of a bot user registered in the wiki
|
|
|
|
|
### This is necesary the edit the contents of the page
|
|
|
|
|
# This is necesary the edit the contents of the page
|
|
|
|
|
site.login(
|
|
|
|
|
username=os.environ.get('MW_BOT'),
|
|
|
|
|
password=os.environ.get('MW_KEY')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 3. Select the page and get the contents
|
|
|
|
|
# page in the padliography comes with the Padliography/ prefix
|
|
|
|
|
padliography = f'Padliography/{padliography}'
|
|
|
|
|
page = site.pages[padliography]
|
|
|
|
|
|
|
|
|
|
# 4. Insert the table template and a user-provided description
|
|
|
|
@ -138,7 +148,7 @@ def init_page(padliography, description):
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
# 5. Apply the edit
|
|
|
|
|
page.edit(textwrap.dedent(text), f'New padliographish page created: {padliography}')
|
|
|
|
|
page.edit(textwrap.dedent(text), f'New padliographish page created: Pads/{padliography}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Routes
|
|
|
|
@ -146,10 +156,16 @@ def init_page(padliography, description):
|
|
|
|
|
@app.route('/')
|
|
|
|
|
def home():
|
|
|
|
|
'''Serve the homepage layout'''
|
|
|
|
|
return render_template('home.html')
|
|
|
|
|
return render_template('home.html', page=DEFAULT_PAGE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/<padliography>/')
|
|
|
|
|
def page(padliography):
|
|
|
|
|
'''Serve a specific padliography'''
|
|
|
|
|
return render_template('home.html', page=padliography)
|
|
|
|
|
|
|
|
|
|
@app.route('/api/<padliography>', methods=['GET', 'POST'])
|
|
|
|
|
|
|
|
|
|
@app.route('/api/<padliography>/', methods=['GET', 'POST'])
|
|
|
|
|
def api(padliography):
|
|
|
|
|
'''Manage the interaction with the MediaWiki API'''
|
|
|
|
|
|
|
|
|
@ -171,15 +187,18 @@ def api(padliography):
|
|
|
|
|
response.headers.add('Access-Control-Allow-Origin', '*')
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/<padliography>/init', methods=['GET', 'POST'])
|
|
|
|
|
def init(padliography):
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
description = request.json.get('description', None)
|
|
|
|
|
if padliography != None:
|
|
|
|
|
if padliography is not None:
|
|
|
|
|
init_page(padliography, description)
|
|
|
|
|
return redirect(url_for('home'))
|
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Get the port and mount the app
|
|
|
|
|
port = os.environ.get('FLASK_RUN_PORT', '')
|
|
|
|
|
app.run(port=port)
|
|
|
|
|
debug = os.environ.get('DEBUG', False)
|
|
|
|
|
app.run(port=port, debug=debug)
|
|
|
|
|