[ADD] get_all_characters_from_user function to User API

This commit is contained in:
Peter Pfeufer
2024-01-24 21:03:09 +01:00
parent 13a8b7678f
commit 76ae9b8849
3 changed files with 48 additions and 6 deletions

View File

@@ -15,7 +15,7 @@ def get_main_character_from_evecharacter(
character: EveCharacter,
) -> Optional[EveCharacter]:
"""
Get the main character for a given Evecharacter or None when no main character is set
Get the main character for a given EveCharacter or None when no main character is set
:param character:
:type character:
@@ -37,7 +37,7 @@ def get_main_character_from_evecharacter(
def get_user_from_evecharacter(character: EveCharacter) -> User:
"""
Get the user for an Evecharacter or the sentinel user when no user is found
Get the user for an EveCharacter or the sentinel user when no user is found
:param character:
:type character:

View File

@@ -6,17 +6,33 @@ from typing import Optional
from django.contrib.auth.models import User
from allianceauth.authentication.models import CharacterOwnership
from allianceauth.eveonline.models import EveCharacter
def get_sentinel_user() -> User:
def get_all_characters_from_user(user: User) -> list[EveCharacter]:
"""
Get the sentinel user or create one
Get all characters from a user or an empty list
when no characters are found for the user or the user is None
:param user:
:type user:
:return:
:rtype:
"""
return User.objects.get_or_create(username="deleted")[0]
if user is None:
return []
try:
characters = [
char.character for char in CharacterOwnership.objects.filter(user=User)
]
except AttributeError:
return []
return characters
def get_main_character_from_user(user: User) -> Optional[EveCharacter]:
"""
@@ -62,3 +78,13 @@ def get_main_character_name_from_user(user: User) -> str:
return str(user)
return username
def get_sentinel_user() -> User:
"""
Get the sentinel user or create one
:return:
"""
return User.objects.get_or_create(username="deleted")[0]