mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 06:42:16 +00:00
Reorganise the file structure into a project tree
This commit is contained in:
5
app/eve_api/tasks/__init__.py
Normal file
5
app/eve_api/tasks/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from account import *
|
||||
from alliance import *
|
||||
from character import *
|
||||
from corporation import *
|
||||
from static import *
|
||||
145
app/eve_api/tasks/account.py
Normal file
145
app/eve_api/tasks/account.py
Normal file
@@ -0,0 +1,145 @@
|
||||
from datetime import datetime, timedelta
|
||||
from xml.dom import minidom
|
||||
import logging
|
||||
|
||||
from celery.decorators import task
|
||||
from celery.task.sets import TaskSet
|
||||
|
||||
from eve_proxy.models import CachedDocument
|
||||
|
||||
from eve_api.models import EVEAccount, EVEPlayerCharacter
|
||||
from eve_api.app_defines import *
|
||||
from eve_api.api_exceptions import *
|
||||
from eve_api.utils import basic_xml_parse_doc
|
||||
from eve_api.tasks.character import import_eve_characters
|
||||
from eve_api.tasks.corporation import import_corp_members, import_corp_details
|
||||
|
||||
from sso.tasks import update_user_access
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
@task(ignore_result=True, expires=120)
|
||||
def queue_apikey_updates(update_delay=86400, batch_size=50):
|
||||
"""
|
||||
Updates all Eve API elements in the database
|
||||
"""
|
||||
|
||||
log = queue_apikey_updates.get_logger()
|
||||
# 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]
|
||||
log.info("%s account(s) to update" % accounts.count())
|
||||
for acc in accounts:
|
||||
log.debug("Queueing UserID %s for update" % acc.api_user_id)
|
||||
if not acc.user:
|
||||
acc.delete()
|
||||
continue
|
||||
import_apikey.delay(api_key=acc.api_key, api_userid=acc.api_user_id)
|
||||
|
||||
|
||||
@task(ignore_result=True)
|
||||
def import_apikey(api_userid, api_key, user=None, force_cache=False):
|
||||
"""
|
||||
Imports a EVE Account from the API, doesn't return a result
|
||||
"""
|
||||
log = import_apikey.get_logger()
|
||||
try:
|
||||
import_apikey_func(api_userid, api_key, user, force_cache, log)
|
||||
except:
|
||||
log.error('Error importing API Key')
|
||||
|
||||
@task()
|
||||
def import_apikey_result(api_userid, api_key, user=None, force_cache=False, callback=None):
|
||||
"""
|
||||
Imports a EVE Account from the API and returns the account object when completed
|
||||
"""
|
||||
|
||||
log = import_apikey_result.get_logger()
|
||||
try:
|
||||
results = import_apikey_func(api_userid, api_key, user, force_cache, log)
|
||||
except APIAccessException, exc:
|
||||
log.error('Error importing API Key - flagging for retry')
|
||||
else:
|
||||
if callback:
|
||||
subtask(callback).delay(account=results)
|
||||
else:
|
||||
return results
|
||||
|
||||
|
||||
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)
|
||||
|
||||
doc = basic_xml_parse_doc(account_doc)['eveapi']
|
||||
|
||||
# Checks for a document error
|
||||
if 'error' in doc:
|
||||
try:
|
||||
account = EVEAccount.objects.get(id=api_userid)
|
||||
except EVEAccount.DoesNotExist:
|
||||
# If no Account exists in the DB, just ignore it
|
||||
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
|
||||
account.api_last_updated = datetime.utcnow()
|
||||
account.save()
|
||||
if account.user:
|
||||
update_user_access.delay(account.user.id)
|
||||
return account
|
||||
|
||||
# Create or retrieve the account last to make sure everything
|
||||
# before here is good to go.
|
||||
account, created = EVEAccount.objects.get_or_create(id=api_userid, api_user_id=api_userid, api_key=api_key)
|
||||
account.api_status = API_STATUS_OK
|
||||
if user and created:
|
||||
account.user = User.objects.get(id=user)
|
||||
|
||||
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
|
||||
42
app/eve_api/tasks/alliance.py
Normal file
42
app/eve_api/tasks/alliance.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from datetime import datetime
|
||||
from xml.dom import minidom
|
||||
|
||||
from celery.decorators import task
|
||||
|
||||
from eve_proxy.models import CachedDocument
|
||||
|
||||
from eve_api.models import EVEAccount, EVEPlayerCorporation, EVEPlayerAlliance
|
||||
from eve_api.utils import basic_xml_parse_doc
|
||||
from eve_api.tasks.corporation import import_corp_details, import_corp_details_result
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
@task(ignore_result=True)
|
||||
def import_alliance_details():
|
||||
"""
|
||||
Imports all in-game alliances and links their related corporations
|
||||
|
||||
"""
|
||||
|
||||
doc = CachedDocument.objects.api_query('/eve/AllianceList.xml.aspx')
|
||||
|
||||
for alliance in basic_xml_parse_doc(doc)['eveapi']['result']['alliances']:
|
||||
allobj, created = EVEPlayerAlliance.objects.get_or_create(pk=alliance['allianceID'])
|
||||
allobj.name = alliance['name']
|
||||
allobj.ticker = alliance['shortName']
|
||||
allobj.date_founded = datetime.strptime(alliance['startDate'],"%Y-%m-%d %H:%M:%S")
|
||||
allobj.executor, created = EVEPlayerCorporation.objects.get_or_create(id=alliance['executorCorpID'])
|
||||
allobj.member_count = alliance['memberCount']
|
||||
allobj.api_last_updated = datetime.utcnow()
|
||||
allobj.save()
|
||||
|
||||
corplist = allobj.eveplayercorporation_set.all().values_list('id', flat=True)
|
||||
|
||||
validcorps = []
|
||||
for corp in alliance['memberCorporations']:
|
||||
if int(corp['corporationID']) not in corplist:
|
||||
import_corp_details.delay(corp['corporationID'])
|
||||
validcorps.append(int(corp['corporationID']))
|
||||
|
||||
delcorps = set(corplist) - set(validcorps)
|
||||
EVEPlayerCorporation.objects.filter(id__in=delcorps).update(alliance=None)
|
||||
158
app/eve_api/tasks/character.py
Normal file
158
app/eve_api/tasks/character.py
Normal file
@@ -0,0 +1,158 @@
|
||||
from datetime import datetime, timedelta
|
||||
from xml.dom import minidom
|
||||
import logging
|
||||
|
||||
from celery.decorators import task
|
||||
from celery.task.sets import subtask
|
||||
|
||||
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.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):
|
||||
"""
|
||||
Imports a character from the API, providing a API key will populate
|
||||
further details. Returns a single EVEPlayerCharacter object
|
||||
|
||||
"""
|
||||
|
||||
log = import_eve_character.get_logger()
|
||||
try:
|
||||
pchar = import_eve_character_func(character_id, api_key, user_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)
|
||||
|
||||
if not pchar:
|
||||
log.error('Error importing character %s' % character_id)
|
||||
else:
|
||||
if callback:
|
||||
subtask(callback).delay(character=pchar.id)
|
||||
else:
|
||||
return pchar
|
||||
|
||||
|
||||
@task()
|
||||
def import_eve_characters(character_list, api_key=None, user_id=None, callback=None, **kwargs):
|
||||
"""
|
||||
Imports characters from the API, providing a API key will populate
|
||||
further details. Returns a list of EVEPlayerCharacter objects
|
||||
|
||||
"""
|
||||
|
||||
log = import_eve_characters.get_logger()
|
||||
try:
|
||||
results = [import_eve_character_func(char, api_key, user_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)
|
||||
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__)):
|
||||
|
||||
try:
|
||||
char_doc = CachedDocument.objects.api_query('/eve/CharacterInfo.xml.aspx', params={'characterID': character_id}, no_cache=False)
|
||||
except DocumentRetrievalError, exc:
|
||||
logger.error('Error retrieving CharacterInfo.xml.aspx for Character ID %s - %s' % (character_id, exc))
|
||||
raise APIAccessException
|
||||
|
||||
d = basic_xml_parse_doc(char_doc)['eveapi']
|
||||
if 'error' in d:
|
||||
logger.debug('EVE API Error enountered in API document')
|
||||
return
|
||||
|
||||
values = d['result']
|
||||
pchar, created = EVEPlayerCharacter.objects.get_or_create(id=character_id)
|
||||
if not values['characterName'] == {}:
|
||||
pchar.name = values['characterName']
|
||||
else:
|
||||
pchar.name = ""
|
||||
pchar.security_status = values['securityStatus']
|
||||
|
||||
corp, created = EVEPlayerCorporation.objects.get_or_create(id=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'])
|
||||
|
||||
pchar.corporation = corp
|
||||
pchar.corporation_date = values['corporationDate']
|
||||
|
||||
for v in API_RACES_CHOICES:
|
||||
val, race = v
|
||||
if race == values['race']:
|
||||
pchar.race = val
|
||||
break
|
||||
|
||||
if api_key and user_id:
|
||||
auth_params = {'userID': user_id, 'apiKey': 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:
|
||||
logger.error('Error retrieving CharacterSheet.xml.aspx for User ID %s, Character ID %s - %s' % (user_id, character_id, exc))
|
||||
raise APIAccessException
|
||||
|
||||
doc = basic_xml_parse_doc(char_doc)['eveapi']
|
||||
if not 'error' in doc:
|
||||
|
||||
values = doc['result']
|
||||
pchar.name = values['name']
|
||||
pchar.balance = values['balance']
|
||||
pchar.attrib_intelligence = values['attributes']['intelligence']
|
||||
pchar.attrib_charisma = values['attributes']['charisma']
|
||||
pchar.attrib_perception = values['attributes']['perception']
|
||||
pchar.attrib_willpower = values['attributes']['willpower']
|
||||
pchar.attrib_memory = values['attributes']['memory']
|
||||
|
||||
# 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'])
|
||||
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'])
|
||||
charskillobj.skillpoints = int(skill['skillpoints'])
|
||||
charskillobj.save()
|
||||
pchar.total_sp = pchar.total_sp + int(skill['skillpoints'])
|
||||
|
||||
# Process the character's roles
|
||||
pchar.director = False
|
||||
pchar.roles.clear()
|
||||
roles = values.get('corporationRoles', None)
|
||||
if roles and len(roles):
|
||||
for r in roles:
|
||||
role, created = EVEPlayerCharacterRole.objects.get_or_create(roleid=r['roleID'], name=r['roleName'])
|
||||
pchar.roles.add(role)
|
||||
if r['roleName'] == 'roleDirector':
|
||||
pchar.director = True
|
||||
|
||||
if values['gender'] == 'Male':
|
||||
pchar.gender = API_GENDER_MALE
|
||||
else:
|
||||
pchar.gender = API_GENDER_FEMALE
|
||||
|
||||
|
||||
pchar.api_last_updated = datetime.utcnow()
|
||||
pchar.save()
|
||||
|
||||
try:
|
||||
acc = EVEAccount.objects.get(api_user_id=user_id)
|
||||
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:
|
||||
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
|
||||
|
||||
return pchar
|
||||
125
app/eve_api/tasks/corporation.py
Normal file
125
app/eve_api/tasks/corporation.py
Normal file
@@ -0,0 +1,125 @@
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from xml.dom import minidom
|
||||
|
||||
from celery.decorators import task
|
||||
from eve_proxy.models import CachedDocument
|
||||
from eve_proxy.exceptions import DocumentRetrievalError
|
||||
from eve_api.models import EVEPlayerCorporation, EVEPlayerCharacter, EVEPlayerAlliance
|
||||
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
|
||||
|
||||
@task(ignore_result=True)
|
||||
def import_corp_details(corp_id, callback=None, **kwargs):
|
||||
log = import_corp_details.get_logger()
|
||||
try:
|
||||
corp = import_corp_details_func(corp_id, log)
|
||||
except APIAccessException, exc:
|
||||
log.error('Error importing corporation - queueing for retry')
|
||||
if not kwargs:
|
||||
kwargs = {}
|
||||
import_corp_details.retry(args=[corp_id, callback], exc=exc, kwargs=kwargs)
|
||||
else:
|
||||
if callback:
|
||||
subtask(callback).delay(corporation=corp.id)
|
||||
|
||||
|
||||
@task()
|
||||
def import_corp_details_result(corp_id, callback=None):
|
||||
log = import_corp_details_result.get_logger()
|
||||
try:
|
||||
corp = import_corp_details_func(corp_id, log)
|
||||
except APIAccessException, exc:
|
||||
log.error('Error importing corporation')
|
||||
else:
|
||||
if callback:
|
||||
subtask(callback).delay(corporation=corp.id)
|
||||
else:
|
||||
return corp
|
||||
|
||||
|
||||
def import_corp_details_func(corp_id, log=logging.getLogger(__name__)):
|
||||
|
||||
corpobj, created = EVEPlayerCorporation.objects.get_or_create(id=corp_id)
|
||||
if created or not corpobj.api_last_updated or corpobj.api_last_updated < (datetime.utcnow() - timedelta(hours=12)):
|
||||
|
||||
try:
|
||||
doc = CachedDocument.objects.api_query('/corp/CorporationSheet.xml.aspx', {'corporationID': corp_id})
|
||||
except DocumentRetrievalError, exc:
|
||||
log.error('Error retrieving CorporationSheet.xml.aspx for ID %s - %s' % (corp_id, exc))
|
||||
raise APIAccessException
|
||||
|
||||
d = basic_xml_parse_doc(doc)['eveapi']
|
||||
|
||||
if 'error' in d:
|
||||
log.error("Error importing Corp %s: %s" % (corp_id, d['error']))
|
||||
raise APIAccessException
|
||||
else:
|
||||
d = d['result']
|
||||
|
||||
tag_mappings = (
|
||||
('corporationName', 'name'),
|
||||
('ticker', 'ticker'),
|
||||
('url', 'url'),
|
||||
('description', 'description'),
|
||||
('memberCount', 'member_count'),
|
||||
)
|
||||
|
||||
for tag_map in tag_mappings:
|
||||
setattr(corpobj, tag_map[1], d[tag_map[0]])
|
||||
|
||||
logo_mappings = (
|
||||
('graphicID', 'logo_graphic_id'),
|
||||
('shape1', 'logo_shape1'),
|
||||
('shape2', 'logo_shape2'),
|
||||
('shape3', 'logo_shape3'),
|
||||
('color1', 'logo_color1'),
|
||||
('color2', 'logo_color2'),
|
||||
('color3', 'logo_color3'),
|
||||
)
|
||||
|
||||
for logo_map in logo_mappings:
|
||||
setattr(corpobj, logo_map[1], d['logo'][logo_map[0]])
|
||||
|
||||
if int(d['allianceID']):
|
||||
corpobj.alliance, created = EVEPlayerAlliance.objects.get_or_create(id=d['allianceID'])
|
||||
corpobj.api_last_updated = datetime.utcnow()
|
||||
corpobj.save()
|
||||
|
||||
import_eve_character.delay(d['ceoID'], callback=link_ceo.subtask(corporation=corpobj.id))
|
||||
|
||||
return EVEPlayerCorporation.objects.get(pk=corpobj.pk)
|
||||
|
||||
|
||||
@task(ignore_result=True)
|
||||
def link_ceo(corporation, character):
|
||||
""" Links a character to the CEO position of a corporation """
|
||||
corpobj = EVEPlayerCorporation.objects.filter(id=corporation).update(ceo_character=EVEPlayerCharacter.objects.get(id=character))
|
||||
|
||||
|
||||
@task(ignore_result=True)
|
||||
def import_corp_members(api_userid, api_key, 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.
|
||||
"""
|
||||
|
||||
# grab and decode /corp/MemberTracking.xml.aspx
|
||||
auth_params = {'userID': api_userid, 'apiKey': api_key, 'characterID': character_id }
|
||||
char_doc = CachedDocument.objects.api_query('/corp/MemberTracking.xml.aspx',
|
||||
params=auth_params,
|
||||
no_cache=False)
|
||||
|
||||
set = basic_xml_parse_doc(char_doc)['eveapi']['result']['members']
|
||||
corp = EVEPlayerCharacter.objects.get(id=character_id).corporation
|
||||
|
||||
charlist = []
|
||||
for character in set:
|
||||
charlist.append(int(character['characterID']))
|
||||
charobj = EVEPlayerCharacter.objects.filter(id=character['characterID'])
|
||||
charobj.update(corporation=corp, last_login=character['logonDateTime'], last_logoff=character['logoffDateTime'], current_location_id=int(character['locationID']), corporation_date=character['startDateTime'])
|
||||
|
||||
for char in EVEPlayerCharacter.objects.filter(corporation=corp).exclude(id__in=charlist):
|
||||
import_eve_character.delay(char.id)
|
||||
|
||||
30
app/eve_api/tasks/static.py
Normal file
30
app/eve_api/tasks/static.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from celery.decorators import task
|
||||
from eve_proxy.models import CachedDocument
|
||||
from eve_api.utils import basic_xml_parse_doc
|
||||
from eve_api.models import EVESkill, EVESkillGroup
|
||||
|
||||
@task()
|
||||
def import_eve_skills():
|
||||
"""
|
||||
Imports the skill tree and groups
|
||||
"""
|
||||
|
||||
char_doc = CachedDocument.objects.api_query('/eve/SkillTree.xml.aspx')
|
||||
d = basic_xml_parse_doc(char_doc)['eveapi']
|
||||
if 'error' in d:
|
||||
return
|
||||
values = d['result']
|
||||
|
||||
for group in values['skillGroups']:
|
||||
gobj, created = EVESkillGroup.objects.get_or_create(id=group['groupID'])
|
||||
if created:
|
||||
gobj.name = group['groupName']
|
||||
gobj.save()
|
||||
|
||||
for skill in group['skills']:
|
||||
skillobj, created = EVESkill.objects.get_or_create(id=skill['typeID'])
|
||||
if created or not skillobj.name or not skillobj.group:
|
||||
skillobj.name = skill['typeName']
|
||||
skillobj.group = gobj
|
||||
skillobj.save()
|
||||
|
||||
Reference in New Issue
Block a user