mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-10 17:16:22 +01:00
Allow assigning factions to states.
This commit is contained in:
@@ -6,6 +6,7 @@ from .providers import ObjectNotFound
|
||||
from .models import EveAllianceInfo
|
||||
from .models import EveCharacter
|
||||
from .models import EveCorporationInfo
|
||||
from .models import EveFactionInfo
|
||||
|
||||
|
||||
class EveEntityExistsError(forms.ValidationError):
|
||||
@@ -34,6 +35,38 @@ class EveEntityForm(forms.ModelForm):
|
||||
pass
|
||||
|
||||
|
||||
def get_faction_choices():
|
||||
# use a method to avoid making an ESI call when the app loads
|
||||
# restrict to only those factions a player can join for faction warfare
|
||||
player_factions = [x for x in EveFactionInfo.provider.get_all_factions() if x['militia_corporation_id']]
|
||||
return [(x['faction_id'], x['name']) for x in player_factions]
|
||||
|
||||
|
||||
|
||||
class EveFactionForm(EveEntityForm):
|
||||
id = forms.ChoiceField(
|
||||
choices=get_faction_choices,
|
||||
label="Name"
|
||||
)
|
||||
|
||||
def clean_id(self):
|
||||
try:
|
||||
assert self.Meta.model.provider.get_faction(self.cleaned_data['id'])
|
||||
except (AssertionError, ObjectNotFound):
|
||||
raise EveEntityNotFoundError('faction', self.cleaned_data['id'])
|
||||
if self.Meta.model.objects.filter(faction_id=self.cleaned_data['id']).exists():
|
||||
raise EveEntityExistsError('faction', self.cleaned_data['id'])
|
||||
return self.cleaned_data['id']
|
||||
|
||||
def save(self, commit=True):
|
||||
faction = self.Meta.model.provider.get_faction(self.cleaned_data['id'])
|
||||
return self.Meta.model.objects.create(faction_id=faction.id, faction_name=faction.name)
|
||||
|
||||
class Meta:
|
||||
fields = ['id']
|
||||
model = EveFactionInfo
|
||||
|
||||
|
||||
class EveCharacterForm(EveEntityForm):
|
||||
entity_type_name = 'character'
|
||||
|
||||
@@ -94,6 +127,21 @@ class EveAllianceForm(EveEntityForm):
|
||||
model = EveAllianceInfo
|
||||
|
||||
|
||||
@admin.register(EveFactionInfo)
|
||||
class EveFactionInfoAdmin(admin.ModelAdmin):
|
||||
search_fields = ['faction_name']
|
||||
list_display = ('faction_name',)
|
||||
ordering = ('faction_name',)
|
||||
|
||||
def has_change_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
if not obj or not obj.pk:
|
||||
return EveFactionForm
|
||||
return super().get_form(request, obj=obj, **kwargs)
|
||||
|
||||
|
||||
@admin.register(EveCorporationInfo)
|
||||
class EveCorporationInfoAdmin(admin.ModelAdmin):
|
||||
search_fields = ['corporation_name']
|
||||
@@ -135,7 +183,7 @@ class EveCharacterAdmin(admin.ModelAdmin):
|
||||
'character_ownership__user__username'
|
||||
]
|
||||
list_display = (
|
||||
'character_name', 'corporation_name', 'alliance_name', 'user', 'main_character'
|
||||
'character_name', 'corporation_name', 'alliance_name', 'faction_name', 'user', 'main_character'
|
||||
)
|
||||
list_select_related = (
|
||||
'character_ownership', 'character_ownership__user__profile__main_character'
|
||||
@@ -143,6 +191,7 @@ class EveCharacterAdmin(admin.ModelAdmin):
|
||||
list_filter = (
|
||||
'corporation_name',
|
||||
'alliance_name',
|
||||
'faction_name',
|
||||
(
|
||||
'character_ownership__user__profile__main_character',
|
||||
admin.RelatedOnlyFieldListFilter
|
||||
|
||||
Reference in New Issue
Block a user