123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- from datetime import datetime
- from peewee import Model
- from peewee import AutoField, BlobField, BooleanField, CharField, DateTimeField, TextField, IPField, ForeignKeyField
- from peewee_async import Manager, PooledPostgresqlDatabase
- from config import config
- database = PooledPostgresqlDatabase(config.DATABASE,
- user=config.DB_USER,
- password=config.DB_PASSWORD,
- autorollback=True)
- class BaseModel(Model):
- class Meta:
- database = database
- class User(BaseModel):
- id = AutoField()
- signup_date = DateTimeField(default=datetime.now)
- ip_address = IPField()
- username = CharField(unique=True)
- password = BlobField() # Store password hash here (SCrypt).
- is_disabled = BooleanField(default=False)
- class Tag(BaseModel):
- id = AutoField()
- tag = CharField(unique=True)
- class Video(BaseModel):
- id = AutoField()
- upload_date = DateTimeField(default=datetime.now)
- video = CharField()
- thumbnail = CharField()
- uploader = ForeignKeyField(User, backref='videos')
- is_hidden = BooleanField(default=False)
- class Comment(BaseModel):
- id = AutoField()
- publish_date = DateTimeField(default=datetime.now)
- text = TextField()
- user = ForeignKeyField(User, backref='comments')
- video = ForeignKeyField(Video, backref='comments')
- is_hidden = BooleanField(default=False)
- class VideoTag(BaseModel):
- video = ForeignKeyField(Video, backref='tags')
- tag = ForeignKeyField(Tag, backref='videos')
- class Like(BaseModel):
- user = ForeignKeyField(User, backref='likes')
- video = ForeignKeyField(Video, backref='likes')
- database.connect()
- database.create_tables([
- User,
- Tag,
- Video,
- Comment,
- VideoTag,
- Like
- ])
- db = Manager(database)
- database.set_allow_sync(False)
|