Files
triton_welcome/flaskapp/functions.py
2017-07-24 15:49:07 +02:00

165 lines
5.0 KiB
Python

# List all non *.md files within the directory
def list_dir_md(mypath):
return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\%s.*' % FLATPAGES_EXTENSION,f) is None]
# List all jpg images within the directory
def list_dir_img(mypath):
return [f for f in os.listdir(mypath) if isfile(os.path.join(mypath, f)) and re.match('.*\.jpg',f) is not None]
def path_depth(path):
p_split=path.split('/')
if path =="":
cc=0
else:
cc=len(path.split('/'))
if p_split[-1]=='index' or p_split[-1]=='index.json' :
cc=cc-1
return cc
# List all subpages of the current page
def get_sub_pages(path):
return [p for p in flatpages
if (p.path.startswith(path) and # is a subpage
( re.match('.*index',p.path) is None) and # is no index
path_depth(p.path)<path_depth(path)+2) ]
# List all index subpages of the current page i.e. all pages in subdirectories
def get_sub_ipages(path):
# ppath=page.path
cc=len(path.split('/'))
if path=="":
cc=0
ppath="index"
else:
ppath=pjoin2([path,"index"])
ps=[p for p in flatpages
if (p.path.startswith(path) and # subpages of this page
not ( re.match('.*index',p.path) is None) and # only index
( ppath != p.path) and # not identical page
len(p.path.split('/'))<=cc+2) ] # only one level
return ps
# breadcrumbs decompose the path
# a/b/index -> [a/index a/b/index]
def get_breadcrumb_paths(path):
elements = path.split('/')
elements2 = ['index']
for i in range(1,len(elements)):
elements2.append(pjoin2(elements[0:i])+u'/index')
return elements2
def get_breadcrumb_pages(paths):
pages=[flatpages.get(p) for p in paths]
return pages
def get_paths(pages):
paths=[p.path for p in pages]
return paths
def get_bc(path):
return get_breadcrumb_pages(get_breadcrumb_paths(path))
# load a page from a path information
def get_flatpage(path):
is_index=False
# root index
if path == 'index' or path=='':
is_index=True
path=''
pathi='index'
else:
if path.split('/')[-1]=='index': # last element is "index" ?
is_index=True
path='/'.join(path.split('/')[0:-1]) # remove index from path
pathi = u'{}/{}'.format(path, 'index') # try to add index to the path
page = flatpages.get(pathi) # try to load index page
if not page is None: # if successful it is a index
is_index=True
else:
page=flatpages.get(path)
return (is_index, path, page)
def pjoin (rt,pth):
return u'{}/{}'.format(rt,pth)
def pjoin2 (pth):
return u'/'.join(pth) or u''
def misskey(a,key):
if not a.has_key(key):
return True
return a[key] is None
def page_defaults(page, is_index, path):
if misskey(page.meta,"title"):
page.meta["title"]=path.split('/')[-1]
if misskey(page.meta, "template"):
page.meta["template"]='page.html'
return page
@app.route('/<path:name>/')
def post(name='index'):
is_index, path, page = get_flatpage(name)
app.logger.info('render '+name)
path2 = pjoin(FLATPAGES_ROOT,path)
if is_index == True and not page is None:
ld=list_dir_md(path2)
il=list_dir_img(path2)
sp=get_sub_pages(path)
spi=get_sub_ipages(path)
else:
ld=[]
sp=[]
spi=[]
il=[]
if not page is None:
page_defaults(page,is_index,path)
app.logger.info("Render Template"+page["template"] +"for "+path)
return render_template(page.meta["template"], ld=ld, post=page, sp=sp, spi=spi, il=il, pth=path, pagebreadcrumbs=get_bc(path))
if os.path.exists(u'{}/{}'.format(FLATPAGES_ROOT,path)):
return send_from_directory(FLATPAGES_ROOT,path)
else:
return send_from_directory('static',path)
@app.route('/<path:name>.json/')
def postjson(name='index'):
is_index, path, page = get_flatpage(name)
app.logger.info('render '+name)
path2 = pjoin(FLATPAGES_ROOT,path)
if is_index == True and not page is None:
ld=list_dir_md(path2)
il=list_dir_img(path2)
sp=get_sub_pages(path)
spi=get_sub_ipages(path)
else:
ld=[]
sp=[]
spi=[]
il=[]
if not page is None:
page_defaults(page,is_index,path)
app.logger.info("Render Template"+page["template"] +"for "+path)
p=page.meta
bc=get_breadcrumb_paths(path)
p["depth"]=path_depth(path)
p["path"]=path
p["breadcrumbs"]=bc
p["subfolders"]=get_paths(spi)
p["subpages"]= [u'%s.json' % p1 for p1 in get_paths(sp)]
p["text"]=page.html
return jsonify(page=p)
# return render_template(page.meta["template"], ld=ld, post=page, sp=sp, spi=spi, il=il, pth=path, pagebreadcrumbs=get_bc(path,page))
if os.path.exists(u'{}/{}'.format(FLATPAGES_ROOT,path)):
return send_from_directory(FLATPAGES_ROOT,path)
else:
return send_from_directory('static',path)