convert to a fotogallery
This commit is contained in:
139
foto_gallery/__init__.py
Normal file
139
foto_gallery/__init__.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""
|
||||
This module is based on flask_flatpages and creates a website structure based on a file structure.
|
||||
By default the .md extension creates a html website
|
||||
"""
|
||||
import logging
|
||||
import sys
|
||||
from flask import Flask, Blueprint,render_template, send_from_directory,jsonify, url_for, abort
|
||||
#from flask_flatpages import FlatPages, pygments_style_defs
|
||||
import flatpages_index
|
||||
from flatpages_index import FlatPagesIndex
|
||||
from flask_frozen import Freezer
|
||||
from config import Config
|
||||
import os
|
||||
import re
|
||||
from PIL import Image, ExifTags, ImageOps
|
||||
from functools import partial
|
||||
#from .flatpages import FileLists
|
||||
|
||||
# This is the directory, required for absolute file paths
|
||||
#package_directory = os.path.dirname(os.path.abspath(__file__))
|
||||
# Loading the config file
|
||||
#cfg = Config((os.path.join(package_directory, '../config.cfg')))
|
||||
cfg = Config("config.cfg")
|
||||
# Loading constants from config file
|
||||
#FLATPAGES_AUTO_RELOAD = cfg.get("FLATPAGES_AUTO_RELOAD",True) # Default can be overwritten by config cfg
|
||||
#FLATPAGES_EXTENSION = cfg.get("FLATPAGES_EXTENSION",".md") # Default can be overwritten by config cfg
|
||||
#FLATPAGES_ROOT = os.path.abspath(cfg.pages_root)
|
||||
#FLATPAGES_DEFAULT_TEMPLATE = cfg.get("FLATPAGES_DEFAULT_TEMPLATE","gallery.html")
|
||||
|
||||
|
||||
|
||||
# Initialize application
|
||||
app = Flask(__name__)
|
||||
app.config.update(cfg)
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
# Initialize FlatPages Index
|
||||
|
||||
flatpages = FlatPagesIndex(app)
|
||||
flatpages_index.Links.endpoint="intern.post"
|
||||
flatpages_index.Links.thumb_url=lambda s,x: url_for("intern.thumb",size=128,name=x)
|
||||
flatpages_index.Links.image_url=lambda s,x: url_for("intern.post",name=x)
|
||||
|
||||
flatpages.get('index')
|
||||
|
||||
app.logger.info('Initialize Foto Gallery App')
|
||||
app.logger.info('flatpages loaded %d pages' % len(flatpages._pages))
|
||||
app.logger.info("Data directory is: %s" % flatpages.root)
|
||||
app.logger.info("Url prefix;: %s" % cfg.url_prefix)
|
||||
app.url_map.strict_slashes=True
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
freezer = Freezer(app)
|
||||
|
||||
|
||||
page_blueprint = Blueprint('intern', __name__)
|
||||
|
||||
@page_blueprint.route('/thumb<int:size>/<path:name>')
|
||||
def thumb(size=64,name=''):
|
||||
if not size in [32,64,128,256,512]:
|
||||
abort(404)
|
||||
file_name=os.path.join(app.config["FLATPAGES_ROOT"],name)
|
||||
thumb_name=os.path.join("thumbs","thumbs_%i" % size, name)
|
||||
thumb_path, thumb_fn=os.path.split(thumb_name)
|
||||
if os.path.exists(file_name)and not os.path.exists(thumb_name):
|
||||
print("creating thumb")
|
||||
image= Image.open(file_name,'r')
|
||||
s=size, size
|
||||
for orientation in ExifTags.TAGS.keys():
|
||||
if ExifTags.TAGS[orientation]=='Orientation':
|
||||
break
|
||||
exif=dict(image._getexif().items())
|
||||
|
||||
if exif[orientation] == 3:
|
||||
image=image.rotate(180, expand=True)
|
||||
elif exif[orientation] == 6:
|
||||
image=image.rotate(270, expand=True)
|
||||
elif exif[orientation] == 8:
|
||||
image=image.rotate(90, expand=True)
|
||||
thumb=ImageOps.fit(image,s,Image.ANTIALIAS)
|
||||
print("writing %s"% thumb_name)
|
||||
|
||||
os.makedirs(thumb_path,exist_ok=True)
|
||||
thumb.save(thumb_name)
|
||||
image.close()
|
||||
thumb.close()
|
||||
print("sending from %s file %s" % ("thumbs/thumbs_%i/" % size,name))
|
||||
if not os.path.exists(os.path.join(thumb_path,thumb_fn)):
|
||||
print ("doesn't exist")
|
||||
return send_from_directory(os.path.join("../thumbs","thumbs_%i" % size),name)
|
||||
|
||||
|
||||
@page_blueprint.route('/<path:name>/',strict_slashes=True)
|
||||
@page_blueprint.route('/')
|
||||
def post(name=''):
|
||||
print("Post: %s" % name)
|
||||
page = flatpages.get(name)
|
||||
|
||||
if not page is None:
|
||||
page["has_img"]=True
|
||||
page.links.endpoint='intern.post'
|
||||
|
||||
|
||||
return render_template(page["template"], post=page,
|
||||
pth=page["dirpath"])
|
||||
|
||||
if os.path.exists(os.path.join(app.config["FLATPAGES_ROOT"],name)):
|
||||
return send_from_directory(app.config["FLATPAGES_ROOT"],name)
|
||||
elif os.path.exists(os.path.join('static',name)):
|
||||
print("send from static dir %s" % name)
|
||||
return send_from_directory('static',name)
|
||||
else:
|
||||
return send_from_directory('blueimp',name)
|
||||
|
||||
|
||||
@page_blueprint.route('/<path:name>.json',strict_slashes=False)
|
||||
@page_blueprint.route('/.json',strict_slashes=False)
|
||||
def postjson(name='index'):
|
||||
print("Post: %s" % name)
|
||||
|
||||
page = flatpages.get(name)
|
||||
if not page is None:
|
||||
page["has_img"]=True # can be overwritten by file
|
||||
page.links.endpoint='intern.postjson'
|
||||
|
||||
return jsonify(page=dict(page))
|
||||
|
||||
if os.path.exists(u'{}/{}'.format(FLATPAGES_ROOT,path)):
|
||||
return send_from_directory(FLATPAGES_ROOT,path)
|
||||
else:
|
||||
return send_from_directory('static',path)
|
||||
|
||||
|
||||
|
||||
app.register_blueprint(page_blueprint, url_prefix=cfg.url_prefix,static_folder='static')
|
||||
app.add_url_rule('%s/<path:name>' % cfg.url_prefix,'page', post)
|
||||
Reference in New Issue
Block a user