pages api

master
km0 2 years ago
parent 20cadf0b03
commit 87f2997edb

@ -0,0 +1,10 @@
Metadata-Version: 2.1
Name: Padliography
Version: 2.0.0
Summary: UNKNOWN
Home-page: UNKNOWN
License: UNKNOWN
Platform: UNKNOWN
UNKNOWN

@ -0,0 +1,8 @@
README.md
setup.py
Padliography.egg-info/PKG-INFO
Padliography.egg-info/SOURCES.txt
Padliography.egg-info/dependency_links.txt
Padliography.egg-info/not-zip-safe
Padliography.egg-info/requires.txt
Padliography.egg-info/top_level.txt

@ -0,0 +1,4 @@
flask
bs4
mwclient
python-dotenv

@ -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)

@ -260,6 +260,10 @@ td.categories {
opacity: 1;
}
.hidden {
visibility: hidden;
}
@keyframes grow {
from {
transform: scale(100%);

@ -1,7 +1,7 @@
const { ref } = Vue;
const padStore = ref([]);
const currentPage = ref("Padliography2");
const currentPage = ref("");
export default function padliographyStore() {
return { padStore, currentPage };

@ -13,7 +13,7 @@ export default {
const pageDescription = ref("");
const initialize = function () {
fetch(`api/${initPage.value}/init`, {
fetch(`/api/${initPage.value}/init`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
@ -37,7 +37,7 @@ export default {
};
watchEffect(() => {
fetch(`api/${currentPage.value}`)
fetch(`/api/${currentPage.value}`)
.then((res) => res.json())
.then((data) => {
pads.value = data.pads;
@ -151,7 +151,7 @@ export default {
<button @click="edit=false"> x </button>
</span>
<a v-else :href="'https://pzwiki.wdka.nl/mediadesign/' + currentPage">{{currentPage}}</a>
<a v-else :href="'https://pzwiki.wdka.nl/mediadesign/Padliography/' + currentPage" target="_blank">{{currentPage}}</a>
<button class="change" v-if="!edit" @click="edit = true">change</button> <br>
</div>

@ -14,13 +14,17 @@
<pad-form></pad-form>
<pad-table></pad-table>
</div>
<div id="current-page" class="hidden">{{page}}</div>
<script type="module">
import PadForm from "{{url_for('static', filename='js/PadForm.js')}}";
import PadTable from "{{url_for('static', filename='js/PadTable.js')}}";
const { createApp } = Vue;
import PadStore from "{{url_for('static', filename='js/PadStore.js')}}";
const { currentPage } = PadStore()
currentPage.value = document.querySelector('#current-page').textContent
createApp({
components: { PadForm, PadTable },
}).mount("#app");

Loading…
Cancel
Save