Use IF NOT EXISTS instead of sqlite specific hack. Clean up formating. Other minor fixes.

This commit is contained in:
Thomas L 2018-03-22 21:15:15 +01:00
parent 235b8524f8
commit 404be47d1b

156
db.py
View file

@ -14,90 +14,89 @@ class DB(object):
def __init__(self):
dbfile = path.join(path.dirname(path.abspath(__file__)),
'ticketfrei.sqlite')
#dbfile = ':memory:'
# dbfile = ':memory:'
self.conn = sqlite3.connect(dbfile)
self.cur = self.conn.cursor()
self.cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user';")
if self.cur.fetchall() == []:
self.create()
print("Initialized new sqlite database.")
self.create()
self.secret = urandom(32)
with open('config.toml') as configfile:
self.config = toml.load(configfile)
def create(self):
# init db
self.cur.executescript('\
CREATE TABLE "user" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`email` TEXT,\
`passhash` TEXT,\
`enabled` INTEGER DEFAULT 1\
);\
CREATE TABLE "twitter_request_tokens" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`user_id` INTEGER,\
`request_token` TEXT,\
FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\
);\
CREATE TABLE "twitter_accounts" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`user_id` INTEGER,\
`client_id` TEXT,\
`client_secret` TEXT,\
FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\
);\
CREATE TABLE "trigger_good" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`user_id` INTEGER,\
`words` TEXT,\
FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\
);\
CREATE TABLE "trigger_bad" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`user_id` INTEGER,\
`words` TEXT,\
FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\
);\
CREATE TABLE "mastodon_instances" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`instance` TEXT,\
`client_id` TEXT,\
`client_secret` TEXT\
);\
CREATE TABLE "mastodon_accounts" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`user_id` INTEGER,\
`access_token` TEXT,\
`instance_id` TEXT,\
`active` INTEGER,\
FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ),\
FOREIGN KEY(`instance_id`) REFERENCES `mastodon_instances` ( `id` )\
);\
CREATE TABLE "seen_toots" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`user_id` INTEGER,\
`mastodon_accounts_id` INTEGER,\
`toot_id` TEXT,\
FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ),\
FOREIGN KEY(`mastodon_accounts_id`) REFERENCES `mastodon_accounts` ( `id` )\
);\
CREATE TABLE "mail" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`user_id` INTEGER,\
`email` TEXT,\
`active` INTEGER,\
FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\
);\
CREATE TABLE "seen_tweets" (\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\
`user_id` INTEGER,\
`twitter_accounts_id` INTEGER,\
`tweet_id` TEXT,\
FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\
FOREIGN KEY(`twitter_accounts_id`) REFERENCES `twitter_accounts` ( `id` )\
);\
')
self.cur.executescript('''
CREATE TABLE IF NOT EXISTS user (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
email TEXT,
passhash TEXT,
enabled INTEGER DEFAULT 1
);
CREATE TABLE IF NOT EXISTS twitter_request_tokens (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
request_token TEXT,
FOREIGN KEY(user_id) REFERENCES user(id)
);
CREATE TABLE IF NOT EXISTS twitter_accounts (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
client_id TEXT,
client_secret TEXT,
FOREIGN KEY(user_id) REFERENCES user(id)
);
CREATE TABLE IF NOT EXISTS trigger_good (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
words TEXT,
FOREIGN KEY(user_id) REFERENCES user(id)
);
CREATE TABLE IF NOT EXISTS trigger_bad (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
words TEXT,
FOREIGN KEY(user_id) REFERENCES user(id)
);
CREATE TABLE IF NOT EXISTS mastodon_instances (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
instance TEXT,
client_id TEXT,
client_secret TEXT
);
CREATE TABLE IF NOT EXISTS mastodon_accounts (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
access_token TEXT,
instance_id TEXT,
active INTEGER,
FOREIGN KEY(user_id) REFERENCES user(id),
FOREIGN KEY(instance_id) REFERENCES mastodon_instances(id)
);
CREATE TABLE IF NOT EXISTS seen_toots (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
mastodon_accounts_id INTEGER,
toot_id TEXT,
FOREIGN KEY(user_id) REFERENCES user(id),
FOREIGN KEY(mastodon_accounts_id)
REFERENCES mastodon_accounts(id)
);
CREATE TABLE IF NOT EXISTS mail (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
email TEXT,
active INTEGER,
FOREIGN KEY(user_id) REFERENCES user(id)
);
CREATE TABLE IF NOT EXISTS seen_tweets (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER,
twitter_accounts_id INTEGER,
tweet_id TEXT,
FOREIGN KEY(user_id) REFERENCES user(id)
FOREIGN KEY(twitter_accounts_id)
REFERENCES twitter_accounts(id)
);
''')
def token(self, email, password):
return jwt.encode({
@ -120,7 +119,7 @@ class DB(object):
row = self.cur.fetchone()
if not row:
return None # No user with this email
if not scrypt_mcf_check(row[1].encode("utf-8"),
if not scrypt_mcf_check(row[1].encode('ascii'),
password.encode('utf-8')):
return None # Wrong passphrase
return User(self, row[0])
@ -135,7 +134,8 @@ class DB(object):
def send_confirmation_mail(self, confirm_link, email):
m = sendmail.Mailer(self.config)
try:
m.send("Complete your registration here: " + confirm_link, email, "[Ticketfrei] Confirm your account")
m.send("Complete your registration here: " + confirm_link,
email, "[Ticketfrei] Confirm your account")
except smtplib.SMTPRecipientsRefused:
return "Please enter a valid E-Mail address."