def test_total_price(self): self.o = Order() self.o.user_prof = self.user.get_profile() self.o.save() item1 = Registration() item1.number = 11 item1.race_class = self.race_class item1.pax_class = None item1.event = self.e item1.price = "40.00" item1.order = self.o item1.club = self.c item1.save() item2 = Membership() item2.user_prof = self.user.get_profile() item2.club = self.c item2.num = 1 item2.start = datetime.date.today() - datetime.timedelta(days=300) item2.valid_thru = datetime.date.today() + datetime.timedelta(days=1) item2.price = "60.00" item2.order = self.o item2.save() self.o.calc_total_price() self.assertEqual(self.o.total_price, "100.00") club = Club() club.name = "test-test" club.save() c = Coupon() c.discount_amount = "10.00" c.permanent = True c.code = "aaa" c.expires = datetime.date.today() + datetime.timedelta(days=1) c.club = club c.save() self.o.coupon = c self.o.calc_total_price() self.assertEqual(self.o.total_price, '90.00')
def test_total_price(self): self.o = Order() self.o.user_prof = self.user.get_profile() self.o.save() item1 = Registration() item1.number = 11 item1.race_class = self.race_class item1.pax_class = None item1.event = self.e item1.price = "40.00" item1.order = self.o item1.club = self.c item1.save() item2 = Membership() item2.user_prof = self.user.get_profile() item2.club = self.c item2.num = 1 item2.start = datetime.date.today() - datetime.timedelta(days=300) item2.valid_thru = datetime.date.today()+datetime.timedelta(days=1) item2.price = "60.00" item2.order = self.o item2.save() self.o.calc_total_price() self.assertEqual(self.o.total_price, "100.00") club = Club() club.name ="test-test" club.save() c = Coupon() c.discount_amount = "10.00" c.permanent = True c.code = "aaa" c.expires = datetime.date.today() + datetime.timedelta(days=1) c.club = club c.save() self.o.coupon = c self.o.calc_total_price() self.assertEqual(self.o.total_price, '90.00')
def test_no_result_reg_dibs(self): reg = Registration() up = UserProfile.objects.get(user=self.u2) reg.user_profile = up reg.number = 15 reg.race_class = self.race_class reg.pax_class = None reg.event = self.e3 reg.club = self.c reg.save() self.c.assign_dibs() self.assertEqual(len(self.c.dibs.filter(club=self.c, user_profile=self.u3.get_profile()).all()), 1) dibs = Dibs.objects.filter(club=self.c, user_profile=self.u3.get_profile).get() self.assertEqual(dibs.number, 3) self.assertEqual(dibs.race_class, self.race_class) self.assertEqual(dibs.expires, self.e4.date+datetime.timedelta(days=30)) reg.delete() Dibs.objects.all().delete()
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()
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()
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
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