view.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 AddUser(View):
  29. token: str
  30. @dataclass
  31. class AdminPane(View):
  32. invites: List[UserInvite]
  33. @dataclass
  34. class Config(View):
  35. username: str
  36. admin_pane: Optional[AdminPane]
  37. msg: Optional[int] = None
  38. def bookmarklet_link(self):
  39. return (
  40. "javascript:(function(){window.open(`"
  41. + c.app_path
  42. + "/u/"
  43. + self.username
  44. + "/l?name=${document.title}&url=${document.URL}`);})();"
  45. )
  46. def message(self) -> Optional[str]:
  47. if self.msg == 1:
  48. return "Password changed."
  49. elif self.msg == 2:
  50. return "Mismatched new passwords; please try again."
  51. elif self.msg == 3:
  52. return "Incorrect old password; please try again."
  53. return None
  54. @dataclass
  55. class Tag(View):
  56. url: str
  57. name: str
  58. @dataclass
  59. class Link(View):
  60. id: int
  61. url: str
  62. name: str
  63. description: str
  64. private: bool
  65. tags: List[Tag]
  66. created: datetime
  67. is_mine: bool
  68. link_url: str
  69. @dataclass
  70. class LinkList(View):
  71. links: List[Any]
  72. tags: List[Tag]
  73. pages: Optional[Pagination] = None
  74. @dataclass
  75. class SingleLink(View):
  76. link: Any
  77. @dataclass
  78. class Message(View):
  79. title: str
  80. message: str
  81. @dataclass
  82. class AddLinkDefaults(View):
  83. url: Optional[str] = None
  84. name: Optional[str] = None
  85. @dataclass
  86. class Page(View):
  87. title: str
  88. content: str
  89. user: Optional[Any]
  90. @dataclass
  91. class Error(View):
  92. code: int
  93. message: str