From 1132fac75c039eb42521f0abfbd6cdbeafe50d08 Mon Sep 17 00:00:00 2001 From: wes Date: Sat, 18 Feb 2017 17:25:29 -0500 Subject: [PATCH] add couchdb and marshmallow --- build/comment.py | 12 ++++---- build/posts.py | 25 ++++++++++++++++- build/requirements.txt | 3 ++ build/scripts/tags.min.js | 2 +- build/templates/index.html | 2 +- build/templates/templates/index.html | 42 ---------------------------- build/website.py | 12 ++++++-- fabfile.py | 3 +- requirements.txt | 3 ++ src/comment.py | 12 ++++---- src/posts.py | 25 ++++++++++++++++- src/tags/post.tag | 4 +-- src/templates/index.html | 2 +- src/website.py | 12 ++++++-- 14 files changed, 91 insertions(+), 68 deletions(-) delete mode 100644 build/templates/templates/index.html diff --git a/build/comment.py b/build/comment.py index ccc1d42..9ea817d 100644 --- a/build/comment.py +++ b/build/comment.py @@ -10,9 +10,9 @@ def comment(author, title, text): testcomments = { 1 : dumps( - [ - comment("Anonymous Coward", "Some comment?", "super duper awesome comment here"), - comment("Anonymous Coward 3", "Something? IDEK", "super duper worse comment here"), - comment("Anonymous Coward 2", "Some other comment?", "super duper dang terrible comment here") - ]) - } + [ + comment("Anonymous Coward 0", "Some comment?", "super duper awesome comment here"), + comment("Anonymous Coward 1", "Something? IDEK", "super duper worse comment here"), + comment("Anonymous Coward 2", "Some other comment?", "super duper dang terrible comment here") + ]) + } diff --git a/build/posts.py b/build/posts.py index 088b114..cb5e966 100644 --- a/build/posts.py +++ b/build/posts.py @@ -1 +1,24 @@ -from functools import total_ordering +#! /usr/bin/python + +import couchdb +from flask import jsonify +from flask_marshmallow import Marshmallow + +class Posts: + def __init__(self, host=None, port=None): + if host is None: + host = "localhost" + if port is None: + port = "5984" + + self.client = couchdb.Server("http://%s:%s" % (host, port)) + + self.db = self.client["blog"] + + def savepost(self, title, content, author): + doc = { + "title" : title, + "content" : content, + "author" : author + } + return jsonify(self.db.save(doc)) diff --git a/build/requirements.txt b/build/requirements.txt index ab16fa6..09283b3 100644 --- a/build/requirements.txt +++ b/build/requirements.txt @@ -1,10 +1,13 @@ appdirs==1.4.0 click==6.7 +CouchDB==1.1 Flask==0.12 flask-appconfig==0.11.1 +flask-marshmallow==0.7.0 itsdangerous==0.24 Jinja2==2.9.5 MarkupSafe==0.23 +marshmallow==2.13.0 packaging==16.8 pyparsing==2.1.10 python-memcached==1.58 diff --git a/build/scripts/tags.min.js b/build/scripts/tags.min.js index bc43a90..8420e22 100644 --- a/build/scripts/tags.min.js +++ b/build/scripts/tags.min.js @@ -79,7 +79,7 @@ this.on("mount", }); -riot.tag2('post', '

No More Posts!

{opts.title}

By {opts.creator}

{content}

', '', '', function(opts) { +riot.tag2('post', '

No More Posts!

{opts.title}

By {opts.creator}

{content}

