Browse Source

make projects route work properly

pull/1/head
wes 7 years ago
parent
commit
b84d112f05
  1. 1
      requirements.txt
  2. 1
      src/scripts/app.tag
  3. 16
      src/scripts/post.tag
  4. 6
      src/templates/index.html
  5. 33
      src/website.py

1
requirements.txt

@ -12,6 +12,7 @@ Fabric==1.13.1
Flask==0.12
flask-appconfig==0.11.1
Flask-Bootstrap==3.3.7.1
Flask-Cache==0.13.1
Flask-Login==0.4.0
flask-marshmallow==0.7.0
Flask-WTF==0.14.2

1
src/scripts/app.tag

@ -124,6 +124,7 @@ RiotControl.on("postswitch",
);
self.state = {
"page" : self.opts.page,
"_id" : self.opts.postid.slice(-hashLength),
"author" : self.opts.author,
"title" : self.opts.title,

16
src/scripts/post.tag

@ -141,6 +141,15 @@ updatePost(postcontent) {
self.update();
}
getPost(_id) {
self.update({"loading" : true});
window.cached(`/blog/getpost/${_id.slice(-hashLength)}/${self.category}`)
.then((resp) => resp.text())
.then((resp) => {
self.updatePost(JSON.parse(resp))
})
}
nextPost(_id) {
self.update({"loading" : true});
window.cached(`/blog/switchpost/${_id.slice(-hashLength)}/${self.category}`)
@ -169,5 +178,12 @@ prevPost(_id) {
self.updatePost(JSON.parse(resp))
})
}
self.on("mount", () => {
if (self.opts.state.page !== "posts") {
self.getPost(self._id);
}
});
</script>
</post>

6
src/templates/index.html

