view.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from dataclasses import dataclass
  2. from datetime import datetime
  3. from typing import Any, Optional, List
  4. import lc.config as c
  5. class View:
  6. pass
  7. @dataclass
  8. class Pagination(View):
  9. current: int
  10. last: int
  11. def previous(self) -> Optional[dict]:
  12. if self.current > 1:
  13. return {"page": self.current - 1}
  14. return None
  15. def next(self) -> Optional[dict]:
  16. if self.current < self.last:
  17. return {"page": self.current + 1}
  18. return None
  19. @classmethod
  20. def from_total(cls, current, total) -> "Pagination":
  21. return cls(current=current, last=((total - 1) // c.per_page) + 1,)
  22. @dataclass
  23. class UserInvite(View):
  24. claimed: bool
  25. claimant: str
  26. token: str
  27. @dataclass
  28. class AdminPane(View):
  29. invites: List[UserInvite]
  30. @dataclass
  31. class Config(View):
  32. username: str
  33. admin_pane: Optional[AdminPane]
  34. @dataclass
  35. class Tag(View):
  36. url: str
  37. name: str
  38. @dataclass
  39. class Link(View):
  40. id: int
  41. url: str
  42. name: str
  43. description: str
  44. private: bool
  45. tags: List[Tag]
  46. created: datetime
  47. is_mine: bool
  48. link_url: str
  49. @dataclass
  50. class LinkList(View):
  51. links: List[Any]
  52. pages: Optional[Pagination] = None
  53. @dataclass
  54. class SingleLink(View):
  55. link: Any
  56. @dataclass
  57. class Message(View):
  58. title: str
  59. message: str
  60. @dataclass
  61. class Page(View):
  62. title: str
  63. content: str
  64. user: Optional[str]