Example #1
0
def refund_transaction(transaction_id):
    """Refund a transaction.

    Allows part or full refunds for whatever reason, sends a request to eWay to
    refund money back to the user's card.
    """
    if flask.request.method != 'POST':
        return flask.redirect(flask.request.referrer
                              or flask.url_for('admin.admin_home'))

    transaction = models.CardTransaction.get_by_id(transaction_id)

    if transaction:
        amount = util.parse_pounds_pence(flask.request.form,
                                         'refund_amount_pounds',
                                         'refund_amount_pence')

        if amount == 0:
            flask.flash('Cannot refund nothing.', 'warning')
            return flask.redirect(
                flask.request.referrer
                or flask.url_for('admin.view_transaction',
                                 transaction_id=transaction.transaction_id))

        if amount > (transaction.value - transaction.refunded):
            flask.flash('Cannot refund more than has been charged.', 'warning')
            return flask.redirect(
                flask.request.referrer
                or flask.url_for('admin.view_transaction',
                                 transaction_id=transaction.transaction_id))

        result = transaction.process_refund(amount)

        if not result:
            flask.flash('Could not process refund.', 'warning')
        else:
            flask.flash('Refund processed successfully.', 'success')

        return flask.redirect(
            flask.request.referrer
            or flask.url_for('admin.view_transaction',
                             transaction_id=transaction.transaction_id))
    else:
        flask.flash('Could not find transaction, could not refund.', 'warning')
        return flask.redirect(flask.request.referrer
                              or flask.url_for('admin.admin_home'))
Example #2
0
def give_user(user_id):
    """Give the user some tickets.

    Overrides the ticket limit.
    """
    if flask.request.method != "POST":
        return flask.redirect(
            flask.request.referrer or flask.url_for("admin.admin_home")
        )

    user = models.User.get_by_id(user_id)

    if user:
        try:
            ticket_type = APP.config["TICKET_TYPES_BY_SLUG"][
                flask.request.form["give_ticket_type"]
            ]
        except KeyError:
            flask.flash("Invalid/no ticket type selected", "error")
            return flask.redirect(
                flask.request.referrer or flask.url_for("admin.admin_home")
            )

        if (
            "give_price_pounds" not in flask.request.form
            or flask.request.form["give_price_pounds"] == ""
        ) and (
            "give_price_pence" not in flask.request.form
            or flask.request.form["give_price_pence"] == ""
        ):
            price = ticket_type.price
        else:
            price = util.parse_pounds_pence(
                flask.request.form, "give_price_pounds", "give_price_pence"
            )

        num_tickets = int(flask.request.form["give_num_tickets"])

        if (
            "give_reason" not in flask.request.form
            or flask.request.form["give_reason"] == ""
        ):
            note = "Given by {0} (#{1}) for no reason.".format(
                login.current_user.full_name, login.current_user.object_id
            )
        else:
            note = "Given by {0} (#{1}) with reason: {2}.".format(
                login.current_user.full_name,
                login.current_user.object_id,
                flask.request.form["give_reason"],
            )

        tickets = [
            models.Ticket(user, ticket_type.slug, price) for _ in xrange(num_tickets)
        ]

        for ticket in tickets:
            ticket.add_note(note)

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

        APP.log_manager.log_event(
            "Gave {0} tickets".format(num_tickets), tickets=tickets, user=user
        )

        flask.flash(
            "Gave {0} {1} tickets".format(user.forenames, num_tickets), "success"
        )

        return flask.redirect(
            flask.request.referrer
            or flask.url_for("admin_users.view_user", user_id=user.object_id)
        )
    else:
        flask.flash("Could not find user, could not give tickets.", "warning")
        return flask.redirect(
            flask.request.referrer or flask.url_for("admin.admin_home")
        )
