def manage_character_api_timers(character): """ Adds and removes character APITimers for a given character depending on the character's key permissions. When we add more functions, we need to add them to the masks dictionary. """ key_mask = character.apikey.accessmask for sheet in CHARACTER_API_ACCESS_MASKS: mask = CHARACTER_API_ACCESS_MASKS[sheet] if ((mask & key_mask) == mask): # If we have permission, create timer if not already present try: APITimer.objects.get(character=character, apisheet=sheet) except APITimer.DoesNotExist: new_timer = APITimer(character=character, corporation=None, apisheet=sheet, nextupdate=pytz.utc.localize(datetime.datetime.utcnow())) new_timer.save() else: # If we are not permitted to do this, remove existent timers try: APITimer.objects.get(character=character, apisheet=sheet).delete except APITimer.DoesNotExist: pass
def api_character(request, api_id, api_verification_code): """ Validate key / ID combination. If it's valid, check security bitmask. """ # Try to authenticate with supplied key / ID pair and fetch api key meta data. try: # Fetch info api = eveapi.EVEAPIConnection() auth = api.auth(keyID=api_id, vCode=api_verification_code) key_info = auth.account.APIKeyInfo() except: # Message and redirect messages.error( request, """Verification of your API key failed. Please follow the instructions on the right half of this page to generate a valid one.""", ) return HttpResponseRedirect(reverse("manage_api_keys")) if key_info.key.type == "Character" or key_info.key.type == "Account": # Minimum access mask min_access_mask = 8 # attributes & implants implant = {} i_stats = {} attributes = ["memory", "intelligence", "perception", "willpower", "charisma"] # Do a simple bitwise operation to determine if we have sufficient rights with this key. if not ((min_access_mask & key_info.key.accessMask) == min_access_mask): # Message and redirect messages.error( request, """The API key you supplied does not have sufficient rights. Please follow the instructions on the right half of this page to generate a valid one.""", ) return HttpResponseRedirect(reverse("manage_api_keys")) # Get characters associated with this key characters = auth.account.Characters().characters # If form is submitted, add characters to account if request.method == "POST": post_characters = request.POST.getlist("characters") added_chars = False for char in characters: if str(char.characterID) in post_characters: # Add key to DB if it does not exist if not APIKey.objects.filter(keyid=api_id, vcode=api_verification_code): # Handle keys which never expire try: key_expiration = datetime.datetime.fromtimestamp(key_info.key.expires) except: key_expiration = "9999-12-31 00:00:00" key = APIKey( user=request.user, keyid=api_id, vcode=api_verification_code, expires=key_expiration, accessmask=key_info.key.accessMask, is_valid=True, is_character_key=True, ) key.save() else: key = APIKey.objects.get(user=request.user, keyid=api_id, vcode=api_verification_code) # If char already is assigned to another key, just move it try: char_to_move = Character.objects.get(id=char.characterID, user=request.user) char_to_move.apikey_id = key.id char_to_move.save() # Update timers manage_character_api_timers(char_to_move) added_chars = True except Character.DoesNotExist: # Add character me = auth.character(char.characterID) sheet = me.CharacterSheet() i_stats["name"] = "" i_stats["value"] = 0 for attr in attributes: implant[attr] = i_stats # have to check because if you don't have an implant in you get nothing back try: implant["memory"] = { "name": sheet.attributeEnhancers.memoryBonus.augmentatorName, "value": sheet.attributeEnhancers.memoryBonus.augmentatorValue, } except: pass try: implant["perception"] = { "name": sheet.attributeEnhancers.perceptionBonus.augmentatorName, "value": sheet.attributeEnhancers.perceptionBonus.augmentatorValue, } except: pass try: implant["intelligence"] = { "name": sheet.attributeEnhancers.intelligenceBonus.augmentatorName, "value": sheet.attributeEnhancers.intelligenceBonus.augmentatorValue, } except: pass try: implant["willpower"] = { "name": sheet.attributeEnhancers.willpowerBonus.augmentatorName, "value": sheet.attributeEnhancers.willpowerBonus.augmentatorValue, } except: pass try: implant["charisma"] = { "name": sheet.attributeEnhancers.charismaBonus.augmentatorName, "value": sheet.attributeEnhancers.charismaBonus.augmentatorValue, } except: pass try: a_name = sheet.allianceName a_id = sheet.allianceID except: a_name = "" a_id = 0 new_char = Character( id=char.characterID, name=char.name, user=request.user, apikey=key, corp_name=sheet.corporationName, corp_id=sheet.corporationID, alliance_name=a_name, alliance_id=a_id, dob=pytz.utc.localize(datetime.datetime.utcfromtimestamp(sheet.DoB)), race=sheet.race, bloodline=sheet.bloodLine, ancestry=sheet.ancestry, gender=sheet.gender, clone_name=sheet.cloneName, clone_skill_points=sheet.cloneSkillPoints, balance=sheet.balance, implant_memory_name=implant["memory"]["name"], implant_memory_bonus=implant["memory"]["value"], implant_perception_name=implant["perception"]["name"], implant_perception_bonus=implant["perception"]["value"], implant_intelligence_name=implant["intelligence"]["name"], implant_intelligence_bonus=implant["intelligence"]["value"], implant_willpower_name=implant["willpower"]["name"], implant_willpower_bonus=implant["willpower"]["value"], implant_charisma_name=implant["charisma"]["name"], implant_charisma_bonus=implant["charisma"]["value"], ) new_char.save() new_apitimer = APITimer( character=new_char, corporation=None, apisheet="CharacterSheet", nextupdate=pytz.utc.localize(datetime.datetime.utcfromtimestamp(sheet._meta.cachedUntil)), ) new_apitimer.save() # Update other timers manage_character_api_timers(new_char) for skill in sheet.skills: new_skill = CharSkill( character=new_char, skill_id=skill.typeID, skillpoints=skill.skillpoints, level=skill.level, ) new_skill.save() added_chars = True # Change message depending on what we did if added_chars: messages.success(request, "Successfully added the selected character(s) to your account.") else: messages.info(request, "No characters were added.") return HttpResponseRedirect(reverse("manage_characters")) else: # This must be a corporation key then # Add key to DB if it does not exist if not APIKey.objects.filter(keyid=api_id, vcode=api_verification_code): # Handle keys which never expire try: key_expiration = datetime.datetime.fromtimestamp(key_info.key.expires) except: key_expiration = "9999-12-31 00:00:00" key = APIKey( user=request.user, keyid=api_id, vcode=api_verification_code, expires=key_expiration, accessmask=key_info.key.accessMask, is_valid=True, is_character_key=False, ) key.save() messages.success(request, "Successfully added your corporate key.") else: messages.info(request, "No keys were added.") return HttpResponseRedirect(reverse("manage_api_keys")) rcontext = RequestContext(request, {"chars": characters}) return render_to_response("settings/api_character.haml", rcontext)
def api_character(request, api_id, api_verification_code): """ Validate key / ID combination. If it's valid, check security bitmask. """ # Try to authenticate with supplied key / ID pair and fetch api key meta data. try: # Fetch info api = eveapi.EVEAPIConnection() auth = api.auth(keyID=api_id, vCode=api_verification_code) key_info = auth.account.APIKeyInfo() except: # Message and redirect messages.error( request, """Verification of your API key failed. Please follow the instructions on the right half of this page to generate a valid one.""" ) return HttpResponseRedirect(reverse('manage_api_keys')) if key_info.key.type == "Character" or key_info.key.type == "Account": # Minimum access mask min_access_mask = 8 # attributes & implants implant = {} i_stats = {} attributes = [ 'memory', 'intelligence', 'perception', 'willpower', 'charisma' ] # Do a simple bitwise operation to determine if we have sufficient rights with this key. if not ((min_access_mask & key_info.key.accessMask) == min_access_mask): # Message and redirect messages.error( request, """The API key you supplied does not have sufficient rights. Please follow the instructions on the right half of this page to generate a valid one.""" ) return HttpResponseRedirect(reverse('manage_api_keys')) # Get characters associated with this key characters = auth.account.Characters().characters # If form is submitted, add characters to account if request.method == 'POST': post_characters = request.POST.getlist('characters') added_chars = False for char in characters: if str(char.characterID) in post_characters: # Add key to DB if it does not exist if not APIKey.objects.filter(keyid=api_id, vcode=api_verification_code): # Handle keys which never expire try: key_expiration = datetime.datetime.fromtimestamp( key_info.key.expires) except: key_expiration = "9999-12-31 00:00:00" key = APIKey(user=request.user, keyid=api_id, vcode=api_verification_code, expires=key_expiration, accessmask=key_info.key.accessMask, is_valid=True, is_character_key=True) key.save() else: key = APIKey.objects.get(user=request.user, keyid=api_id, vcode=api_verification_code) # If char already is assigned to another key, just move it try: char_to_move = Character.objects.get( id=char.characterID, user=request.user) char_to_move.apikey_id = key.id char_to_move.save() # Update timers manage_character_api_timers(char_to_move) added_chars = True except Character.DoesNotExist: # Add character me = auth.character(char.characterID) sheet = me.CharacterSheet() i_stats['name'] = "" i_stats['value'] = 0 for attr in attributes: implant[attr] = i_stats # have to check because if you don't have an implant in you get nothing back try: implant['memory'] = { 'name': sheet.attributeEnhancers.memoryBonus. augmentatorName, 'value': sheet.attributeEnhancers.memoryBonus. augmentatorValue } except: pass try: implant['perception'] = { 'name': sheet.attributeEnhancers.perceptionBonus. augmentatorName, 'value': sheet.attributeEnhancers.perceptionBonus. augmentatorValue } except: pass try: implant['intelligence'] = { 'name': sheet.attributeEnhancers.intelligenceBonus. augmentatorName, 'value': sheet.attributeEnhancers.intelligenceBonus. augmentatorValue } except: pass try: implant['willpower'] = { 'name': sheet.attributeEnhancers.willpowerBonus. augmentatorName, 'value': sheet.attributeEnhancers.willpowerBonus. augmentatorValue } except: pass try: implant['charisma'] = { 'name': sheet.attributeEnhancers.charismaBonus. augmentatorName, 'value': sheet.attributeEnhancers.charismaBonus. augmentatorValue } except: pass try: a_name = sheet.allianceName a_id = sheet.allianceID except: a_name = "" a_id = 0 new_char = Character( id=char.characterID, name=char.name, user=request.user, apikey=key, corp_name=sheet.corporationName, corp_id=sheet.corporationID, alliance_name=a_name, alliance_id=a_id, dob=pytz.utc.localize( datetime.datetime.utcfromtimestamp(sheet.DoB)), race=sheet.race, bloodline=sheet.bloodLine, ancestry=sheet.ancestry, gender=sheet.gender, clone_name=sheet.cloneName, clone_skill_points=sheet.cloneSkillPoints, balance=sheet.balance, implant_memory_name=implant['memory']['name'], implant_memory_bonus=implant['memory']['value'], implant_perception_name=implant['perception'] ['name'], implant_perception_bonus=implant['perception'] ['value'], implant_intelligence_name=implant['intelligence'] ['name'], implant_intelligence_bonus=implant['intelligence'] ['value'], implant_willpower_name=implant['willpower'] ['name'], implant_willpower_bonus=implant['willpower'] ['value'], implant_charisma_name=implant['charisma']['name'], implant_charisma_bonus=implant['charisma'] ['value']) new_char.save() new_apitimer = APITimer( character=new_char, corporation=None, apisheet="CharacterSheet", nextupdate=pytz.utc.localize( datetime.datetime.utcfromtimestamp( sheet._meta.cachedUntil))) new_apitimer.save() # Update other timers manage_character_api_timers(new_char) for skill in sheet.skills: new_skill = CharSkill( character=new_char, skill_id=skill.typeID, skillpoints=skill.skillpoints, level=skill.level) new_skill.save() added_chars = True # Change message depending on what we did if added_chars: messages.success( request, "Successfully added the selected character(s) to your account." ) else: messages.info(request, "No characters were added.") return HttpResponseRedirect(reverse('manage_characters')) else: # This must be a corporation key then # Add key to DB if it does not exist if not APIKey.objects.filter(keyid=api_id, vcode=api_verification_code): # Handle keys which never expire try: key_expiration = datetime.datetime.fromtimestamp( key_info.key.expires) except: key_expiration = "9999-12-31 00:00:00" key = APIKey(user=request.user, keyid=api_id, vcode=api_verification_code, expires=key_expiration, accessmask=key_info.key.accessMask, is_valid=True, is_character_key=False) key.save() messages.success(request, "Successfully added your corporate key.") else: messages.info(request, "No keys were added.") return HttpResponseRedirect(reverse('manage_api_keys')) rcontext = RequestContext(request, {'chars': characters}) return render_to_response('api_character.haml', rcontext)