commit 366c8f957d624d7aa0850945d0b21fb9e3e71535
parent 4fa9e20750e15a65a2c75b45143f0553862045f9
Author: parazyd <parazyd@dyne.org>
Date: Wed, 16 Jan 2019 22:25:57 +0100
Add support for story abstracts.
Diffstat:
6 files changed, 33 insertions(+), 28 deletions(-)
diff --git a/db.py b/db.py
@@ -54,7 +54,8 @@ def initdb(dbpath):
story text NOT NULL,
timestamp integer NOT NULL,
visible integer NOT NULL,
- deletekey text NOT NULL
+ deletekey text NOT NULL,
+ abstract text NOT NULL
);
'''
@@ -104,10 +105,15 @@ def sql_update_row_where(vals, col, val, table='stories'):
length = len(vals) - 1
valstring = ''
for i, j in enumerate(vals):
- valstring += "%s = '%s'" % (j[0], j[1])
+ if not isinstance(j[1], int):
+ valstring += "%s = '%s'" % (j[0], j[1].replace("'", "''"))
+ else:
+ valstring += "%s = '%s'" % (j[0], j[1])
if i < length:
valstring += ', '
+ print(valstring)
+
db.execute("""
UPDATE %s
SET %s
@@ -122,14 +128,14 @@ def sql_insert(args):
TODO: Make this more generic.
"""
- if len(args) == 10:
+ if len(args) == 11:
# Story
db.execute("""
INSERT INTO stories VALUES (
- ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
);
""", (args[0], args[1], args[2], args[3], args[4], args[5],
- args[6], args[7], args[8], args[9]))
+ args[6], args[7], args[8], args[9], args[10]))
elif len(args) == 9:
# User
db.execute("""
diff --git a/diaspora.py b/diaspora.py
@@ -22,7 +22,7 @@ from argparse import ArgumentParser
from random import choice
from time import time
-from flask import Flask, render_template, request
+from flask import Markup, Flask, render_template, request
from db import (sql_delete_row_where, sql_update_row_where, sql_insert,
sql_select_col, sql_select_col_where)
@@ -55,7 +55,9 @@ def submit():
int(time()),
0,
delkey,
+ Markup(request.form['Story']).striptags()[:127],
]
+ print(storyargs)
sql_insert(storyargs)
if request.form['Email']:
@@ -82,6 +84,7 @@ def edit():
('email', request.form['Email']),
('contact', request.form['Contact']),
('story', request.form['Story']),
+ ('abstract', request.form['Abstract']),
]
sql_update_row_where(vals, 'id', request.form['Id'])
return render_template('success_edit.html')
@@ -123,18 +126,16 @@ def country():
If no country is given, it will show a random country.
"""
cc = request.args.get('id')
- # if not cc
- # cc = random(existing_cc)
- # TODO: if not cc: return random existing cc
+ if not cc:
+ cc = choice(sql_select_col('disembark'))[0]
ccfrom = request.args.get('from')
filtered = None
if ccfrom:
filtered = get_multiple_stories_filtered('disembark', cc,
- ('embark', ccfrom),
- strip=True)
+ ('embark', ccfrom))
- stories = get_multiple_stories('disembark', cc, strip=True)
+ stories = get_multiple_stories('disembark', cc)
clist = []
for i in stories:
clist.append((i['embark'], i['embarkname']))
@@ -155,7 +156,7 @@ def dashboard():
sql_update_row_where([('visible', 1)], 'id', story_id)
return render_template('success_approve.html')
- stories = get_multiple_stories('visible', 0, strip=True)
+ stories = get_multiple_stories('visible', 0)
return render_template('dashboard.html', stories=stories)
diff --git a/templates/country.html b/templates/country.html
@@ -30,7 +30,7 @@
<div class="col-md-4">
<div class="card-body">
<p class="card-text">
- {{ i['story'] }}
+ {{ i['abstract'] }}
</p>
<div class="d-flex justify-content-between align-items-center">
diff --git a/templates/dashboard.html b/templates/dashboard.html
@@ -23,7 +23,7 @@
<div class="col-md-4">
<div class="card-body">
<p class="card-text">
- {{ i['story'] }}
+ {{ i['abstract'] }}
</p>
<div class="d-flex justify-content-between align-items-center">
diff --git a/templates/edit.html b/templates/edit.html
@@ -49,7 +49,11 @@
<textarea name="Contact" form="storyform">{{ story['contact'] }}</textarea>
</p>
- <p class="lead">Write your story:<br>
+ <p class="lead">Story abstract:<br>
+ <textarea name="Abstract" form="storyform">{{ story['abstract'] }}</textarea>
+ </p>
+
+ <p class="lead">The story:<br>
<textarea id="storytext" name="Story" form="storyform" required>{{ story['story'] }}</textarea>
</p>
diff --git a/utils.py b/utils.py
@@ -23,7 +23,6 @@ from string import ascii_lowercase, digits
from time import gmtime, strftime, time
from bcrypt import gensalt, hashpw
-from flask import Markup
from db import sql_select_col_where, sql_insert
@@ -71,6 +70,7 @@ def fill_story(row):
'time': strftime('%H:%M UTC', gmtime(row[7])),
'visible': row[8],
'deletekey': row[9],
+ 'abstract': row[10],
}
@@ -82,7 +82,7 @@ def get_story(story_id):
return fill_story(sql_select_col_where('*', 'id', story_id)[0])
-def get_multiple_stories(col, val, reverse=True, strip=False):
+def get_multiple_stories(col, val, reverse=True):
"""
Returns a list of stories where col=val.
"""
@@ -91,9 +91,6 @@ def get_multiple_stories(col, val, reverse=True, strip=False):
stories = []
for i in rows:
j = fill_story(i)
- if strip:
- j['story'] = Markup(j['story']).striptags()[:127]
- j['story'] += '...'
stories.append(j)
if reverse:
@@ -101,13 +98,13 @@ def get_multiple_stories(col, val, reverse=True, strip=False):
return stories
-def get_multiple_stories_filtered(col, val, param, reverse=True, strip=False):
+def get_multiple_stories_filtered(col, val, param, reverse=True):
"""
Returns a filtered list of stories where col=val.
param is a tuple, where [0] will be compared to [1] for filtering.
"""
- stories = get_multiple_stories(col, val, reverse=reverse, strip=strip)
+ stories = get_multiple_stories(col, val, reverse=reverse)
# TODO: test if not stories
filtered_stories = []
@@ -127,10 +124,7 @@ def makenav(randomize=True):
havec = [i[0] for i in rows]
havec = list(set(havec))
-
- navlist = []
- for i in havec:
- navlist.append((i, getcountryname(i)))
+ navlist = [(i, getcountryname(i)) for i in havec]
if randomize:
shuffle(navlist)
@@ -146,7 +140,7 @@ def make_profile(name, email):
# hashed = bcrypt.hashpw(password, bcrypt.gensalt())
# bcrypt.hashpw(plaintext, hashed) == hashed
- password = hashpw('password', gensalt())
+ password = hashpw('password'.encode(), gensalt())
userargs = [
None,