Exemplo n.º 1
0
def race_upload(request):
    errmsg = []
    if request.method == 'POST':
        errmsg += ['beginning import']
        
        import re
        import os
        
        from bios.models import Country, Athlete, Crew
        file = request.FILES['csv']
        uploadfile=file.name; 
        
        file_contents = request.FILES['csv'].read().strip()
        
        p = re.compile('\r', re.IGNORECASE)
        file_contents = p.sub('\n',file_contents)
        #file_contents = self.request.get('lif').strip()
        import csv
        imported = []
        importReader = csv.reader(file_contents.split('\n'))

        for row in importReader:
            imported += [row]
        #validate data structure
        
        ci = 0
        selectedEvent = None
        selectedRace = None
        errmsg += ['in the loop']
        for r in imported:
            try:
                if selectedEvent is not None and selectedEvent.eventString != r[0] or selectedEvent is None:
                    #does this event already exist?
                    evtest = Event.all().filter("eventString =",r[0]).fetch(1)
                    #print evtest
                    if len(evtest) > 0:
                        selectedEvent = evtest[0]
                        #print "re-using event"
                    else:
                        #print "making new event"
                        eventInfo = [x.strip() for x in r[0].split('-')]
                        event = Event()
                        event.eventClass=eventInfo[0]
                        event.gender=eventInfo[1]
                        event.distance=eventInfo[2]
                        event.eventString=r[0]
                        event.put()
                        selectedEvent = event
                    
                if selectedRace is not None and (selectedRace.event != selectedEvent or selectedRace.heatNumber != r[1]) or selectedRace is None:         
                    #print "making new race"
                    #check and re-use old races.
                    ractest = Race.all().filter("event =",selectedEvent).filter("heatNumber =",r[1]).fetch(1)
                    if len(ractest) > 0:
                        selectedRace = ractest[0]
                    else:
                    
                        race = Race()
                        race.event = selectedEvent
                        race.heatNumber = r[1]
                        race.hasResults = False
                        race.put()
                        
                        selectedRace = race
                
                
                #athelte or crew?
                athcrew = None
                if len(r) > 6 and r[6] != '':
                        if len(r) >8 and r[7] != '':
                            """
                                it's a 4 man crew
                            """
                            fourteam = Athlete.all().filter("bibNum IN",[int(r[5]),int(r[6]),int(r[7]),int(r[8])]).fetch(4)
                            
                            crew = Crew()
                            crew.athletes = fourteam
                            crew.put()
                            athcrew = crew
                        else:
                            """
                                it's a crew@@
                            """
                            #crew = Crew()
                            
                            twoteam = Athlete.all().filter("bibNum IN",[int(r[5]),int(r[6])]).fetch(2)
                            crew = Crew()
                            crew.athletes = twoteam
                            crew.put()                      
                            athcrew = crew
                else:
                    selath = Athlete.all().filter("bibNum =", int(r[5])).fetch(1)
                    athcrew = selath[0] if len(selath)>0 else None

                
                result = Results()
                #print athcrew.__class__.__name__

                if athcrew.__class__.__name__ is "Crew":
                    #print "its crew"
                    result.crew = athcrew
                elif athcrew.__class__.__name__ is "Athlete":
                    #print "its athlete"
                    result.athlete = athcrew
                
                
                #result.athlete = selath[0] if len(selath)>0 else None
                result.laneNumber = r[2]
                selc = Country.all().filter("code =", r[3]).fetch(1)
                result.country = selc[0] if len(selc)>0 else None
                result.race = selectedRace
                result.put()
                
                
                ci = ci + 1
                #if ci > 100:
                #    return UA_direct(request, 'results/race-upload.html')
                #print result
                #print r
            except:
                    errmsg += ["Error on Line :%s of CSV, line looks like: %s" % (ci,r)]
                    
            errmsg += ["%s rows imported<br/>" % ci]
    
    return render_to_response(request, 'results/race-upload.html', extra_context={"errmsg":errmsg})
Exemplo n.º 2
0
def bio_delete_crew(request):
    list = Crew.all(keys_only=True).fetch(250)
    db.delete(list)
    return UA_direct(request, 'homepage.html')
Exemplo n.º 3
0
def show_crews(request):
	return UA_object_list(request,Crew.all())
Exemplo n.º 4
0
def show_crew(request, key):
    crew = Crew.get(key)
    return UA_object_list(request,Athlete.gql("WHERE crew = :1", crew), extra_context={'crew':crew})
Exemplo n.º 5
0
def show_athletes_country_crew(request, country, crewNum):
    selectedCountry = Country.gql("WHERE code = :cc", cc=country).get()
    return UA_object_detail(request,Crew.all(),slug_field="crewNum",slug=long(crewNum), template_name="mobile-bioCrew.html", template_object_name = "crew", extra_context={'country':selectedCountry})