from citywok_ms.file.models import EmployeeFile, File
import citywok_ms.employee.messages as employee_msg
import citywok_ms.file.messages as file_msg
from citywok_ms.employee.forms import EmployeeForm
from citywok_ms.file.forms import FileForm
from flask import Blueprint, flash, redirect, render_template, url_for
from citywok_ms.employee.models import Employee

employee = Blueprint("employee", __name__, url_prefix="/employee")


@employee.route("/")
def index():
    return render_template(
        "employee/index.html",
        title=employee_msg.INDEX_TITLE,
        active_employees=Employee.get_active(),
        suspended_employees=Employee.get_suspended(),
    )


@employee.route("/new", methods=["GET", "POST"])
def new():
    form = EmployeeForm()
    if form.validate_on_submit():
        employee = Employee.create_by_form(form)
        flash(employee_msg.NEW_SUCCESS.format(name=employee.full_name), "success")
        return redirect(url_for("employee.index"))
    return render_template(
        "employee/form.html", title=employee_msg.NEW_TITLE, form=form
    )


@employee.route("/<int:employee_id>")
def detail(employee_id):
    return render_template(
        "employee/detail.html",
        title=employee_msg.DETAIL_TITLE,
        employee=Employee.get_or_404(employee_id),
        file_form=FileForm(),
    )


@employee.route("/<int:employee_id>/update", methods=["GET", "POST"])
def update(employee_id):
    employee = Employee.get_or_404(employee_id)
    form = EmployeeForm()
    form.hide_id.data = employee_id
    if form.validate_on_submit():
        employee.update_by_form(form)
        flash(employee_msg.UPDATE_SUCCESS.format(name=employee.full_name), "success")
        return redirect(url_for("employee.detail", employee_id=employee_id))

    form.process(obj=employee)

    return render_template(
        "employee/form.html",
        employee=employee,
        form=form,
        title=employee_msg.UPDATE_TITLE,
    )


@employee.route("/<int:employee_id>/suspend", methods=["POST"])
def suspend(employee_id):
    employee = Employee.get_or_404(employee_id)
    employee.suspend()
    flash(employee_msg.SUSPEND_SUCCESS.format(name=employee.full_name), "success")
    return redirect(url_for("employee.detail", employee_id=employee_id))


@employee.route("/<int:employee_id>/activate", methods=["POST"])
def activate(employee_id):
    employee = Employee.get_or_404(employee_id)
    employee.activate()
    flash(employee_msg.ACTIVATE_SUCCESS.format(name=employee.full_name), "success")
    return redirect(url_for("employee.detail", employee_id=employee_id))


@employee.route("/<int:employee_id>/upload", methods=["POST"])
def upload(employee_id):
    form = FileForm()
    file = form.file.data
    if form.validate_on_submit():
        db_file = EmployeeFile.create_by_form(form, Employee.get_or_404(employee_id))
        flash(file_msg.UPLOAD_SUCCESS.format(name=db_file.full_name), "success")
    elif file is not None:
        flash(
            file_msg.INVALID_FORMAT.format(format=File.split_file_format(file)),
            "danger",
        )
    else:
        flash(file_msg.NO_FILE, "danger")
    return redirect(url_for("employee.detail", employee_id=employee_id))

