Echoesong / DestinyCharacterViewer

1 stars 1 forks source link

Grow #13

Open Echoesong opened 1 year ago

Echoesong commented 1 year ago
from django.http import HttpResponseRedirect
from django.conf import settings
import uuid
import requests
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
from django.views.generic.edit import DeleteView
from .models import *   

def gatheringdata(request):
    race1 = Race.objects.get(id=1)
    race2 = Race.objects.get(id=2)
    race3 = Race.objects.get(id=3)

    class1 = Class.objects.get(id=1)
    class2 = Class.objects.get(id=2)
    class3 = Class.objects.get(id=3)

    user_profile = Profile.objects.get(user=request.user)
    destiny2_membership_id = user_profile.destiny2_membership_id
    token = user_profile.access_token

    response3 = requests.get(f'https://www.bungie.net/Platform/Destiny2/3/Profile/{destiny2_membership_id}', headers={'x-api-key': settings.BUNGIE_API_KEY, 'Authorization': f'Bearer {token}'}, params={'components': 'characters'})

    parsed = response3.json()
    characters = parsed['Response']['characters']['data']
    character_id_list = characters.keys()

    existing_characters = Character.objects.filter(user=request.user)
    if existing_characters:
        existing_characters.delete()

    for key in character_id_list:
            character_data = characters[key]
            if character_data['raceType'] == 0:
              character_race = race1
            elif character_data['raceType'] == 1:
              character_race = race2
            elif character_data['raceType'] == 2:
              character_race = race3

            if character_data['classType'] == 0:
              character_class = class1
            elif character_data['classType'] == 1:
              character_class = class2
            elif character_data['classType'] == 2:
              character_class = class3
            Character.objects.create(
                user = request.user,
                light = character_data['light'],
                total_minutes = character_data['minutesPlayedTotal'],
                session_minutes = character_data['minutesPlayedThisSession'],
                last_played = character_data['dateLastPlayed'],
                emblem_icon = character_data['emblemPath'],
                emblem_background = character_data['emblemBackgroundPath'],
                race_type = character_race,
                class_type = character_class

            )  

    print('Request resolved')
    # Instead of redirecting to home, chain this request with the request to get destinyMembershipId
    return render(request, 'gatheringdata.html')

The above function is the main point in our app that we think could use some improvements. The purpose of the function is to instantiate Characters using data we get from an API call.

We think it could use some work because though we accomplish our ultimate goal with the function, it is done in a very messy/WET manner. Our current thoughts on how it would be improved would be rather than getting each race/class object and storing in variable, we should directly assign the Race object for the Character through Race.objects.get(id=character_data['raceType'] +1 ). The same syntax can be applied for the Class. This would reduce 6 database interactions into 2