<commit_before>"""
When an admin submits a form to create/edit an attendee/group/job/etc we usually want to perform some basic validations
on the data that was entered.  We put those validations here.  To make a validation for the Attendee model, you can
just write a function decorated with the @validation.Attendee decorator.  That function should return None on success
and an error string on failure.

In addition, you can define a set of required fields by setting the .required field like the AdminAccount.required list
below.  This should be a list of tuples where the first tuple element is the name of the field, and the second is the
name that should be displayed in the "XXX is a required field" error message.

To perform these validations, call the "check" method on the instance you're validating.  That method returns None
on success and a string error message on validation failure.
"""
from uber.common import *
from email_validator import validate_email, EmailNotValidError


AdminAccount.required = [('attendee', 'Attendee'), ('hashed', 'Password')]


@validation.AdminAccount
def duplicate_admin(account):
    if account.is_new:
        with Session() as session:
            if session.query(AdminAccount).filter_by(attendee_id=account.attendee_id).all():
                return 'That attendee already has an admin account'


@validation.AdminAccount
def has_email_address(account):
    if account.is_new:
        with Session() as session:
            if session.query(Attendee).filter_by(id=account.attendee_id).first().email == '':
                return "Attendee doesn't have a valid email set"


Group.required = [('name', 'Group Name')]


@prereg_validation.Group
def dealer_wares(group):
    if group.tables and not group.wares:
        return "You must provide a detailed explanation of what you sell for us to evaluate your submission"


@prereg_validation.Group
def dealer_website(group):
    if group.tables and not group.website:
        return "Please enter your business' website address"


@prereg_validation.Group
def dealer_description(group):
    if group.tables and not group.description:
        return "Please provide a brief description of your business"


@prereg_validation.Group
def dealer_address(group):
    if group.tables and not (group.address1 and group.city and group.region and group.country and group.zip_code):
        "Please provide your full address for tax purposes"


@validation.Group
def group_money(group):
    if not group.auto_recalc:
        try:
            cost = int(float(group.cost if group.cost else 0))
            if cost < 0:
                return 'Total Group Price must be a number that is 0 or higher.'
        except:
            return "What you entered for Total Group Price ({}) isn't even a number".format(group.cost)

    try:
        amount_paid = int(float(group.amount_paid if group.amount_paid else 0))
        if amount_paid < 0:
            return 'Amount Paid must be a number that is 0 or higher.'
    except:
        return "What you entered for Amount Paid ({}) isn't even a number".format(group.amount_paid)

    try:
        amount_refunded = int(float(group.amount_refunded if group.amount_refunded else 0))
        if amount_refunded < 0:
            return 'Amount Refunded must be positive'
        elif amount_refunded > amount_paid:
            return 'Amount Refunded cannot be greater than Amount Paid'
    except:
        return "What you entered for Amount Refunded ({}) wasn't even a number".format(group.amount_refunded)


def _invalid_phone_number(s):
    if not s.startswith('+'):
        return len(re.findall(r'\d', s)) != 10 or re.search(c.SAME_NUMBER_REPEATED, re.sub(r'[^0-9]', '', s))


def _invalid_zip_code(s):
    return len(re.findall(r'\d', s)) not in [5, 9]


def ignore_unassigned_and_placeholders(func):
    @wraps(func)
    def with_skipping(attendee):
        unassigned_group_reg = attendee.group_id and not attendee.first_name and not attendee.last_name
        valid_placeholder = attendee.placeholder and attendee.first_name and attendee.last_name
        if not unassigned_group_reg and not valid_placeholder:
            return func(attendee)
    return with_skipping


@prereg_validation.Attendee
def shirt_size(attendee):
    if attendee.amount_extra >= c.SHIRT_LEVEL and attendee.shirt == c.NO_SHIRT:
        return 'Your shirt size is required'


@prereg_validation.Attendee
def total_cost_over_paid(attendee):
    if attendee.total_cost < attendee.amount_paid:
        return 'You have already paid ${}, you cannot reduce your extras below that.'.format(attendee.amount_paid)


@prereg_validation.Attendee
def promo_code_is_useful(attendee):
    if attendee.promo_code:
        if not attendee.is_unpaid:
            return "You can't apply a promo code after you've paid or if you're in a group."
        elif attendee.overridden_price:
            return "You already have a special badge price, you can't use a promo code on top of that."
        elif attendee.badge_cost >= attendee.badge_cost_without_promo_code:
            return "That promo code doesn't make your badge any cheaper. " \
                "You may already have other discounts."


@prereg_validation.Attendee
def promo_code_not_is_expired(attendee):<commit_msg>Prevent attendees from trying to pay over $999,999 Stripe literally cannot charge an attendee more than eight digits ($999,999.99). Fixes https://github.com/magfest/ubersystem/issues/2257 by preventing attendees from registering a badge with a total_cost more than $999,999.<commit_after>"""
When an admin submits a form to create/edit an attendee/group/job/etc we usually want to perform some basic validations
on the data that was entered.  We put those validations here.  To make a validation for the Attendee model, you can
just write a function decorated with the @validation.Attendee decorator.  That function should return None on success
and an error string on failure.

