Added ability to manually trigger api refresh

Ignored cache databases
This commit is contained in:
Adarnof 2016-02-07 11:40:11 +00:00
parent 43eeee503f
commit 2d2afa79eb
5 changed files with 106 additions and 76 deletions

View File

@ -37,6 +37,7 @@ urlpatterns = patterns('',
url(r'^add_api_key/', 'eveonline.views.add_api_key', name='auth_add_api_key'), url(r'^add_api_key/', 'eveonline.views.add_api_key', name='auth_add_api_key'),
url(r'^api_key_management/', 'eveonline.views.api_key_management_view', url(r'^api_key_management/', 'eveonline.views.api_key_management_view',
name='auth_api_key_management'), name='auth_api_key_management'),
url(r'^refresh_api_pair/([0-9]+)/$', 'eveonline.views.user_refresh_api', name='auth_user_refresh_api'),
url(r'^delete_api_pair/(\w+)/$', 'eveonline.views.api_key_removal', name='auth_api_key_removal'), url(r'^delete_api_pair/(\w+)/$', 'eveonline.views.api_key_removal', name='auth_api_key_removal'),
url(r'^characters/', 'eveonline.views.characters_view', name='auth_characters'), url(r'^characters/', 'eveonline.views.characters_view', name='auth_characters'),
url(r'^main_character_change/(\w+)/$', 'eveonline.views.main_character_change', url(r'^main_character_change/(\w+)/$', 'eveonline.views.main_character_change',

4
cache/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*
!README.md
!.gitignore

View File

@ -1,5 +1,6 @@
from django.conf import settings from django.conf import settings
from celery.task import periodic_task from celery.task import periodic_task
from celery.task import shared_task
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
@ -394,6 +395,86 @@ def run_discord_token_cleanup():
logger.debug("DiscordAuthToken failed validation. Deleting %s" % auth) logger.debug("DiscordAuthToken failed validation. Deleting %s" % auth)
auth.delete() auth.delete()
@shared_task
def refresh_api(api_key_pair):
logger.debug("Running update on api key %s" % api_key_pair.api_id)
user = api_key_pair.user
if EveApiManager.api_key_is_valid(api_key_pair.api_id, api_key_pair.api_key):
#check to ensure API key meets min spec
logger.info("Determined api key %s is still active." % api_key_pair.api_id)
still_valid = True
state = determine_membership_by_user(user)
if state == "BLUE":
if settings.BLUE_API_ACCOUNT:
type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key)
if type == None:
api_key_pair.error_count += 1
api_key_pair.save()
logger.info("API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count))
still_valid = None
elif type == False:
logger.info("Determined api key %s for blue user %s is no longer type account as requred." % (api_key_pair.api_id, user))
still_valid = False
full = EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key)
if full == None:
api_key_pair.error_count += 1
api_key_pair.save()
logger.info("API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count))
still_valid = None
elif full == False:
logger.info("Determined api key %s for blue user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user))
still_valid = False
elif state == "MEMBER":
if settings.MEMBER_API_ACCOUNT:
type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key)
if type == None:
api_key_pair.error_count += 1
api_key_pair.save()
logger.info("API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count))
still_valid = None
elif type == False:
logger.info("Determined api key %s for user %s is no longer type account as required." % (api_key_pair.api_id, user))
still_valid = False
full = EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key)
if full == None:
api_key_pair.error_count += 1
api_key_pair.save()
logger.info("API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count))
still_valid = None
elif full == False:
logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user))
still_valid = False
if still_valid == None:
if api_key_pair.error_count >= 3:
logger.info("API key %s has incurred 3 or more errors. Assuming invalid." % api_key_pair.api_id)
still_valid = False
if still_valid == False:
logger.debug("API key %s has failed validation; it and its characters will be deleted." % api_key_pair.api_id)
EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id)
EveManager.delete_api_key_pair(api_key_pair.api_id, user.id)
elif still_valid == True:
if api_key_pair.error_count != 0:
logger.info("Clearing error count for api %s as it passed validation" % api_key_pair.api_id)
api_key_pair.error_count = 0
api_key_pair.save()
logger.info("Determined api key %s still meets requirements." % api_key_pair.api_id)
# Update characters
characters = EveApiManager.get_characters_from_api(api_key_pair.api_id, api_key_pair.api_key)
EveManager.update_characters_from_list(characters)
new_character = False
for char in characters.result:
# Ensure we have a model for all characters on key
if not EveManager.check_if_character_exist(characters.result[char]['name']):
new_character = True
logger.debug("API key %s has a new character on the account: %s" % (api_key_pair.api_id, characters.result[char]['name']))
if new_character:
logger.debug("Creating new character %s from api key %s" % (characters.result[char]['name'], api_key_pair.api_id))
EveManager.create_characters_from_list(characters, user, api_key_pair.api_key)
else:
logger.debug("API key %s is no longer valid; it and its characters will be deleted." % api_key_pair.api_id)
EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id)
EveManager.delete_api_key_pair(api_key_pair.api_id, user.id)
# Run every 3 hours # Run every 3 hours
@periodic_task(run_every=crontab(minute=0, hour="*/3")) @periodic_task(run_every=crontab(minute=0, hour="*/3"))
def run_api_refresh(): def run_api_refresh():
@ -409,82 +490,7 @@ def run_api_refresh():
authserviceinfo, c = AuthServicesInfo.objects.get_or_create(user=user) authserviceinfo, c = AuthServicesInfo.objects.get_or_create(user=user)
logger.debug("User %s has api keys. Proceeding to refresh." % user) logger.debug("User %s has api keys. Proceeding to refresh." % user)
for api_key_pair in api_key_pairs: for api_key_pair in api_key_pairs:
logger.debug("Running update on api key %s" % api_key_pair.api_id) refresh_api(api_key_pair)
if EveApiManager.api_key_is_valid(api_key_pair.api_id, api_key_pair.api_key):
#check to ensure API key meets min spec
logger.info("Determined api key %s is still active." % api_key_pair.api_id)
still_valid = True
state = determine_membership_by_user(user)
if state == "BLUE":
if settings.BLUE_API_ACCOUNT:
type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key)
if type == None:
api_key_pair.error_count += 1
api_key_pair.save()
logger.info("API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count))
still_valid = None
elif type == False:
logger.info("Determined api key %s for blue user %s is no longer type account as requred." % (api_key_pair.api_id, user))
still_valid = False
full = EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key)
if full == None:
api_key_pair.error_count += 1
api_key_pair.save()
logger.info("API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count))
still_valid = None
elif full == False:
logger.info("Determined api key %s for blue user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user))
still_valid = False
elif state == "MEMBER":
if settings.MEMBER_API_ACCOUNT:
type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key)
if type == None:
api_key_pair.error_count += 1
api_key_pair.save()
logger.info("API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count))
still_valid = None
elif type == False:
logger.info("Determined api key %s for user %s is no longer type account as required." % (api_key_pair.api_id, user))
still_valid = False
full = EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key)
if full == None:
api_key_pair.error_count += 1
api_key_pair.save()
logger.info("API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count))
still_valid = None
elif full == False:
logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user))
still_valid = False
if still_valid == None:
if api_key_pair.error_count >= 3:
logger.info("API key %s has incurred 3 or more errors. Assuming invalid." % api_key_pair.api_id)
still_valid = False
if still_valid == False:
logger.debug("API key %s has failed validation; it and its characters will be deleted." % api_key_pair.api_id)
EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id)
EveManager.delete_api_key_pair(api_key_pair.api_id, user.id)
elif still_valid == True:
if api_key_pair.error_count != 0:
logger.info("Clearing error count for api %s as it passed validation" % api_key_pair.api_id)
api_key_pair.error_count = 0
api_key_pair.save()
logger.info("Determined api key %s still meets requirements." % api_key_pair.api_id)
# Update characters
characters = EveApiManager.get_characters_from_api(api_key_pair.api_id, api_key_pair.api_key)
EveManager.update_characters_from_list(characters)
new_character = False
for char in characters.result:
# Ensure we have a model for all characters on key
if not EveManager.check_if_character_exist(characters.result[char]['name']):
new_character = True
logger.debug("API key %s has a new character on the account: %s" % (api_key_pair.api_id, characters.result[char]['name']))
if new_character:
logger.debug("Creating new character %s from api key %s" % (characters.result[char]['name'], api_key_pair.api_id))
EveManager.create_characters_from_list(characters, user, api_key_pair.api_key)
else:
logger.debug("API key %s is no longer valid; it and its characters will be deleted." % api_key_pair.api_id)
EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id)
EveManager.delete_api_key_pair(api_key_pair.api_id, user.id)
# Check our main character # Check our main character
if EveCharacter.objects.filter(character_id=authserviceinfo.main_char_id).exists() is False: if EveCharacter.objects.filter(character_id=authserviceinfo.main_char_id).exists() is False:
logger.info("User %s main character id %s missing model. Clearning main character." % (user, authserviceinfo.main_char_id)) logger.info("User %s main character id %s missing model. Clearning main character." % (user, authserviceinfo.main_char_id))

