view.py 1.7 KB

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