@ -7,13 +7,13 @@
</head>
<html>
<body>
<div data-is="app" author="{{ postcontent['author'] }}" postid="{{ postcontent['_id'] }}" title="{{ postcontent['title'] }}" initial_post="{{ quote(postcontent['content']) }}" csrf_token="{{ csrf_token() }}"></div>
<div data-is="app" page="{{ page }}" author="{{ postcontent['author'] }}" postid="{{ postid }}" title="{{ postcontent['title'] }}" initial_post="{{ quote(postcontent['content']) }}" csrf_token="{{ csrf_token() }}"></div>
</body>
<script async type="text/javascript" src="/scripts/riotblog.min.js"></script>
<footer>
<script async type="text/javascript" src="/scripts/riotblog.min.js"></script>
<script type="text/javascript">
(function(w){"use strict";var loadCSS=function(href,before,media){var doc=w.document;var ss=doc.createElement("link");var ref;if(before){ref=before}else{var refs=(doc.body||doc.getElementsByTagName("head")[0]).childNodes;ref=refs[refs.length-1]}var sheets=doc.styleSheets;ss.rel="stylesheet";ss.href=href;ss.media="only x";function ready(cb){if(doc.body){return cb()}setTimeout(function(){ready(cb)})}ready(function(){ref.parentNode.insertBefore(ss,before?ref:ref.nextSibling)});var onloadcssdefined=function(cb){var resolvedHref=ss.href;var i=sheets.length;while(i--){if(sheets[i].href===resolvedHref){return cb()}}setTimeout(function(){onloadcssdefined(cb)})};function loadCB(){if(ss.addEventListener){ss.removeEventListener("load",loadCB)}ss.media=media||"all"}if(ss.addEventListener){ss.addEventListener("load",loadCB)}ss.onloadcssdefined=onloadcssdefined;onloadcssdefined(loadCB);return ss};if(typeof exports!=="undefined"){exports.loadCSS=loadCSS}else{w.loadCSS=loadCSS}})(typeof global!=="undefined"?global:this);(function(w){if(!w.loadCSS){return}var rp=loadCSS.relpreload={};rp.support=function(){try{return w.document.createElement("link").relList.supports("preload")}catch(e){return false}};rp.poly=function(){var links=w.document.getElementsByTagName("link");for(var i=0;i<links.length;i++){var link=links[i];if(link.rel==="preload"&&link.getAttribute("as")==="style"){w.loadCSS(link.href,link,link.getAttribute("media"));link.rel=null}}};if(!rp.support()){rp.poly();var run=w.setInterval(rp.poly,300);if(w.addEventListener){w.addEventListener("load",function(){rp.poly();w.clearInterval(run)})}if(w.attachEvent){w.attachEvent("onload",function(){w.clearInterval(run)})}}})(this);
var hrefs = ['/styles/primop.me.min.css', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'];
var hrefs = ['/styles/primop.me.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css'];
for (var i = 0, l = hrefs.length; i < l; i++) {
loadCSS(hrefs[i]);
}

33
src/website.py

@ -1,11 +1,13 @@
#! /usr/bin/python3
from functools import partial
from collections import defaultdict
from flask import abort, Flask, render_template, flash, request, send_from_directory, jsonify, g
from werkzeug.local import Local, LocalProxy, LocalManager
from flask_appconfig import AppConfig
from flask_login import LoginManager, login_required, login_user
from flask_wtf.csrf import CSRFProtect
from flask.ext.cache import Cache
from urllib.parse import unquote
from urllib.parse import quote, unquote
@ -14,13 +16,15 @@ from json import dumps, loads
from admin import Admin
from werkzeug.contrib.cache import MemcachedCache
cache = MemcachedCache(['127.0.0.1:11211'])
memcache = MemcachedCache(['127.0.0.1:11211'])
import os
from posts import Posts
from projects import getProjects
cache = Cache(config={'CACHE_TYPE': 'memcached'})
login_manager = LoginManager()
def cacheit(key, thunk):
@ -29,11 +33,11 @@ def cacheit(key, thunk):
If there is no cached version then it will
evaluate thunk and cache that
"""
cached = cache.get(quote(key))
cached = memcache.get(quote(key))
if cached is None:
print("cache miss for %s" % key)
result = thunk()
cache.set(quote(key), result)
memcache.set(quote(key), result)
return result
print("cache hit for %s" % key)
return cached
@ -82,35 +86,43 @@ def NeverWhere(configfile=None):
def projects():
return jsonify(loads(cacheit("projects", getProjects)))
@app.route("/blog/stuff", methods=("GET",))
def stuff():
return render_template("projects.html")
# blog post routes
# page routes
@cache.cached(timeout=50)
@app.route("/blog/posts/", methods=("GET",))
def renderInitial():
return render_template("index.html", quote=quote, postcontent=dict(initial_post))
return render_template("index.html", postid=initial_post["_id"], page="posts", quote=quote, postcontent=dict(initial_post))
@app.route("/blog/posts/<_id>", methods=("GET",))
def renderPost(_id):
post_content = loads(cacheit(_id, lambda: dumps(posts.getpost(_id, json=False))))
return render_template("index.html", quote=quote, postcontent=dict(post_content))
return render_template("index.html", postid=initial_post["_id"], page="posts", quote=quote, postcontent=dict(post_content))
@cache.cached(timeout=50)
@app.route("/blog/projects", methods=("GET",))
def showProjects():
return render_template("index.html", postid=initial_post["_id"], page="projects", quote=quote, postcontent=defaultdict(str))
@cache.cached(timeout=50)
@app.route("/blog/", methods=("GET", "POST"))
def index():
return renderInitial()
# get the next post
@cache.cached(timeout=50)
@app.route("/blog/switchpost/<pid>/<category>")
def getpostid(pid, category):
return posts.iterpost(startkey=pid, category=category)
# get the post previous to this one
@cache.cached(timeout=50)
@app.route("/blog/prevpost/<pid>/<category>")
def prevpost(pid, category):
return posts.iterpost(endkey=pid, category=category)
# get the contents of any post
@cache.cached(timeout=50)
@app.route("/blog/getpost/<_id>/<category>")
def getpost(_id, category):
return posts.getpost(_id, category=category)
@ -120,6 +132,7 @@ def NeverWhere(configfile=None):
def allposts():
return posts.allposts()
@cache.cached(timeout=50)
@app.route("/blog/categories")
def categories():
return posts.categories()
@ -187,5 +200,7 @@ csrf = CSRFProtect()
csrf.init_app(app)
cache.init_app(app)
if __name__ == "__main__":
NeverWhere().run(host="localhost", port=8001, debug=True)

Loading…
Cancel
Save