def new_agent_code(self): from uber.models import PromoCode new_agent_code = PromoCode.generate_random_code() self.session.add( PromoCode(discount=0, discount_type=PromoCode._FIXED_DISCOUNT, code=new_agent_code)) return new_agent_code
def test_promo_code_not_is_expired(self): expire = datetime.now(pytz.UTC) - timedelta(days=9) promo_code = PromoCode(discount=1, expiration_date=expire) attendee = Attendee(promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') assert check(attendee, prereg=True) == 'That promo code is expired.'
def test_promo_code_is_useful_special_price(self, monkeypatch): monkeypatch.setattr(c, 'get_attendee_price', lambda r: 0) promo_code = PromoCode(discount=1, expiration_date=next_week) attendee = Attendee(promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') assert check(attendee, prereg=True) == \ "That promo code doesn't make your badge any cheaper. You may already have other discounts."
def test_promo_code_is_useful_overridden_price(self): promo_code = PromoCode(discount=1, expiration_date=next_week) attendee = Attendee(overridden_price=10, promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') assert check(attendee, prereg=True) == \ "You already have a special badge price, you can't use a promo code on top of that."
def test_promo_code_does_not_help_minor(self, monkeypatch): promo_code = PromoCode(discount=1, expiration_date=next_week) attendee = Attendee(birthdate=last_week, promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') assert check(attendee, prereg=True) == \ "You are already receiving an age based discount, you can't use a promo code on top of that."
def test_promo_code_is_useful_not_is_unpaid(self, paid): promo_code = PromoCode(discount=1, expiration_date=next_week) attendee = Attendee(paid=paid, promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') assert check( attendee, prereg=True ) == "You can't apply a promo code after you've paid or if you're in a group."
def test_promo_code_does_not_help_one_day_badge(self, monkeypatch): monkeypatch.setattr(c, 'get_oneday_price', lambda r: 10) promo_code = PromoCode(discount=1, expiration_date=next_week) attendee = Attendee(badge_type=c.ONE_DAY_BADGE, promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') assert check(attendee, prereg=True) == \ "You can't apply a promo code to a one day badge."
def test_promo_code_does_not_help_dealer(self, monkeypatch): promo_code = PromoCode(discount=1, expiration_date=next_week) attendee = Attendee(badge_type=c.PSEUDO_DEALER_BADGE, group=Group(), cellphone='555-555-1234', promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') assert check(attendee, prereg=True) == \ "You can't apply a promo code to a dealer registration."
def disambiguated_promo_code(self): with Session() as session: session.add( PromoCode(id=self.disambiguated_promo_code_id, code='012568', uses_allowed=100)) with Session() as session: promo_code = session.query(PromoCode).filter( PromoCode.id == self.disambiguated_promo_code_id).one() yield promo_code session.delete(promo_code)
def test_promo_code_has_uses_remaining(self): promo_code = PromoCode(uses_allowed=1, expiration_date=next_week) sess = Attendee(promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') Charge.unpaid_preregs[sess.id] = Charge.to_sessionized(sess) sess.promo_code = None sess.promo_code_id = None assert len(promo_code.used_by) == 0 attendee = Attendee(promo_code=promo_code, placeholder=True, first_name='First', last_name='Last') assert check( attendee, prereg=True) == 'That promo code has been used too many times.'
def test_valid_uses_allowed(self, uses_allowed, message): assert message == check( PromoCode(discount=1, uses_allowed=uses_allowed))
def test_valid_discount(self, discount, message): assert message == check(PromoCode(discount=discount, uses_allowed=1))
def test_code_whitespace_removal(self, code, expected): promo_code = PromoCode(code=code) promo_code._attribute_adjustments() assert expected == promo_code.code
def test_empty_code_auto_generated(self, code, monkeypatch): monkeypatch.setattr(PromoCode, 'generate_random_code', Mock()) promo_code = PromoCode(code=code) promo_code._attribute_adjustments() assert PromoCode.generate_random_code.called
def test_badge_price_fixed_discount_is_free(self, discount): promo_code = PromoCode(discount=discount, discount_type=PromoCode._FIXED_DISCOUNT) promo_code._attribute_adjustments() assert promo_code.is_free
def test_100_percent_discount_is_free(self, discount): promo_code = PromoCode(discount=discount, discount_type=PromoCode._PERCENT_DISCOUNT) promo_code._attribute_adjustments() assert promo_code.is_free
def test_empty_discount_set_to_none(self, discount, discount_type): promo_code = PromoCode(discount=discount, discount_type=discount_type) promo_code._attribute_adjustments() assert promo_code.discount is None assert promo_code.is_free
def test_no_unlimited_free_badges(self, discount, uses_allowed): assert check( PromoCode(discount=discount, uses_allowed=uses_allowed) ) == 'Unlimited-use, free-badge promo codes are not allowed.'
def test_no_unlimited_100_percent_discount(self, discount, uses_allowed): assert check( PromoCode(discount=discount, discount_type=PromoCode._PERCENT_DISCOUNT, uses_allowed=uses_allowed) ) == 'Unlimited-use, free-badge promo codes are not allowed.'
def generate_promo_codes(self, session, message='', **params): defaults = dict( is_single_promo_code=1, count=1, use_words=False, length=9, segment_length=3, code='', expiration_date=c.ESCHATON, discount_type=0, discount=10, uses_allowed=1, export=False) params = dict(defaults, **{k: v for k, v in params.items() if k in defaults}) params['code'] = params['code'].strip() params['expiration_date'] = PromoCode.normalize_expiration_date(params['expiration_date']) try: params['count'] = int(params['count']) except Exception: params['count'] = 1 try: params['is_single_promo_code'] = int(params['is_single_promo_code']) except Exception: params['is_single_promo_code'] = 0 words = PromoCodeWord.group_by_parts_of_speech( session.query(PromoCodeWord).order_by(PromoCodeWord.normalized_word).all()) result = dict( params, message=message, promo_codes=[], words=[(i, s) for (i, s) in words.items()]) if cherrypy.request.method == 'POST': codes = None if params['is_single_promo_code']: params['count'] = 1 if params['code']: codes = [params['code']] if params['use_words'] and not codes and \ not any(s for (_, s) in words.items()): result['message'] = 'Please add some promo code words!' return result if not codes: if params['use_words']: codes = PromoCode.generate_word_code(params['count']) else: try: length = int(params['length']) except Exception: length = 9 try: segment_length = int(params['segment_length']) except Exception: segment_length = 3 codes = PromoCode.generate_random_code( params['count'], length, segment_length) promo_codes = [] for code in codes: params['code'] = code promo_codes.append(PromoCode().apply(params, restricted=False)) message = check_all(promo_codes) if message: result['message'] = message return result result['promo_codes'] = session.bulk_insert(promo_codes) generated_count = len(result['promo_codes']) if generated_count <= 0: result['message'] = "Could not generate any of the requested " \ "promo codes. Perhaps they've all been taken already?" return result if generated_count != params['count']: result['message'] = 'Some of the requested promo codes could not be generated' if params['export']: return self.export_promo_codes(codes=result['promo_codes']) result.update(defaults) return result
def test_no_unlimited_full_badge_discount(self, discount, uses_allowed): assert check( PromoCode(discount=discount, discount_type=PromoCode._FIXED_DISCOUNT, uses_allowed=uses_allowed) ) == 'Unlimited-use, free-badge promo codes are not allowed.'
def test_empty_uses_set_to_none(self, uses_allowed): promo_code = PromoCode(uses_allowed=uses_allowed) promo_code._attribute_adjustments() assert promo_code.uses_allowed is None assert promo_code.is_unlimited
def test_no_dupe_code(self, code): assert check(PromoCode(discount=1, code=code)) == \ 'The code you entered already belongs to another promo code. Note that promo codes are not case sensitive.'
def init_db(request): if os.path.exists(TEST_DB_FILE): os.remove(TEST_DB_FILE) patch_session(Session, request) initialize_db(modify_tables=True) register_session_listeners() with Session() as session: session.add( Attendee(placeholder=True, first_name='Regular', last_name='Volunteer', ribbon=c.VOLUNTEER_RIBBON, staffing=True)) session.add( Attendee(placeholder=True, first_name='Regular', last_name='Attendee')) d_arcade_trusted_dept_role = DeptRole(name='Trusted', description='Trusted in Arcade') d_arcade = Department(name='Arcade', description='Arcade', dept_roles=[d_arcade_trusted_dept_role]) d_console_trusted_dept_role = DeptRole( name='Trusted', description='Trusted in Console') d_console = Department(name='Console', description='Console', dept_roles=[d_console_trusted_dept_role]) session.add_all([ d_arcade, d_arcade_trusted_dept_role, d_console, d_console_trusted_dept_role ]) assigned_depts = { 'One': [d_arcade], 'Two': [d_console], 'Three': [d_arcade, d_console], 'Four': [d_arcade, d_console], 'Five': [] } trusted_depts = { 'One': [], 'Two': [], 'Three': [], 'Four': [d_arcade, d_console], 'Five': [] } for name in ['One', 'Two', 'Three', 'Four', 'Five']: dept_memberships = [] for dept in assigned_depts[name]: is_trusted = dept in trusted_depts[name] dept_memberships.append( DeptMembership( department_id=dept.id, dept_roles=(dept.dept_roles if is_trusted else []))) session.add_all(dept_memberships) session.add( Attendee(placeholder=True, first_name=name, last_name=name, paid=c.NEED_NOT_PAY, badge_type=c.STAFF_BADGE, dept_memberships=dept_memberships)) session.add( Attendee(placeholder=True, first_name=name, last_name=name, paid=c.NEED_NOT_PAY, badge_type=c.SUPPORTER_BADGE)) session.commit() session.add( WatchList(first_names='Banned, Alias, Nickname', last_name='Attendee', email='*****@*****.**', birthdate=date(1980, 7, 10), action='Action', reason='Reason')) session.add( Job(name='Job One', start_time=c.EPOCH, slots=1, weight=1, duration=2, department=d_arcade, extra15=True)) session.add( Job(name='Job Two', start_time=c.EPOCH + timedelta(hours=1), slots=1, weight=1, duration=2, department=d_arcade)) session.add( Job(name='Job Three', start_time=c.EPOCH + timedelta(hours=2), slots=1, weight=1, duration=2, department=d_arcade)) session.add( Job(name='Job Four', start_time=c.EPOCH, slots=2, weight=1, duration=2, department=d_console, extra15=True)) session.add( Job(name='Job Five', start_time=c.EPOCH + timedelta(hours=2), slots=1, weight=1, duration=2, department=d_console)) session.add( Job(name='Job Six', start_time=c.EPOCH, slots=1, weight=1, duration=2, department=d_console, required_roles=[d_console_trusted_dept_role])) session.add( PromoCode(code='ten percent off', discount=10, discount_type=PromoCode._PERCENT_DISCOUNT)) session.add( PromoCode(code='ten dollars off', discount=10, discount_type=PromoCode._FIXED_DISCOUNT)) session.add( PromoCode(code='ten dollar badge', discount=10, discount_type=PromoCode._FIXED_PRICE)) session.add(PromoCode(code='free badge', discount=0, uses_allowed=100)) session.commit()
def generate_promo_codes(self, session, message='', **params): defaults = dict(is_single_promo_code=1, count=1, use_words=False, length=9, segment_length=3, code='', expiration_date=c.ESCHATON, discount_type=0, discount=10, uses_allowed=1) params = dict(defaults, **{k: v for k, v in params.items() if k in defaults}) params['code'] = params['code'].strip() try: params['count'] = int(params['count']) except Exception: params['count'] = 1 try: params['is_single_promo_code'] = int( params['is_single_promo_code']) except Exception: params['is_single_promo_code'] = 0 words = PromoCodeWord.group_by_parts_of_speech( session.query(PromoCodeWord).order_by( PromoCodeWord.normalized_word).all()) result = dict(params, message=message, promo_codes=[], words=[(i, s) for (i, s) in words.items()]) if cherrypy.request.method == 'POST': codes = None if params['is_single_promo_code']: params['count'] = 1 if params['code']: codes = [params['code']] if params['use_words'] and not codes and \ not any(s for (_, s) in words.items()): result['message'] = 'Please add some promo code words!' return result if not codes: if params['use_words']: codes = PromoCode.generate_word_code(params['count']) else: try: length = int(params['length']) except Exception: length = 9 try: segment_length = int(params['segment_length']) except Exception: segment_length = 3 codes = PromoCode.generate_random_code( params['count'], length, segment_length) promo_codes = [] for code in codes: params['code'] = code promo_codes.append(PromoCode().apply(params, restricted=False)) message = check_all(promo_codes) if message: result['message'] = message return result result['promo_codes'] = session.bulk_insert(promo_codes) generated_count = len(result['promo_codes']) if generated_count <= 0: result['message'] = "Could not generate any of the requested " \ "promo codes. Perhaps they've all been taken already?" return result if generated_count != params['count']: result[ 'message'] = 'Some of the requested promo codes could not be generated' if 'export' in params: return self.export_promo_codes(codes=result['promo_codes']) result.update(defaults) return result