summaryrefslogtreecommitdiff
path: root/f1elo/db.py
diff options
context:
space:
mode:
Diffstat (limited to 'f1elo/db.py')
-rw-r--r--f1elo/db.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/f1elo/db.py b/f1elo/db.py
new file mode 100644
index 0000000..49ccc3d
--- /dev/null
+++ b/f1elo/db.py
@@ -0,0 +1,25 @@
+import __main__
+from os import path
+
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+
+import json
+
+config = json.load(open(path.dirname(__main__.__file__) + '/config/db.json'))
+engine = create_engine("mysql://{0[user]}:{0[pass]}@{0[host]}/{0[db]}?charset=utf8".format(config))
+Session = sessionmaker(bind=engine)
+
+from f1elo.model import Driver
+
+def find_driver(name, country, session):
+ driver = session.query(Driver).filter(Driver.driver==name).first()
+ if driver:
+ return driver
+ else:
+ driver = Driver()
+ driver.driver = name
+ driver.country = country
+ session.add(driver)
+ return driver
+