-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
45 lines (34 loc) · 1.01 KB
/
models.py
File metadata and controls
45 lines (34 loc) · 1.01 KB
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
from peewee import *
import os
from playhouse.db_url import connect
import datetime
from flask_login import UserMixin
if 'ON_HEROKU' in os.environ:
DATABASE = connect(os.environ.get('DATABASE_URL'))
else :
DATABASE = PostgresqlDatabase('rantz')
class Users(UserMixin, Model):
username = CharField(unique=True)
password = CharField()
class Meta:
database = DATABASE
class Rants(Model):
title = CharField()
body = CharField()
topic = CharField()
created_by = ForeignKeyField(Users, backref='rants')
created_at = DateTimeField(default=datetime.datetime.now)
class Meta:
database = DATABASE
class Comments(Model):
created_by = ForeignKeyField(Users, backref='user_comment')
body = CharField()
parent_post = ForeignKeyField(Rants, backref='comments')
created_at = DateTimeField(default=datetime.datetime.now)
class Meta:
database = DATABASE
def initialize():
DATABASE.connect()
DATABASE.create_tables([Rants, Users, Comments], safe=True)
print("tables created")
DATABASE.close()