# Path: eveonline/managers.py
# class EVEManager:
# 
#     @staticmethod
#     def create_alliances(alliance_ids):
#         for id in alliance_ids:
#             alliance, created = EVEAlliance.objects.get_or_create(id = id)
#             if created:
#                 logger.info("Created model for alliance id %s" % id)
#             else:
#                 logger.warn("Attempting to create existing model for alliance id %s" % id)
#             EVEManager.update_alliances([id])
# 
#     @staticmethod
#     def update_alliances(ids):
#         api = evelink.eve.EVE()
#         result = api.alliances().result
#         for id in ids:
#             alliance = EVEManager.get_alliance_by_id(id)
#             if id in result:
#                 logger.debug("Alliance id %s found in alliance list, updating." % id)
#                 alliance.name = result[id]['name']
#                 alliance.ticker = result[id]['ticker']
#                 alliance.save()
#                 logger.info("Updated alliance info for %s" % alliance)
#             else:
#                 logger.info("Alliance %s no longer exists. Deleting model." % id)
#                 alliance.delete()
#         logger.debug("Finished updating alliance models %s" % ids)
# 
#     @staticmethod
#     def check_if_character_id_valid(id):
#         logger.debug("Checking if %s %s is valid character id" % (type(id), id))
#         id = int(id)
#         api = evelink.eve.EVE()
#         result = api.affiliations_for_characters(id).result[id]
#         if 'name' in result:
#             logger.debug("Determined character id %s is valid" % id)
#             return True
#         else:
#             logger.debug("Determined character id %s is invalid" % id)
#             return False
# 
#     @staticmethod
#     def check_if_corp_id_valid(id):
#         logger.debug("Checking if %s %s is valid corp id" % (type(id), id))
#         id = int(id)
#         a = evelink.api.API()
#         api = evelink.corp.Corp(a)
#         try:
#             result = api.corporation_sheet(corp_id=id).result
#             logger.debug("Determined corp id %s is valid" % id)
#             return True
#         except:
#             logger.debug("Determined corp id %s is invalid" % id)
#             return False
# 
#     @staticmethod
#     def check_if_alliance_id_valid(id):
#         logger.debug("Checking if %s %s is valid alliance id" % (type(id), id))
#         id = int(id)
#         api = evelink.eve.EVE()
#         result = api.alliances().result
#         if id in result:
#             logger.debug("Determind alliance id %s is valid" % id)
#             return True
#         else:
#             logger.debug("Determined alliance id %s is invalid" % id)
#             return False


from django import forms
from eveonline.managers import EVEManager

class CharacterAccessForm(forms.Form):
    id = forms.IntegerField(label="Character ID", required=True, min_value=1)

    def clean_id(self):
        if not EVEManager.check_if_character_id_valid(self.cleaned_data['id']):
            raise forms.ValidationError("Supplied character ID is invalid.")
        return self.cleaned_data['id']

class CorpAccessForm(forms.Form):
    id = forms.IntegerField(label="Corp ID", required=True, min_value=1)

    def clean_id(self):
        if not EVEManager.check_if_corp_id_valid(self.cleaned_data['id']):
            raise forms.ValidationError("Supplied corp ID is invalid.")
        return self.cleaned_data['id']

class AllianceAccessForm(forms.Form):
    id = forms.IntegerField(label="Alliance ID", required=True, min_value=1)

    def clean_id(self):

# Path: eveonline/managers.py
# class EVEManager:
# 
#     @staticmethod
#     def create_alliances(alliance_ids):
#         for id in alliance_ids:
#             alliance, created = EVEAlliance.objects.get_or_create(id = id)
#             if created:
#                 logger.info("Created model for alliance id %s" % id)
#             else:
#                 logger.warn("Attempting to create existing model for alliance id %s" % id)
#             EVEManager.update_alliances([id])
# 
#     @staticmethod
#     def update_alliances(ids):
#         api = evelink.eve.EVE()
#         result = api.alliances().result
#         for id in ids:
#             alliance = EVEManager.get_alliance_by_id(id)
#             if id in result:
#                 logger.debug("Alliance id %s found in alliance list, updating." % id)
#                 alliance.name = result[id]['name']
#                 alliance.ticker = result[id]['ticker']
#                 alliance.save()
#                 logger.info("Updated alliance info for %s" % alliance)
#             else:
#                 logger.info("Alliance %s no longer exists. Deleting model." % id)
#                 alliance.delete()
#         logger.debug("Finished updating alliance models %s" % ids)
# 
#     @staticmethod
#     def check_if_character_id_valid(id):
#         logger.debug("Checking if %s %s is valid character id" % (type(id), id))
#         id = int(id)
#         api = evelink.eve.EVE()
#         result = api.affiliations_for_characters(id).result[id]
#         if 'name' in result:
#             logger.debug("Determined character id %s is valid" % id)
#             return True
#         else:
#             logger.debug("Determined character id %s is invalid" % id)
#             return False
# 
#     @staticmethod
#     def check_if_corp_id_valid(id):
#         logger.debug("Checking if %s %s is valid corp id" % (type(id), id))
#         id = int(id)
#         a = evelink.api.API()
#         api = evelink.corp.Corp(a)
#         try:
#             result = api.corporation_sheet(corp_id=id).result
#             logger.debug("Determined corp id %s is valid" % id)
#             return True
#         except:
#             logger.debug("Determined corp id %s is invalid" % id)
#             return False
# 
#     @staticmethod
#     def check_if_alliance_id_valid(id):
#         logger.debug("Checking if %s %s is valid alliance id" % (type(id), id))
#         id = int(id)
#         api = evelink.eve.EVE()
#         result = api.alliances().result
#         if id in result:
#             logger.debug("Determind alliance id %s is valid" % id)
#             return True
#         else:
#             logger.debug("Determined alliance id %s is invalid" % id)
#             return False


# Path: access/forms.py
from django import forms
from eveonline.managers import EVEManager


class CharacterAccessForm(forms.Form):
    id = forms.IntegerField(label="Character ID", required=True, min_value=1)

    def clean_id(self):
        if not EVEManager.check_if_character_id_valid(self.cleaned_data['id']):
            raise forms.ValidationError("Supplied character ID is invalid.")
        return self.cleaned_data['id']

class CorpAccessForm(forms.Form):
    id = forms.IntegerField(label="Corp ID", required=True, min_value=1)

    def clean_id(self):
        if not EVEManager.check_if_corp_id_valid(self.cleaned_data['id']):
            raise forms.ValidationError("Supplied corp ID is invalid.")
        return self.cleaned_data['id']

class AllianceAccessForm(forms.Form):
    id = forms.IntegerField(label="Alliance ID", required=True, min_value=1)

    def clean_id(self):

        if not EVEManager.check_if_alliance_id_valid(self.cleaned_data['id']):