Merge branch '834-check-if-character-is-online' into 'master'

[FIX] Check if character is online before accepting FAT click

Closes #834

See merge request allianceauth/allianceauth!1451
This commit is contained in:
Ariel Rin 2022-09-07 06:29:19 +00:00
commit 8d255fb720

View File

@ -248,21 +248,31 @@ def fatlink_monthly_personal_statistics_view(request, year, month, char_id=None)
@login_required
@token_required(
scopes=['esi-location.read_location.v1', 'esi-location.read_ship_type.v1', 'esi-universe.read_structures.v1'])
scopes=[
'esi-location.read_location.v1',
'esi-location.read_ship_type.v1',
'esi-universe.read_structures.v1',
'esi-location.read_online.v1',
]
)
def click_fatlink_view(request, token, fat_hash=None):
c = token.get_esi_client(spec_file=SWAGGER_SPEC_PATH)
character = EveCharacter.objects.get_character_by_id(token.character_id)
character_online = c.Location.get_characters_character_id_online(
character_id=token.character_id
).result()
if character_online["online"] is True:
fatlink = get_object_or_404(Fatlink, hash=fat_hash)
if (timezone.now() - fatlink.fatdatetime) < datetime.timedelta(seconds=(fatlink.duration * 60)):
character = EveCharacter.objects.get_character_by_id(token.character_id)
if character:
# get data
c = token.get_esi_client(spec_file=SWAGGER_SPEC_PATH)
location = c.Location.get_characters_character_id_location(character_id=token.character_id).result()
ship = c.Location.get_characters_character_id_ship(character_id=token.character_id).result()
location['solar_system_name'] = \
c.Universe.get_universe_systems_system_id(system_id=location['solar_system_id']).result()['name']
if location['station_id']:
location['station_name'] = \
c.Universe.get_universe_stations_station_id(station_id=location['station_id']).result()['name']
@ -272,6 +282,7 @@ def click_fatlink_view(request, token, fat_hash=None):
'name']
else:
location['station_name'] = "No Station"
ship['ship_type_name'] = provider.get_itemtype(ship['ship_type_id']).name
fat = Fat()
@ -281,14 +292,17 @@ def click_fatlink_view(request, token, fat_hash=None):
fat.fatlink = fatlink
fat.character = character
fat.user = request.user
try:
fat.full_clean()
fat.save()
messages.success(request, _('Fleet participation registered.'))
except ValidationError as e:
err_messages = []
for errorname, message in e.message_dict.items():
err_messages.append(message[0])
messages.error(request, ' '.join(err_messages))
else:
context = {
@ -298,9 +312,18 @@ def click_fatlink_view(request, token, fat_hash=None):
token.character_id, 128
),
}
return render(request, 'fleetactivitytracking/characternotexisting.html', context=context)
else:
messages.error(request, _('FAT link has expired.'))
else:
messages.warning(
request,
_(
f"Cannot register the fleet participation for {character.character_name}. The character needs to be online."
),
)
return redirect('fatlink:view')