diasporadiaries

a platform for writing stories with personal accounts and messages
git clone git://parazyd.org/diasporadiaries.git
Log | Files | Refs | Submodules | README | LICENSE

commit 2281957ee4cf0430c6237093cba1e66662f1c43a
parent a1d0e044295652e5e70116b215f6562327a00ddb
Author: parazyd <parazyd@dyne.org>
Date:   Wed, 16 Jan 2019 21:35:07 +0100

Add support for users.

Diffstat:
Mdb.py | 27+++++++++++++++++++--------
Mdiaspora.py | 16++++++++++------
Mutils.py | 31+++++++++++++++++++++++++++++--
3 files changed, 58 insertions(+), 16 deletions(-)

diff --git a/db.py b/db.py @@ -116,16 +116,27 @@ def sql_update_row_where(vals, col, val, table='stories'): dbctx.commit() -def sql_insert_story(args): +def sql_insert(args): """ - Executes an sql INSERT query where *args are VALUES to insert. + Executes an sql INSERT query where args are VALUES to insert. TODO: Make this more generic. """ - 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])) + if len(args) == 10: + # 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])) + elif len(args) == 9: + # User + db.execute(""" + INSERT INTO users VALUES ( + ?, ?, ?, ?, ?, ?, ?, ?, ? + ); + """, (args[0], args[1], args[2], args[3], args[4], args[5], + args[6], args[7], args[8])) + dbctx.commit() diff --git a/diaspora.py b/diaspora.py @@ -24,10 +24,11 @@ from time import time from flask import Flask, render_template, request -from db import (sql_delete_row_where, sql_update_row_where, sql_insert_story, +from db import (sql_delete_row_where, sql_update_row_where, sql_insert, sql_select_col, sql_select_col_where) from utils import (get_story, makenav, randomstring, getcountryname, - get_multiple_stories, get_multiple_stories_filtered) + get_multiple_stories, get_multiple_stories_filtered, + make_profile) app = Flask(__name__) @@ -55,7 +56,10 @@ def submit(): 0, delkey, ] - sql_insert_story(storyargs) + sql_insert(storyargs) + + if request.form['Email']: + make_profile(request.form['Name'], request.form['Email']) return render_template('success_submit.html', delkey=delkey) @@ -119,9 +123,9 @@ 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 = random(existing_cc) + # TODO: if not cc: return random existing cc ccfrom = request.args.get('from') filtered = None diff --git a/utils.py b/utils.py @@ -20,11 +20,12 @@ Utility functions. from json import load from random import SystemRandom, shuffle from string import ascii_lowercase, digits -from time import gmtime, strftime +from time import gmtime, strftime, time +from bcrypt import gensalt, hashpw from flask import Markup -from db import sql_select_col_where +from db import sql_select_col_where, sql_insert countrymap = {} @@ -134,3 +135,29 @@ def makenav(randomize=True): if randomize: shuffle(navlist) return navlist + + +def make_profile(name, email): + """ + Helper function to generate and insert a profile into the database. + """ + if sql_select_col_where('email', 'email', email, table='users'): + return + + # hashed = bcrypt.hashpw(password, bcrypt.gensalt()) + # bcrypt.hashpw(plaintext, hashed) == hashed + password = hashpw('password', gensalt()) + + userargs = [ + None, + email, + name, + password, + 0, + int(time()), + int(time()), + 1, + 0, + ] + + sql_insert(userargs)