def test_calc_times(self):
        self.r.save()

        sess = Session()
        sess.name = "AM"
        sess.event = self.e
        sess.club = self.c
        sess.save()

        res = Result()
        res.reg = self.r
        res.session = sess
        res.club = self.c
        res.save()

        r = Run()
        r.base_time = 10.0
        r.result = res
        r.club = self.c
        r.save()

        res = Result()
        res.reg = self.r
        res.session = sess
        res.club = self.c
        res.save()

        r = Run()
        r.base_time = 10.0
        r.result = res
        r.club = self.c
        r.save()

        self.r.save()
        self.r.calc_times()

        self.assertEqual(self.r.total_raw_time, 20.0)
        self.assertEqual(self.r.total_index_time,
                         self.r.total_raw_time * self.race_class.pax)
    def test_calc_times(self):
        self.r.save()

        sess = Session()
        sess.name = "AM"
        sess.event = self.e
        sess.club = self.c
        sess.save()

        res = Result()
        res.reg = self.r
        res.session = sess
        res.club = self.c
        res.save()

        r = Run()
        r.base_time = 10.0
        r.result = res
        r.club = self.c
        r.save()

        res = Result()
        res.reg = self.r
        res.session = sess
        res.club = self.c
        res.save()

        r = Run()
        r.base_time = 10.0
        r.result = res
        r.club = self.c
        r.save()

        self.r.save()
        self.r.calc_times()

        self.assertEqual(self.r.total_raw_time, 20.0)
        self.assertEqual(self.r.total_index_time, self.r.total_raw_time * self.race_class.pax)
示例#3
0
 def test_no_dibs(self): 
     reg = Registration()
     up = UserProfile.objects.get(user=self.u1)
     reg.user_profile = up
     reg.number = 15
     reg.race_class = self.race_class
     reg.pax_class = None
     reg.event = self.e4
     reg.club = self.c
     reg.save()   
     
     s = Session()
     s.event = self.e4
     s.name = "AM"
     s.club = self.c
     s.save()
     
     result = Result()
     result.reg = reg
     result.session = s
     result.club = self.c
     result.save()
     
     for k in range(0, 3):
         r = Run()
         r.base_time = 10.0
         r.calc_time = 10.0
         r.index_time = 10.0
         r.result = result
         r.club = self.c
         r.save()        
     
     self.c.assign_dibs()
     self.assertEqual(len(Dibs.objects.all()), 0) 
                     
     reg.delete()
     s.delete()
示例#4
0
 def setUp(self): 
     
     self.today = datetime.date.today()
     
     self.c = Club()
     self.c.name = "test"
     self.c.events_for_dibs = 2
     self.c.save()
     
     self.race_class = RaceClass()
     self.race_class.name = "CSP"
     self.race_class.pax = .875
     self.race_class.club = self.c
     self.race_class.save()
     
     self.s = Season()
     self.s.year = 2011
     self.s.club = self.c
     self.s.save()
     
     self.e1 = Event()
     self.e1.name = "event1"
     self.e1.date = self.today-datetime.timedelta(days=10)
     self.e1.season = self.s
     self.e1.club = self.c
     self.e1.save()
     
     self.e2 = Event()
     self.e2.name = "event2"
     self.e2.date = self.today-datetime.timedelta(days=8)
     self.e2.season = self.s
     self.e2.club = self.c
     self.e2.save()
     
     self.e3 = Event()
     self.e3.name = "event3"
     self.e3.date = self.today-datetime.timedelta(days=5)
     self.e3.season = self.s
     self.e3.club = self.c
     self.e3.save()
     
     self.e4 = Event()
     self.e4.name = "event4"
     self.e4.date = self.today+datetime.timedelta(days=10)
     self.e4.season = self.s
     self.e4.club = self.c
     self.e4.save()
     
     self.events = [self.e1, self.e2, self.e3]       
     
     for event in self.events: 
         s = Session()
         s.event = event
         s.name = "AM"
         s.club = self.c
         s.save()
     
     self.u1 = User()
     self.u1.username = "******"
     self.u1.save()
     
     self.u2 = User()
     self.u2.username = "******"
     self.u2.save()
     
     self.u3 = User()
     self.u3.username = "******"
     self.u3.save()
     
     self.users = [self.u1, self.u2, self.u3]
     
     for i, user in enumerate(self.users): 
         up = user.get_profile()
         for j, event in enumerate(self.events): 
             if j<=i: 
                 r = Registration()
                 r.number = "%d"%(i+1)
                 r.race_class = self.race_class
                 r.pax_class = None
                 r.event = event    
                 r.user_profile = up
                 r.club = self.c
                 r.save()
                 
                 result = Result()
                 result.reg = r
                 result.session = event.sessions.all()[0]
                 result.club = self.c
                 result.save()
                 
                 for k in range(0, 3):
                     r = Run()
                     r.base_time = 10.0
                     r.calc_time = 10.0
                     r.index_time = 10.0
                     r.result = result
                     r.club = self.c
                     r.save()
