gallery blueimp
This commit is contained in:
90
__init__.py
90
__init__.py
@@ -6,79 +6,49 @@ from flask_frozen import Freezer
|
||||
from config import Config
|
||||
import os
|
||||
import re
|
||||
from os.path import isfile, abspath
|
||||
from utils import pjoin2
|
||||
from utils import pjoin, list_dir, path_depth
|
||||
from utils import path_depth, page_to_link, page_to_link, file_to_link
|
||||
from functools import partial
|
||||
from collections import namedtuple
|
||||
from flatpages import FileLists
|
||||
|
||||
package_directory = os.path.dirname(os.path.abspath(__file__))
|
||||
cfg = Config(file(os.path.join(package_directory, 'config.cfg')))
|
||||
|
||||
# Loading constants from config file
|
||||
FLATPAGES_AUTO_RELOAD = cfg.pages_reload
|
||||
FLATPAGES_AUTO_RELOAD = True
|
||||
FLATPAGES_EXTENSION = '.md'
|
||||
FLATPAGES_ROOT = abspath(cfg.pages_root)
|
||||
FLATPAGES_FP_ROOT = abspath(cfg.pages_root)
|
||||
FLATPAGES_ROOT = os.path.abspath(cfg.pages_root)
|
||||
FLATPAGES_FP_ROOT = os.path.abspath(cfg.pages_root)
|
||||
|
||||
# Initialize application
|
||||
app = Flask(__name__)
|
||||
|
||||
fp_cfg=dict(filter(lambda v: v[0].startswith("FLATPAGES"), zip(cfg, dict(cfg).values())))
|
||||
app.config.from_object(__name__)
|
||||
app.config.update(cfg)
|
||||
|
||||
FlatPagesIndex.FLATPAGES_ROOT=FLATPAGES_ROOT
|
||||
flatpages = FlatPagesIndex(app)
|
||||
flatpages.get('index')
|
||||
freezer = Freezer(app)
|
||||
app.config.from_object(__name__)
|
||||
|
||||
page_blueprint = Blueprint('intern', __name__)
|
||||
|
||||
|
||||
# List all non *.md files within the directory
|
||||
list_dir_md=partial(list_dir, not_ext=["md","~"])
|
||||
|
||||
# List all jpg images within the directory
|
||||
list_dir_img=partial(list_dir, ext="jpg")
|
||||
|
||||
# List all subpages of the current page
|
||||
get_sub_pages = partial(FlatPagesIndex.get_sub_pages, flatpages)
|
||||
get_sub_ipages = partial(FlatPagesIndex.get_sub_ipages, flatpages)
|
||||
|
||||
|
||||
def get_bc(path):
|
||||
return flatpages.get_pages(FlatPagesIndex.breadcrumbs(path))
|
||||
# return get_breadcrumb_pages(get_breadcrumb_paths(path))
|
||||
|
||||
|
||||
# completes the meta hash if a configuration is not set
|
||||
def page_defaults(page, is_index, path):
|
||||
def page_defaults(page, path):
|
||||
page.meta["title"]=page.meta.get("title", path.split('/')[-1])
|
||||
page.meta["template"]=page.meta.get("template", cfg.default_template)
|
||||
return page
|
||||
|
||||
def load_lists_for_templates(path, is_index):
|
||||
path2 = pjoin(FLATPAGES_ROOT,path)
|
||||
if is_index == True:
|
||||
ld=list_dir_md(path2)
|
||||
il=list_dir_img(path2)
|
||||
sp=list(get_sub_pages(path))
|
||||
spi=list(get_sub_ipages(path))
|
||||
else:
|
||||
ld=[]
|
||||
sp=[]
|
||||
spi=[]
|
||||
il=[]
|
||||
bc=get_bc(path)
|
||||
return (ld, il, sp, spi,bc)
|
||||
|
||||
@page_blueprint.route('/<path:name>/')
|
||||
@page_blueprint.route('/')
|
||||
def post(name='index'):
|
||||
is_index, path, page = flatpages.get_flatpage(name)
|
||||
app.logger.info('render '+name)
|
||||
if not page is None:
|
||||
page=page_defaults(page, is_index, path)
|
||||
ld, il, sp, spi, bc = load_lists_for_templates(path,is_index)
|
||||
return render_template(page.meta["template"], ld=ld, post=page, sp=sp, spi=spi, il=il, pth=path, pagebreadcrumbs=bc)
|
||||
|
||||
page = page_defaults(page, path)
|
||||
fl = linklist_dict_lists(flatpages.load_linklists_for_templates(page.path,path, is_index, page.meta,partial(url_for,'intern.post')))
|
||||
|
||||
return render_template(page.meta["template"], post=page,
|
||||
pth=path, filelists=fl)
|
||||
|
||||
if os.path.exists(os.path.join(FLATPAGES_ROOT,path)):
|
||||
return send_from_directory(FLATPAGES_ROOT,path)
|
||||
else:
|
||||
@@ -88,19 +58,33 @@ def post(name='index'):
|
||||
def json_url_for(paths):
|
||||
for p in paths:
|
||||
yield url_for('intern.postjson', name=p)
|
||||
|
||||
|
||||
def filelist_dict_lists(filelists):
|
||||
return {"breadcrumbs": list(filelists.breadcrumbs),
|
||||
"sub_index_pages": list(json_url_for(FlatPagesIndex.get_paths(filelists.sub_index_pages))),
|
||||
"sub_pages": list(json_url_for(FlatPagesIndex.get_paths(filelists.sub_pages))),
|
||||
"image_list": list(filelists.list_images)
|
||||
}
|
||||
def linklist_dict_lists(filelists):
|
||||
return {"breadcrumbs": list(filelists.breadcrumbs),
|
||||
"sub_index_pages": list(filelists.sub_index_pages ),
|
||||
"sub_pages": list(filelists.sub_pages) ,
|
||||
"image_list": list(filelists.list_images),
|
||||
"file_list": list(filelists.list_files)
|
||||
}
|
||||
|
||||
@page_blueprint.route('/<path:name>.json/')
|
||||
def postjson(name='index'):
|
||||
is_index, path, page = flatpages.get_flatpage(name)
|
||||
if not page is None:
|
||||
ld, il, sp, spi, bc = load_lists_for_templates(path, is_index)
|
||||
page_defaults(page, is_index, path)
|
||||
# filelists=flatpages.load_lists_for_templates(page.path, is_index,page.meta)
|
||||
page=page_defaults(page, path)
|
||||
p=page.meta
|
||||
# p["links"]=linklist_dict_lists(flatpages.load_linklists_for_templates(page.path, path, is_index,page.meta))
|
||||
p.update(linklist_dict_lists(flatpages.load_linklists_for_templates(page.path, path, is_index,page.meta)))
|
||||
|
||||
p["depth"]=path_depth(path)
|
||||
p["path"]=path
|
||||
p["breadcrumbs"]=list(bc)
|
||||
p["subfolders"]=list(json_url_for(FlatPagesIndex.get_paths(spi)))
|
||||
p["subpages"]= list(json_url_for(FlatPagesIndex.get_paths(sp)))
|
||||
p["text"]=page.html
|
||||
return jsonify(page=p)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user