Add support for svg collections, add svg browser pagination and filtering

svg collections must be added manually to the svg directory
This commit is contained in:
2017-11-12 19:38:47 +01:00
parent 902ae45ab3
commit 7cb8fbd184
4 changed files with 103 additions and 24 deletions
+17
View File
@@ -41,3 +41,20 @@ body >h1,
form { form {
text-align: center; text-align: center;
} }
h4 {
margin:0;
}
.splitcontainer {
width: 100%;
overflow: hidden;
}
.splitl {
width: 45%;
float:left;
}
.splitr {
width: 45%;
float:right;
}
+54 -18
View File
@@ -1,38 +1,74 @@
<!DOCTYPE html> <!DOCTYPE html>
<title>SVG list</title>
<link rel="stylesheet" href="svg.css"> <link rel="stylesheet" href="svg.css">
<title>SVG list</title>
#macro(form) #macro(form)
<form action="svg" method="post" enctype="multipart/form-data"> <form action="svg" method="post" enctype="multipart/form-data">
<h3>Upload SVG:</h3> <h3>Upload SVG:</h3>
<p> <p>
<input type="file" name="file" accept="image/svg+xml"><br/> <input type="file" name="file" accept="image/svg+xml"><br/>
<input type="text" name="name" placeholder="name"> <input type="text" name="name" placeholder="name">
<input type="submit"> <input type="submit">
#if($uploaded) #if($uploaded)
<i>Upload was successfull!</i> <i>Upload was successfull!</i>
#end #end
</p> </p>
</form> </form>
#end
#macro(pagination)
<center>
#set($self = "svg?")
#if($current_collection)
#set($self = "${self}collection=$escape_html($current_collection)&amp;")
#end
#if($filter)
#set($self = "${self}filter=$escape_html($filter)&amp;")
#end
#if($page>1)<a href="${self}page=$page.__sub__(1)">Previous page</a> - #end
<a href="${self}page=$page.__add__(1)">Next page</a>
</center>
#end #end
<a href="../" style="opacity:0.6;">Return to cardlist</a> <a href="../" style="opacity:0.6;">Return to cardlist</a>
<div class="splitcontainer">
#form() <div class="splitl">
#form()
</div>
<div class="splitr">
<form method="get" action="svg">
<h3>filter</h3>
<input type="text" name="filter" value="$escape_html($filter)">
#if($current_collection)
<input type="hidden" name="collection" value="$escape_html($current_collection)">
#end
<input type="submit">
</form>
<center>
<h4>Collections:</h4>
<a href="svg">No collection</a>
#foreach($collection in $collections)
- <a href="svg?collection=$escape_html($collection)">$escape_html($collection)</a>
#end
</center>
</div>
</div>
<h1>Available SVGs:</h1> <h1>Available SVGs:</h1>
#pagination()
<ul> <ul>
#foreach($name in $svgs) #foreach($name in $svgs)
<li><!--todo: add "copy to clipboard" feature--> <li>
<h1>$name</h1> <h1>$escape_html($name)</h1>
<img src="/svg/${escape_html($name)}.svg"> <img src="/svg/${escape_html($name)}.svg">
</li> </li>
#end #end
</ul> </ul>
#pagination()
#form() #form()
<a href="../" style="opacity:0.6;">Return to cardlist</a> <a href="../" style="opacity:0.6;">Return to cardlist</a>
+15 -1
View File
@@ -124,8 +124,19 @@ async def preview_card(request, template={}):
@app.get("/cards/svg") @app.get("/cards/svg")
@withTemplate("cards/svg.vm") @withTemplate("cards/svg.vm")
async def svg_list(request, template={}): async def svg_list(request, template={}):
svgs = svg.list_all() filter = request.args.get("filter")
page = int(request.args.get("page") or 1)
current_collection = request.args.get("collection")
collections = svg.list_collections()
svgs = svg.list_all(current_collection)
if filter:
svgs = [i for i in svgs if filter in i]
svgs.sort() svgs.sort()
svgs = svgs[(page-1)*100:page*100]
return response.html(template["svg.vm"].merge(locals())) return response.html(template["svg.vm"].merge(locals()))
@app.post("/cards/svg") @app.post("/cards/svg")
@@ -161,6 +172,9 @@ for j in glob.iglob(os.path.join(config.resourcedir, "**","*"), recursive=True):
@app.get(f"/svg/<name>.svg") @app.get(f"/svg/<name>.svg")
async def get_svg(request, name): async def get_svg(request, name):
return response.text(svg.get(name), headers={"Content-Type": "image/svg+xml"}) return response.text(svg.get(name), headers={"Content-Type": "image/svg+xml"})
@app.get(f"/svg/<collection>/<name>.svg")
async def get_svg(request, collection, name):
return response.text(svg.get(os.path.join(collection, name)), headers={"Content-Type": "image/svg+xml"})
if __name__ == "__main__": if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000) app.run(host="0.0.0.0", port=8000)
+17 -5
View File
@@ -3,16 +3,28 @@ import config
from common import memoize, listify_output from common import memoize, listify_output
@listify_output @listify_output
def list_all(): def list_all(collection = None):
for i in glob.glob(os.path.join(config.svgdir, "*.svg")): if collection:
yield os.path.basename(i)[:-4] for i in glob.glob(os.path.join(config.svgdir, collection, "*.svg")):
yield os.path.join(collection, os.path.basename(i)[:-4])
else:
for i in glob.glob(os.path.join(config.svgdir, "*.svg")):
yield os.path.basename(i)[:-4]
@listify_output
def list_collections():
for i in glob.glob(os.path.join(config.svgdir, "*")):
if not i.endswith(".svg"):
yield os.path.basename(i)
@memoize @memoize
def get(name): def get(name):
with open(os.path.join(config.svgdir, f"{name}.svg"), "r") as f: with open(os.path.join(config.svgdir, f"{name}.svg"), "r") as f:
return f.read() return f.read()
def store(name, data): def store(name, data):
with open(os.path.join(config.svgdir, f"{name}.svg"), "wb" if type(data) is bytes else "w") as f: with open(os.path.join(config.svgdir, f"{name}.svg"), "wb" if type(data) is bytes else "w") as f:
return f.write(data) return f.write(data)