Provide feedback when ESI errors occur.

Closes #620
This commit is contained in:
Adarnof 2017-01-13 21:56:27 -05:00
parent 73641a7a1e
commit 24bc9d4b7f
4 changed files with 63 additions and 27 deletions

View File

@ -1,7 +1,7 @@
from django.conf.urls import url from django.conf.urls import url
import corputils.views import corputils.views
app_name='corputils' app_name = 'corputils'
urlpatterns = [ urlpatterns = [
url(r'^$', corputils.views.corpstats_view, name='view'), url(r'^$', corputils.views.corpstats_view, name='view'),
url(r'^add/$', corputils.views.corpstats_add, name='add'), url(r'^add/$', corputils.views.corpstats_add, name='add'),

View File

@ -10,9 +10,11 @@ from django.conf import settings
from eveonline.models import EveCharacter, EveCorporationInfo from eveonline.models import EveCharacter, EveCorporationInfo
from corputils.models import CorpStats from corputils.models import CorpStats
from esi.decorators import token_required from esi.decorators import token_required
from bravado.exception import HTTPError
MEMBERS_PER_PAGE = int(getattr(settings, 'CORPSTATS_MEMBERS_PER_PAGE', 20)) MEMBERS_PER_PAGE = int(getattr(settings, 'CORPSTATS_MEMBERS_PER_PAGE', 20))
def get_page(model_list, page_num): def get_page(model_list, page_num):
p = Paginator(model_list, MEMBERS_PER_PAGE) p = Paginator(model_list, MEMBERS_PER_PAGE)
try: try:
@ -23,8 +25,11 @@ def get_page(model_list, page_num):
members = p.page(p.num_pages) members = p.page(p.num_pages)
return members return members
def access_corpstats_test(user): def access_corpstats_test(user):
return user.has_perm('corputils.view_corp_corpstats') or user.has_perm('corputils.view_alliance_corpstats') or user.has_perm('corputils.view_blue_corpstats') return user.has_perm('corputils.view_corp_corpstats') or user.has_perm(
'corputils.view_alliance_corpstats') or user.has_perm('corputils.view_blue_corpstats')
@login_required @login_required
@user_passes_test(access_corpstats_test) @user_passes_test(access_corpstats_test)
@ -35,11 +40,13 @@ def corpstats_add(request, token):
if EveCharacter.objects.filter(character_id=token.character_id).exists(): if EveCharacter.objects.filter(character_id=token.character_id).exists():
corp_id = EveCharacter.objects.get(character_id=token.character_id).corporation_id corp_id = EveCharacter.objects.get(character_id=token.character_id).corporation_id
else: else:
corp_id = token.get_esi_client().Character.get_characters_character_id(character_id=token.character_id).result()['corporation_id'] corp_id = \
token.get_esi_client().Character.get_characters_character_id(character_id=token.character_id).result()[
'corporation_id']
corp = EveCorporationInfo.objects.get(corporation_id=corp_id) corp = EveCorporationInfo.objects.get(corporation_id=corp_id)
cs = CorpStats.objects.create(token=token, corp=corp) cs = CorpStats.objects.create(token=token, corp=corp)
cs.update() cs.update()
assert cs.pk # ensure update was succesful assert cs.pk # ensure update was succesful
if CorpStats.objects.filter(pk=cs.pk).visible_to(request.user).exists(): if CorpStats.objects.filter(pk=cs.pk).visible_to(request.user).exists():
return redirect('corputils:view_corp', corp_id=corp.corporation_id) return redirect('corputils:view_corp', corp_id=corp.corporation_id)
except EveCorporationInfo.DoesNotExist: except EveCorporationInfo.DoesNotExist:
@ -50,11 +57,11 @@ def corpstats_add(request, token):
messages.error(request, 'Failed to gather corporation statistics with selected token.') messages.error(request, 'Failed to gather corporation statistics with selected token.')
return redirect('corputils:view') return redirect('corputils:view')
@login_required @login_required
@user_passes_test(access_corpstats_test) @user_passes_test(access_corpstats_test)
def corpstats_view(request, corp_id=None): def corpstats_view(request, corp_id=None):
corpstats = None corpstats = None
show_apis = False
# get requested model # get requested model
if corp_id: if corp_id:
@ -65,7 +72,7 @@ def corpstats_view(request, corp_id=None):
available = CorpStats.objects.visible_to(request.user) available = CorpStats.objects.visible_to(request.user)
# ensure we can see the requested model # ensure we can see the requested model
if corpstats and not corpstats in available: if corpstats and corpstats not in available:
raise PermissionDenied('You do not have permission to view the selected corporation statistics module.') raise PermissionDenied('You do not have permission to view the selected corporation statistics module.')
# get default model if none requested # get default model if none requested
@ -84,22 +91,31 @@ def corpstats_view(request, corp_id=None):
if corpstats: if corpstats:
context.update({ context.update({
'corpstats': corpstats.get_view_model(request.user), 'corpstats': corpstats.get_view_model(request.user),
'members': members, 'members': members,
}) })
return render(request, 'corputils/corpstats.html', context=context) return render(request, 'corputils/corpstats.html', context=context)
@login_required @login_required
@user_passes_test(access_corpstats_test) @user_passes_test(access_corpstats_test)
def corpstats_update(request, corp_id): def corpstats_update(request, corp_id):
corp = get_object_or_404(EveCorporationInfo, corporation_id=corp_id) corp = get_object_or_404(EveCorporationInfo, corporation_id=corp_id)
corpstats = get_object_or_404(CorpStats, corp=corp) corpstats = get_object_or_404(CorpStats, corp=corp)
if corpstats.can_update(request.user): if corpstats.can_update(request.user):
corpstats.update() try:
corpstats.update()
except HTTPError as e:
messages.error(request, str(e))
else: else:
raise PermissionDenied('You do not have permission to update member data for the selected corporation statistics module.') raise PermissionDenied(
return redirect('corputils:view_corp', corp_id=corp.corporation_id) 'You do not have permission to update member data for the selected corporation statistics module.')
if corpstats.pk:
return redirect('corputils:view_corp', corp_id=corp.corporation_id)
else:
return redirect('corputils:view')
@login_required @login_required
@user_passes_test(access_corpstats_test) @user_passes_test(access_corpstats_test)
@ -109,9 +125,11 @@ def corpstats_search(request):
if search_string: if search_string:
has_similar = CorpStats.objects.filter(_members__icontains=search_string).visible_to(request.user) has_similar = CorpStats.objects.filter(_members__icontains=search_string).visible_to(request.user)
for corpstats in has_similar: for corpstats in has_similar:
similar = [(member_id, corpstats.members[member_id]) for member_id in corpstats.members if search_string.lower() in corpstats.members[member_id].lower()] similar = [(member_id, corpstats.members[member_id]) for member_id in corpstats.members if
search_string.lower() in corpstats.members[member_id].lower()]
for s in similar: for s in similar:
results.append((corpstats, CorpStats.MemberObject(s[0], s[1], show_apis=corpstats.show_apis(request.user)))) results.append(
(corpstats, CorpStats.MemberObject(s[0], s[1], show_apis=corpstats.show_apis(request.user))))
page = request.GET.get('page', 1) page = request.GET.get('page', 1)
results = sorted(results, key=lambda x: x[1].character_name) results = sorted(results, key=lambda x: x[1].character_name)
results_page = get_page(results, page) results_page = get_page(results, page)

View File

@ -13,6 +13,7 @@ from eveonline.models import EveCorporationInfo
from eveonline.managers import EveManager from eveonline.managers import EveManager
from fleetactivitytracking.forms import FatlinkForm from fleetactivitytracking.forms import FatlinkForm
from fleetactivitytracking.models import Fatlink, Fat from fleetactivitytracking.models import Fatlink, Fat
from bravado.exception import HTTPError
from esi.decorators import token_required from esi.decorators import token_required
@ -28,14 +29,15 @@ logger = logging.getLogger(__name__)
FATS_PER_PAGE = int(getattr(settings, 'FATS_PER_PAGE', 20)) FATS_PER_PAGE = int(getattr(settings, 'FATS_PER_PAGE', 20))
def get_page(model_list, page_num): def get_page(model_list, page_num):
p = Paginator(model_list, FATS_PER_PAGE) p = Paginator(model_list, FATS_PER_PAGE)
try: try:
fats = p.page(page_num) fats = p.page(page_num)
except PageNotAnInteger: except PageNotAnInteger:
fatss = p.page(1) fats = p.page(1)
except EmptyPage: except EmptyPage:
fatss = p.page(p.num_pages) fats = p.page(p.num_pages)
return fats return fats
@ -45,7 +47,8 @@ class CorpStat(object):
self.corp = corp self.corp = corp
else: else:
self.corp = EveCorporationInfo.objects.get(corporation_id=corp_id) self.corp = EveCorporationInfo.objects.get(corporation_id=corp_id)
self.n_fats = Fat.objects.filter(character__corporation_id=self.corp.corporation_id).filter(fatlink__fatdatetime__gte=start_of_month).filter(fatlink__fatdatetime__lte=start_of_next_month).count() self.n_fats = Fat.objects.filter(character__corporation_id=self.corp.corporation_id).filter(
fatlink__fatdatetime__gte=start_of_month).filter(fatlink__fatdatetime__lte=start_of_next_month).count()
self.blue = self.corp.is_blue self.blue = self.corp.is_blue
def avg_fat(self): def avg_fat(self):
@ -96,7 +99,6 @@ def fatlink_statistics_view(request, year=datetime.date.today().year, month=date
fat_stats = {} fat_stats = {}
# get FAT stats for member corps # get FAT stats for member corps
for corp_id in settings.STR_CORP_IDS: for corp_id in settings.STR_CORP_IDS:
fat_stats[corp_id] = CorpStat(corp_id, start_of_month, start_of_next_month) fat_stats[corp_id] = CorpStat(corp_id, start_of_month, start_of_next_month)
@ -110,8 +112,9 @@ def fatlink_statistics_view(request, year=datetime.date.today().year, month=date
fatlink__fatdatetime__lt=start_of_next_month).exclude(character__corporation_id__in=fat_stats) fatlink__fatdatetime__lt=start_of_next_month).exclude(character__corporation_id__in=fat_stats)
for fat in fats_in_span: for fat in fats_in_span:
if not fat.character.corporation_id in fat_stats: if fat.character.corporation_id not in fat_stats:
fat_stats[fat.character.corporation_id] = CorpStat(fat.character.corporation_id, start_of_month, start_of_next_month) fat_stats[fat.character.corporation_id] = CorpStat(fat.character.corporation_id, start_of_month,
start_of_next_month)
# collect and sort stats # collect and sort stats
stat_list = [fat_stats[x] for x in fat_stats] stat_list = [fat_stats[x] for x in fat_stats]
@ -129,7 +132,7 @@ def fatlink_statistics_view(request, year=datetime.date.today().year, month=date
@login_required @login_required
def fatlink_personal_statistics_view(request, year=datetime.date.today().year, main_name=None): def fatlink_personal_statistics_view(request, year=datetime.date.today().year):
year = int(year) year = int(year)
logger.debug("Personal statistics view for year %i called by %s" % (year, request.user)) logger.debug("Personal statistics view for year %i called by %s" % (year, request.user))
@ -191,7 +194,8 @@ def fatlink_monthly_personal_statistics_view(request, year, month, char_id=None)
@login_required @login_required
@token_required(scopes=['esi-location.read_location.v1', 'esi-location.read_ship_type.v1', 'esi-universe.read_structures.v1']) @token_required(
scopes=['esi-location.read_location.v1', 'esi-location.read_ship_type.v1', 'esi-universe.read_structures.v1'])
def click_fatlink_view(request, token, hash, fatname): def click_fatlink_view(request, token, hash, fatname):
try: try:
fatlink = Fatlink.objects.filter(hash=hash)[0] fatlink = Fatlink.objects.filter(hash=hash)[0]
@ -205,14 +209,21 @@ def click_fatlink_view(request, token, hash, fatname):
c = token.get_esi_client() c = token.get_esi_client()
location = c.Location.get_characters_character_id_location(character_id=token.character_id).result() 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() 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()['solar_system_name'] location['solar_system_name'] = \
c.Universe.get_universe_systems_system_id(system_id=location['solar_system_id']).result()[
'solar_system_name']
if location['structure_id']: if location['structure_id']:
location['station_name'] = c.Universe.get_universe_structures_structure_id(structure_id=location['structure_id']).result()['name'] location['station_name'] = \
c.Universe.get_universe_structures_structure_id(structure_id=location['structure_id']).result()[
'name']
elif location['station_id']: elif location['station_id']:
location['station_name'] = c.Universe.get_universe_stations_station_id(station_id=location['station_id']).result()['station_name'] location['station_name'] = \
c.Universe.get_universe_stations_station_id(station_id=location['station_id']).result()[
'station_name']
else: else:
location['station_name'] = "No Station" location['station_name'] = "No Station"
ship['ship_type_name'] = c.Universe.get_universe_types_type_id(type_id=ship['ship_type_id']).result()['type_name'] ship['ship_type_name'] = c.Universe.get_universe_types_type_id(type_id=ship['ship_type_id']).result()[
'type_name']
fat = Fat() fat = Fat()
fat.system = location['solar_system_name'] fat.system = location['solar_system_name']
@ -238,6 +249,8 @@ def click_fatlink_view(request, token, hash, fatname):
messages.error(request, 'FAT link has expired.') messages.error(request, 'FAT link has expired.')
except (ObjectDoesNotExist, KeyError): except (ObjectDoesNotExist, KeyError):
messages.error(request, 'Invalid FAT link.') messages.error(request, 'Invalid FAT link.')
except HTTPError as e:
messages.error(request, str(e))
return redirect('auth_fatlink_view') return redirect('auth_fatlink_view')

View File

@ -16,6 +16,7 @@ from notifications import notify
from django.utils import timezone from django.utils import timezone
from authentication.decorators import members_and_blues from authentication.decorators import members_and_blues
from esi.clients import esi_client_factory from esi.clients import esi_client_factory
from bravado.exception import HTTPError
import uuid import uuid
import logging import logging
@ -255,8 +256,12 @@ def srp_request_view(request, fleet_srp):
messages.error(request, messages.error(request,
"Your SRP request Killmail link is invalid. Please make sure you are using zKillboard.") "Your SRP request Killmail link is invalid. Please make sure you are using zKillboard.")
return redirect("auth_srp_management_view") return redirect("auth_srp_management_view")
c = esi_client_factory() try:
srp_ship_name = c.Universe.get_universe_types_type_id(type_id=srp_kill_data).result()['type_name'] c = esi_client_factory()
srp_ship_name = c.Universe.get_universe_types_type_id(type_id=srp_kill_data).result()['type_name']
except HTTPError as e:
messages.error(request, str(e))
return redirect('auth_dashboard')
srp_request.srp_ship_name = srp_ship_name srp_request.srp_ship_name = srp_ship_name
kb_total_loss = ship_value kb_total_loss = ship_value
srp_request.kb_total_loss = kb_total_loss srp_request.kb_total_loss = kb_total_loss