147 lines
4.9 KiB
Python
147 lines
4.9 KiB
Python
import os
|
|
from flask import render_template, send_from_directory, redirect, url_for, request, render_template_string
|
|
from werkzeug.utils import secure_filename
|
|
from . import app, db, basic_auth
|
|
from .models import Page, Photo
|
|
from .forms import PageForm, PhotoForm
|
|
|
|
basedir = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
@app.route('/')
|
|
@app.route('/<path:permalink>')
|
|
def page(permalink=""):
|
|
permalink = permalink.strip("/")
|
|
print("asked for:" + permalink)
|
|
|
|
page = Page.query.filter_by(permalink=permalink).first_or_404()
|
|
|
|
return render_template("page.html", page = page)
|
|
|
|
@app.route('/photo/<id>')
|
|
def photo(id):
|
|
return send_from_directory('photos', '{}.jpg'.format(id))
|
|
@app.route('/thumbnail/<id>')
|
|
def thumbnail(id):
|
|
return send_from_directory('photos/thumbs', '{}.jpg'.format(id))
|
|
@app.route('/slide/<id>')
|
|
def slide(id):
|
|
return send_from_directory('photos/slides', '{}.jpg'.format(id))
|
|
|
|
@app.route('/favicon.ico')
|
|
def favicon():
|
|
return send_from_directory('static', 'favicon.ico')
|
|
|
|
|
|
@app.route('/admin')
|
|
@basic_auth.required
|
|
def admin():
|
|
pages = [Page.query.first()]
|
|
photos = Photo.query.all()
|
|
return render_template("admin.html", pages=pages, photos=photos)
|
|
|
|
@app.route('/admin/page/<id>', methods=['GET', 'POST'])
|
|
@basic_auth.required
|
|
def admin_page(id):
|
|
page = Page.query.filter_by(id=id).first_or_404()
|
|
form = PageForm(obj=page)
|
|
form.thumbnail.query = form.photos.query = Photo.query.all()
|
|
form.parent.query = Page.query.filter_by(endpoint = False)
|
|
# print(form.parent.query)
|
|
|
|
if form.validate_on_submit():
|
|
print((form.parent.data))
|
|
if form.parent.data == page:
|
|
if page.id == 1:
|
|
print("Clearing")
|
|
form.parent.data = None
|
|
else:
|
|
print("Setting root")
|
|
form.parent.data = Page.query.filter_by(id=1).first()
|
|
form.populate_obj(page)
|
|
db.session.commit()
|
|
return redirect(url_for('admin'))
|
|
else:
|
|
return render_template('admin_page.html', page=page, form=form)
|
|
@app.route('/admin/newpage/<parent>')
|
|
@basic_auth.required
|
|
def admin_newpage(parent):
|
|
parent = Page.query.filter_by(id=parent).first_or_404()
|
|
new = Page(name="child_{}".format(len(parent.children)+1), title="Ny sida", endpoint=True, parent=parent, parent_id=parent.id)
|
|
|
|
db.session.add(new)
|
|
|
|
db.session.commit()
|
|
return redirect(url_for('admin'))
|
|
@app.route('/admin/delpage/<id>')
|
|
@basic_auth.required
|
|
def admin_delpage(id):
|
|
page = Page.query.filter_by(id=id).first_or_404()
|
|
root = Page.query.filter_by(id=1).first()
|
|
db.session.delete(page)
|
|
db.session.commit()
|
|
return redirect(url_for('admin'))
|
|
@app.route('/admin/markdown', methods=['POST'])
|
|
def markdown():
|
|
return render_template_string('{{ md | markdown }}', md=request.form['md'])
|
|
|
|
@app.route('/admin/upload', methods=['POST'])
|
|
@basic_auth.required
|
|
def upload():
|
|
from PIL import Image, ImageOps
|
|
def genimg(img, imgout, size):
|
|
img.thumbnail(size)
|
|
hpad = int((size[0]-img.size[0])/2)
|
|
vpad = int((size[1]-img.size[1])/2)
|
|
thumb = Image.new(mode='RGBA', size=size, color=(255,255,255,0))
|
|
thumb.paste(img, (hpad, vpad))
|
|
thumb.save(imgout)
|
|
if 'photo' in request.files:
|
|
file = request.files['photo']
|
|
photo = Photo(alt="Nytt foto")
|
|
db.session.add(photo)
|
|
db.session.commit()
|
|
id = photo.id
|
|
tempname = os.path.join(basedir, 'photos', secure_filename(file.filename))
|
|
file.save(tempname)
|
|
|
|
img = Image.open(tempname)
|
|
filename = '{}.jpg'.format(id)
|
|
imgpath = os.path.join(basedir, 'photos', filename)
|
|
thumb = os.path.join(basedir, 'photos', 'thumbs', filename)
|
|
slide = os.path.join(basedir, 'photos', 'slides', filename)
|
|
img.save(imgpath, 'JPEG')
|
|
genimg(img, imgpath, img.size)
|
|
genimg(img, slide, (550, 400))
|
|
genimg(img, thumb, (171, 180))
|
|
os.remove(tempname)
|
|
|
|
return redirect(url_for('admin'))
|
|
@app.route('/admin/delphoto/<id>')
|
|
@basic_auth.required
|
|
def admin_delphoto(id):
|
|
photo = Photo.query.filter_by(id=id).first_or_404()
|
|
db.session.delete(photo)
|
|
db.session.commit()
|
|
filename = '{}.jpg'.format(id)
|
|
imgpath = os.path.join(basedir, 'photos', filename)
|
|
thumb = os.path.join(basedir, 'photos', 'thumbs', filename)
|
|
slide = os.path.join(basedir, 'photos', 'slides', filename)
|
|
os.remove(imgpath)
|
|
os.remove(thumb)
|
|
os.remove(slide)
|
|
return redirect(url_for('admin'))
|
|
@app.route('/admin/photo/<id>', methods=['GET', 'POST'])
|
|
@basic_auth.required
|
|
def admin_photo(id):
|
|
photo = Photo.query.filter_by(id=id).first_or_404()
|
|
form = PhotoForm(obj=photo)
|
|
form.link.query = Page.query.all()
|
|
|
|
if form.validate_on_submit():
|
|
form.populate_obj(photo)
|
|
db.session.commit()
|
|
return redirect(url_for('admin'))
|
|
else:
|
|
return render_template('admin_photo.html', photo=photo, form=form)
|