View File

@ -22,6 +22,7 @@ from eveonline.models import EveApiKeyPair
from authentication.models import AuthServicesInfo from authentication.models import AuthServicesInfo
from celerytask.tasks import determine_membership_by_user from celerytask.tasks import determine_membership_by_user
from celerytask.tasks import set_state from celerytask.tasks import set_state
from celerytask.tasks import refresh_api
import logging import logging
@ -160,3 +161,17 @@ def corp_stats_view(request):
else: else:
logger.error("Unable to locate user %s main character model with id %s. Cannot provide corp stats." % (request.user, auth_info.main_char_id)) logger.error("Unable to locate user %s main character model with id %s. Cannot provide corp stats." % (request.user, auth_info.main_char_id))
return render_to_response('registered/corpstats.html', None, context_instance=RequestContext(request)) return render_to_response('registered/corpstats.html', None, context_instance=RequestContext(request))
@login_required
def user_refresh_api(request, api_id)
logger.debug("user_refresh_api called by user %s for api id %s" % (request.user, api_id))
if EveApiKeyPair.objects.filter(api_id=api_id).exists():
api_key_pair = EveApiKeyPair.objects.get(api_id=api_id)
if api_key_pair.user == request.user:
refresh_api(api_key_pair)
set_state(request.user)
else:
logger.warn("User %s not authorized to refresh api id %s" % (request.user, api_id))
else:
logger.warn("User %s unable to refresh api id %s - api key not found" % (request.user, api_id))
return HttpResponseRedirect("/api_key_management/")

View File

@ -32,6 +32,10 @@
<td class="text-center">{{ pair.api_id }}</td> <td class="text-center">{{ pair.api_id }}</td>
<td class="text-center">{{ pair.api_key }}</td> <td class="text-center">{{ pair.api_key }}</td>
<td class="text-center"> <td class="text-center">
<a href="/refresh_api_pair/{{ pair.api_id }}">
<button type="button" class="btn btn-success"><span
class="glyphicon glyphicon-refresh"></span></button>
</a>
<a href="/delete_api_pair/{{ pair.api_id }}"> <a href="/delete_api_pair/{{ pair.api_id }}">
<button type="button" class="btn btn-danger"><span <button type="button" class="btn btn-danger"><span
class="glyphicon glyphicon-remove"></span></button> class="glyphicon glyphicon-remove"></span></button>