[((9, 11, 9, 66), 'flask.Blueprint', 'Blueprint', (), '', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((24, 11, 24, 25), 'citywok_ms.employee.forms.EmployeeForm', 'EmployeeForm', ({}, {}), '()', False, 'from citywok_ms.employee.forms import EmployeeForm\n'), ((29, 11, 31, 5), 'flask.render_template', 'render_template', (), '', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((46, 15, 46, 47), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', ({(46, 35, 46, 46): 'employee_id'}, {}), '(employee_id)', False, 'from citywok_ms.employee.models import Employee\n'), ((47, 11, 47, 25), 'citywok_ms.employee.forms.EmployeeForm', 'EmployeeForm', ({}, {}), '()', False, 'from citywok_ms.employee.forms import EmployeeForm\n'), ((56, 11, 61, 5), 'flask.render_template', 'render_template', (), '', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((66, 15, 66, 47), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', ({(66, 35, 66, 46): 'employee_id'}, {}), '(employee_id)', False, 'from citywok_ms.employee.models import Employee\n'), ((74, 15, 74, 47), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', ({(74, 35, 74, 46): 'employee_id'}, {}), '(employee_id)', False, 'from citywok_ms.employee.models import Employee\n'), ((82, 11, 82, 21), 'citywok_ms.file.forms.FileForm', 'FileForm', ({}, {}), '()', False, 'from citywok_ms.file.forms import FileForm\n'), ((26, 19, 26, 48), 'citywok_ms.employee.models.Employee.create_by_form', 'Employee.create_by_form', ({(26, 43, 26, 47): 'form'}, {}), '(form)', False, 'from citywok_ms.employee.models import Employee\n'), ((68, 10, 68, 70), 'citywok_ms.employee.messages.SUSPEND_SUCCESS.format', 'employee_msg.SUSPEND_SUCCESS.format', (), '', True, 'import citywok_ms.employee.messages as employee_msg\n'), ((69, 20, 69, 71), 'flask.url_for', 'url_for', (), '', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((76, 10, 76, 71), 'citywok_ms.employee.messages.ACTIVATE_SUCCESS.format', 'employee_msg.ACTIVATE_SUCCESS.format', (), '', True, 'import citywok_ms.employee.messages as employee_msg\n'), ((77, 20, 77, 71), 'flask.url_for', 'url_for', (), '', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((94, 20, 94, 71), 'flask.url_for', 'url_for', (), '', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((17, 25, 17, 46), 'citywok_ms.employee.models.Employee.get_active', 'Employee.get_active', ({}, {}), '()', False, 'from citywok_ms.employee.models import Employee\n'), ((18, 28, 18, 52), 'citywok_ms.employee.models.Employee.get_suspended', 'Employee.get_suspended', ({}, {}), '()', False, 'from citywok_ms.employee.models import Employee\n'), ((27, 14, 27, 70), 'citywok_ms.employee.messages.NEW_SUCCESS.format', 'employee_msg.NEW_SUCCESS.format', (), '', True, 'import citywok_ms.employee.messages as employee_msg\n'), ((28, 24, 28, 49), 'flask.url_for', 'url_for', ({(28, 32, 28, 48): '"""employee.index"""'}, {}), "('employee.index')", False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((39, 17, 39, 49), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', ({(39, 37, 39, 48): 'employee_id'}, {}), '(employee_id)', False, 'from citywok_ms.employee.models import Employee\n'), ((40, 18, 40, 28), 'citywok_ms.file.forms.FileForm', 'FileForm', ({}, {}), '()', False, 'from citywok_ms.file.forms import FileForm\n'), ((51, 14, 51, 73), 'citywok_ms.employee.messages.UPDATE_SUCCESS.format', 'employee_msg.UPDATE_SUCCESS.format', (), '', True, 'import citywok_ms.employee.messages as employee_msg\n'), ((52, 24, 52, 75), 'flask.url_for', 'url_for', (), '', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((85, 52, 85, 84), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', ({(85, 72, 85, 83): 'employee_id'}, {}), '(employee_id)', False, 'from citywok_ms.employee.models import Employee\n'), ((86, 14, 86, 68), 'citywok_ms.file.messages.UPLOAD_SUCCESS.format', 'file_msg.UPLOAD_SUCCESS.format', (), '', True, 'import citywok_ms.file.messages as file_msg\n'), ((93, 8, 93, 41), 'flask.flash', 'flash', ({(93, 14, 93, 30): 'file_msg.NO_FILE', (93, 32, 93, 40): '"""danger"""'}, {}), "(file_msg.NO_FILE, 'danger')", False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((89, 50, 89, 78), 'citywok_ms.file.models.File.split_file_format', 'File.split_file_format', ({(89, 73, 89, 77): 'file'}, {}), '(file)', False, 'from citywok_ms.file.models import EmployeeFile, File\n')]