view.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. def bookmarklet_link(self):
  35. return (
  36. "javascript:(function(){window.open(`"
  37. + c.app_path
  38. + "/u/"
  39. + self.username
  40. + "/l?name=${document.title}&url=${document.URL}`);})();"
  41. )
  42. @dataclass
  43. class Tag(View):
  44. url: str
  45. name: str
  46. @dataclass
  47. class Link(View):
  48. id: int
  49. url: str
  50. name: str
  51. description: str
  52. private: bool
  53. tags: List[Tag]
  54. created: datetime
  55. is_mine: bool
  56. link_url: str
  57. @dataclass
  58. class LinkList(View):
  59. links: List[Any]
  60. pages: Optional[Pagination] = None
  61. @dataclass
  62. class SingleLink(View):
  63. link: Any
  64. @dataclass
  65. class Message(View):
  66. title: str
  67. message: str
  68. @dataclass
  69. class AddLinkDefaults(View):
  70. url: Optional[str] = None
  71. name: Optional[str] = None
  72. @dataclass
  73. class Page(View):
  74. title: str
  75. content: str
  76. user: Optional[Any]