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))

[((375, 430), 'flask.Blueprint', 'Blueprint', (['"""employee"""', '__name__'], {'url_prefix': '"""/employee"""'}), "('employee', __name__, url_prefix='/employee')\n", (384, 430), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((747, 761), 'citywok_ms.employee.forms.EmployeeForm', 'EmployeeForm', ([], {}), '()\n', (759, 761), False, 'from citywok_ms.employee.forms import EmployeeForm\n'), ((990, 1068), 'flask.render_template', 'render_template', (['"""employee/form.html"""'], {'title': 'employee_msg.NEW_TITLE', 'form': 'form'}), "('employee/form.html', title=employee_msg.NEW_TITLE, form=form)\n", (1005, 1068), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((1448, 1480), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', (['employee_id'], {}), '(employee_id)\n', (1467, 1480), False, 'from citywok_ms.employee.models import Employee\n'), ((1492, 1506), 'citywok_ms.employee.forms.EmployeeForm', 'EmployeeForm', ([], {}), '()\n', (1504, 1506), False, 'from citywok_ms.employee.forms import EmployeeForm\n'), ((1822, 1927), 'flask.render_template', 'render_template', (['"""employee/form.html"""'], {'employee': 'employee', 'form': 'form', 'title': 'employee_msg.UPDATE_TITLE'}), "('employee/form.html', employee=employee, form=form, title=\n    employee_msg.UPDATE_TITLE)\n", (1837, 1927), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((2069, 2101), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', (['employee_id'], {}), '(employee_id)\n', (2088, 2101), False, 'from citywok_ms.employee.models import Employee\n'), ((2390, 2422), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', (['employee_id'], {}), '(employee_id)\n', (2409, 2422), False, 'from citywok_ms.employee.models import Employee\n'), ((2705, 2715), 'citywok_ms.file.forms.FileForm', 'FileForm', ([], {}), '()\n', (2713, 2715), False, 'from citywok_ms.file.forms import FileForm\n'), ((815, 844), 'citywok_ms.employee.models.Employee.create_by_form', 'Employee.create_by_form', (['form'], {}), '(form)\n', (838, 844), False, 'from citywok_ms.employee.models import Employee\n'), ((2135, 2195), 'citywok_ms.employee.messages.SUSPEND_SUCCESS.format', 'employee_msg.SUSPEND_SUCCESS.format', ([], {'name': 'employee.full_name'}), '(name=employee.full_name)\n', (2170, 2195), True, 'import citywok_ms.employee.messages as employee_msg\n'), ((2228, 2279), 'flask.url_for', 'url_for', (['"""employee.detail"""'], {'employee_id': 'employee_id'}), "('employee.detail', employee_id=employee_id)\n", (2235, 2279), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((2457, 2518), 'citywok_ms.employee.messages.ACTIVATE_SUCCESS.format', 'employee_msg.ACTIVATE_SUCCESS.format', ([], {'name': 'employee.full_name'}), '(name=employee.full_name)\n', (2493, 2518), True, 'import citywok_ms.employee.messages as employee_msg\n'), ((2551, 2602), 'flask.url_for', 'url_for', (['"""employee.detail"""'], {'employee_id': 'employee_id'}), "('employee.detail', employee_id=employee_id)\n", (2558, 2602), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((3170, 3221), 'flask.url_for', 'url_for', (['"""employee.detail"""'], {'employee_id': 'employee_id'}), "('employee.detail', employee_id=employee_id)\n", (3177, 3221), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((591, 612), 'citywok_ms.employee.models.Employee.get_active', 'Employee.get_active', ([], {}), '()\n', (610, 612), False, 'from citywok_ms.employee.models import Employee\n'), ((642, 666), 'citywok_ms.employee.models.Employee.get_suspended', 'Employee.get_suspended', ([], {}), '()\n', (664, 666), False, 'from citywok_ms.employee.models import Employee\n'), ((859, 915), 'citywok_ms.employee.messages.NEW_SUCCESS.format', 'employee_msg.NEW_SUCCESS.format', ([], {'name': 'employee.full_name'}), '(name=employee.full_name)\n', (890, 915), True, 'import citywok_ms.employee.messages as employee_msg\n'), ((952, 977), 'flask.url_for', 'url_for', (['"""employee.index"""'], {}), "('employee.index')\n", (959, 977), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((1266, 1298), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', (['employee_id'], {}), '(employee_id)\n', (1285, 1298), False, 'from citywok_ms.employee.models import Employee\n'), ((1318, 1328), 'citywok_ms.file.forms.FileForm', 'FileForm', ([], {}), '()\n', (1326, 1328), False, 'from citywok_ms.file.forms import FileForm\n'), ((1629, 1688), 'citywok_ms.employee.messages.UPDATE_SUCCESS.format', 'employee_msg.UPDATE_SUCCESS.format', ([], {'name': 'employee.full_name'}), '(name=employee.full_name)\n', (1663, 1688), True, 'import citywok_ms.employee.messages as employee_msg\n'), ((1725, 1776), 'flask.url_for', 'url_for', (['"""employee.detail"""'], {'employee_id': 'employee_id'}), "('employee.detail', employee_id=employee_id)\n", (1732, 1776), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((2828, 2860), 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', (['employee_id'], {}), '(employee_id)\n', (2847, 2860), False, 'from citywok_ms.employee.models import Employee\n'), ((2876, 2930), 'citywok_ms.file.messages.UPLOAD_SUCCESS.format', 'file_msg.UPLOAD_SUCCESS.format', ([], {'name': 'db_file.full_name'}), '(name=db_file.full_name)\n', (2906, 2930), True, 'import citywok_ms.file.messages as file_msg\n'), ((3116, 3149), 'flask.flash', 'flash', (['file_msg.NO_FILE', '"""danger"""'], {}), "(file_msg.NO_FILE, 'danger')\n", (3121, 3149), False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), ((3035, 3063), 'citywok_ms.file.models.File.split_file_format', 'File.split_file_format', (['file'], {}), '(file)\n', (3057, 3063), False, 'from citywok_ms.file.models import EmployeeFile, File\n')]