Example #1
0
def purchase_home():
    """First step of the purchasing flow.

    Checks if the user can purchase tickets, and processes the purchase form.
    """
    if login.current_user.purchase_group:
        if login.current_user == login.current_user.purchase_group.leader:
            if APP.config['TICKETS_ON_SALE']:
                return flask.redirect(flask.url_for('group_purchase.checkout'))
            else:
                flask.flash(
                    (
                        'You cannot currently purchase tickets because you are '
                        'leading a purchase group. You will be able to '
                        'purchase tickets on behalf of your group when general '
                        'release starts.'
                    ),
                    'info'
                )
                return flask.redirect(flask.url_for('dashboard.dashboard_home'))
        else:
            flask.flash(
                (
                    'You cannot currently purchase tickets because you are a '
                    'member of a purchase group. Your group leader {0} will be '
                    'able to purchase tickets on behalf of your group when '
                    'general release starts.'
                ).format(login.current_user.purchase_group.leader.full_name),
                'info'
            )
            return flask.redirect(flask.url_for('dashboard.dashboard_home'))

    ticket_info = purchase_logic.get_ticket_info(
        login.current_user
    )
    if models.Waiting.query.count() > 0 or not ticket_info.ticket_types:
        flask.flash(
            'You are not able to purchase tickets at this time.',
            'info'
        )

        if purchase_logic.wait_available(login.current_user):
            flask.flash(
                (
                    'Please join the waiting list, and you will be allocated '
                    'tickets as they become available'
                ),
                'info'
            )
            return flask.redirect(flask.url_for('purchase.wait'))
        else:
            return flask.redirect(flask.url_for('dashboard.dashboard_home'))

    num_tickets = {
        ticket_type.slug: 0
        for ticket_type, _ in ticket_info.ticket_types
    }

    if flask.request.method == 'POST':
        for ticket_type, _ in ticket_info.ticket_types:
            num_tickets[ticket_type.slug] = int(
                flask.request.form['num_tickets_{0}'.format(ticket_type.slug)]
            )

        flashes = purchase_logic.validate_tickets(
            ticket_info,
            num_tickets
        )

        payment_method, payment_term = purchase_logic.check_payment_method(
            flashes
        )

        voucher = None
        if (
                'voucher_code' in flask.request.form and
                flask.request.form['voucher_code'] != ''
        ):
            (result, response, voucher) = validators.validate_voucher(
                flask.request.form['voucher_code'])
            if not result:
                flashes.append(
                    (
                        '{} Please clear the discount code field to continue '
                        'without using a voucher.'
                    ).format(response['message'])
                )

        postage, address = purchase_logic.check_postage(flashes)

        if flashes:
            flask.flash(
                (
                    'There were errors in your order. Please fix '
                    'these and try again'
                ),
                'error'
            )
            for msg in flashes:
                flask.flash(msg, 'warning')

            return flask.render_template(
                'purchase/purchase_home.html',
                form=flask.request.form,
                num_tickets=num_tickets,
                ticket_info=ticket_info
            )


        tickets = purchase_logic.create_tickets(
            login.current_user,
            ticket_info,
            num_tickets
        )

        if voucher is not None:
            (success, tickets, error) = voucher.apply(tickets,
                                                      login.current_user)
            if not success:
                flask.flash('Could not use voucher - ' + error, 'error')


        roundup_price = purchase_logic.check_roundup_donation(flashes)

        if roundup_price is not 0:
            roundup_donation = None

            roundup_donation = models.RoundupDonation(
                    roundup_price,
                    login.current_user,
            )

            if roundup_donation is not None:
                # Tack on the roundup donation fee to their ticket(s)
                # Entry
                tickets = roundup_donation.apply(tickets)
                DB.session.add(roundup_donation);


        DB.session.add_all(tickets)
        DB.session.commit()

        APP.log_manager.log_event(
            'Purchased Tickets',
            tickets=tickets,
            user=login.current_user
        )

        # return flask.render_template(
        #    'purchase/purchase_home.html',
        #    num_tickets=num_tickets,
        #    ticket_info=ticket_info
        # )
        return payment_logic.do_payment(
         tickets,
         postage,
         payment_method,
         payment_term,
         address
        )
    else:
        return flask.render_template(
            'purchase/purchase_home.html',
            num_tickets=num_tickets,
            ticket_info=ticket_info
        )
Example #2
0
def purchase_home():
    """First step of the purchasing flow.

    Checks if the user can purchase tickets, and processes the purchase form.
    """
    ticket_info = purchase_logic.get_ticket_info(login.current_user)

    if not ticket_info.ticket_types:
        flask.flash('You are not able to purchase tickets at this time.',
                    'info')

        (waiting_permitted, _, _) = login.current_user.can_wait()
        if waiting_permitted:
            flask.flash(
                ('Please join the waiting list, and you will be allocated '
                 'tickets as they become available'), 'info')
            return flask.redirect(flask.url_for('purchase.wait'))
        else:
            return flask.redirect(flask.url_for('dashboard.dashboard_home'))

    num_tickets = {
        ticket_type.slug: 0
        for ticket_type, _ in ticket_info.ticket_types
    }

    ticket_names = []

    if flask.request.method == 'POST':
        for ticket_type, _ in ticket_info.ticket_types:
            num_tickets[ticket_type.slug] = int(
                flask.request.form['num_tickets_{0}'.format(ticket_type.slug)])

        ticket_names = [
            name for name in flask.request.form.getlist('ticket_name[]')
            if name
        ]

        flashes = purchase_logic.validate_tickets(ticket_info, num_tickets,
                                                  ticket_names)

        payment_method, payment_term = purchase_logic.check_payment_method(
            flashes)

        voucher = None
        if ('voucher_code' in flask.request.form
                and flask.request.form['voucher_code'] != ''):
            (result, response, voucher) = validators.validate_voucher(
                flask.request.form['voucher_code'])
            if not result:
                flashes.append(
                    ('{} Please clear the discount code field to continue '
                     'without using a voucher.').format(response['message']))

        postage, address = purchase_logic.check_postage(flashes)

        if flashes:
            flask.flash(('There were errors in your order. Please fix '
                         'these and try again'), 'error')
            for msg in flashes:
                flask.flash(msg, 'warning')

            return flask.render_template('purchase/purchase_home.html',
                                         form=flask.request.form,
                                         ticket_names=ticket_names,
                                         num_tickets=num_tickets,
                                         ticket_info=ticket_info)

        tickets = purchase_logic.create_tickets(login.current_user,
                                                ticket_info, num_tickets,
                                                ticket_names)

        if voucher is not None:
            (success, tickets, error) = voucher.apply(tickets,
                                                      login.current_user)
            if not success:
                flask.flash('Could not use voucher - ' + error, 'error')

        DB.session.add_all(tickets)
        DB.session.commit()

        APP.log_manager.log_event('Purchased Tickets', tickets,
                                  login.current_user)

        return payment_logic.do_payment(tickets, postage, payment_method,
                                        payment_term, address)
    else:
        return flask.render_template('purchase/purchase_home.html',
                                     num_tickets=num_tickets,
                                     ticket_names=ticket_names,
                                     ticket_info=ticket_info)
Example #3
0
def purchase_home():
    """First step of the purchasing flow.

    Checks if the user can purchase tickets, and processes the purchase form.
    """
    if login.current_user.purchase_group:
        if login.current_user == login.current_user.purchase_group.leader:
            if APP.config['TICKETS_ON_SALE']:
                return flask.redirect(flask.url_for('group_purchase.checkout'))
            else:
                flask.flash(
                    ('You cannot currently purchase tickets because you are '
                     'leading a purchase group. You will be able to '
                     'purchase tickets on behalf of your group when general '
                     'release starts.'), 'info')
                return flask.redirect(
                    flask.url_for('dashboard.dashboard_home'))
        else:
            flask.flash(
                ('You cannot currently purchase tickets because you are a '
                 'member of a purchase group. Your group leader {0} will be '
                 'able to purchase tickets on behalf of your group when '
                 'general release starts.').format(
                     login.current_user.purchase_group.leader.full_name),
                'info')
            return flask.redirect(flask.url_for('dashboard.dashboard_home'))

    ticket_info = purchase_logic.get_ticket_info(login.current_user)
    if models.Waiting.query.count() > 0 or not ticket_info.ticket_types:
        flask.flash('You are not able to purchase tickets at this time.',
                    'info')

        if purchase_logic.wait_available(login.current_user):
            flask.flash(
                ('Please join the waiting list, and you will be allocated '
                 'tickets as they become available'), 'info')
            return flask.redirect(flask.url_for('purchase.wait'))
        else:
            return flask.redirect(flask.url_for('dashboard.dashboard_home'))

    num_tickets = {
        ticket_type.slug: 0
        for ticket_type, _ in ticket_info.ticket_types
    }

    if flask.request.method == 'POST':
        for ticket_type, _ in ticket_info.ticket_types:
            num_tickets[ticket_type.slug] = int(
                flask.request.form['num_tickets_{0}'.format(ticket_type.slug)])

        flashes = purchase_logic.validate_tickets(ticket_info, num_tickets)

        payment_method, payment_term = purchase_logic.check_payment_method(
            flashes)

        voucher = None
        if ('voucher_code' in flask.request.form
                and flask.request.form['voucher_code'] != ''):
            (result, response, voucher) = validators.validate_voucher(
                flask.request.form['voucher_code'])
            if not result:
                flashes.append(
                    ('{} Please clear the discount code field to continue '
                     'without using a voucher.').format(response['message']))

        postage, address = purchase_logic.check_postage(flashes)

        if flashes:
            flask.flash(('There were errors in your order. Please fix '
                         'these and try again'), 'error')
            for msg in flashes:
                flask.flash(msg, 'warning')

            return flask.render_template('purchase/purchase_home.html',
                                         form=flask.request.form,
                                         num_tickets=num_tickets,
                                         ticket_info=ticket_info)

        tickets = purchase_logic.create_tickets(login.current_user,
                                                ticket_info, num_tickets)

        if voucher is not None:
            (success, tickets, error) = voucher.apply(tickets,
                                                      login.current_user)
            if not success:
                flask.flash('Could not use voucher - ' + error, 'error')

        roundup_price = purchase_logic.check_roundup_donation(flashes)

        if roundup_price is not 0:
            roundup_donation = None

            roundup_donation = models.RoundupDonation(
                roundup_price,
                login.current_user,
            )

            if roundup_donation is not None:
                # Tack on the roundup donation fee to their ticket(s)
                # Entry
                tickets = roundup_donation.apply(tickets)
                DB.session.add(roundup_donation)

        DB.session.add_all(tickets)
        DB.session.commit()

        APP.log_manager.log_event('Purchased Tickets',
                                  tickets=tickets,
                                  user=login.current_user)

        # return flask.render_template(
        #    'purchase/purchase_home.html',
        #    num_tickets=num_tickets,
        #    ticket_info=ticket_info
        # )
        return payment_logic.do_payment(tickets, postage, payment_method,
                                        payment_term, address)
    else:
        return flask.render_template('purchase/purchase_home.html',
                                     num_tickets=num_tickets,
                                     ticket_info=ticket_info)
def purchase_home():
    """First step of the purchasing flow.

    Checks if the user can purchase tickets, and processes the purchase form.
    """
    ticket_info = purchase_logic.get_ticket_info(
        login.current_user
    )

    if not ticket_info.ticket_types:
        flask.flash(
            'You are not able to purchase tickets at this time.',
            'info'
        )

        (waiting_permitted, _, _) = login.current_user.can_wait()
        if waiting_permitted:
            flask.flash(
                (
                    'Please join the waiting list, and you will be allocated '
                    'tickets as they become available'
                ),
                'info'
            )
            return flask.redirect(flask.url_for('purchase.wait'))
        else:
            return flask.redirect(flask.url_for('dashboard.dashboard_home'))

    num_tickets = {
        ticket_type.slug: 0
        for ticket_type, _ in ticket_info.ticket_types
    }

    ticket_names = []

    if flask.request.method == 'POST':
        for ticket_type, _ in ticket_info.ticket_types:
            num_tickets[ticket_type.slug] = int(
                flask.request.form['num_tickets_{0}'.format(ticket_type.slug)]
            )

        ticket_names = [
            name
            for name in flask.request.form.getlist('ticket_name[]')
            if name
        ]

        flashes = purchase_logic.validate_tickets(
            ticket_info,
            num_tickets,
            ticket_names
        )

        payment_method, payment_term = purchase_logic.check_payment_method(
            flashes
        )

        voucher = None
        if (
                'voucher_code' in flask.request.form and
                flask.request.form['voucher_code'] != ''
        ):
            (result, response, voucher) = validators.validate_voucher(
                flask.request.form['voucher_code'])
            if not result:
                flashes.append(
                    (
                        '{} Please clear the discount code field to continue '
                        'without using a voucher.'
                    ).format(response['message'])
                )

        postage, address = purchase_logic.check_postage(flashes)

        if flashes:
            flask.flash(
                (
                    'There were errors in your order. Please fix '
                    'these and try again'
                ),
                'error'
            )
            for msg in flashes:
                flask.flash(msg, 'warning')

            return flask.render_template(
                'purchase/purchase_home.html',
                form=flask.request.form,
                ticket_names=ticket_names,
                num_tickets=num_tickets,
                ticket_info=ticket_info
            )

        tickets = purchase_logic.create_tickets(
            login.current_user,
            ticket_info,
            num_tickets,
            ticket_names
        )

        if voucher is not None:
            (success, tickets, error) = voucher.apply(tickets,
                                                      login.current_user)
            if not success:
                flask.flash('Could not use voucher - ' + error, 'error')

        DB.session.add_all(tickets)
        DB.session.commit()

        APP.log_manager.log_event(
            'Purchased Tickets',
            tickets,
            login.current_user
        )

        return payment_logic.do_payment(
            tickets,
            postage,
            payment_method,
            payment_term,
            address
        )
    else:
        return flask.render_template(
            'purchase/purchase_home.html',
            num_tickets=num_tickets,
            ticket_names=ticket_names,
            ticket_info=ticket_info
        )
