def get_alliance(allianceID): """ allianceID: int : Alliance object pulled from alliance list if needed """ try: alliance = Alliance.objects.get(allianceID=allianceID) except Alliance.DoesNotExist: api_conn = api.eveapi.EVEAPIConnection() alliancesApi = api_conn.eve.AllianceList() alliance = Alliance() alliance.allianceID = allianceID for a in alliancesApi.alliances: if a.allianceID == allianceID: alliance.shortName = a.shortName alliance.name = a.name LOG.info("Adding new Alliance: " + a.name) alliance.save() break return alliance
def get_alliance(allianceID): """ allianceID: int : Alliance object pulled from alliance list if needed """ try: alliance = Alliance.objects.get(allianceID = allianceID) except Alliance.DoesNotExist: api_conn = api.eveapi.EVEAPIConnection() alliancesApi = api_conn.eve.AllianceList() alliance = Alliance() alliance.allianceID = allianceID for a in alliancesApi.alliances: if a.allianceID == allianceID: alliance.shortName = a.shortName alliance.name = a.name LOG.info("Adding new Alliance: "+ a.name) alliance.save() break return alliance
def handle_contact(request): if request.method == 'POST': public_info = Corporation.objects.mine().get_public_info() try: corp_info = json.loads(request.body) ecm_url = corp_info['ecm_url'] corporationID = corp_info['corporationID'] corporationName = corp_info['corporationName'] ticker = corp_info['ticker'] public_key = corp_info['public_key'] key_fingerprint = corp_info['key_fingerprint'] try: alliance = Alliance.objects.get(allianceID = corp_info['alliance']) except Alliance.DoesNotExist: alliance = Alliance() alliance.allianceID = corp_info['alliance'] alliancesApi = api.connect().eve.AllianceList() for a in alliancesApi.alliances: if a.allianceID == corp_info['alliance']: alliance.shortName = a.shortName alliance.name = a.name alliance.save() break new_request = False if Corporation.objects.filter(corporationID=corporationID).exists(): corp = Corporation.objects.get(corporationID=corporationID) if not corp.key_fingerprint: # This corp was created by some internal task but we don't know their # public info yet. corp.corporationName = corporationName corp.ticker = ticker corp.alliance = alliance corp.ecm_url = ecm_url corp.public_key = public_key corp.key_fingerprint = key_fingerprint corp.is_trusted = False new_request = True else: if corp.key_fingerprint != key_fingerprint: # tentative of hack? return an error LOG.error(WRONG_FINGERPRINT_MSG % (corporationName, corporationID)) raise ValueError('wrong key_fingerprint') else: # create the corp in our db corp = Corporation(corporationID=corporationID, corporationName=corporationName, ticker=ticker, alliance=alliance, ecm_url=ecm_url, public_key=public_key, key_fingerprint=key_fingerprint, is_trusted=False, ) new_request = True if new_request: corp.save() # notify the admins that a new corp tried to contact us subject = ugettext('%s wants to exchange data with us') % corp.corporationName ctx_dict = { 'host_name': settings.EXTERNAL_HOST_NAME, 'use_https': settings.USE_HTTPS, 'corp': corp, } txt_content = render_to_string('ecm/corp/email/notify_contact.txt', ctx_dict, Ctx(request)) html_content = render_to_string('ecm/corp/email/notify_contact.html', ctx_dict, Ctx(request)) mail_admins(subject, txt_content, html_message=html_content) # if everything went well, return back our public info return HttpResponse(json.dumps(public_info)) except (ValueError, KeyError), e: # invalid field value return HttpResponseBadRequest(str(e))
def handle_contact(request): if request.method == 'POST': public_info = Corporation.objects.mine().get_public_info() try: corp_info = json.loads(request.body) ecm_url = corp_info['ecm_url'] corporationID = corp_info['corporationID'] corporationName = corp_info['corporationName'] ticker = corp_info['ticker'] public_key = corp_info['public_key'] key_fingerprint = corp_info['key_fingerprint'] try: alliance = Alliance.objects.get( allianceID=corp_info['alliance']) except Alliance.DoesNotExist: alliance = Alliance() alliance.allianceID = corp_info['alliance'] alliancesApi = api.connect().eve.AllianceList() for a in alliancesApi.alliances: if a.allianceID == corp_info['alliance']: alliance.shortName = a.shortName alliance.name = a.name alliance.save() break new_request = False if Corporation.objects.filter( corporationID=corporationID).exists(): corp = Corporation.objects.get(corporationID=corporationID) if not corp.key_fingerprint: # This corp was created by some internal task but we don't know their # public info yet. corp.corporationName = corporationName corp.ticker = ticker corp.alliance = alliance corp.ecm_url = ecm_url corp.public_key = public_key corp.key_fingerprint = key_fingerprint corp.is_trusted = False new_request = True else: if corp.key_fingerprint != key_fingerprint: # tentative of hack? return an error LOG.error(WRONG_FINGERPRINT_MSG % (corporationName, corporationID)) raise ValueError('wrong key_fingerprint') else: # create the corp in our db corp = Corporation( corporationID=corporationID, corporationName=corporationName, ticker=ticker, alliance=alliance, ecm_url=ecm_url, public_key=public_key, key_fingerprint=key_fingerprint, is_trusted=False, ) new_request = True if new_request: corp.save() # notify the admins that a new corp tried to contact us subject = ugettext( '%s wants to exchange data with us') % corp.corporationName ctx_dict = { 'host_name': settings.EXTERNAL_HOST_NAME, 'use_https': settings.USE_HTTPS, 'corp': corp, } txt_content = render_to_string( 'ecm/corp/email/notify_contact.txt', ctx_dict, Ctx(request)) html_content = render_to_string( 'ecm/corp/email/notify_contact.html', ctx_dict, Ctx(request)) mail_admins(subject, txt_content, html_message=html_content) # if everything went well, return back our public info return HttpResponse(json.dumps(public_info)) except (ValueError, KeyError), e: # invalid field value return HttpResponseBadRequest(str(e))
def update_corp_info(corpApi, currentTime): try: try: try: alliance = Alliance.objects.get(allianceID = corpApi.allianceID) except Alliance.DoesNotExist: LOG.info("Adding new Alliance: "+ corpApi.allianceName) alliance = Alliance() alliance.allianceID = corpApi.allianceID alliance.name = corpApi.allianceName alliancesApi = api.connect().eve.AllianceList() for a in alliancesApi.alliances: if a.allianceID == corpApi.allianceID: alliance.shortName = a.shortName alliance.save() break except api.Error: LOG.exception("Failed to fetch AllianceList.xml.aspx from EVE API server") corp = Corporation.objects.mine() alliance = None except: alliance = None description = fix_description(corpApi.description) # reset all other corps Corporation.objects.exclude(corporationID=corpApi.corporationID).update(is_my_corp=False) try: # try to retrieve the db stored corp info corp = Corporation.objects.get(corporationID=corpApi.corporationID) corp.is_my_corp = True corp.corporationID = corpApi.corporationID corp.corporationName = corpApi.corporationName corp.ticker = corpApi.ticker corp.ceoID = corpApi.ceoID corp.ceoName = corpApi.ceoName corp.stationID = corpApi.stationID corp.stationName = corpApi.stationName corp.alliance = alliance corp.description = description corp.taxRate = corpApi.taxRate corp.memberLimit = corpApi.memberLimit except Corporation.DoesNotExist: LOG.debug('First scan, creating corp...') # no corp parsed yet corp = Corporation(is_my_corp = True, corporationID = corpApi.corporationID, corporationName = corpApi.corporationName, ticker = corpApi.ticker, ceoID = corpApi.ceoID, ceoName = corpApi.ceoName, stationID = corpApi.stationID, stationName = corpApi.stationName, description = description, alliance = alliance, taxRate = corpApi.taxRate, memberLimit = corpApi.memberLimit ) if settings.USE_HTTPS: corp.ecm_url = 'https://' + settings.EXTERNAL_HOST_NAME else: corp.ecm_url = 'http://' + settings.EXTERNAL_HOST_NAME if not (corp.private_key and corp.public_key and corp.key_fingerprint): # as this is the first time, we must generate the RSA keypair of our own corp LOG.debug('Generating RSA key pair...') corp.private_key = crypto.generate_rsa_keypair() corp.public_key = crypto.extract_public_key(corp.private_key) corp.key_fingerprint = crypto.key_fingerprint(corp.public_key) LOG.info('Generated RSA key pair for corporation ID %d.' % corpApi.corporationID) corp.save() # we store the update time of the table UpdateDate.mark_updated(model=Corporation, date=currentTime) return corp