mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 12:30:15 +02:00
Add pseudo foreign keys to EveCharacter model
This commit is contained in:
parent
650408f61c
commit
d993a299ef
@ -1,4 +1,5 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from .managers import EveCharacterManager, EveCharacterProviderManager
|
from .managers import EveCharacterManager, EveCharacterProviderManager
|
||||||
from .managers import EveCorporationManager, EveCorporationProviderManager
|
from .managers import EveCorporationManager, EveCorporationProviderManager
|
||||||
@ -6,34 +7,6 @@ from .managers import EveAllianceManager, EveAllianceProviderManager
|
|||||||
from . import providers
|
from . import providers
|
||||||
|
|
||||||
|
|
||||||
class EveCharacter(models.Model):
|
|
||||||
character_id = models.CharField(max_length=254, unique=True)
|
|
||||||
character_name = models.CharField(max_length=254, unique=True)
|
|
||||||
corporation_id = models.CharField(max_length=254)
|
|
||||||
corporation_name = models.CharField(max_length=254)
|
|
||||||
corporation_ticker = models.CharField(max_length=254)
|
|
||||||
alliance_id = models.CharField(max_length=254, blank=True, null=True, default='')
|
|
||||||
alliance_name = models.CharField(max_length=254, blank=True, null=True, default='')
|
|
||||||
|
|
||||||
objects = EveCharacterManager()
|
|
||||||
provider = EveCharacterProviderManager()
|
|
||||||
|
|
||||||
def update_character(self, character: providers.Character = None):
|
|
||||||
if character is None:
|
|
||||||
character = self.provider.get_character(self.character_id)
|
|
||||||
self.character_name = character.name
|
|
||||||
self.corporation_id = character.corp.id
|
|
||||||
self.corporation_name = character.corp.name
|
|
||||||
self.corporation_ticker = character.corp.ticker
|
|
||||||
self.alliance_id = character.alliance.id
|
|
||||||
self.alliance_name = character.alliance.name
|
|
||||||
self.save()
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return self.character_name
|
|
||||||
|
|
||||||
|
|
||||||
class EveAllianceInfo(models.Model):
|
class EveAllianceInfo(models.Model):
|
||||||
alliance_id = models.CharField(max_length=254, unique=True)
|
alliance_id = models.CharField(max_length=254, unique=True)
|
||||||
alliance_name = models.CharField(max_length=254, unique=True)
|
alliance_name = models.CharField(max_length=254, unique=True)
|
||||||
@ -86,3 +59,51 @@ class EveCorporationInfo(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.corporation_name
|
return self.corporation_name
|
||||||
|
|
||||||
|
|
||||||
|
class EveCharacter(models.Model):
|
||||||
|
character_id = models.CharField(max_length=254, unique=True)
|
||||||
|
character_name = models.CharField(max_length=254, unique=True)
|
||||||
|
corporation_id = models.CharField(max_length=254)
|
||||||
|
corporation_name = models.CharField(max_length=254)
|
||||||
|
corporation_ticker = models.CharField(max_length=254)
|
||||||
|
alliance_id = models.CharField(max_length=254, blank=True, null=True, default='')
|
||||||
|
alliance_name = models.CharField(max_length=254, blank=True, null=True, default='')
|
||||||
|
|
||||||
|
objects = EveCharacterManager()
|
||||||
|
provider = EveCharacterProviderManager()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def alliance(self) -> Union[EveAllianceInfo, None]:
|
||||||
|
"""
|
||||||
|
Pseudo foreign key from alliance_id to EveAllianceInfo
|
||||||
|
:raises: EveAllianceInfo.DoesNotExist
|
||||||
|
:return: EveAllianceInfo or None
|
||||||
|
"""
|
||||||
|
if self.alliance_id is None:
|
||||||
|
return None
|
||||||
|
return EveAllianceInfo.objects.get(alliance_id=self.alliance_id)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def corporation(self) -> EveCorporationInfo:
|
||||||
|
"""
|
||||||
|
Pseudo foreign key from corporation_id to EveCorporationInfo
|
||||||
|
:raises: EveCorporationInfo.DoesNotExist
|
||||||
|
:return: EveCorporationInfo
|
||||||
|
"""
|
||||||
|
return EveCorporationInfo.objects.get(corporation_id=self.corporation_id)
|
||||||
|
|
||||||
|
def update_character(self, character: providers.Character = None):
|
||||||
|
if character is None:
|
||||||
|
character = self.provider.get_character(self.character_id)
|
||||||
|
self.character_name = character.name
|
||||||
|
self.corporation_id = character.corp.id
|
||||||
|
self.corporation_name = character.corp.name
|
||||||
|
self.corporation_ticker = character.corp.ticker
|
||||||
|
self.alliance_id = character.alliance.id
|
||||||
|
self.alliance_name = character.alliance.name
|
||||||
|
self.save()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.character_name
|
||||||
|
121
allianceauth/eveonline/tests/test_models.py
Normal file
121
allianceauth/eveonline/tests/test_models.py
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from ..models import EveCharacter, EveCorporationInfo, EveAllianceInfo
|
||||||
|
|
||||||
|
|
||||||
|
class EveCharacterTestCase(TestCase):
|
||||||
|
def test_corporation_prop(self):
|
||||||
|
"""
|
||||||
|
Test that the correct corporation is returned by the corporation property
|
||||||
|
"""
|
||||||
|
character = EveCharacter.objects.create(
|
||||||
|
character_id='1234',
|
||||||
|
character_name='character.name',
|
||||||
|
corporation_id='2345',
|
||||||
|
corporation_name='character.corp.name',
|
||||||
|
corporation_ticker='character.corp.ticker',
|
||||||
|
alliance_id='character.alliance.id',
|
||||||
|
alliance_name='character.alliance.name',
|
||||||
|
)
|
||||||
|
|
||||||
|
expected = EveCorporationInfo.objects.create(
|
||||||
|
corporation_id='2345',
|
||||||
|
corporation_name='corp.name',
|
||||||
|
corporation_ticker='corp.ticker',
|
||||||
|
member_count=10,
|
||||||
|
alliance=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
incorrect = EveCorporationInfo.objects.create(
|
||||||
|
corporation_id='9999',
|
||||||
|
corporation_name='corp.name1',
|
||||||
|
corporation_ticker='corp.ticker1',
|
||||||
|
member_count=10,
|
||||||
|
alliance=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(character.corporation, expected)
|
||||||
|
self.assertNotEqual(character.corporation, incorrect)
|
||||||
|
|
||||||
|
def test_corporation_prop_exception(self):
|
||||||
|
"""
|
||||||
|
Check that an exception is raised when the expected
|
||||||
|
object is not in the database
|
||||||
|
"""
|
||||||
|
character = EveCharacter.objects.create(
|
||||||
|
character_id='1234',
|
||||||
|
character_name='character.name',
|
||||||
|
corporation_id='2345',
|
||||||
|
corporation_name='character.corp.name',
|
||||||
|
corporation_ticker='character.corp.ticker',
|
||||||
|
alliance_id='character.alliance.id',
|
||||||
|
alliance_name='character.alliance.name',
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(EveCorporationInfo.DoesNotExist):
|
||||||
|
result = character.corporation
|
||||||
|
|
||||||
|
def test_alliance_prop(self):
|
||||||
|
"""
|
||||||
|
Test that the correct alliance is returned by the alliance property
|
||||||
|
"""
|
||||||
|
character = EveCharacter.objects.create(
|
||||||
|
character_id='1234',
|
||||||
|
character_name='character.name',
|
||||||
|
corporation_id='2345',
|
||||||
|
corporation_name='character.corp.name',
|
||||||
|
corporation_ticker='character.corp.ticker',
|
||||||
|
alliance_id='3456',
|
||||||
|
alliance_name='character.alliance.name',
|
||||||
|
)
|
||||||
|
|
||||||
|
expected = EveAllianceInfo.objects.create(
|
||||||
|
alliance_id='3456',
|
||||||
|
alliance_name='alliance.name',
|
||||||
|
alliance_ticker='alliance.ticker',
|
||||||
|
executor_corp_id='alliance.executor_corp_id',
|
||||||
|
)
|
||||||
|
|
||||||
|
incorrect = EveAllianceInfo.objects.create(
|
||||||
|
alliance_id='9001',
|
||||||
|
alliance_name='alliance.name1',
|
||||||
|
alliance_ticker='alliance.ticker1',
|
||||||
|
executor_corp_id='alliance.executor_corp_id1',
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(character.alliance, expected)
|
||||||
|
self.assertNotEqual(character.alliance, incorrect)
|
||||||
|
|
||||||
|
def test_alliance_prop_exception(self):
|
||||||
|
"""
|
||||||
|
Check that an exception is raised when the expected
|
||||||
|
object is not in the database
|
||||||
|
"""
|
||||||
|
character = EveCharacter.objects.create(
|
||||||
|
character_id='1234',
|
||||||
|
character_name='character.name',
|
||||||
|
corporation_id='2345',
|
||||||
|
corporation_name='character.corp.name',
|
||||||
|
corporation_ticker='character.corp.ticker',
|
||||||
|
alliance_id='3456',
|
||||||
|
alliance_name='character.alliance.name',
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(EveAllianceInfo.DoesNotExist):
|
||||||
|
result = character.alliance
|
||||||
|
|
||||||
|
def test_alliance_prop_none(self):
|
||||||
|
"""
|
||||||
|
Check that None is returned when the character has no alliance
|
||||||
|
"""
|
||||||
|
character = EveCharacter.objects.create(
|
||||||
|
character_id='1234',
|
||||||
|
character_name='character.name',
|
||||||
|
corporation_id='2345',
|
||||||
|
corporation_name='character.corp.name',
|
||||||
|
corporation_ticker='character.corp.ticker',
|
||||||
|
alliance_id=None,
|
||||||
|
alliance_name=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertIsNone(character.alliance)
|
Loading…
x
Reference in New Issue
Block a user