# -*- coding:utf-8 -*-

import tornado.web
import config
from config import cfg as c

from torcms.torlite.core import tools
from torcms.torlite.core.base_handler import BaseHandler

from claslite.model.catalog_model import MCatalog
from claslite.model.infor_model import MInfor


class DeleteHandler(BaseHandler):
    def initialize(self, hinfo=''):
        self.init()
        self.minfo = MInfor()
        self.mcat = MCatalog()
        self.template_dir_name = 'claslite'

    def get(self, input=''):
        if len(input) == 36:
            inf = self.minfo.get_by_id(input)
            if inf is None:
                self.render('html/404.html')
                return
            self.delete(input)
        else:
            self.render('html/404.html')

    def is_editable(self, info):
        if self.userinfo.privilege[4] == '1':
            return True
        return False

    @tornado.web.authenticated
    def delete(self, infoid):
        post_data = self.minfo.get_by_id(infoid)
        if self.is_editable(post_data):
            if self.minfo.delete_by_uid(infoid):
                self.redirect('/list/{0}'.format(post_data['catid'][0]))
            else:
                return False
        else:
            return False