def give_user(user_id):
    """Give the user some tickets.

    Overrides the ticket limit.
    """
    if flask.request.method != 'POST':
        return flask.redirect(flask.request.referrer or
                              flask.url_for('admin.admin_home'))

    user = models.User.get_by_id(user_id)

    if user:
        try:
            ticket_type = APP.config['TICKET_TYPES_BY_SLUG'][
                flask.request.form['give_ticket_type']
            ]
        except KeyError:
            flask.flash("Invalid/no ticket type selected", "error")
            return flask.redirect(flask.request.referrer or
                                  flask.url_for('admin.admin_home'))

        if (
            (
                'give_price_pounds' not in flask.request.form or
                flask.request.form['give_price_pounds'] == ''
            ) and (
                'give_price_pence' not in flask.request.form or
                flask.request.form['give_price_pence'] == ''
            )
        ):
            price = ticket_type.price
        else:
            price = util.parse_pounds_pence(flask.request.form,
                                            'give_price_pounds',
                                            'give_price_pence')

        num_tickets = int(flask.request.form['give_num_tickets'])

        if (
                'give_reason' not in flask.request.form or
                flask.request.form['give_reason'] == ''
        ):
            note = 'Given by {0} (#{1}) for no reason.'.format(
                current_user.full_name,
                current_user.object_id
            )
        else:
            note = 'Given by {0} (#{1}) with reason: {2}.'.format(
                current_user.full_name,
                current_user.object_id,
                flask.request.form['give_reason']
            )

        tickets = [
            models.Ticket(user, ticket_type.slug, price)
            for _ in xrange(num_tickets)
        ]

        for ticket in tickets:
            ticket.add_note(note)

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

        APP.log_manager.log_event(
            'Gave {0} tickets'.format(
                num_tickets
            ),
            tickets,
            user
        )

        flask.flash(
            'Gave {0} {1} tickets'.format(
                user.forenames,
                num_tickets
            ),
            'success'
        )

        return flask.redirect(flask.request.referrer or
                              flask.url_for('admin_users.view_user',
                                            user_id=user.object_id))
    else:
        flask.flash(
            'Could not find user, could not give tickets.',
            'warning'
        )
        return flask.redirect(flask.request.referrer or
                              flask.url_for('admin.admin_home'))
Example #4
0
def give_user(user_id):
    """Give the user some tickets.

    Overrides the ticket limit.
    """
    if flask.request.method != 'POST':
        return flask.redirect(flask.request.referrer
                              or flask.url_for('admin.admin_home'))

    user = models.User.get_by_id(user_id)

    if user:
        try:
            ticket_type = APP.config['TICKET_TYPES_BY_SLUG'][
                flask.request.form['give_ticket_type']]
        except KeyError:
            flask.flash("Invalid/no ticket type selected", "error")
            return flask.redirect(flask.request.referrer
                                  or flask.url_for('admin.admin_home'))

        if (('give_price_pounds' not in flask.request.form
             or flask.request.form['give_price_pounds'] == '')
                and ('give_price_pence' not in flask.request.form
                     or flask.request.form['give_price_pence'] == '')):
            price = ticket_type.price
        else:
            price = util.parse_pounds_pence(flask.request.form,
                                            'give_price_pounds',
                                            'give_price_pence')

        num_tickets = int(flask.request.form['give_num_tickets'])

        if ('give_reason' not in flask.request.form
                or flask.request.form['give_reason'] == ''):
            note = 'Given by {0} (#{1}) for no reason.'.format(
                current_user.full_name, current_user.object_id)
        else:
            note = 'Given by {0} (#{1}) with reason: {2}.'.format(
                current_user.full_name, current_user.object_id,
                flask.request.form['give_reason'])

        tickets = [
            models.Ticket(user, ticket_type.slug, price)
            for _ in xrange(num_tickets)
        ]

        for ticket in tickets:
            ticket.add_note(note)

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

        APP.log_manager.log_event('Gave {0} tickets'.format(num_tickets),
                                  tickets, user)

        flask.flash('Gave {0} {1} tickets'.format(user.forenames, num_tickets),
                    'success')

        return flask.redirect(
            flask.request.referrer
            or flask.url_for('admin_users.view_user', user_id=user.object_id))
    else:
        flask.flash('Could not find user, could not give tickets.', 'warning')
        return flask.redirect(flask.request.referrer
                              or flask.url_for('admin.admin_home'))
