from datetime import datetime
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 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, default_retry_delay=10 * 60)
def import_alliance_details():
    """
    Imports all in-game alliances and links their related corporations 

    """

    try:
        doc = CachedDocument.objects.api_query('/eve/AllianceList.xml.aspx')
    except DocumentRetrievalError, exc:
        import_alliance_details.retry(exc=exc)
        return

    parsedoc = basic_xml_parse_doc(doc)

    if 'eveapi' in parsedoc and not 'error' in parsedoc['eveapi']:
        for alliance in parsedoc['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)
    else:
        # We got a error, retry
        import_alliance_details.retry()

@@ -1257,16 +1257,17 @@
 tDate'%5D,
+ 
 %22%25Y-%25m-%25
@@ -1549,126 +1549,45 @@
     
-corplist = allobj.eveplayercorporation_set.all().values_list('id', flat=True)%0A%0A            validcorps = %5B%5D%0A           
+members = %5Bint(corp%5B'corporationID'%5D)
  for
@@ -1625,17 +1625,16 @@
 ations'%5D
-:
 %0A       
@@ -1642,244 +1642,82 @@
     
-    if int(corp%5B'corporationID'%5D) not in corplist:%0A                    import_corp_details.delay(corp%5B'corporationID'%5D)%0A                validcorps.append(int(corp%5B'corporationID'%5D))%0A%0A            delcorps = set(corplist) - set(validcorps
+EVEPlayerCorporation.objects.filter(id__in=members).update(alliance=allobj
 )%0A  
@@ -1766,22 +1766,46 @@
 ter(
+alliance=allobj).exclude(
 id__in=
-delcorp
+member
 s).u
@@ -1824,16 +1824,94 @@
 ce=None)
+%0A%0A            for id in members:%0A                import_corp_details.delay(id)
 %0A    els

Refactor the alliance update function, try to avoid corps being left out without their alliance