db.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from datetime import datetime
  2. from peewee import Model
  3. from peewee import AutoField, BlobField, BooleanField, CharField, DateTimeField, TextField, IPField, ForeignKeyField
  4. from peewee_async import Manager, PooledPostgresqlDatabase
  5. from config import config
  6. database = PooledPostgresqlDatabase(config.DATABASE,
  7. user=config.DB_USER,
  8. password=config.DB_PASSWORD,
  9. autorollback=True)
  10. class BaseModel(Model):
  11. class Meta:
  12. database = database
  13. class User(BaseModel):
  14. id = AutoField()
  15. signup_date = DateTimeField(default=datetime.now)
  16. ip_address = IPField()
  17. username = CharField(unique=True)
  18. password = BlobField() # Store password hash here (SCrypt).
  19. is_disabled = BooleanField(default=False)
  20. class Tag(BaseModel):
  21. id = AutoField()
  22. tag = CharField(unique=True)
  23. class Video(BaseModel):
  24. id = AutoField()
  25. upload_date = DateTimeField(default=datetime.now)
  26. video = CharField()
  27. thumbnail = CharField()
  28. uploader = ForeignKeyField(User, backref='videos')
  29. is_hidden = BooleanField(default=False)
  30. class Comment(BaseModel):
  31. id = AutoField()
  32. publish_date = DateTimeField(default=datetime.now)
  33. text = TextField()
  34. user = ForeignKeyField(User, backref='comments')
  35. video = ForeignKeyField(Video, backref='comments')
  36. is_hidden = BooleanField(default=False)
  37. class VideoTag(BaseModel):
  38. video = ForeignKeyField(Video, backref='tags')
  39. tag = ForeignKeyField(Tag, backref='videos')
  40. class Like(BaseModel):
  41. user = ForeignKeyField(User, backref='likes')
  42. video = ForeignKeyField(Video, backref='likes')
  43. database.connect()
  44. database.create_tables([
  45. User,
  46. Tag,
  47. Video,
  48. Comment,
  49. VideoTag,
  50. Like
  51. ])
  52. db = Manager(database)
  53. database.set_allow_sync(False)