This repository has been archived on 2023-10-10. You can view files and clone it, but cannot push or open issues/pull-requests.
cutedb/manage.py

62 lines
2.0 KiB
Python

import sys
import sqlite3
import hashlib
import getpass
import random
try:
from webapp import config
except ImportError:
raise Exception("Could not import config file. Does config.py exist?")
def usage():
sys.stderr.write("""Usage: %s <action> <parameters>
Where action is one of:
createdb - create database and tables (THIS WILL DESTROY YOUR STUFF)
createadmin - create admin user\n""")
def main():
if len(sys.argv) < 2:
usage()
return 1
action = sys.argv[1]
if action not in ["createdb", "createadmin"]:
usage()
return 1
if action == "createdb":
conn = sqlite3.connect(config.DB_FILE)
c = conn.cursor()
c.execute("DROP TABLE IF EXISTS quotes;")
c.execute("""CREATE TABLE quotes (
_id integer primary key autoincrement not null,
_date datetime not null,
_text text not null,
_meta text,
_up integer not null default 0,
_down integer not null default 0,
_approved boolean default 0
);""")
c.execute("DROP TABLE IF EXISTS users;")
c.execute("""CREATE TABLE users (
_id integer primary key autoincrement not null,
_username text not null,
_password text not null,
_salt text not null,
_real_name text
);""")
conn.commit()
conn.close()
sys.stdout.write("Database create succesfully.\n")
elif action == "createadmin":
username = raw_input("Username: ")
password = getpass.getpass()
realname = raw_input("Real Name: ")
conn = sqlite3.connect(config.DB_FILE)
salt = ''.join([random.choice([chr(c) for c in range(ord('a'), ord('z'))]) for _ in range(20)])
c = conn.cursor()
hashed = hashlib.sha256(password + salt).hexdigest()
c.execute("INSERT INTO users (_username, _password, _salt, _real_name) VALUES (?, ?, ?, ?)", [username, hashed, salt, realname])
conn.commit()
conn.close()
sys.exit(main())