Skip to content

Commit e9294e4

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

2 files changed

Lines changed: 34 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: 33 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
@@ -382,6 +383,13 @@ def from_dict(data: models.Role) -> Role:
382383
permissions=[Permission.from_dict(role) for role in data.get("permissions") or []],
383384
)
384385

386+
def to_dict(self) -> models.Role:
387+
return models.Role(
388+
id=self.role_id or -1,
389+
name=self.name,
390+
permissions=[Permission.to_dict(role) for role in self.permissions],
391+
)
392+
385393

386394
@dataclasses.dataclass
387395
class GroupRolePair:
@@ -395,6 +403,9 @@ def from_dict(data: models.GroupRolePair) -> GroupRolePair:
395403
role=Role.from_dict(data["role"]),
396404
)
397405

406+
def to_dict(self) -> models.GroupRolePair:
407+
return models.GroupRolePair(group=self.group.to_dict(), role=self.role.to_dict())
408+
398409

399410
class User(FlixType):
400411
def __init__(
@@ -417,7 +428,7 @@ def __init__(
417428
super().__init__(_client)
418429
self.user_id = user_id
419430
self.username = username
420-
self.password = password
431+
self.password = hashlib.sha256(password.encode()) if password else None
421432
self.email = email
422433
self.groups = groups or []
423434
self.created_date: datetime.datetime = created_date or datetime.datetime.now(
@@ -449,9 +460,30 @@ def from_dict(
449460
into.metadata = Metadata.from_dict(data.get("metadata"), parent=into, _client=_client)
450461
return into
451462

463+
def to_dict(self) -> models.User:
464+
return models.User(
465+
id=self.user_id if self.user_id else -1,
466+
username=self.username,
467+
email=self.email,
468+
groups=[GroupRolePair.to_dict(group) for group in self.groups or []],
469+
type=self.user_type,
470+
is_admin=self.is_admin,
471+
is_system=self.is_system,
472+
is_third_party=self.is_third_party,
473+
deleted=self.deleted,
474+
metadata=self.metadata.to_dict(),
475+
password=self.password.hexdigest() if self.password else None,
476+
)
477+
452478
def path_prefix(self) -> str:
453479
return f"/user/{self.user_id}"
454480

481+
async def save(self) -> None:
482+
"""Save this user."""
483+
path = "/user"
484+
result = cast(models.User, await self.client.post(path, body=self.to_dict()))
485+
self.from_dict(result, into=self, _client=self.client)
486+
455487

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

0 commit comments

Comments
 (0)