view.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 Message(View):
  55. title: str
  56. message: str
  57. @dataclass
  58. class Page(View):
  59. title: str
  60. content: str
  61. user: Optional[str]