# Path: toughwlan/manage/base.py
# ADMIN_MENUS = (MenuSys, MenuRes, MenuUser) = (u"系统管理", u"资源管理", u"用户管理")
# MENU_ICONS = {
#     u"系统管理": "fa fa-cog",
#     u"资源管理": "fa fa-desktop",
#     u"用户管理": "fa fa-users"
# }
# class BaseHandler(cyclone.web.RequestHandler):
#     def __init__(self, *argc, **argkw):
#     def initialize(self):
#     def on_finish(self):
#     def get_error_html(self, status_code=500, **kwargs):
#     def render(self, template_name, **template_vars):
#     def render_error(self, **template_vars):
#     def render_json(self, **template_vars):
#     def render_string(self, template_name, **template_vars):
#     def render_from_string(self, template_string, **template_vars):
#     def get_page_data(self, query):
#     def get_page_url(self, page, form_id=None):
#     def set_session_user(self, username, ipaddr, opr_type, login_time):
#     def clear_session(self):
#     def get_current_user(self):
#     def get_params(self):
#     def get_params_obj(self, obj):
#     def add_oplog(self,message):


import os
import os.path
import cyclone.auth
import cyclone.escape
import cyclone.web
from twisted.python import log
from toughlib import utils
from toughlib.permit import permit
from toughwlan.manage.base import BaseHandler, MenuSys

@permit.route(r"/backup/dump", u"备份数据", MenuSys, order=5.0002)
class DumpHandler(BaseHandler):
    @cyclone.web.authenticated
    def post(self):
        backup_path = self.settings.config.database.backup_path
        backup_file = "toughwlan_db_%s.json.gz" % utils.gen_backep_id()
        try:
            self.db_backup.dumpdb(os.path.join(backup_path, backup_file))
            return self.render_json(code=0, msg="backup done!")
        except Exception as err:
            log.err()
            return self.render_json(code=1, msg="backup fail! %s" % (err))

@permit.route(r"/backup/restore", u"恢复数据", MenuSys, order=5.0003)
class RestoreHandler(BaseHandler):
    @cyclone.web.authenticated
    def post(self):
        backup_path = self.settings.config.database.backup_path
        backup_file = "toughwlan_db_%s.before_restore.json.gz" % utils.gen_backep_id()
        rebakfs = self.get_argument("bakfs")
        try:
            self.db_backup.dumpdb(os.path.join(backup_path, backup_file))
            self.db_backup.restoredb(os.path.join(backup_path, rebakfs))
            return self.render_json(code=0, msg="restore done!")
        except Exception as err:
            return self.render_json(code=1, msg="restore fail! %s" % (err))


@permit.route(r"/backup/delete", u"删除数据", MenuSys, order=5.0004)

# Path: toughwlan/manage/base.py
# ADMIN_MENUS = (MenuSys, MenuRes, MenuUser) = (u"系统管理", u"资源管理", u"用户管理")
# MENU_ICONS = {
#     u"系统管理": "fa fa-cog",
#     u"资源管理": "fa fa-desktop",
#     u"用户管理": "fa fa-users"
# }
# class BaseHandler(cyclone.web.RequestHandler):
#     def __init__(self, *argc, **argkw):
#     def initialize(self):
#     def on_finish(self):
#     def get_error_html(self, status_code=500, **kwargs):
#     def render(self, template_name, **template_vars):
#     def render_error(self, **template_vars):
#     def render_json(self, **template_vars):
#     def render_string(self, template_name, **template_vars):
#     def render_from_string(self, template_string, **template_vars):
#     def get_page_data(self, query):
#     def get_page_url(self, page, form_id=None):
#     def set_session_user(self, username, ipaddr, opr_type, login_time):
#     def clear_session(self):
#     def get_current_user(self):
#     def get_params(self):
#     def get_params_obj(self, obj):
#     def add_oplog(self,message):


# Path: toughwlan/manage/system/backup.py
import os
import os.path
import cyclone.auth
import cyclone.escape
import cyclone.web
from twisted.python import log
from toughlib import utils
from toughlib.permit import permit
from toughwlan.manage.base import BaseHandler, MenuSys


@permit.route(r"/backup/dump", u"备份数据", MenuSys, order=5.0002)
class DumpHandler(BaseHandler):
    @cyclone.web.authenticated
    def post(self):
        backup_path = self.settings.config.database.backup_path
        backup_file = "toughwlan_db_%s.json.gz" % utils.gen_backep_id()
        try:
            self.db_backup.dumpdb(os.path.join(backup_path, backup_file))
            return self.render_json(code=0, msg="backup done!")
        except Exception as err:
            log.err()
            return self.render_json(code=1, msg="backup fail! %s" % (err))

@permit.route(r"/backup/restore", u"恢复数据", MenuSys, order=5.0003)
class RestoreHandler(BaseHandler):
    @cyclone.web.authenticated
    def post(self):
        backup_path = self.settings.config.database.backup_path
        backup_file = "toughwlan_db_%s.before_restore.json.gz" % utils.gen_backep_id()
        rebakfs = self.get_argument("bakfs")
        try:
            self.db_backup.dumpdb(os.path.join(backup_path, backup_file))
            self.db_backup.restoredb(os.path.join(backup_path, rebakfs))
            return self.render_json(code=0, msg="restore done!")
        except Exception as err:
            return self.render_json(code=1, msg="restore fail! %s" % (err))


@permit.route(r"/backup/delete", u"删除数据", MenuSys, order=5.0004)

class DeleteHandler(BaseHandler):