-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
executable file
·32 lines (24 loc) · 934 Bytes
/
db.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
#!/usr/bin/python3
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from db_declarative import Base, User, Usage
from configuration import Configuration
class Database:
def __init__(self):
config = Configuration()
db = config.get("db_endpoint")
if db is None:
logging.error("The configuration doesn't specify the DB endpoint")
raise Exception("DB isn not given")
self.engine = create_engine(db)
Base.metadata.bind = self.engine
self.DBSession = sessionmaker(bind=self.engine)
def create(self):
Base.metadata.create_all(self.engine)
def store_ur(self, ur):
session = self.DBSession()
user = User.get_unique(session, username=ur.user)
session.add(user)
usage = Usage(start_time=ur.startTime, end_time=ur.endTime, user=user)
session.add(usage)
session.commit()