Add voting

This commit is contained in:
Christine Dodrill 2016-02-06 00:30:37 -08:00
parent 38bb2c3ac0
commit 4290688acb
2 changed files with 30 additions and 4 deletions

View File

@ -21,7 +21,17 @@ try:
nick TEXT, nick TEXT,
message TEXT, message TEXT,
time REAL, time REAL,
deleted INTEGER DEFAULT 0)""") deleted INTEGER DEFAULT 0);""")
db.exec(sql"""create table if not exists votes (
id integer primary key,
quoteid integer,
ip TEXT UNIQUE);""")
try:
db.exec(sql"""ALTER TABLE quotes
ADD votes INTEGER DEFAULT 0 NOT NULL""")
except: discard
except: except:
echo getCurrentExceptionMsg() echo getCurrentExceptionMsg()
@ -69,6 +79,7 @@ template contextQuote(ctx: Context, qid: int, quote: Row): expr =
ctx["adder"] = quote[3] ctx["adder"] = quote[3]
ctx["message"] = quote[4] ctx["message"] = quote[4]
ctx["time"] = parseInt(split(quote[5], '.')[0]) ctx["time"] = parseInt(split(quote[5], '.')[0])
ctx["score"] = quote[7]
template pagination(ctx: Context, qid: int, kind: string): expr = template pagination(ctx: Context, qid: int, kind: string): expr =
ctx["paging"] = true ctx["paging"] = true
@ -133,6 +144,23 @@ routes:
except: except:
fail() fail()
get "/quotes/@id/vote":
try:
let
row = db.getRow(sql"SELECT * FROM quotes WHERE id=?", @"id")
if row[0] == "":
raise newException(ValueError, "no such quote")
let
qid = row[0].parseInt()
db.exec(sql"insert into votes values(null, ?, ?);", qid, request.headers.getOrDefault "X-Forwarded-For")
db.exec(sql"update quotes set votes = votes + 1 where id = ?", qid)
redirect "/quotes/" & row[0]
except: fail()
get "/@kind/@id": get "/@kind/@id":
var var
title: string = "" title: string = ""
@ -165,8 +193,6 @@ routes:
else: else:
halt Http404, "not found" halt Http404, "not found"
echo request.headers.getOrDefault "X-Forwarded-For"
ctx = newContext() ctx = newContext()
var quoteSeq = newSeq[Context]() var quoteSeq = newSeq[Context]()

View File

@ -2,7 +2,7 @@
<div class="entries"> <div class="entries">
<h3><a href="/{{ channelsafe }}/0">{{channel}}</a> - #{{listid}} - {{time}}</h3> <h3><a href="/{{ channelsafe }}/0">{{channel}}</a> - #{{listid}} - {{time}}</h3>
<p>({{nick}}) {{message}}</p> <p>({{nick}}) {{message}}</p>
<small><i>Added by {{adder}}. <a href="/quotes/{{id}}">permalink</a></i></small> <small><i>added by {{adder}}.</i> <a href="/quotes/{{id}}">permalink</a> <a href="/quotes/{{listid}}/vote">upvote</a> score: {{ score }}</small>
<br /> <br />
</div> </div>
{{/ quotes }} {{/ quotes }}