123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import abc
- from dataclasses import dataclass
- from dataclasses_json import dataclass_json
- from datetime import datetime
- from typing import List, Mapping, Optional, TypeVar, Type
- import lc.config as c
- import lc.error as e
- T = TypeVar("T")
- class Request(metaclass=abc.ABCMeta):
- @classmethod
- @abc.abstractmethod
- def from_form(cls: Type[T], form: Mapping[str, str]) -> T:
- pass
- # technically this gets added by dataclass_json, but mypy isn't
- # aware of it, so it's going to get declared here as though it
- # weren't abstract and then dataclass_json will add it
- @classmethod
- def from_json(cls: Type[T], json: bytes) -> T:
- pass
- @dataclass_json
- @dataclass
- class User(Request):
- name: str
- password: str
- @classmethod
- def from_form(cls, form: Mapping[str, str]):
- return cls(
- name=form["username"],
- password=form["password"],
- )
- def to_token(self) -> str:
- return c.app.serialize_token({"name": self.name})
- @dataclass_json
- @dataclass
- class NewUser(Request):
- name: str
- n1: str
- n2: str
- @classmethod
- def from_form(cls, form: Mapping[str, str]):
- return cls(
- name=form["username"],
- n1=form["n1"],
- n2=form["n2"],
- )
- def to_user_request(self) -> User:
- if self.n1 != self.n2:
- raise e.MismatchedPassword()
- return User(name=self.name, password=self.n1)
- @dataclass_json
- @dataclass
- class PasswordChange(Request):
- n1: str
- n2: str
- old: str
- @classmethod
- def from_form(cls, form: Mapping[str, str]):
- return cls(
- old=form["old"],
- n1=form["n1"],
- n2=form["n2"],
- )
- def require_match(self):
- if self.n1 != self.n2:
- raise e.MismatchedPassword()
- @dataclass_json
- @dataclass
- class Link(Request):
- url: str
- name: str
- description: str
- private: bool
- tags: List[str]
- created: Optional[datetime] = None
- @classmethod
- def from_form(cls, form: Mapping[str, str]):
- return cls(
- url=form["url"],
- name=form["name"],
- description=form["description"],
- private="private" in form,
- tags=form["tags"].split(),
- )
|