def set_user_signed_in(email, token): db = connect_db() cur = db.cursor() cur.execute("CREATE TABLE IF NOT EXISTS online_users(user_email TEXT, user_token TEXT)") cur.execute("INSERT INTO online_users (user_email, user_token) VALUES(?,?)", (email, token)) db.commit() db.close()
def set_user_password(email, new_password): db = connect_db() cur = db.cursor() cur.execute("UPDATE users SET user_pass=? WHERE user_email=?", (new_password,email)) db.commit() db.close()
def create_user(email, password, firstname, lastname, gender, city, country): user = (email, password, firstname, lastname, gender, city, country) db = connect_db() cur = db.cursor() cur.execute("INSERT INTO users VALUES(?, ?, ?, ?, ?, ?, ?)", user) db.commit() db.close()
def get_users(): db = connect_db() cur = db.execute('SELECT * FROM users') users = {} for row in cur.fetchall(): users[row[0]] = {'email' : row[0], 'password' : row[1], 'firstname' : row[2], 'lastname' : row[3], 'gender' : row[4], 'city' : row[5], 'country' : row[6]} db.close() return users
def retrive_messages(email): db = connect_db() cur = db.cursor() cur.execute("SELECT msg_index, sender_email, msg_content FROM user_messages WHERE receiver_email=?", (email,)) messages = [] for row in cur.fetchall(): messages.append({'msg_id' : row[0], 'sender_email' : row[1], 'msg_content' : row[2]}) return messages
def write_message(token, email, msg): db = connect_db() cur = db.cursor() cur.execute("CREATE TABLE IF NOT EXISTS user_messages (receiver_email TEXT, msg_index INTEGER, sender_email TEXT, msg_content TEXT)") cur.execute("SELECT COUNT(*) FROM user_messages WHERE receiver_email=?", (email,)) cur.execute("INSERT INTO user_messages VALUES(?, ?, ?, ?)", (email, msg_index, token_to_email(token), msg)) db.commit() cur.close() return online_users
def get_online_users(): db = connect_db() cur = db.execute("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name='online_users'") online_users = {} if cur.fetchall() != []: cur = db.execute("SELECT user_email, user_token FROM online_users") for row in cur.fetchall(): online_users[row[1]] = row[0] db.close() return online_users
def set_user_offline(token): db = connect_db() db.execute('DELETE FROM online_users WHERE user_token=?', (token,)) db.commit() db.close()
from server import connect_db tuples = [ ("test", "alpha"), ("test", "beta"), ("test", "gamma"), ("test", "theta"), ("test", "epsilon"), ] if __name__ == '__main__': with open('schema.sql') as fp: setup = fp.read() with connect_db() as db: db.executescript(setup) db.executemany( """insert into submissions(group_id, content) values (?,?)""", tuples)
import sys from lxml.etree import tostring from itertools import chain from flask import Flask from operator import itemgetter from flask import render_template, json, jsonify, g, request, send_from_directory from flask.json import JSONEncoder import psycopg2 import psycopg2.extras import datetime import os import elasticsearch import re import os from server import connect_db, get_act_exact, tohtml def requires_contents(act): return len(act.xpath('.//toc')) def create_contents(act): if requires_contents(act): print etree.tostring(tohtml(act, 'contents.xslt'), encoding='UTF-8', pretty_print=True) if __name__ == "__main__": db = connect_db() create_contents(get_act_exact('Companies Act 1993', db=db))
def init(): if __name__ != "__main__": return "ERROR: Must start from terminal." print("Initializes the server config and database.") reset = False conf = config_file_ok() if conf: inp = input("Config file looks ok. Do you want to reset it (y/n)?") reset = inp == "y" if reset: print("Will create a new config file.") if reset or not conf: # db = input("Database name: ") # if not db: # print("No database name. Aborting...") # return None secret_key = input("Super secret key: ") if not secret_key: print("Need a secret key. Aborting...") return with open("server_config.py", "w") as f: f.write("from os import path\n") f.write("DEBUG = True\n") f.write("SECRET_KEY = '{}'\n".format(secret_key)) f.write("DATABASE = path.join('db', 'database.db')") if path.isdir("db") and path.exists(path.join("db", "database.db")): print("A database already exists.") print("1. Reset current database.") print("2. Transfer data to new database.") print("3. Keep old database.") inp = input("Action (1,2,3): ") # inp = input("A database already exists. Do you want to reset current database? (y/n) ") # if inp != "y": # print("Database was not reseted.") # return None if inp == "2": pass elif inp != "1": print("Old database keeped.") return None print("Will initialize a new database.") print("You will need an admin account.") username = input("Username: "******"Password: "******"Retype password: "******"Error! Username or password is empty. Aborting...") else: if not path.isdir("db"): makedirs("db") with open(path.join("db", "database.db"), "w") as f: pass from server import init_db, hash_password, connect_db init_db() db = connect_db() try: r = db.execute("insert into User values (?,?,?)", [username, hash_password(password), True]) # db.execute("insert into Beer_type values (?)", ["Ingen"]) db.commit() print("Database was successfully created.") except Exception as e: print("Database not created, error.") print(e) pass finally: if db: db.close()