', '', '', function(opts) { var self = this; this.loading = false; diff --git a/build/templates/index.html b/build/templates/index.html index f37844c..3376075 100644 --- a/build/templates/index.html +++ b/build/templates/index.html @@ -2,7 +2,7 @@ {% endblock %} diff --git a/build/templates/templates/index.html b/build/templates/templates/index.html deleted file mode 100644 index f37844c..0000000 --- a/build/templates/templates/index.html +++ /dev/null @@ -1,42 +0,0 @@ -{% block head %} - - -{% endblock %} - - - - {% block content %} - - - - - - - - {% endblock %} - - - -{% block styles %} - - - - -{% endblock %} - -{% block scripts %} - - - - - - -{% endblock %} - - - diff --git a/build/website.py b/build/website.py index 48f4007..5f6ef5b 100755 --- a/build/website.py +++ b/build/website.py @@ -2,6 +2,7 @@ from functools import partial from flask import abort, Flask, render_template, flash, request, send_from_directory +from werkzeug.local import Local, LocalProxy, LocalManager from flask_appconfig import AppConfig from urllib.parse import unquote @@ -14,6 +15,9 @@ from werkzeug.contrib.cache import MemcachedCache cache = MemcachedCache(['127.0.0.1:11211']) import os +from posts import Posts + +posts = Posts() def cacheit(key, thunk): """ @@ -33,6 +37,8 @@ def NeverWhere(configfile=None): app = Flask(__name__) app.config["TEMPLATES_AUTO_RELOAD"] = True + app.config["COUCHDB_SERVER"] = "http://localhost:5984" + app.config["COUCHDB_DATABASE"] = "blog" #def favicon(): #return send_from_directory("/srv/http/goal/favicon.ico", #'favicon.ico', mimetype='image/vnd.microsoft.icon') @@ -68,9 +74,9 @@ def NeverWhere(configfile=None): print(e) return dumps([]) - @app.route("/blog/insert/") - def insert(pid): - print("inserting new post") + @app.route("/blog/insert/") + def insert(): + return posts.savepost(**request.args) @app.route("/") def page_not_found(path): diff --git a/fabfile.py b/fabfile.py index 377b188..45a7480 100644 --- a/fabfile.py +++ b/fabfile.py @@ -80,6 +80,8 @@ def update(): @task def locbuild(): local("mkdir -p build/{scripts,styles}") + local("cp requirements.txt ./build/requirements.txt") + buildLocalVenv() buildTags() buildScss() minifyJS() @@ -91,4 +93,3 @@ def locbuild(): local("sudo systemctl daemon-reload") local("sudo systemctl enable blog.service") local("sudo systemctl restart blog.service") - local("sudo systemctl restart nginx") diff --git a/requirements.txt b/requirements.txt index ab16fa6..09283b3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,13 @@ appdirs==1.4.0 click==6.7 +CouchDB==1.1 Flask==0.12 flask-appconfig==0.11.1 +flask-marshmallow==0.7.0 itsdangerous==0.24 Jinja2==2.9.5 MarkupSafe==0.23 +marshmallow==2.13.0 packaging==16.8 pyparsing==2.1.10 python-memcached==1.58 diff --git a/src/comment.py b/src/comment.py index ccc1d42..9ea817d 100644 --- a/src/comment.py +++ b/src/comment.py @@ -10,9 +10,9 @@ def comment(author, title, text): testcomments = { 1 : dumps( - [ - comment("Anonymous Coward", "Some comment?", "super duper awesome comment here"), - comment("Anonymous Coward 3", "Something? IDEK", "super duper worse comment here"), - comment("Anonymous Coward 2", "Some other comment?", "super duper dang terrible comment here") - ]) - } + [ + comment("Anonymous Coward 0", "Some comment?", "super duper awesome comment here"), + comment("Anonymous Coward 1", "Something? IDEK", "super duper worse comment here"), + comment("Anonymous Coward 2", "Some other comment?", "super duper dang terrible comment here") + ]) + } diff --git a/src/posts.py b/src/posts.py index 088b114..cb5e966 100644 --- a/src/posts.py +++ b/src/posts.py @@ -1 +1,24 @@ -from functools import total_ordering +#! /usr/bin/python + +import couchdb +from flask import jsonify +from flask_marshmallow import Marshmallow + +class Posts: + def __init__(self, host=None, port=None): + if host is None: + host = "localhost" + if port is None: + port = "5984" + + self.client = couchdb.Server("http://%s:%s" % (host, port)) + + self.db = self.client["blog"] + + def savepost(self, title, content, author): + doc = { + "title" : title, + "content" : content, + "author" : author + } + return jsonify(self.db.save(doc)) diff --git a/src/tags/post.tag b/src/tags/post.tag index 269e7a5..018c666 100644 --- a/src/tags/post.tag +++ b/src/tags/post.tag @@ -1,7 +1,7 @@
- - + +

diff --git a/src/templates/index.html b/src/templates/index.html index f37844c..3376075 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -2,7 +2,7 @@ {% endblock %} diff --git a/src/website.py b/src/website.py index 48f4007..5f6ef5b 100755 --- a/src/website.py +++ b/src/website.py @@ -2,6 +2,7 @@ from functools import partial from flask import abort, Flask, render_template, flash, request, send_from_directory +from werkzeug.local import Local, LocalProxy, LocalManager from flask_appconfig import AppConfig from urllib.parse import unquote @@ -14,6 +15,9 @@ from werkzeug.contrib.cache import MemcachedCache cache = MemcachedCache(['127.0.0.1:11211']) import os +from posts import Posts + +posts = Posts() def cacheit(key, thunk): """ @@ -33,6 +37,8 @@ def NeverWhere(configfile=None): app = Flask(__name__) app.config["TEMPLATES_AUTO_RELOAD"] = True + app.config["COUCHDB_SERVER"] = "http://localhost:5984" + app.config["COUCHDB_DATABASE"] = "blog" #def favicon(): #return send_from_directory("/srv/http/goal/favicon.ico", #'favicon.ico', mimetype='image/vnd.microsoft.icon') @@ -68,9 +74,9 @@ def NeverWhere(configfile=None): print(e) return dumps([]) - @app.route("/blog/insert/") - def insert(pid): - print("inserting new post") + @app.route("/blog/insert/") + def insert(): + return posts.savepost(**request.args) @app.route("/") def page_not_found(path):