request.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import abc
  2. from dataclasses import dataclass
  3. from dataclasses_json import dataclass_json
  4. from datetime import datetime
  5. from typing import List, Mapping, Optional
  6. import lc.config as c
  7. class Request(metaclass=abc.ABCMeta):
  8. @classmethod
  9. @abc.abstractmethod
  10. def from_form(cls, form: Mapping[str, str]):
  11. pass
  12. @dataclass_json
  13. @dataclass
  14. class User(Request):
  15. name: str
  16. password: str
  17. @classmethod
  18. def from_form(cls, form: Mapping[str, str]):
  19. return cls(name=form["username"], password=form["password"],)
  20. def to_token(self) -> str:
  21. return c.SERIALIZER.dumps({"name": self.name, "password": self.password,})
  22. @dataclass_json
  23. @dataclass
  24. class Link(Request):
  25. url: str
  26. name: str
  27. description: str
  28. private: bool
  29. tags: List[str]
  30. created: Optional[datetime] = None
  31. @classmethod
  32. def from_form(cls, form: Mapping[str, str]):
  33. return cls(
  34. url=form["url"],
  35. name=form["name"],
  36. description=form["description"],
  37. private="private" in form,
  38. tags=form["tags"].split(),
  39. )