-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
37 lines (30 loc) · 1.06 KB
/
models.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
import os
import peewee
if 'HEROKU' in os.environ:
import urllib.parse
urllib.parse.uses_netloc.append('postgres')
url = urllib.parse.urlparse(os.environ["DATABASE_URL"])
db = peewee.PostgresqlDatabase(database=url.path[1:], user=url.username, password=url.password, host=url.hostname, port=url.port)
else:
db = peewee.PostgresqlDatabase('ivle_bot_test', user='postgres')
class IBModel(peewee.Model):
class Meta:
database = db
class User(IBModel):
user_id = peewee.CharField(max_length=128, primary_key=True)
auth_token = peewee.TextField()
class Module(IBModel):
module_id = peewee.TextField()
module_code = peewee.CharField(max_length=64)
acad_year = peewee.CharField(max_length=16)
semester = peewee.IntegerField()
class Meta:
primary_key = peewee.CompositeKey('module_code', 'acad_year', 'semester')
def setup_database():
db.connect()
try:
db.create_tables([User, Module])
except peewee.OperationalError as e:
print(e)
if __name__ == '__main__':
setup_database()