Created
September 26, 2015 09:45
-
-
Save eswarm/21c98e3d43be59a197c4 to your computer and use it in GitHub Desktop.
Simple flask server which takes a post request and generates content for pelican
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, render_template, url_for | |
import shutil | |
from werkzeug import secure_filename | |
import sys | |
import subprocess | |
import os | |
app = Flask(__name__) | |
USERNAME = "user" | |
PASSWORD = "password" | |
#give the name of the direcory you want the post to be moved to. | |
PELICAN_PATH = "/home/pelican/pelican_blog/" | |
PELICAN_CONTENT = "content" | |
PELICAN_OUTPUT = "output" | |
OUTPUT_DIR = "/var/www/output/" | |
""" | |
@app.route('/') | |
def home(): | |
return render_template('form.html') | |
""" | |
def valid_login(username, password): | |
if username == USERNAME and password == PASSWORD : | |
return True | |
else : | |
return False | |
@app.route('/post/', methods=['POST']) | |
def make_post(): | |
# Make a post from the server, save it to markdown format. | |
if valid_login(request.form['username'], request.form['password']): | |
print("valid login") | |
title = request.form['title'] | |
post = request.form['post'] | |
typeFile = request.form['extension'] | |
return write_post(title, typeFile, post) | |
else: | |
title = "Failed" | |
post = "None" | |
return '{{ "status" : "{0}", "message" : "{1}" }}'.format("failed", "invalid credentials") | |
def write_post(title, typeFile, post) : | |
try : | |
title = secure_filename(title) | |
title = os.path.splitext(title)[0] + "." + typeFile | |
print title | |
fd = open(title, 'w') | |
fd.write(post) | |
fd.close() | |
except : | |
print("unable to write the file") | |
return '{{ "status" : "{0}", "message" : "{1}" }}'.format("failed", sys.exc_info()[0]) | |
try : | |
shutil.move(title,PELICAN_PATH + PELICAN_CONTENT) | |
exitVal = subprocess.call(["make", "-C", PELICAN_PATH, "html"]) | |
print exitVal | |
copytree(PELICAN_PATH+PELICAN_OUTPUT, OUTPUT_DIR) | |
print "copy done" | |
except : | |
print("Exception in adding content") | |
print(sys.exc_info()) | |
return '{{ "status" : "{0}", "message" : "{1}" }}'.format("failed", sys.exc_info()[0]) | |
return '{{ "status" : "{0}", "message" : "{1}" }}'.format("success", "none") | |
def copytree(src, dst, symlinks=False, ignore=None): | |
if not os.path.exists(dst): | |
os.makedirs(dst) | |
for item in os.listdir(src): | |
s = os.path.join(src, item) | |
d = os.path.join(dst, item) | |
if os.path.isdir(s): | |
copytree(s, d, symlinks, ignore) | |
else: | |
if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1: | |
shutil.copy2(s, d) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0',port=2015) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment