from flask import render_template, Blueprint, redirect, url_for, request, flash from flask_user import login_required from app.models import Account, PlayKey, db from datatables import ColumnDT, DataTables from app.forms import CreatePlayKeyForm, EditPlayKeyForm from app import gm_level, log_audit play_keys_blueprint = Blueprint('play_keys', __name__) # Key creation page @play_keys_blueprint.route('/', methods=['GET']) @login_required @gm_level(5) def index(): return render_template('play_keys/index.html.j2') @play_keys_blueprint.route('/create//', methods=['GET'], defaults={'count': 1, 'uses': 1}) @login_required @gm_level(5) def create(count=1, uses=1): key = PlayKey.create(count=count, uses=uses) log_audit(f"Created {count} Play Key(s) with {uses} uses!") flash(f"Created {count} Play Key(s) with {uses} uses! {key}", "success") return redirect(url_for('play_keys.index')) @play_keys_blueprint.route('/create/bulk', methods=('GET', 'POST')) @login_required @gm_level(5) def bulk_create(): form = CreatePlayKeyForm() if form.validate_on_submit(): PlayKey.create(count=form.count.data, uses=form.uses.data) log_audit(f"Created {form.count.data} Play Key(s) with {form.uses.data} uses!") flash(f"Created {form.count.data} Play Key(s) with {form.uses.data} uses!", "success") return redirect(url_for('play_keys.index')) return render_template('play_keys/bulk.html.j2', form=form) @play_keys_blueprint.route('/delete/', methods=('GET', 'POST')) @login_required @gm_level(5) def delete(id): key = PlayKey.query.filter(PlayKey.id == id).first() # associated_accounts = Account.query.filter(Account.play_key_id==id).all() log_audit(f"Deleted Play Key {key.key_string}") flash(f"Deleted Play Key {key.key_string}", "danger") key.delete() return redirect(url_for('play_keys.index')) @play_keys_blueprint.route('/edit/', methods=('GET', 'POST')) @login_required @gm_level(5) def edit(id): key = PlayKey.query.filter(PlayKey.id == id).first() form = EditPlayKeyForm() if form.validate_on_submit(): log_audit(f"Updated Play key {key.id} \ Uses: {key.key_uses}:{form.uses.data} \ Active: {key.active}:{form.active.data} \ Notes: {key.notes}:{form.notes.data} \ ") key.key_uses = form.uses.data key.active = form.active.data key.notes = form.notes.data key.save() return redirect(url_for('play_keys.index')) form.uses.data = key.key_uses form.active.data = key.active form.notes.data = key.notes return render_template('play_keys/edit.html.j2', form=form, key=key) @play_keys_blueprint.route('/view/', methods=('GET', 'POST')) @login_required @gm_level(5) def view(id): key = PlayKey.query.filter(PlayKey.id == id).first() accounts = Account.query.filter(Account.play_key_id == id).all() return render_template('play_keys/view.html.j2', key=key, accounts=accounts) @play_keys_blueprint.route('/get', methods=['GET']) @login_required @gm_level(5) def get(): columns = [ ColumnDT(PlayKey.id), ColumnDT(PlayKey.key_string), ColumnDT(PlayKey.key_uses), ColumnDT(PlayKey.times_used), ColumnDT(PlayKey.created_at), ColumnDT(PlayKey.active), ] query = db.session.query().select_from(PlayKey) params = request.args.to_dict() rowTable = DataTables(params, query, columns) data = rowTable.output_result() for play_key in data["data"]: # Hackily shove buttons into response play_key["0"] = f""" View Edit Delete """ if play_key["5"]: play_key["5"] = '''

''' else: play_key["5"] = '''

''' return data