示例#5
0
def parse_axtime(event, session, results_file):
    """Expects a Session object and a file object with 
    and iterates through to return either a list of result instances, 
    or a dictionary containing error messages"""

    validate_error = {}
    results = []
    club = session.club

    reader = csv.reader(results_file)
    header = reader.next()
    header_check = ["First", "Last", "Reg", "Class", "Num", "Vehicle", "Pax_Cls", "Color", "Notes", "Heat"]
    if not all([h in header for h in header_check]):
        validate_error["results_file"] = "your file was missing some columns"
        return validate_error

    time_keys = [k for k in header if "Run" in k]
    cone_keys = [k for k in header if "Pyl" in k]
    pen_keys = [k for k in header if "Pen" in k]

    if not all([time_keys, cone_keys, pen_keys]):
        validate_error["result_file"] = "your file was missing some run results columns"
        return validate_error

    keys = header_check + time_keys + cone_keys + pen_keys
    for line in reader:
        result = Result()
        result.session = session
        result.club = session.club

        data = dict(zip(header, line))
        reg_id = data["Notes"]

        # find the group class and raceclass:
        if data["Class"] == data["Pax_Cls"]:
            group = None
        else:
            try:
                group = RaceClass.objects.get(abrv=data["Class"], club=club)

            except RaceClass.DoesNotExist:
                validate_error["result_file"] = (
                    "Your Reults for %s %s included a unknown entry, %s in the Class column"
                    % (data["First"], data["Last"], data["Class"])
                )
                return validate_error

        try:
            race_class = RaceClass.objects.get(abrv=data["Pax_Cls"], club=club)
        except RaceClass.DoesNotExist:
            if not data["Pax_Cls"]:
                validate_error["results_file"] = "Your results for %s %s included an blank race class" % (
                    data["First"],
                    data["Last"],
                )
            else:
                validate_error["results_file"] = "Your results for %s %s included an unknown race class: %s" % (
                    data["First"],
                    data["Last"],
                    data["Pax_Cls"],
                )
            return validate_error

        car_data = data["Vehicle"].split()
        if len(car_data) == 3 and int_check.match(car_data[0]):
            data["Vehicle_year"] = car_data[0]
            data["Vehicle_make"] = car_data[1]
            data["Vehicle_model"] = car_data[2]

        elif len(car_data) == 2:
            data["Vehicle_year"] = None
            data["Vehicle_make"] = car_data[0]
            data["Vehicle_model"] = car_data[1]
        else:
            data["Vehicle_year"] = None
            data["Vehicle_make"] = None
            data["Vehicle_model"] = data["Vehicle"]

        # try REALLY hard to find the registration that maps to this driver
        try:
            user = User.objects.filter(
                username__icontains=data["Mem_Num"],
                first_name__icontains=data["First"],
                last_name__icontains=data["Last"],
            )[0]
        except IndexError:  # coudn't find one
            user = None

        if user:
            try:
                reg = Registration.objects.get(event=event, user_profile__user__username=user.username)
                if reg.race_class != race_class:
                    reg.race_class = race_class

            except Registration.DoesNotExist:
                reg = Registration()
                reg.user_profile = user.get_profile()
                reg.number = int(data["Num"])
                reg.race_class = race_class
                reg.pax_class = group
                reg._anon_car = data["Vehicle"]
                reg.event = event
                reg.club = club
            # not sure how this could happen at all...
            # except Registration.MultipleObjectsReturned:
            #    reg = session.query(model.Registration).join(model.Driver).join(model.Event).\
            #            filter(model.Event.id==c.event.id).filter(model.Driver.user_name==driver.user_name).all()
            #    for r in reg:
            #        session.delete(r)
            #
            #    reg = model.Registration()
            #    reg.driver = driver
            #    reg.number = int(data['Num'])
            #    reg.race_class = race_class
            #    reg.anon_car = unicode(data['Vehicle'])
            #    reg.event = c.event

        else:
            # try to find a previous anon_reg
            try:
                reg = Registration.objects.get(event=event, _anon_f_name=data["First"], _anon_l_name=data["Last"])

            except Registration.DoesNotExist:
                reg = Registration()
                reg.number = int(data["Num"])
                reg.race_class = race_class
                reg.pax_class = group

                reg._anon_f_name = data["First"]
                reg._anon_l_name = data["Last"]
                reg._anon_car = data["Vehicle"]
                reg.event = event
                reg.club = club
        try:
            reg.save()
        except:
            validate_error["results_file"] = "%s %s has a duplicate class/number: %d %s" % (
                reg.first_name,
                reg.last_name,
                reg.number,
                reg.race_class,
            )
            return validate_error
        try:
            reg.number = int(data["Num"])
        except Exception:
            validate_error["results_file"] = "Problem with car number for entry: %s %s" % (
                reg.first_name,
                reg.last_name,
            )
            return validate_error

        result.reg = reg
        result.save()

        for r_key, c_key, p_key in zip(time_keys, cone_keys, pen_keys):
            run = Run()
            run.result = result
            run.club = club
            try:
                if float(data[r_key]) <= 0.0:
                    continue
            except ValueError:
                continue

            run.base_time = float(data[r_key])
            run.cones = int(data[c_key])
            if data[p_key].strip():
                run.penalty = data[p_key]
            run.save()

        # note: not sure why this has to be done here
        try:
            reg.save()
        except Exception:
            validate_error["results_file"] = "%s %s has a duplicate class/number: %d %s" % (
                reg.first_name,
                reg.last_name,
                reg.number,
                reg.race_class,
            )
            return validate_error

        results.append(result)

    return results
