ran black code formatter
This commit is contained in:
parent
b9635e5d97
commit
1269053f0f
156
app.py
156
app.py
@ -12,120 +12,136 @@ DATABASE = "quotes.db"
|
||||
|
||||
# util methods
|
||||
|
||||
|
||||
def get_db():
|
||||
db = getattr(g, "_database", None)
|
||||
if db is None:
|
||||
db = g._database = sqlite3.connect(DATABASE)
|
||||
db.row_factory = sqlite3.Row
|
||||
return db
|
||||
db = getattr(g, "_database", None)
|
||||
if db is None:
|
||||
db = g._database = sqlite3.connect(DATABASE)
|
||||
db.row_factory = sqlite3.Row
|
||||
return db
|
||||
|
||||
|
||||
def query_db(query, args=(), one=False):
|
||||
cur = get_db().execute(query, args)
|
||||
rv = cur.fetchall()
|
||||
cur.close()
|
||||
return (rv[0] if rv else None) if one else rv
|
||||
cur = get_db().execute(query, args)
|
||||
rv = cur.fetchall()
|
||||
cur.close()
|
||||
return (rv[0] if rv else None) if one else rv
|
||||
|
||||
|
||||
@app.teardown_request
|
||||
def teardown(exc):
|
||||
db = getattr(g, "_database", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
db = getattr(g, "_database", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
|
||||
def url_for_other_page(page):
|
||||
args = request.view_args.copy()
|
||||
args['page'] = page
|
||||
return url_for(request.endpoint, **args)
|
||||
app.jinja_env.globals['url_for_other_page'] = url_for_other_page
|
||||
args = request.view_args.copy()
|
||||
args["page"] = page
|
||||
return url_for(request.endpoint, **args)
|
||||
|
||||
|
||||
app.jinja_env.globals["url_for_other_page"] = url_for_other_page
|
||||
|
||||
|
||||
# db helper methods
|
||||
|
||||
|
||||
def get_post(id=1):
|
||||
row = query_db("SELECT * FROM QUOTES WHERE ID=?", (id,))
|
||||
if not row:
|
||||
return False, []
|
||||
return True, row[0]
|
||||
row = query_db("SELECT * FROM QUOTES WHERE ID=?", (id,))
|
||||
if not row:
|
||||
return False, []
|
||||
return True, row[0]
|
||||
|
||||
|
||||
def get_next_post_id():
|
||||
return query_db("SELECT MAX(ID) FROM QUOTES", one=True)[0] + 1
|
||||
return query_db("SELECT MAX(ID) FROM QUOTES", one=True)[0] + 1
|
||||
|
||||
def insert_new_post(id,text):
|
||||
query_db("INSERT INTO QUOTES VALUES (?,?,?)", (id, text, 0))
|
||||
get_db().commit()
|
||||
|
||||
def vote(id,d=1):
|
||||
query_db("UPDATE QUOTES SET SCORE=SCORE+? WHERE ID=?", (d, id))
|
||||
get_db().commit()
|
||||
def insert_new_post(id, text):
|
||||
query_db("INSERT INTO QUOTES VALUES (?,?,?)", (id, text, 0))
|
||||
get_db().commit()
|
||||
|
||||
|
||||
def vote(id, d=1):
|
||||
query_db("UPDATE QUOTES SET SCORE=SCORE+? WHERE ID=?", (d, id))
|
||||
get_db().commit()
|
||||
|
||||
|
||||
def count_all_quotes():
|
||||
return query_db("SELECT COUNT(*) FROM QUOTES", one=True)[0]
|
||||
return query_db("SELECT COUNT(*) FROM QUOTES", one=True)[0]
|
||||
|
||||
|
||||
def get_quotes_for_page(page, per_page, order_by="ID DESC"):
|
||||
offset = count_all_quotes() - ((page - 1) * per_page) + 1
|
||||
return query_db(
|
||||
"SELECT * FROM QUOTES WHERE ID < ? ORDER BY {} LIMIT ?".format(order_by),
|
||||
(offset, per_page)
|
||||
)
|
||||
offset = count_all_quotes() - ((page - 1) * per_page) + 1
|
||||
return query_db(
|
||||
"SELECT * FROM QUOTES WHERE ID < ? ORDER BY {} LIMIT ?".format(order_by),
|
||||
(offset, per_page),
|
||||
)
|
||||
|
||||
|
||||
# routes
|
||||
|
||||
@app.route("/", defaults={'page': 1})
|
||||
|
||||
@app.route("/", defaults={"page": 1})
|
||||
@app.route("/page/<int:page>")
|
||||
def home(page):
|
||||
count = count_all_quotes()
|
||||
quotes = get_quotes_for_page(page, PER_PAGE)
|
||||
if not quotes and page != 1:
|
||||
abort(404)
|
||||
return render_template(
|
||||
'index.html',
|
||||
pagination=Pagination(page, PER_PAGE, count),
|
||||
quotes=quotes
|
||||
)
|
||||
count = count_all_quotes()
|
||||
quotes = get_quotes_for_page(page, PER_PAGE)
|
||||
if not quotes and page != 1:
|
||||
abort(404)
|
||||
return render_template(
|
||||
"index.html", pagination=Pagination(page, PER_PAGE, count), quotes=quotes
|
||||
)
|
||||
|
||||
@app.route("/top", defaults={'page': 1})
|
||||
|
||||
@app.route("/top", defaults={"page": 1})
|
||||
@app.route("/top/<int:page>")
|
||||
def top(page):
|
||||
count = count_all_quotes()
|
||||
quotes = get_quotes_for_page(page, PER_PAGE, order_by="SCORE DESC")
|
||||
if not quotes and page != 1:
|
||||
abort(404)
|
||||
return render_template(
|
||||
'top.html',
|
||||
pagination=Pagination(page, PER_PAGE, count),
|
||||
quotes=quotes
|
||||
)
|
||||
count = count_all_quotes()
|
||||
quotes = get_quotes_for_page(page, PER_PAGE, order_by="SCORE DESC")
|
||||
if not quotes and page != 1:
|
||||
abort(404)
|
||||
return render_template(
|
||||
"top.html", pagination=Pagination(page, PER_PAGE, count), quotes=quotes
|
||||
)
|
||||
|
||||
|
||||
@app.route("/quote/<int:id>")
|
||||
def quote(id):
|
||||
ok, row = get_post(id)
|
||||
if not ok:
|
||||
return render_template("oops.html", id=id)
|
||||
return render_template("quote.html", row=row)
|
||||
ok, row = get_post(id)
|
||||
if not ok:
|
||||
return render_template("oops.html", id=id)
|
||||
return render_template("quote.html", row=row)
|
||||
|
||||
|
||||
@app.route("/quote/<int:id>/upvote")
|
||||
def upvote(id):
|
||||
vote(id, 1)
|
||||
return redirect(url_for('quote', id=id), code=307)
|
||||
vote(id, 1)
|
||||
return redirect(url_for("quote", id=id), code=307)
|
||||
|
||||
|
||||
@app.route("/quote/<int:id>/downvote")
|
||||
def downvote(id):
|
||||
vote(id, -1)
|
||||
return redirect(url_for('quote', id=id), code=307)
|
||||
vote(id, -1)
|
||||
return redirect(url_for("quote", id=id), code=307)
|
||||
|
||||
|
||||
@app.route("/quote/submit")
|
||||
def submitform():
|
||||
return render_template("submit.html")
|
||||
return render_template("submit.html")
|
||||
|
||||
@app.route("/submit",methods=["POST"])
|
||||
|
||||
@app.route("/submit", methods=["POST"])
|
||||
def submit():
|
||||
if request.form['quote']:
|
||||
nid = get_next_post_id()
|
||||
insert_new_post(nid, request.form['quote'])
|
||||
return redirect(url_for('quote', id=nid), code=302)
|
||||
else:
|
||||
return redirect(url_for('submitform'), code=307)
|
||||
if request.form["quote"]:
|
||||
nid = get_next_post_id()
|
||||
insert_new_post(nid, request.form["quote"])
|
||||
return redirect(url_for("quote", id=nid), code=302)
|
||||
else:
|
||||
return redirect(url_for("submitform"), code=307)
|
||||
|
||||
|
||||
@app.route("/random")
|
||||
def randomquote():
|
||||
return redirect(url_for('quote', id=random.randint(1, get_next_post_id() - 1)))
|
||||
return redirect(url_for("quote", id=random.randint(1, get_next_post_id() - 1)))
|
||||
|
@ -5,7 +5,7 @@ cur = db.cursor()
|
||||
|
||||
cur.execute("CREATE TABLE QUOTES (ID INT PRIMARY_KEY, CONTENT TEXT, SCORE INT)")
|
||||
|
||||
cur.execute("INSERT INTO QUOTES VALUES (?,?,?)", (1,"welp, this is empty.",0))
|
||||
cur.execute("INSERT INTO QUOTES VALUES (?,?,?)", (1, "welp, this is empty.", 0))
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
|
@ -2,7 +2,6 @@ from math import ceil
|
||||
|
||||
|
||||
class Pagination(object):
|
||||
|
||||
def __init__(self, page, per_page, total_count):
|
||||
self.page = page
|
||||
self.per_page = per_page
|
||||
@ -20,14 +19,17 @@ class Pagination(object):
|
||||
def has_next(self):
|
||||
return self.page < self.pages
|
||||
|
||||
def iter_pages(self, left_edge=2, left_current=2,
|
||||
right_current=5, right_edge=2):
|
||||
def iter_pages(self, left_edge=2, left_current=2, right_current=5, right_edge=2):
|
||||
last = 0
|
||||
for num in range(1, self.pages + 1):
|
||||
if num <= left_edge or \
|
||||
(num > self.page - left_current - 1 and \
|
||||
num < self.page + right_current) or \
|
||||
num > self.pages - right_edge:
|
||||
if (
|
||||
num <= left_edge
|
||||
or (
|
||||
num > self.page - left_current - 1
|
||||
and num < self.page + right_current
|
||||
)
|
||||
or num > self.pages - right_edge
|
||||
):
|
||||
if last + 1 != num:
|
||||
yield None
|
||||
yield num
|
||||
|
Loading…
Reference in New Issue
Block a user