Skip to content

Commit 38c3a92

Browse files
committed
TP-595667 Adds the ability to save new users.
1 parent bd3405a commit 38c3a92

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

flixpy/flix/lib/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class User(TypedDict, total=False):
4848
type: str
4949
username: str
5050
metadata: list[MetadataField]
51+
password: str | None
5152

5253

5354
class Hash(TypedDict, total=False):

flixpy/flix/lib/types.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import dataclasses
1111
import datetime
1212
import enum
13+
import hashlib
1314
import os
1415
import pathlib
1516
from collections.abc import AsyncIterable, Callable, Iterable, Iterator, Mapping, MutableMapping
@@ -417,7 +418,7 @@ def __init__(
417418
super().__init__(_client)
418419
self.user_id = user_id
419420
self.username = username
420-
self.password = password
421+
self.password = hashlib.sha256(password.encode()) if password else None
421422
self.email = email
422423
self.groups = groups or []
423424
self.created_date: datetime.datetime = created_date or datetime.datetime.now(
@@ -449,9 +450,29 @@ def from_dict(
449450
into.metadata = Metadata.from_dict(data.get("metadata"), parent=into, _client=_client)
450451
return into
451452

453+
def to_dict(self) -> models.User:
454+
return models.User(
455+
id=self.user_id,
456+
username=self.username,
457+
email=self.email,
458+
groups=self.groups,
459+
type=self.user_type,
460+
is_admin=self.is_admin,
461+
is_system=self.is_system,
462+
is_third_party=self.is_third_party,
463+
deleted=self.deleted,
464+
metadata=self.metadata.to_dict(),
465+
password=self.password.hexdigest() if self.password else None
466+
)
467+
452468
def path_prefix(self) -> str:
453469
return f"/user/{self.user_id}"
454470

471+
async def save(self) -> None:
472+
"""Save this user."""
473+
path = "/user"
474+
result = cast(models.User, await self.client.post(path, body=self.to_dict()))
475+
self.from_dict(result, into=self, _client=self.client)
455476

456477
class MediaObjectStatus(enum.Enum):
457478
"""Describes the status of a media object."""

0 commit comments

Comments
 (0)