-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_manage.py
executable file
·101 lines (80 loc) · 2.88 KB
/
db_manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime
from time import mktime
import argparse
import getpass
import nacl.pwhash
import psycopg2
from webquotes.conf import DSN
from webquotes.sql.create import get_create_queries, Triggers, Procedures
from webquotes.sql.insert import InsertQueries
def init_db():
queries = get_create_queries()
with psycopg2.connect(DSN) as conn:
with conn.cursor() as cur:
# Create tables
for q in queries:
cur.execute(q)
# Create functions and triggers
cur.execute(Procedures.quotes_increase)
cur.execute(Procedures.quotes_decrease)
cur.execute(Procedures.rating_increase)
cur.execute(Procedures.rating_decrease)
cur.execute(Procedures.new_rating)
cur.execute(Triggers.quotes_decrease)
cur.execute(Triggers.quotes_increase)
cur.execute(Triggers.rating)
# Insert values to "service" table with counters
cur.execute(InsertQueries.counter_quotes)
def create_user(username):
password = getpass.getpass()
hashed = nacl.pwhash.str(password.encode())
with psycopg2.connect(DSN) as conn:
with conn.cursor() as cur:
cur.execute(InsertQueries.users, (username, hashed))
def insert_quote(title, text, username):
now = mktime(datetime.now().utctimetuple())
with psycopg2.connect(DSN) as conn:
with conn.cursor() as cur:
args = (title, text, now, username)
cur.execute(InsertQueries.quote, args)
quote_id = cur.fetchall()[0][0]
print(quote_id)
COMMANDS = {
'init': {
'func': init_db,
'kw': []
},
'user-add': {
'func': create_user,
'kw': ['username']
},
'quote-add': {
'func': insert_quote,
'kw': ['title', 'text', 'username']
}
}
def main():
parser = argparse.ArgumentParser(prog='webquotes-manager-cli')
subparsers = parser.add_subparsers()
init_parser = subparsers.add_parser('init')
init_parser.set_defaults(used='init')
user_add_parser = subparsers.add_parser('user-add')
user_add_parser.set_defaults(used='user-add')
user_add_parser.add_argument('-u', '--username', type=str, required=True)
quote_add_parser = subparsers.add_parser('quote-add')
quote_add_parser.set_defaults(used='quote-add')
quote_add_parser.add_argument('-T', '--title', type=str, default=None)
quote_add_parser.add_argument('-t', '--text', type=str, required=True)
quote_add_parser.add_argument('-u', '--username', type=str, default=None)
args = parser.parse_args()
if 'used' not in args:
return
else:
_args = vars(args)
func = COMMANDS[args.used]['func']
kw = {k: _args[k] for k in COMMANDS[args.used]['kw']}
func(**kw)
if __name__ == '__main__':
main()