Browse Source

actually pull posts from database

pull/1/head
wes 8 years ago
parent
commit
ed15a8b100
  1. 20
      src/comment.py
  2. 6
      src/posts.py
  3. 42
      src/scripts/post.tag
  4. 1
      src/scripts/riotblog.js
  5. 2
      src/templates/index.html
  6. 21
      src/website.py

20
src/comment.py

@ -1,13 +1,11 @@
from json import dumps from json import dumps
from random import randint
from time import time from time import time
from uuid import uuid4
def comment(author, title, text, t): def comment(author, title, text, t, uid):
return { return {
"time" : t, "time" : t,
"id" : randint(1,2**32-1), "id" : uid,
"title" : title, "title" : title,
"author" : author, "author" : author,
"text" : text "text" : text
@ -17,14 +15,14 @@ def comment(author, title, text, t):
testcomments = { testcomments = {
1 : dumps( 1 : dumps(
[ [
comment("Person 1", "Two", "A Comment here", 1), comment("Person 1", "Two", "A Comment here", 1, str(uuid4())),
comment("Person 2", "One", "Some other comment", 2), comment("Person 2", "One", "Some other comment", 2, str(uuid4())),
comment("Person 3", "Three", "My opinion is correct", 3) comment("Person 3", "Three", "My opinion is correct", 3, str(uuid4()))
]), ]),
2 : dumps( 2 : dumps(
[ [
comment("Person 1", "Two", "A cool terrible Comment here", 1), comment("Person 1", "Two", "A cool terrible Comment here", 1, str(uuid4())),
comment("Person 2", "One", "Some other awful comment", 2), comment("Person 2", "One", "Some other awful comment", 2, str(uuid4())),
comment("Person 3", "Three", "My opinion is not correct", 3) comment("Person 3", "Three", "My opinion is not correct", 3, str(uuid4()))
]) ])
} }

6
src/posts.py

@ -3,6 +3,7 @@
import couchdb import couchdb
from flask import jsonify from flask import jsonify
from flask_marshmallow import Marshmallow from flask_marshmallow import Marshmallow
#from comment import comment
class Posts: class Posts:
def __init__(self, host=None, port=None): def __init__(self, host=None, port=None):
@ -23,8 +24,9 @@ class Posts:
} }
return jsonify(self.db.save(doc)) return jsonify(self.db.save(doc))
def getposts(self, start, end): def getposts(self, limit, start):
return jsonify([]) result = self.db.iterview("blogPosts/blog-posts", 10, include_docs=True, limit=limit, skip=start)
return jsonify(list(result))
def getcomments(self, postID): def getcomments(self, postID):
return jsonify([]) return jsonify([])

42
src/scripts/post.tag

@ -4,13 +4,13 @@
<button class={"btn btn-primary " + (this.nomore ? "disabled" : " ") + this.nextloading} onclick={this.next}>Next One</button> <button class={"btn btn-primary " + (this.nomore ? "disabled" : " ") + this.nextloading} onclick={this.next}>Next One</button>
</div> </div>
<h4 class="post centered" if={nomore}> <h4 class="post centered" if={this.nomore}>
No More Posts! No More Posts!
</h4> </h4>
<div if={!(this.loading || this.nomore)} class="post centered"> <div if={!(this.loading || this.nomore)} class="post centered">
<h4>{ opts.title }</h4> <h4>{ this.title }</h4>
<h5>Posted by { opts.creator }</h5> <h5>Posted by { this.author }</h5>
<p class="post-content centered text-break"> <p class="post-content centered text-break">
{ this.content } { this.content }
</p> </p>
@ -28,13 +28,16 @@ import { default as R } from 'ramda';
var self = this; var self = this;
this.loading = false; self.author = "";
this.prevloading = ""; self.title = "";
this.nextloading = ""; self.content = "";
self.loading = false;
self.prevloading = "";
self.nextloading = "";
this.nomore = false self.nomore = false;
this.pid = 1; self.pid = 1;
this.content = ""; self.content = "";
prev(ev) { prev(ev) {
ev.preventDefault(); ev.preventDefault();
@ -68,7 +71,7 @@ next(ev) {
setPost(pid) { setPost(pid) {
this.update(); this.update();
this.loading = true; this.loading = true;
fetch(`/blog/switchpost/${pid}`) fetch(`/blog/switchpost/${pid-1}`)
.then((resp) => resp.text()) .then((resp) => resp.text())
.then( .then(
(body) => { (body) => {
@ -78,7 +81,24 @@ setPost(pid) {
self.update() self.update()
} }
else { else {
self.content = body; var postcontent = JSON.parse(body);
console.log(postcontent);
if (postcontent.length == 0) {
self.loading = false;
self.prevloading = "";
self.nextloading = "";
self.author = "";
self.content = "";
self.title = "No more posts!";
self.nomore = true;
self.update();
route(`/${pid}`);
return;
}
self.author = postcontent[0].doc.author[0];
self.content = postcontent[0].doc.content[0];
self.title = postcontent[0].doc.title[0];
self.update();
route(`/${pid}`); route(`/${pid}`);
} }

1
src/scripts/riotblog.js

@ -4,7 +4,6 @@ import './comment.tag';
import './bbutton.tag'; import './bbutton.tag';
import './post.tag'; import './post.tag';
import './posts.tag'; import './posts.tag';
import "./decision.tag";
riot.mount("post", riot.mount("post",
{ {

2
src/templates/index.html

@ -2,7 +2,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<header class="text-center nav navbar"> <header class="text-center nav navbar">
<section class="centered page-top navbar-section"> <section class="centered page-top navbar-section">
<h1 class="blog-title">blog title</h1> <h1 class="blog-title">w00t</h1>
</section> </section>
</header> </header>

21
src/website.py

@ -1,7 +1,7 @@
#! /usr/bin/python3 #! /usr/bin/python3
from functools import partial from functools import partial
from flask import abort, Flask, render_template, flash, request, send_from_directory from flask import abort, Flask, render_template, flash, request, send_from_directory, jsonify
from werkzeug.local import Local, LocalProxy, LocalManager from werkzeug.local import Local, LocalProxy, LocalManager
from flask_appconfig import AppConfig from flask_appconfig import AppConfig
@ -17,7 +17,7 @@ cache = MemcachedCache(['127.0.0.1:11211'])
import os import os
from posts import Posts from posts import Posts
#posts = Posts() posts = Posts()
def cacheit(key, thunk): def cacheit(key, thunk):
""" """
@ -62,15 +62,6 @@ def NeverWhere(configfile=None):
def send_style(filename): def send_style(filename):
return send_from_directory("/srv/http/riotblog/styles", filename) return send_from_directory("/srv/http/riotblog/styles", filename)
@app.route("/blog/switchpost/<pid>")
def switchPost(pid):
posts = {
"1" : "post 1",
"2" : "post 2"
}
return posts.get(pid, "false")
@app.route("/blog/comments/<pid>") @app.route("/blog/comments/<pid>")
def comments(pid): def comments(pid):
try: try:
@ -83,6 +74,14 @@ def NeverWhere(configfile=None):
def insert(): def insert():
return posts.savepost(**request.args) return posts.savepost(**request.args)
@app.route("/blog/switchpost/<pid>")
def getposts(pid):
try:
index = int(pid)
except ValueError:
index = 0
return posts.getposts(index+1, index)
@app.route("/<path:path>") @app.route("/<path:path>")
def page_not_found(path): def page_not_found(path):
return "Oops, couldn't find that :/" return "Oops, couldn't find that :/"

Loading…
Cancel
Save