model.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import datetime
  2. from passlib.apps import custom_app_context as pwd
  3. import peewee
  4. import typing
  5. import lc.config as c
  6. import lc.error as e
  7. import lc.request as r
  8. class Model(peewee.Model):
  9. class Meta:
  10. database = c.DB
  11. # TODO: figure out authorization for users (oauth? passwd?)
  12. class User(Model):
  13. """
  14. A user! you know tf this is about
  15. """
  16. name = peewee.TextField(unique=True)
  17. passhash = peewee.TextField()
  18. @staticmethod
  19. def from_request(user: r.User) -> "User":
  20. passhash = pwd.hash(user.password)
  21. try:
  22. return User.create(name=user.name, passhash=passhash,)
  23. except peewee.IntegrityError:
  24. raise e.UserExists(name=user.name)
  25. def authenticate(self, password: str) -> bool:
  26. return pwd.verify(password, self.passhash)
  27. @staticmethod
  28. def login(user: r.User) -> "User":
  29. u = User.by_slug(user.name)
  30. if not u.authenticate(user.password):
  31. raise e.BadPassword(name=user.name)
  32. return u
  33. @staticmethod
  34. def by_slug(slug: str) -> "User":
  35. return User.get(name=slug)
  36. def base_url(self) -> str:
  37. return f"/u/{self.name}"
  38. def get_links(self, page: int) -> typing.List["Link"]:
  39. return Link.select().where(Link.user == self).paginate(page, c.PER_PAGE)
  40. def get_tag(self, tag_name: str) -> "Tag":
  41. return Tag.get((Tag.user == self) & (Tag.name == tag_name))
  42. class Link(Model):
  43. """
  44. A link as stored in the database
  45. """
  46. url = peewee.TextField()
  47. name = peewee.TextField()
  48. description = peewee.TextField()
  49. # TODO: do we need to track modified time?
  50. created = peewee.DateTimeField()
  51. # is the field entirely private?
  52. private = peewee.BooleanField()
  53. # owned by
  54. user = peewee.ForeignKeyField(User, backref="links")
  55. def link_url(self) -> str:
  56. return f"/u/{self.user.name}/l/{self.id}"
  57. @staticmethod
  58. def from_request(user: User, link: r.Link) -> "Link":
  59. l = Link.create(
  60. url=link.url,
  61. name=link.name,
  62. description=link.description,
  63. private=link.private,
  64. created=datetime.datetime.now(),
  65. user=user,
  66. )
  67. for tag_name in link.tags:
  68. t = Tag.get_or_create_tag(user, tag_name)
  69. HasTag.create(
  70. link=l, tag=t,
  71. )
  72. return l
  73. class Tag(Model):
  74. """
  75. A tag. This just indicates that a user has used this tag at some point.
  76. """
  77. name = peewee.TextField()
  78. parent = peewee.ForeignKeyField("self", null=True, backref="children")
  79. user = peewee.ForeignKeyField(User, backref="tags")
  80. def url(self) -> str:
  81. return f"/u/{self.user.name}/t/{self.name}"
  82. def get_links(self, page: int) -> typing.List[Link]:
  83. return [
  84. ht.link
  85. for ht in HasTag.select()
  86. .where((HasTag.tag == self))
  87. .paginate(page, c.PER_PAGE)
  88. ]
  89. @staticmethod
  90. def get_or_create_tag(user: User, tag_name: str):
  91. if (t := Tag.get_or_none(name=tag_name, user=user)) :
  92. return t
  93. parent = None
  94. if "/" in tag_name:
  95. parent_name = tag_name[: tag_name.rindex("/")]
  96. parent = Tag.get_or_create_tag(user, parent_name)
  97. return Tag.create(name=tag_name, parent=parent, user=user)
  98. class HasTag(Model):
  99. """
  100. Establishes that a link is tagged with a given tag.
  101. """
  102. link = peewee.ForeignKeyField(Link, backref="tags")
  103. tag = peewee.ForeignKeyField(Tag, backref="models")
  104. MODELS = [
  105. User,
  106. Link,
  107. Tag,
  108. HasTag,
  109. ]
  110. def create_tables():
  111. c.DB.create_tables(MODELS, safe=True)