Skip to content

Commit

Permalink
idiomatically use super() constructors in DB models
Browse files Browse the repository at this point in the history
and fix one bug from incorrect usage
  • Loading branch information
brassy-endomorph committed Sep 28, 2024
1 parent 90cbf25 commit 5d2c99f
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions hushline/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ def fetch_or_default(cls) -> Self:
return cls.fetch() or cls()

def __init__(self, id: int | None = None) -> None:
super().__init__()
self.id = id if id is not None else self._DEFAULT_ID
super().__init__(id=id if id is not None else self._DEFAULT_ID)


@dataclass(frozen=True, repr=False, eq=False)
Expand Down Expand Up @@ -99,9 +98,11 @@ def __init__(
is_primary: bool,
**kwargs: Any,
) -> None:
super().__init__(**kwargs)
self._username = _username
self.is_primary = is_primary
super().__init__(
_username=_username,
is_primary=is_primary,
**kwargs,
)

@property
def username(self) -> str:
Expand Down Expand Up @@ -245,7 +246,7 @@ def __init__(self, **kwargs: Any) -> None:
if key in kwargs:
raise ValueError(f"Key {key!r} cannot be mannually set. Try 'password' instead.")
pw = kwargs.pop("password", None)
super().__init__()
super().__init__(**kwargs)
self.password_hash = pw


Expand Down Expand Up @@ -281,11 +282,12 @@ def __init__(
otp_code: str | None = None,
timecode: int | None = None,
) -> None:
super().__init__()
self.user_id = user_id
self.successful = successful
self.otp_code = otp_code
self.timecode = timecode
super().__init__(
user_id=user_id,
successful=successful,
otp_code=otp_code,
timecode=timecode,
)


class Message(Model):
Expand Down Expand Up @@ -323,9 +325,10 @@ class InviteCode(Model):
expiration_date: Mapped[datetime]

def __init__(self) -> None:
super().__init__()
self.code = secrets.token_urlsafe(16)
self.expiration_date = datetime.now(timezone.utc) + timedelta(days=365)
super().__init__(
code=secrets.token_urlsafe(16),
expiration_date=datetime.now(timezone.utc) + timedelta(days=365),
)

def __repr__(self) -> str:
return f"<InviteCode {self.code}>"

0 comments on commit 5d2c99f

Please sign in to comment.