Example #5
0
def vouchers(page=1):
    """Manage vouchers.

    Handles the creation of discount vouchers, and allows their deletion.
    """
    form = {}

    if flask.request.method == 'POST':
        form = flask.request.form

        success = True

        expires = None

        if 'expires' in form and form['expires'] != '':
            try:
                expires = parser.parse(form['expires'])
                if expires < datetime.datetime.utcnow():
                    flask.flash('Expiry date cannot be in the past', 'warning')
                    success = False
            except (KeyError, ValueError) as _:
                flask.flash('Could not parse expiry date', 'warning')
                success = False

        if 'voucher_type' not in form or form['voucher_type'] == '':
            flask.flash('You must select a discount type', 'warning')
            success = False
        elif form['voucher_type'] == 'Fixed Price':
            value = util.parse_pounds_pence(flask.request.form,
                                            'fixed_price_pounds',
                                            'fixed_price_pence')
        elif form['voucher_type'] == 'Fixed Discount':
            value = util.parse_pounds_pence(flask.request.form,
                                            'fixed_discount_pounds',
                                            'fixed_discount_pence')

            if value == 0:
                flask.flash('Cannot give no discount', 'warning')
                success = False
        else:
            try:
                value = int(form['fixed_discount'])
            except ValueError:
                value = 0

            if value == 0:
                flask.flash('Cannot give 0% discount', 'warning')
                success = False
            elif value > 100:
                flask.flash('Cannot give greater than 100% discount',
                            'warning')
                success = False

        if not re.match('[a-zA-Z0-9]+', form['voucher_prefix']):
            flask.flash(('Voucher prefix must be non-empty and contain only '
                         'letters and numbers'), 'warning')
            success = False

        if success:
            num_vouchers = int(form['num_vouchers'])
            single_use = 'single_use' in form and form['single_use'] == 'yes'

            for _ in xrange(num_vouchers):
                key = util.generate_key(10)
                voucher = models.Voucher(
                    '{0}-{1}'.format(form['voucher_prefix'],
                                     key), expires, form['voucher_type'],
                    value, form['applies_to'], single_use)
                DB.session.add(voucher)

            DB.session.commit()

            flask.flash('Voucher(s) created successfully', 'success')

            form = {}

    voucher_query = models.Voucher.query

    if 'search' in flask.request.args:
        voucher_query = voucher_query.filter(
            models.Voucher.code.like('%{0}%'.format(
                flask.request.args['search'])))

    voucher_results = voucher_query.paginate(page, 10)

    return flask.render_template('admin/vouchers.html',
                                 form=form,
                                 vouchers=voucher_results)
Example #6
0
def vouchers(page=1):
    """Manage vouchers.

    Handles the creation of discount vouchers, and allows their deletion.
    """
    form = {}

    if flask.request.method == 'POST':
        form = flask.request.form

        success = True

        expires = None

        if 'expires' in form and form['expires'] != '':
            try:
                expires = parser.parse(form['expires'])
                if expires < datetime.datetime.utcnow():
                    flask.flash(
                        'Expiry date cannot be in the past',
                        'warning'
                    )
                    success = False
            except (KeyError, ValueError) as _:
                flask.flash(
                    'Could not parse expiry date',
                    'warning'
                )
                success = False

        if 'voucher_type' not in form or form['voucher_type'] == '':
            flask.flash(
                'You must select a discount type',
                'warning'
            )
            success = False
        elif form['voucher_type'] == 'Fixed Price':
            value = util.parse_pounds_pence(flask.request.form,
                                            'fixed_price_pounds',
                                            'fixed_price_pence')
        elif form['voucher_type'] == 'Fixed Discount':
            value = util.parse_pounds_pence(flask.request.form,
                                            'fixed_discount_pounds',
                                            'fixed_discount_pence')

            if value == 0:
                flask.flash(
                    'Cannot give no discount',
                    'warning'
                )
                success = False
        else:
            try:
                value = int(form['fixed_discount'])
            except ValueError:
                value = 0

            if value == 0:
                flask.flash(
                    'Cannot give 0% discount',
                    'warning'
                )
                success = False
            elif value > 100:
                flask.flash(
                    'Cannot give greater than 100% discount',
                    'warning'
                )
                success = False

        if not re.match('[a-zA-Z0-9]+', form['voucher_prefix']):
            flask.flash(
                (
                    'Voucher prefix must be non-empty and contain only '
                    'letters and numbers'
                ),
                'warning'
            )
            success = False

        if success:
            num_vouchers = int(form['num_vouchers'])
            single_use = 'single_use' in form and form['single_use'] == 'yes'

            for _ in xrange(num_vouchers):
                key = util.generate_key(10)
                voucher = models.Voucher(
                    '{0}-{1}'.format(
                        form['voucher_prefix'],
                        key
                    ),
                    expires,
                    form['voucher_type'],
                    value,
                    form['applies_to'],
                    single_use
                )
                DB.session.add(voucher)

            DB.session.commit()

            flask.flash(
                'Voucher(s) created successfully',
                'success'
            )

            form = {}

    voucher_query = models.Voucher.query

    if 'search' in flask.request.args:
        voucher_query = voucher_query.filter(
            models.Voucher.code.like(
                '%{0}%'.format(
                    flask.request.args['search']
                )
            )
        )

    voucher_results = voucher_query.paginate(
        page,
        10
    )

    return flask.render_template(
        'admin_vouchers/vouchers.html',
        form=form,
        vouchers=voucher_results
    )
Example #7
0
def vouchers(page=1):
    """Manage vouchers.

    Handles the creation of discount vouchers, and allows their deletion.
    """
    form = {}

    if flask.request.method == "POST":
        form = flask.request.form

        success = True

        expires = None

        if "expires" in form and form["expires"] != "":
            try:
                expires = parser.parse(form["expires"])
                if expires < datetime.datetime.utcnow():
                    flask.flash("Expiry date cannot be in the past", "warning")
                    success = False
            except (KeyError, ValueError) as _:
                flask.flash("Could not parse expiry date", "warning")
                success = False

        if "voucher_type" not in form or form["voucher_type"] == "":
            flask.flash("You must select a discount type", "warning")
            success = False
        elif form["voucher_type"] == "Fixed Price":
            value = util.parse_pounds_pence(flask.request.form,
                                            "fixed_price_pounds",
                                            "fixed_price_pence")
        elif form["voucher_type"] == "Fixed Discount":
            value = util.parse_pounds_pence(flask.request.form,
                                            "fixed_discount_pounds",
                                            "fixed_discount_pence")

            if value == 0:
                flask.flash("Cannot give no discount", "warning")
                success = False
        else:
            try:
                value = int(form["fixed_discount"])
            except ValueError:
                value = 0

            if value == 0:
                flask.flash("Cannot give 0% discount", "warning")
                success = False
            elif value > 100:
                flask.flash("Cannot give greater than 100% discount",
                            "warning")
                success = False

        if not re.match("[a-zA-Z0-9]+", form["voucher_prefix"]):
            flask.flash(
                ("Voucher prefix must be non-empty and contain only "
                 "letters and numbers"),
                "warning",
            )
            success = False

        if success:
            num_vouchers = int(form["num_vouchers"])
            single_use = "single_use" in form and form["single_use"] == "yes"

            for _ in xrange(num_vouchers):
                key = util.generate_key(10)
                voucher = models.Voucher(
                    "{0}-{1}".format(form["voucher_prefix"], key),
                    expires,
                    form["voucher_type"],
                    value,
                    form["applies_to"],
                    single_use,
                )
                DB.session.add(voucher)

            DB.session.commit()

            flask.flash("Voucher(s) created successfully", "success")

            form = {}

    voucher_query = models.Voucher.query

    if "search" in flask.request.args:
        voucher_query = voucher_query.filter(
            models.Voucher.code.like("%{0}%".format(
                flask.request.args["search"])))

    voucher_results = voucher_query.paginate(page, 10)

    return flask.render_template("admin_vouchers/vouchers.html",
                                 form=form,
                                 vouchers=voucher_results)
Example #8
0
def refund_transaction(transaction_id):
    """Refund a transaction.

    Allows part or full refunds for whatever reason, sends a request to eWay to
    refund money back to the user's card.
    """
    if flask.request.method != 'POST':
        return flask.redirect(flask.request.referrer or
                              flask.url_for('admin.admin_home'))

    transaction = models.CardTransaction.get_by_id(transaction_id)

    if transaction:
        amount = util.parse_pounds_pence(flask.request.form,
                                            'refund_amount_pounds',
                                            'refund_amount_pence')

        if amount == 0:
            flask.flash(
                'Cannot refund nothing.',
                'warning'
            )
            return flask.redirect(
                flask.request.referrer or
                flask.url_for('admin.view_transaction',
                              transaction_id=transaction.transaction_id)
            )

        if amount > (transaction.value - transaction.refunded):
            flask.flash(
                'Cannot refund more than has been charged.',
                'warning'
            )
            return flask.redirect(
                flask.request.referrer or
                flask.url_for('admin.view_transaction',
                              transaction_id=transaction.transaction_id)
            )

        result = transaction.process_refund(amount)

        if not result:
            flask.flash(
                'Could not process refund.',
                'warning'
            )
        else:
            flask.flash(
                'Refund processed successfully.',
                'success'
            )

        return flask.redirect(
            flask.request.referrer or
            flask.url_for('admin.view_transaction',
                          transaction_id=transaction.transaction_id)
        )
    else:
        flask.flash(
            'Could not find transaction, could not refund.',
            'warning'
        )
        return flask.redirect(flask.request.referrer or
                              flask.url_for('admin.admin_home'))