Skip to content

Commit

Permalink
UserEntity instead of httpx client call for user update.
Browse files Browse the repository at this point in the history
  • Loading branch information
scott committed Feb 22, 2024
1 parent 4d7844d commit 7559d8b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 34 deletions.
40 changes: 6 additions & 34 deletions server/kitsu/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
calculate_end_frame,
create_folder,
create_task,
create_user,
delete_folder,
delete_task,
get_folder_by_kitsu_id,
Expand All @@ -26,6 +27,7 @@
remove_accents,
update_folder,
update_task,
update_user,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -231,7 +233,6 @@ async def sync_person(
)

payload = {
"name": username,
"attrib": {
"fullName": entity_dict["full_name"],
"email": entity_dict["email"],
Expand All @@ -251,43 +252,16 @@ async def sync_person(

# User exists but doesn't have a kitsuId assigned it it
if ayon_user and not target_user:
logging.info(f"updating ayon user with kitsu_id: {username}")
target_user = ayon_user

if target_user: # Update user
try:
session = await Session.create(user)
headers = {"Authorization": f"Bearer {session.token}"}
logging.info(f"updating user: {username}")

async with httpx.AsyncClient() as client:
await client.patch(
f"{entity_dict['ayon_server_url']}/api/users/{target_user.name}",
json=payload,
headers=headers,
)
# Rename the user
payload = {
"newName": remove_accents(
f"{entity_dict['first_name']}.{entity_dict['last_name']}".lower().strip()
)
}
async with httpx.AsyncClient() as client:
await client.patch(
f"{entity_dict['ayon_server_url']}/api/users/{target_user.name}/rename",
json=payload,
headers=headers,
)
except Exception as e:
print(e)
await update_user(target_user, username, **payload)

else: # Create user
logging.info(f"creating user: {username}")
user = UserEntity(payload)
settings = await addon.get_studio_settings()
password = settings.sync_settings.sync_users.default_password.trim()
if password:
user.set_password(password)
await user.save()
password = settings.sync_settings.sync_users.default_password.strip()
await create_user(username, password, **payload)

# update the id map
existing_users[entity_dict["id"]] = username
Expand Down Expand Up @@ -566,8 +540,6 @@ async def push_entities(
assert "type" in entity_dict
assert "id" in entity_dict

logging.info(f"push entity type: {entity_dict['type']}")

if entity_dict["type"] not in get_args(KitsuEntityType):
logging.warning(f"Unsupported kitsu entity type: {entity_dict['type']}")
continue
Expand Down
55 changes: 55 additions & 0 deletions server/kitsu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,58 @@ async def delete_task(
"project": project_name,
}
await dispatch_event(**event)


async def update_user(
user: Any,
name: str,
**kwargs,
) -> bool:
changed = False
payload = kwargs

# update name
if user.name != name:
user.name = name
changed = True

# keys that can be updated
for key in ["data"]:
if key in payload and getattr(user, key) != payload[key]:
setattr(user, key, payload[key])
changed = True
if "attrib" in payload:
for key, value in payload["attrib"].items():
if getattr(user.attrib, key) != value:
setattr(user.attrib, key, value)
if key not in user.own_attrib:
user.own_attrib.append(key)
changed = True
if changed:
await user.save()
event = {
"topic": "entity.user.updated",
"description": f"User {user.name} updated",
"summary": {"userName": user.name},
}
await dispatch_event(**event)
return changed


async def create_user(
name: str,
password: str | None,
**kwargs,
):
user = UserEntity({"name": name, **kwargs})

if password:
user.set_password(password)
await user.save()
event = {
"topic": "entity.user.created",
"description": f"User {user.name} created",
"summary": {"userName": user.name},
}
await dispatch_event(**event)
return user

0 comments on commit 7559d8b

Please sign in to comment.