Example #5
0
def purchase_home():
    """First step of the purchasing flow.

    Checks if the user can purchase tickets, and processes the purchase form.
    """
    if login.current_user.purchase_group:
        if login.current_user == login.current_user.purchase_group.leader:
            if APP.config["TICKETS_ON_SALE"]:
                return flask.redirect(flask.url_for("group_purchase.checkout"))
            else:
                flask.flash(
                    ("You cannot currently purchase tickets because you are "
                     "leading a purchase group. You will be able to "
                     "purchase tickets on behalf of your group when general "
                     "release starts."),
                    "info",
                )
                return flask.redirect(
                    flask.url_for("dashboard.dashboard_home"))
        else:
            flask.flash(
                ("You cannot currently purchase tickets because you are a "
                 "member of a purchase group. Your group leader {0} will be "
                 "able to purchase tickets on behalf of your group when "
                 "general release starts.").format(
                     login.current_user.purchase_group.leader.full_name),
                "info",
            )
            return flask.redirect(flask.url_for("dashboard.dashboard_home"))

    ticket_info = purchase_logic.get_ticket_info(login.current_user)
    if models.Waiting.query.count() > 0 or not ticket_info.ticket_types:
        flask.flash("You are not able to purchase tickets at this time.",
                    "info")

        if purchase_logic.wait_available(login.current_user):
            flask.flash(
                ("Please join the waiting list, and you will be allocated "
                 "tickets as they become available"),
                "info",
            )
            return flask.redirect(flask.url_for("purchase.wait"))
        else:
            return flask.redirect(flask.url_for("dashboard.dashboard_home"))

    num_tickets = {
        ticket_type.slug: 0
        for ticket_type, _ in ticket_info.ticket_types
    }
    addons_selected = {addon.slug: False for addon, _ in ticket_info.addons}

    if flask.request.method == "POST":
        for ticket_type, _ in ticket_info.ticket_types:
            num_tickets[ticket_type.slug] = int(
                flask.request.form["num_tickets_{0}".format(ticket_type.slug)])
        for addon, _ in ticket_info.addons:
            try:
                addons_selected[addon.slug] = (
                    flask.request.form["addon_{0}".format(
                        addon.slug)] == "Yes")
            except KeyError:
                pass

        flashes = purchase_logic.validate_tickets(ticket_info, num_tickets,
                                                  addons_selected)

        payment_method, payment_term = purchase_logic.check_payment_method(
            flashes)

        voucher = None
        if ("voucher_code" in flask.request.form
                and flask.request.form["voucher_code"] != ""):
            (result, response, voucher) = validators.validate_voucher(
                flask.request.form["voucher_code"])
            if not result:
                flashes.append(
                    ("{} Please clear the discount code field to continue "
                     "without using a voucher.").format(response["message"]))

        postage, address = purchase_logic.check_postage(flashes)

        if flashes:
            flask.flash(
                ("There were errors in your order. Please fix "
                 "these and try again"),
                "error",
            )
            for msg in flashes:
                flask.flash(msg, "warning")

            return flask.render_template(
                "purchase/purchase_home.html",
                form=flask.request.form,
                num_tickets=num_tickets,
                addons_selected=addons_selected,
                ticket_info=ticket_info,
            )

        tickets, addons = purchase_logic.create_tickets(
            login.current_user, ticket_info, num_tickets, addons_selected)

        if voucher is not None:
            (success, tickets, error) = voucher.apply(tickets,
                                                      login.current_user)
            if not success:
                flask.flash("Could not use voucher - " + error, "error")

        roundup_price = purchase_logic.check_roundup_donation(flashes)

        if roundup_price is not 0:
            roundup_donation = None

            roundup_donation = models.RoundupDonation(roundup_price,
                                                      login.current_user)

            if roundup_donation is not None:
                # Tack on the roundup donation fee to their ticket(s)
                # Entry
                tickets = roundup_donation.apply(tickets)
                DB.session.add(roundup_donation)

        DB.session.add_all(tickets)
        DB.session.add_all(addons)
        DB.session.commit()

        APP.log_manager.log_event("Purchased Tickets",
                                  tickets=tickets,
                                  user=login.current_user)

        # return flask.render_template(
        #    'purchase/purchase_home.html',
        #    num_tickets=num_tickets,
        #    ticket_info=ticket_info
        # )
        return payment_logic.do_payment(tickets, postage, payment_method,
                                        payment_term, address)
    else:
        return flask.render_template(
            "purchase/purchase_home.html",
            num_tickets=num_tickets,
            ticket_info=ticket_info,
        )