from . import db import bottle from urllib.parse import unquote, quote import os import pathlib static_dir = os.path.join(os.path.expanduser('~'), 'lazy_wiki') bottle.TEMPLATE_PATH = [os.path.join(os.path.dirname(__file__), 'views')] app = bottle.Bottle() # Serve CSS @app.get('/static/css/') def serve_css(filename): return bottle.static_file(filename, root=pathlib.Path(__file__).parent / 'static/css') @app.get('/favicon.ico', method='GET') def get_favicon(): return bottle.static_file('favicon.ico', root=pathlib.Path(__file__).parent / 'static/img') @app.get('/edit/') def new_article(): ''' Write a new article. ''' return bottle.template('edit', article = {'title' : '', 'content' : ''}) @app.get('/delete/') def delete_article(keyword): ''' Delete an article. ''' article = db.select_formatted_article(unquote(keyword)) return bottle.template('view', article = article) @app.get('/edit/') def edit_article(keyword): ''' Edit an existing article. ''' article = db.select_article(unquote(keyword)) return bottle.template('edit', article = article) @app.get('/view/') def get_article(keyword): ''' Get an article. ''' article = db.select_formatted_article(unquote(keyword)) return bottle.template('view', article = article) @app.post('/article') def post_article(): ''' Post a new article. ''' POST = bottle.request.POST.decode() article = db.select_article(POST['title']) if article: db.update_article(**POST) else: db.insert_article(**POST) bottle.redirect('/view/{}'.format(quote(POST['title'])))