示例#6
0
def parse_axtime(event, session, results_file):
    """Expects a Session object and a file object with 
    and iterates through to return either a list of result instances, 
    or a dictionary containing error messages"""

    validate_error = {}
    results = []
    club = session.club

    reader = csv.reader(results_file)
    header = reader.next()
    header_check = [
        'First', 'Last', 'Reg', 'Class', 'Num', 'Vehicle', 'Pax_Cls', 'Color',
        "Notes", "Heat"
    ]
    if not all([h in header for h in header_check]):
        validate_error['results_file'] = "your file was missing some columns"
        return validate_error

    time_keys = [k for k in header if 'Run' in k]
    cone_keys = [k for k in header if 'Pyl' in k]
    pen_keys = [k for k in header if 'Pen' in k]

    if not all([time_keys, cone_keys, pen_keys]):
        validate_error[
            'result_file'] = "your file was missing some run results columns"
        return validate_error

    keys = header_check + time_keys + cone_keys + pen_keys
    for line in reader:
        result = Result()
        result.session = session
        result.club = session.club

        data = dict(zip(header, line))
        reg_id = data['Notes']

        #find the group class and raceclass:
        if data['Class'] == data['Pax_Cls']:
            group = None
        else:
            try:
                group = RaceClass.objects.get(abrv=data['Class'], club=club)

            except RaceClass.DoesNotExist:
                validate_error[
                    'result_file'] = 'Your Reults for %s %s included a unknown entry, %s in the Class column' % (
                        data['First'], data['Last'], data['Class'])
                return validate_error

        try:
            race_class = RaceClass.objects.get(abrv=data['Pax_Cls'], club=club)
        except RaceClass.DoesNotExist:
            if not data['Pax_Cls']:
                validate_error[
                    'results_file'] = "Your results for %s %s included an blank race class" % (
                        data['First'], data['Last'])
            else:
                validate_error[
                    'results_file'] = "Your results for %s %s included an unknown race class: %s" % (
                        data['First'], data['Last'], data['Pax_Cls'])
            return validate_error

        car_data = data['Vehicle'].split()
        if len(car_data) == 3 and int_check.match(car_data[0]):
            data['Vehicle_year'] = car_data[0]
            data['Vehicle_make'] = car_data[1]
            data['Vehicle_model'] = car_data[2]

        elif len(car_data) == 2:
            data['Vehicle_year'] = None
            data['Vehicle_make'] = car_data[0]
            data['Vehicle_model'] = car_data[1]
        else:
            data['Vehicle_year'] = None
            data['Vehicle_make'] = None
            data['Vehicle_model'] = data['Vehicle']

        #try REALLY hard to find the registration that maps to this driver
        try:
            user = User.objects.filter(username__icontains=data['Mem_Num'],
                                       first_name__icontains=data['First'],
                                       last_name__icontains=data['Last'])[0]
        except IndexError:  #coudn't find one
            user = None

        if user:
            try:
                reg = Registration.objects.get(
                    event=event, user_profile__user__username=user.username)
                if reg.race_class != race_class:
                    reg.race_class = race_class

            except Registration.DoesNotExist:
                reg = Registration()
                reg.user_profile = user.get_profile()
                reg.number = int(data['Num'])
                reg.race_class = race_class
                reg.pax_class = group
                reg._anon_car = data['Vehicle']
                reg.event = event
                reg.club = club
            #not sure how this could happen at all...
            #except Registration.MultipleObjectsReturned:
            #    reg = session.query(model.Registration).join(model.Driver).join(model.Event).\
            #            filter(model.Event.id==c.event.id).filter(model.Driver.user_name==driver.user_name).all()
            #    for r in reg:
            #        session.delete(r)
            #
            #    reg = model.Registration()
            #    reg.driver = driver
            #    reg.number = int(data['Num'])
            #    reg.race_class = race_class
            #    reg.anon_car = unicode(data['Vehicle'])
            #    reg.event = c.event

        else:
            #try to find a previous anon_reg
            try:
                reg = Registration.objects.get(event=event,
                                               _anon_f_name=data['First'],
                                               _anon_l_name=data['Last'])

            except Registration.DoesNotExist:
                reg = Registration()
                reg.number = int(data['Num'])
                reg.race_class = race_class
                reg.pax_class = group

                reg._anon_f_name = data['First']
                reg._anon_l_name = data['Last']
                reg._anon_car = data['Vehicle']
                reg.event = event
                reg.club = club
        try:
            reg.save()
        except:
            validate_error[
                'results_file'] = "%s %s has a duplicate class/number: %d %s" % (
                    reg.first_name, reg.last_name, reg.number, reg.race_class)
            return validate_error
        try:
            reg.number = int(data['Num'])
        except Exception:
            validate_error[
                'results_file'] = 'Problem with car number for entry: %s %s' % (
                    reg.first_name, reg.last_name)
            return validate_error

        result.reg = reg
        result.save()

        for r_key, c_key, p_key in zip(time_keys, cone_keys, pen_keys):
            run = Run()
            run.result = result
            run.club = club
            try:
                if float(data[r_key]) <= 0.0:
                    continue
            except ValueError:
                continue

            run.base_time = float(data[r_key])
            run.cones = int(data[c_key])
            if data[p_key].strip():
                run.penalty = data[p_key]
            run.save()

        #note: not sure why this has to be done here
        try:
            reg.save()
        except Exception:
            validate_error[
                'results_file'] = "%s %s has a duplicate class/number: %d %s" % (
                    reg.first_name, reg.last_name, reg.number, reg.race_class)
            return validate_error

        results.append(result)

    return results