def modal_add(facility_name, table_name, page_context):
    module_name = MODULE_NAME
    if page_context:
        page_context += ",modal_form"
    else:
        page_context = "modal_form"

    fg = FormGenerator('modal_add', 'single', table_name, page_context, module_name)
    fg.data_entry_form('new')

    return fg.page_template_context()
def multiple_entry(facility_name, table_name, row_names, page_context):
    module_name = MODULE_NAME
    row_names = json.loads(row_names)

    fg = FormGenerator('data_entry', 'multiple', table_name, page_context, module_name)
    fg.multiple_data_entry_form(row_names)

    if request.method == 'POST' and fg.form_class.validate_csrf_data(request.form.get('csrf_token')):
        fp = FormParser(table_name)
        fp.parse()

        fg.multiple_data_entry_form(row_names, fp.instance)
        fg.form_class.validate_on_submit()

        # Token has been validated above, this removes the error since the form was regenerated with dynamically
        # added fields and the new token is no longer valid with the session token
        del fg.form_class.errors['csrf_token']

        if not fg.form_class.errors:
            save_status, save_msg = fp.save()
            if save_status is True:
                return saved_data(facility_name, module_name, table_name, save_msg, page_context)
            else:
                fg.add_page_context({'page_msg': save_msg})
        else:
            fp.undo()

    return fg.page_template_context()
def data_entry(facility_name, table_name, row_name, page_context):
    module_name = MODULE_NAME

    if row_name == "new":
        depth = request.args.get("depth", 0, int)
    else:
        depth = request.args.get("depth", 2, int)
    fg = FormGenerator("data_entry", "single", table_name, page_context, module_name)
    fg.data_entry_form(row_name, None, depth)

    if request.method == "POST" and fg.form_class.validate_csrf_data(request.form.get("csrf_token")):
        fp = FormParser(table_name)
        fp.parse()

        fg.data_entry_form(row_name, fp.instance)
        fg.form_class.validate_on_submit()

        # Token has been validated above, this removes the error since the form was regenerated with dynamically
        # added fields and the new token is no longer valid with the session token
        del fg.form_class.errors["csrf_token"]

        if not fg.form_class.errors:
            save_status, save_msg = fp.save()

            if save_status is True:
                return saved_data(facility_name, module_name, table_name, save_msg, page_context)
            else:
                fg.add_page_context({"page_msg": save_msg})
                fp.undo()
        else:
            fp.undo()

    return fg.page_template_context()