In addition, you can define a set of required fields by setting the .required field like the AdminAccount.required list
below.  This should be a list of tuples where the first tuple element is the name of the field, and the second is the
name that should be displayed in the "XXX is a required field" error message.

To perform these validations, call the "check" method on the instance you're validating.  That method returns None
on success and a string error message on validation failure.
"""
from uber.common import *
from email_validator import validate_email, EmailNotValidError


AdminAccount.required = [('attendee', 'Attendee'), ('hashed', 'Password')]


@validation.AdminAccount
def duplicate_admin(account):
    if account.is_new:
        with Session() as session:
            if session.query(AdminAccount).filter_by(attendee_id=account.attendee_id).all():
                return 'That attendee already has an admin account'


@validation.AdminAccount
def has_email_address(account):
    if account.is_new:
        with Session() as session:
            if session.query(Attendee).filter_by(id=account.attendee_id).first().email == '':
                return "Attendee doesn't have a valid email set"


Group.required = [('name', 'Group Name')]


@prereg_validation.Group
def dealer_wares(group):
    if group.tables and not group.wares:
        return "You must provide a detailed explanation of what you sell for us to evaluate your submission"


@prereg_validation.Group
def dealer_website(group):
    if group.tables and not group.website:
        return "Please enter your business' website address"


@prereg_validation.Group
def dealer_description(group):
    if group.tables and not group.description:
        return "Please provide a brief description of your business"


@prereg_validation.Group
def dealer_address(group):
    if group.tables and not (group.address1 and group.city and group.region and group.country and group.zip_code):
        "Please provide your full address for tax purposes"


@validation.Group
def group_money(group):
    if not group.auto_recalc:
        try:
            cost = int(float(group.cost if group.cost else 0))
            if cost < 0:
                return 'Total Group Price must be a number that is 0 or higher.'
        except:
            return "What you entered for Total Group Price ({}) isn't even a number".format(group.cost)

    try:
        amount_paid = int(float(group.amount_paid if group.amount_paid else 0))
        if amount_paid < 0:
            return 'Amount Paid must be a number that is 0 or higher.'
    except:
        return "What you entered for Amount Paid ({}) isn't even a number".format(group.amount_paid)

    try:
        amount_refunded = int(float(group.amount_refunded if group.amount_refunded else 0))
        if amount_refunded < 0:
            return 'Amount Refunded must be positive'
        elif amount_refunded > amount_paid:
            return 'Amount Refunded cannot be greater than Amount Paid'
    except:
        return "What you entered for Amount Refunded ({}) wasn't even a number".format(group.amount_refunded)


def _invalid_phone_number(s):
    if not s.startswith('+'):
        return len(re.findall(r'\d', s)) != 10 or re.search(c.SAME_NUMBER_REPEATED, re.sub(r'[^0-9]', '', s))


def _invalid_zip_code(s):
    return len(re.findall(r'\d', s)) not in [5, 9]


def ignore_unassigned_and_placeholders(func):
    @wraps(func)
    def with_skipping(attendee):
        unassigned_group_reg = attendee.group_id and not attendee.first_name and not attendee.last_name
        valid_placeholder = attendee.placeholder and attendee.first_name and attendee.last_name
        if not unassigned_group_reg and not valid_placeholder:
            return func(attendee)
    return with_skipping


@prereg_validation.Attendee
def shirt_size(attendee):
    if attendee.amount_extra >= c.SHIRT_LEVEL and attendee.shirt == c.NO_SHIRT:
        return 'Your shirt size is required'


@prereg_validation.Attendee
def total_cost_over_paid(attendee):
    if attendee.total_cost < attendee.amount_paid:
        return 'You have already paid ${}, you cannot reduce your extras below that.'.format(attendee.amount_paid)


@validation.Attendee
def reasonable_total_cost(attendee):
    if attendee.total_cost >= 999999:
        return 'We cannot charge ${:,.2f}. Please reduce extras so the total is below $999,999.'.format(attendee.total_cost)


@prereg_validation.Attendee
def promo_code_is_useful(attendee):
    if attendee.promo_code:
        if not attendee.is_unpaid:
            return "You can't apply a promo code after you've paid or if you're in a group."
        elif attendee.overridden_price:
            return "You already have a special badge price, you can't use a promo code on top of that."
        elif attendee.badge_cost >= attendee.badge_cost_without_promo_code:
            return "That promo code doesn't make your badge any cheaper. " \
                "You may already have other discounts."


@prereg_validation.Attendee
def promo_code_not_is_expired(attendee):
{'fraction_non_alphanumeric': 0.0770625566636446, 'fraction_numerical': 0.006074342701722575, 'mean_word_length': 3.4659919028340083, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5484226', 'n_tokens_mistral': 3275, 'n_tokens_neox': 3134, 'n_words': 1154}