mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-22 14:19:28 +00:00
Support for Incarna v1.1, Customizable API Keys
- Support CAK keys and their spinoffs - Add support for the Employment History data available in CharacterInfo.xml.aspx - Add a Gargoyle flag to disable backend processing when needed - Clean up a few inconsistancies with how the models are handled
This commit is contained in:
@@ -5,6 +5,7 @@ import logging
|
||||
|
||||
from celery.decorators import task
|
||||
from celery.task.sets import TaskSet
|
||||
from gargoyle import gargoyle
|
||||
|
||||
from eve_proxy.exceptions import *
|
||||
from eve_proxy.models import CachedDocument
|
||||
@@ -27,18 +28,26 @@ def queue_apikey_updates(update_delay=86400, batch_size=50):
|
||||
"""
|
||||
|
||||
log = queue_apikey_updates.get_logger()
|
||||
|
||||
if gargoyle.is_active('api-disableprocessing'):
|
||||
log.info("Backend processing disabled, exiting")
|
||||
return
|
||||
|
||||
# Update all the eve accounts and related corps
|
||||
delta = timedelta(seconds=update_delay)
|
||||
log.info("Updating APIs older than %s" % (datetime.now() - delta))
|
||||
|
||||
accounts = EVEAccount.objects.filter(api_last_updated__lt=(datetime.now() - delta)).exclude(api_status=API_STATUS_ACC_EXPIRED).exclude(api_status=API_STATUS_AUTH_ERROR).order_by('api_last_updated')[:batch_size]
|
||||
if gargoyle.is_active('eve-cak'):
|
||||
accounts = EVEAccount.objects.filter(api_last_updated__lt=(datetime.now() - delta)).exclude(api_status=API_STATUS_ACC_EXPIRED).exclude(api_status=API_STATUS_AUTH_ERROR).order_by('api_last_updated')[:batch_size]
|
||||
else:
|
||||
accounts = EVEAccount.objects.filter(api_last_updated__lt=(datetime.now() - delta)).exclude(api_status=API_STATUS_ACC_EXPIRED).exclude(api_status=API_STATUS_AUTH_ERROR).exclude(api_keytype__gt=2).order_by('api_last_updated')[:batch_size]
|
||||
log.info("%s account(s) to update" % accounts.count())
|
||||
for acc in accounts:
|
||||
log.debug("Queueing UserID %s for update" % acc.api_user_id)
|
||||
log.debug("Queueing UserID %s for update" % acc.pk)
|
||||
if not acc.user:
|
||||
acc.delete()
|
||||
continue
|
||||
import_apikey.delay(api_key=acc.api_key, api_userid=acc.api_user_id)
|
||||
import_apikey.delay(api_key=acc.api_key, api_userid=acc.pk)
|
||||
|
||||
|
||||
@task(ignore_result=True)
|
||||
@@ -76,78 +85,123 @@ def import_apikey_result(api_userid, api_key, user=None, force_cache=False, call
|
||||
def import_apikey_func(api_userid, api_key, user=None, force_cache=False, log=logging.getLogger(__name__)):
|
||||
log.info('Importing %s/%s' % (api_userid, api_key))
|
||||
|
||||
auth_params = {'userid': api_userid, 'apikey': api_key}
|
||||
account_doc = CachedDocument.objects.api_query('/account/Characters.xml.aspx', params=auth_params, no_cache=force_cache)
|
||||
try:
|
||||
account = EVEAccount.objects.get(pk=api_userid)
|
||||
except EVEAccount.DoesNotExist:
|
||||
account = None
|
||||
|
||||
doc = basic_xml_parse_doc(account_doc)['eveapi']
|
||||
# Use CAK if enabled and either a new key or flagged as so
|
||||
if (gargoyle.is_active('eve-cak') and (not account or account.is_cak)):
|
||||
auth_params = {'keyid': api_userid, 'vcode': api_key}
|
||||
keycheck = CachedDocument.objects.api_query('/account/APIKeyInfo.xml.aspx', params=auth_params, no_cache=True)
|
||||
doc = basic_xml_parse_doc(keycheck)['eveapi']
|
||||
|
||||
# Checks for a document error
|
||||
if 'error' in doc:
|
||||
try:
|
||||
account = EVEAccount.objects.get(api_user_id=api_userid)
|
||||
except EVEAccount.DoesNotExist:
|
||||
# If no Account exists in the DB, just ignore it
|
||||
return
|
||||
if not 'error' in doc:
|
||||
if not account:
|
||||
account, created = EVEAccount.objects.get_or_create(pk=api_userid)
|
||||
if user:
|
||||
account.user = User.objects.get(id=user)
|
||||
if not account.api_key == api_key:
|
||||
account.api_key = api_key
|
||||
|
||||
keydoc = doc['result']['key']
|
||||
if keydoc['type'] == 'Character':
|
||||
account.api_keytype = API_KEYTYPE_CHARACTER
|
||||
elif keydoc['type'] == 'Corporation':
|
||||
account.api_keytype = API_KEYTYPE_CORPORATION
|
||||
elif keydoc['type'] == 'Account':
|
||||
account.api_keytype = API_KEYTYPE_ACCOUNT
|
||||
account.api_accessmask = keydoc['accessMask']
|
||||
if not keydoc['expires'] == '':
|
||||
account.api_expiry = datetime.strptime(keydoc['expires'], '%Y-%m-%d %H:%M:%S')
|
||||
account.api_status = API_STATUS_OK
|
||||
|
||||
# Remove deleted or traded characters
|
||||
newcharlist = [int(char['characterID']) for char in doc['result']['key']['characters']]
|
||||
for char in account.characters.all().exclude(id__in=newcharlist):
|
||||
account.characters.remove(char)
|
||||
|
||||
# Schedule a task to update the characters
|
||||
if account.user:
|
||||
cb = update_user_access.subtask(kwargs={'user': account.user.id })
|
||||
else:
|
||||
cb = None
|
||||
import_eve_characters.delay(account.characters.all().values_list('id', flat=True), key_id=account.pk, callback=cb)
|
||||
|
||||
error = doc['error']['code']
|
||||
if int(error) >= 500:
|
||||
# API disabled, down or rejecting, return without changes
|
||||
return
|
||||
elif error in ['202', '203', '204', '205', '212']:
|
||||
account.api_status = API_STATUS_AUTH_ERROR
|
||||
elif error == '211':
|
||||
account.api_status = API_STATUS_ACC_EXPIRED
|
||||
else:
|
||||
account.api_status = API_STATUS_OTHER_ERROR
|
||||
account.api_last_updated = datetime.utcnow()
|
||||
account.save()
|
||||
if account.user:
|
||||
update_user_access.delay(account.user.id)
|
||||
return account
|
||||
# No account object, just return
|
||||
if not account:
|
||||
return
|
||||
|
||||
# Create or retrieve the account last to make sure everything
|
||||
# before here is good to go.
|
||||
account, created = EVEAccount.objects.get_or_create(api_user_id=api_userid)
|
||||
if not account.api_key == api_key:
|
||||
account.api_key = api_key
|
||||
account.api_keytype = API_KEYTYPE_UNKNOWN
|
||||
account.api_status = API_STATUS_OK
|
||||
if user and created:
|
||||
account.user = User.objects.get(id=user)
|
||||
error = doc['error']['code']
|
||||
if int(error) >= 500:
|
||||
# API disabled, down or rejecting, return without changes
|
||||
return
|
||||
elif error in ['202', '203', '204', '205', '212']:
|
||||
account.api_status = API_STATUS_AUTH_ERROR
|
||||
elif error == '211':
|
||||
account.api_status = API_STATUS_ACC_EXPIRED
|
||||
else:
|
||||
account.api_status = API_STATUS_OTHER_ERROR
|
||||
|
||||
if account.user:
|
||||
update_user_access.delay(account.user.id)
|
||||
|
||||
else:
|
||||
auth_params = {'userid': api_userid, 'apikey': api_key}
|
||||
account_doc = CachedDocument.objects.api_query('/account/Characters.xml.aspx', params=auth_params, no_cache=force_cache)
|
||||
doc = basic_xml_parse_doc(account_doc)['eveapi']
|
||||
|
||||
if not 'error' in doc:
|
||||
if not account:
|
||||
account, created = EVEAccount.objects.get_or_create(pk=api_userid)
|
||||
if user:
|
||||
account.user = User.objects.get(id=user)
|
||||
if not account.api_key == api_key:
|
||||
account.api_key = api_key
|
||||
account.api_status = API_STATUS_OK
|
||||
|
||||
keycheck = CachedDocument.objects.api_query('/account/AccountStatus.xml.aspx', params=auth_params, no_cache=True)
|
||||
keydoc = basic_xml_parse_doc(keycheck)['eveapi']
|
||||
if 'error' in keydoc:
|
||||
account.api_keytype = API_KEYTYPE_LIMITED
|
||||
elif not 'error' in keydoc:
|
||||
account.api_keytype = API_KEYTYPE_FULL
|
||||
else:
|
||||
account.api_keytype = API_KEYTYPE_UNKNOWN
|
||||
|
||||
# Remove deleted or traded characters
|
||||
newcharlist = [int(char['characterID']) for char in doc['result']['characters']]
|
||||
for char in account.characters.all().exclude(id__in=newcharlist):
|
||||
account.characters.remove(char)
|
||||
|
||||
# Schedule a task to update the characters
|
||||
if account.user:
|
||||
cb = update_user_access.subtask(kwargs={'user': account.user.id })
|
||||
else:
|
||||
cb = None
|
||||
import_eve_characters.delay(account.characters.all().values_list('id', flat=True), key_id=account.pk, callback=cb)
|
||||
|
||||
else:
|
||||
# No account object, just return
|
||||
if not account:
|
||||
return
|
||||
|
||||
error = doc['error']['code']
|
||||
if int(error) >= 500:
|
||||
# API disabled, down or rejecting, return without changes
|
||||
return
|
||||
elif error in ['202', '203', '204', '205', '212']:
|
||||
account.api_status = API_STATUS_AUTH_ERROR
|
||||
elif error == '211':
|
||||
account.api_status = API_STATUS_ACC_EXPIRED
|
||||
else:
|
||||
account.api_status = API_STATUS_OTHER_ERROR
|
||||
|
||||
if account.user:
|
||||
update_user_access.delay(account.user.id)
|
||||
|
||||
account.api_last_updated = datetime.utcnow()
|
||||
account.save()
|
||||
|
||||
# Check API keytype if we have a character and a unknown key status
|
||||
if account.api_keytype == API_KEYTYPE_UNKNOWN:
|
||||
keycheck = CachedDocument.objects.api_query('/account/AccountStatus.xml.aspx', params=auth_params, no_cache=True)
|
||||
keydoc = basic_xml_parse_doc(keycheck)['eveapi']
|
||||
|
||||
if 'error' in keydoc:
|
||||
account.api_keytype = API_KEYTYPE_LIMITED
|
||||
elif not 'error' in keydoc:
|
||||
account.api_keytype = API_KEYTYPE_FULL
|
||||
else:
|
||||
account.api_keytype = API_KEYTYPE_UNKNOWN
|
||||
|
||||
account.api_last_updated = datetime.utcnow()
|
||||
account.save()
|
||||
|
||||
tasklist = []
|
||||
|
||||
# Process the account's character list
|
||||
charlist = set(account.characters.all().values_list('id', flat=True))
|
||||
newcharlist = [int(char['characterID']) for char in doc['result']['characters']]
|
||||
|
||||
log.info("[CHAR] Current %s, New: %s, Remove: %s" % (charlist, newcharlist, set(charlist - set(newcharlist))))
|
||||
|
||||
for char in account.characters.filter(id__in=set(charlist - set(newcharlist))):
|
||||
account.characters.remove(char)
|
||||
|
||||
if account.user:
|
||||
cb = update_user_access.subtask(kwargs={'user': account.user.id })
|
||||
else:
|
||||
cb = None
|
||||
import_eve_characters.delay(newcharlist, api_key, api_userid, callback=cb)
|
||||
|
||||
return account
|
||||
|
||||
|
||||
@@ -4,18 +4,19 @@ import logging
|
||||
|
||||
from celery.decorators import task
|
||||
from celery.task.sets import subtask
|
||||
from gargoyle import gargoyle
|
||||
|
||||
from eve_proxy.exceptions import *
|
||||
from eve_proxy.models import CachedDocument
|
||||
|
||||
from eve_api.api_exceptions import *
|
||||
from eve_api.models import EVEPlayerCorporation, EVEPlayerCharacter, EVEPlayerCharacterRole, EVEPlayerCharacterSkill, EVESkill, EVEAccount
|
||||
from eve_api.models import EVEPlayerCorporation, EVEPlayerCharacter, EVEPlayerCharacterRole, EVEPlayerCharacterSkill, EVESkill, EVEAccount, EVEPlayerCharacterEmploymentHistory
|
||||
from eve_api.app_defines import *
|
||||
from eve_api.utils import basic_xml_parse, basic_xml_parse_doc
|
||||
|
||||
|
||||
@task()
|
||||
def import_eve_character(character_id, api_key=None, user_id=None, callback=None, **kwargs):
|
||||
def import_eve_character(character_id, key_id=None, callback=None, **kwargs):
|
||||
"""
|
||||
Imports a character from the API, providing a API key will populate
|
||||
further details. Returns a single EVEPlayerCharacter object
|
||||
@@ -24,10 +25,10 @@ def import_eve_character(character_id, api_key=None, user_id=None, callback=None
|
||||
|
||||
log = import_eve_character.get_logger()
|
||||
try:
|
||||
pchar = import_eve_character_func(character_id, api_key, user_id, log)
|
||||
pchar = import_eve_character_func(character_id, key_id, log)
|
||||
except APIAccessException, exc:
|
||||
log.error('Error importing character - flagging for retry')
|
||||
import_eve_character.retry(args=[character_id, api_key, user_id, callback], exc=exc, kwargs=kwargs)
|
||||
import_eve_character.retry(args=[character_id, key_id, callback], exc=exc, kwargs=kwargs)
|
||||
|
||||
if not pchar:
|
||||
log.error('Error importing character %s' % character_id)
|
||||
@@ -39,7 +40,7 @@ def import_eve_character(character_id, api_key=None, user_id=None, callback=None
|
||||
|
||||
|
||||
@task()
|
||||
def import_eve_characters(character_list, api_key=None, user_id=None, callback=None, **kwargs):
|
||||
def import_eve_characters(character_list, key_id=None, callback=None, **kwargs):
|
||||
"""
|
||||
Imports characters from the API, providing a API key will populate
|
||||
further details. Returns a list of EVEPlayerCharacter objects
|
||||
@@ -48,17 +49,17 @@ def import_eve_characters(character_list, api_key=None, user_id=None, callback=N
|
||||
|
||||
log = import_eve_characters.get_logger()
|
||||
try:
|
||||
results = [import_eve_character_func(char, api_key, user_id, log) for char in character_list]
|
||||
results = [import_eve_character_func(char, key_id, log) for char in character_list]
|
||||
except APIAccessException, exc:
|
||||
log.error('Error importing characters - flagging for retry')
|
||||
import_eve_characters.retry(args=[character_list, api_key, user_id, callback], exc=exc, kwargs=kwargs)
|
||||
import_eve_characters.retry(args=[character_list, key_id, callback], exc=exc, kwargs=kwargs)
|
||||
if callback:
|
||||
subtask(callback).delay(characters=results)
|
||||
else:
|
||||
return results
|
||||
|
||||
|
||||
def import_eve_character_func(character_id, api_key=None, user_id=None, logger=logging.getLogger(__name__)):
|
||||
def import_eve_character_func(character_id, key_id=None, logger=logging.getLogger(__name__)):
|
||||
|
||||
try:
|
||||
char_doc = CachedDocument.objects.api_query('/eve/CharacterInfo.xml.aspx', params={'characterID': character_id}, no_cache=False)
|
||||
@@ -82,7 +83,7 @@ def import_eve_character_func(character_id, api_key=None, user_id=None, logger=l
|
||||
pchar.security_status = values['securityStatus']
|
||||
|
||||
# Set corporation and join date
|
||||
corp, created = EVEPlayerCorporation.objects.get_or_create(id=values['corporationID'])
|
||||
corp, created = EVEPlayerCorporation.objects.get_or_create(pk=values['corporationID'])
|
||||
from eve_api.tasks.corporation import import_corp_details
|
||||
if created or not corp.name or corp.api_last_updated < (datetime.utcnow() - timedelta(hours=12)):
|
||||
import_corp_details.delay(values['corporationID'])
|
||||
@@ -97,9 +98,34 @@ def import_eve_character_func(character_id, api_key=None, user_id=None, logger=l
|
||||
pchar.race = val
|
||||
break
|
||||
|
||||
# If we have a valid API key, import the full character sheet
|
||||
if api_key and user_id:
|
||||
auth_params = {'userID': user_id, 'apiKey': api_key, 'characterID': character_id }
|
||||
# Import employment history if its made available
|
||||
if 'employmentHistory' in values:
|
||||
reclist = pchar.employmenthistory.values_list('pk', flat=True)
|
||||
for emp in values['employmentHistory']:
|
||||
if not emp['recordID'] in reclist:
|
||||
corp, created = EVEPlayerCorporation.objects.get_or_create(pk=emp['corporationID'])
|
||||
if created:
|
||||
import_corp_details.delay(emp['corporationID'])
|
||||
eobj, created = EVEPlayerCharacterEmploymentHistory.objects.get_or_create(pk=emp['recordID'], corporation=corp, character=pchar, start_date=emp['startDate'])
|
||||
|
||||
# We've been passed a Key ID, try and work with it
|
||||
if key_id:
|
||||
try:
|
||||
acc = EVEAccount.objects.get(pk=key_id)
|
||||
except EVEAccount.DoesNotExist:
|
||||
acc = None
|
||||
else:
|
||||
acc = None
|
||||
|
||||
# Actual Key? Get further information
|
||||
if acc:
|
||||
if gargoyle.is_active('eve-cak') and acc.is_cak:
|
||||
if not acc.has_access(3):
|
||||
logger.error('Key %s does not have access to CharacterSheet' % acc.pk)
|
||||
raise APIAccessException
|
||||
auth_params = {'keyid': acc.api_user_id, 'vcode': acc.api_key, 'characterid': character_id }
|
||||
else:
|
||||
auth_params = {'userID': acc.api_user_id, 'apiKey': acc.api_key, 'characterID': character_id }
|
||||
try:
|
||||
char_doc = CachedDocument.objects.api_query('/char/CharacterSheet.xml.aspx', params=auth_params, no_cache=False)
|
||||
except DocumentRetrievalError, exc:
|
||||
@@ -121,7 +147,7 @@ def import_eve_character_func(character_id, api_key=None, user_id=None, logger=l
|
||||
# Process the character's skills
|
||||
pchar.total_sp = 0
|
||||
for skill in values.get('skills', None):
|
||||
skillobj, created = EVESkill.objects.get_or_create(id=skill['typeID'])
|
||||
skillobj, created = EVESkill.objects.get_or_create(pk=skill['typeID'])
|
||||
charskillobj, created = EVEPlayerCharacterSkill.objects.get_or_create(skill=skillobj, character=pchar)
|
||||
if created or not charskillobj.level == int(skill['level']) or not charskillobj.skillpoints == int(skill['skillpoints']):
|
||||
charskillobj.level = int(skill['level'])
|
||||
@@ -139,7 +165,7 @@ def import_eve_character_func(character_id, api_key=None, user_id=None, logger=l
|
||||
queuedoc = queuedoc['eveapi']['result']
|
||||
EVEPlayerCharacterSkill.objects.filter(character=pchar).update(in_training=0)
|
||||
if int(queuedoc['skillInTraining']):
|
||||
skillobj, created = EVESkill.objects.get_or_create(id=queuedoc['trainingTypeID'])
|
||||
skillobj, created = EVESkill.objects.get_or_create(pk=queuedoc['trainingTypeID'])
|
||||
charskillobj, created = EVEPlayerCharacterSkill.objects.get_or_create(skill=skillobj, character=pchar)
|
||||
charskillobj.in_training = queuedoc['trainingToLevel']
|
||||
charskillobj.save()
|
||||
@@ -157,19 +183,14 @@ def import_eve_character_func(character_id, api_key=None, user_id=None, logger=l
|
||||
else:
|
||||
pchar.gender = API_GENDER_FEMALE
|
||||
|
||||
|
||||
pchar.api_last_updated = datetime.utcnow()
|
||||
pchar.save()
|
||||
|
||||
try:
|
||||
acc = EVEAccount.objects.get(pk=user_id)
|
||||
if acc:
|
||||
if not pchar.id in acc.characters.all().values_list('id', flat=True):
|
||||
acc.characters.add(pchar)
|
||||
|
||||
if pchar.director and acc.api_keytype == API_KEYTYPE_FULL:
|
||||
if pchar.director and acc.api_keytype in [API_KEYTYPE_FULL, API_KEYTYPE_CORPORATION]:
|
||||
from eve_api.tasks.corporation import import_corp_members
|
||||
import_corp_members.delay(api_key=api_key, api_userid=user_id, character_id=pchar.id)
|
||||
except EVEAccount.DoesNotExist:
|
||||
pass
|
||||
import_corp_members.delay(key_id=acc.pk, character_id=pchar.id)
|
||||
|
||||
return pchar
|
||||
|
||||
@@ -4,10 +4,12 @@ from datetime import datetime, timedelta
|
||||
from xml.dom import minidom
|
||||
|
||||
from celery.decorators import task
|
||||
from gargoyle import gargoyle
|
||||
|
||||
from eve_proxy.models import CachedDocument
|
||||
from eve_proxy.exceptions import DocumentRetrievalError
|
||||
from eve_api.models import EVEPlayerCorporation, EVEPlayerCharacter, EVEPlayerAlliance
|
||||
from eve_api.models import EVEPlayerCorporation, EVEPlayerCharacter, EVEPlayerAlliance, EVEAccount
|
||||
from eve_api.app_defines import *
|
||||
from eve_api.utils import basic_xml_parse_doc
|
||||
from eve_api.tasks.character import import_eve_character
|
||||
from eve_api.api_exceptions import APIAccessException
|
||||
@@ -110,7 +112,7 @@ def link_ceo(corporation, character):
|
||||
|
||||
|
||||
@task(ignore_result=True)
|
||||
def import_corp_members(api_userid, api_key, character_id):
|
||||
def import_corp_members(key_id, character_id):
|
||||
"""
|
||||
This function pulls all corporation members from the EVE API using a director's
|
||||
API key. It'll add as much information as it can about the character.
|
||||
@@ -118,8 +120,19 @@ def import_corp_members(api_userid, api_key, character_id):
|
||||
|
||||
log = import_corp_members.get_logger()
|
||||
|
||||
try:
|
||||
acc = EVEAccount.objects.get(pk=key_id)
|
||||
except EVEAccount.DoesNotExist:
|
||||
return
|
||||
|
||||
# grab and decode /corp/MemberTracking.xml.aspx
|
||||
auth_params = {'userID': api_userid, 'apiKey': api_key, 'characterID': character_id }
|
||||
if gargoyle.is_active('eve-cak') and acc.api_keytype == API_KEYTYPE_CORPORATION:
|
||||
if not acc.has_access(11):
|
||||
log.error('Key does not have access to MemberTracking', extra={'data': {'key_id': key_id, 'character_id': character_id}})
|
||||
return
|
||||
auth_params = {'keyid': acc.api_user_id, 'vcode': acc.api_key, 'characterID': character_id }
|
||||
else:
|
||||
auth_params = {'userID': acc.api_user_id, 'apiKey': acc.api_key, 'characterID': character_id }
|
||||
char_doc = CachedDocument.objects.api_query('/corp/MemberTracking.xml.aspx',
|
||||
params=auth_params,
|
||||
no_cache=False,
|
||||
@@ -127,7 +140,7 @@ def import_corp_members(api_userid, api_key, character_id):
|
||||
|
||||
pdoc = basic_xml_parse_doc(char_doc)
|
||||
if not 'eveapi' in pdoc or not 'result' in pdoc['eveapi']:
|
||||
log.error('Invalid XML document / API Error recceived', extra={'data': {'xml': char_doc.body, 'api_userid': api_userid, 'api_key': api_key, 'character_id': character_id}})
|
||||
log.error('Invalid XML document / API Error recceived', extra={'data': {'xml': char_doc.body, 'key_id': key_id, 'character_id': character_id}})
|
||||
return
|
||||
|
||||
corp = EVEPlayerCharacter.objects.get(id=character_id).corporation
|
||||
|
||||
Reference in New Issue
Block a user