Exemple #1
0
APP_ID = "frc4030:frcscout.com:v1"

TBA_URL = "http://www.thebluealliance.com/api/v2/events/2016?X-TBA-App-Id=" + APP_ID

tba_locations = None

try:
    tba_locations = requests.get(TBA_URL).json()
except ValueError:
    print("Unable to parse JSON, check your URL.")
    exit(-1)

for tba_location in tba_locations:
    already_existed = False
    try:
        scout_location = Location.objects.get(name=tba_location['name'])
        already_existed = True
    except Location.DoesNotExist:
        scout_location = Location(name=tba_location['name'])

    print(
        str(scout_location) + " -- already existed in our db: " +
        str(already_existed) + " added code " + tba_location['event_code'])

    scout_location.tba_event_code = tba_location['event_code']
    scout_location.venue_address = tba_location['venue_address']
    scout_location.location = tba_location['location']

    scout_location.save()
django.setup()

from frc_scout.models import Location

APP_ID = "frc4030:frcscout.com:v1"

TBA_URL = "http://www.thebluealliance.com/api/v2/events/2015?X-TBA-App-Id=" + APP_ID

try:
    tba_locations = requests.get(TBA_URL).json()
except ValueError:
    print("Unable to parse JSON, check your URL.")
    exit(-1)

for tba_location in tba_locations:
    already_existed = False
    try:
        scout_location = Location.objects.get(name=tba_location['name'])
        already_existed = True
    except Location.DoesNotExist:
        scout_location = Location(name=tba_location['name'])

    print(str(scout_location) + " -- already existed in our db: " + str(already_existed) + " added code " + tba_location['event_code'])

    scout_location.tba_event_code = tba_location['event_code']
    scout_location.venue_address = tba_location['venue_address']
    scout_location.location = tba_location['location']

    scout_location.save()
# NOTE: Only run this file once!
# This file will setup the database for initial use.
# Specifically, it will insert the default location names into the database
# so that people don't have to find them on their own every time.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "frc_scout_2015.settings")
import django
django.setup()

from frc_scout.models import Location
from frc_scout.views.loc_list import locations

if __name__ == "__main__":
    for loc_name in locations:
        new_loc = Location(name=loc_name)
        new_loc.save()
        print("Added location: " + loc_name)
def submit_match_scouting_data(request):
    if request.method != "POST":
        raise Http404
    else:
        data = str(request.POST.get('data'))
        print(data)
        matches = json.loads(data)
        
        eventsStorage = []
        
        errors = []

        for match in matches:

            if match:

                #
                # All variables that are NOT in separate database tables
                #

                if match['practice_scouting']:
                    try:
                        location = Location.objects.filter(name="TEST")[0]
                    except Location.DoesNotExist and IndexError:
                        Location(name="TEST").save()
                        location = Location.objects.filter(name="TEST")[0]

                else:
                    try:
                        location = Location.objects.get(id=request.session.get('location_id'))
                    except Location.DoesNotExist:
                        pass

                match_object = Match(scout=request.user,
                                     location=location,
                                     scout_name=request.user.first_name + " " + request.user.last_name[:1],
                                     scout_team_number=request.user.userprofile.team.team_number)

                # TELEOPERATED
                if match['events']:
                    events = match.pop('events')
                    
                    for x in range(len(events)):
                        event = events[x]
                        event_object = Event(ev_num=x,team_number=match['team_number'])
                        for event_attr in event:
                            setattr(event_object, event_attr, event.get(event_attr)) #todo: data validation. just in case.
                        eventsStorage.append(event_object)
                
                for attr in match:
                    setattr(match_object, attr, match.get(attr))
                print(str(match_object.__dict__))
                try:
                    match_object.save()
                    for event in eventsStorage:
                        event.match=match_object
                        event.save()
                except (IntegrityError, ValueError, DataError, decimal.InvalidOperation) as e:
                    try:
                        errors.append({
                            'team_number': match.get('prematch').get('team_number'),
                            'match_number': match.get('prematch').get('match_number'),
                            'error':str(e)
                            })
                    except AttributeError:
                        errors.append({
                            'team_number': "Unknown",
                            'match_number': "Unknown",
                            'error':str(e)
                        })
                

        if len(errors) != 0:
            return JsonResponse(errors, status=400, safe=False)
        else:
            return HttpResponse(status=200)