#!/usr/bin/env python # -*- coding: utf-8 -*- """ demeter load author:rabin """ import tornado.web from demeter import * import json class Load(tornado.web.RequestHandler): KEYS = ('farm', 'admin') def initialize(self): self.data = {} self.setting() def setting(self): self.page() self.search() self.cookie() self.admin() self.assign() def get_current_user(self): return self.get_secure_cookie(self.KEYS[1]) def assign(self): self.data['setting'] = Demeter.config['setting'] def cookie(self): for key in self.KEYS: cookie = None if key in Demeter.config['base']: cookie = Demeter.config['base'][key] if not cookie: cookie = self.get_secure_cookie(key) #cookie = self.get_cookie(key) if not cookie: value = self.input(key) if value: #self.set_secure_cookie(key, value) Demeter.config['setting'][key] = value else: Demeter.config['setting'][key] = 0 else: Demeter.config['setting'][key] = cookie def admin(self): admin = 1 self.data['auth'] = True if 'admin' in Demeter.config['setting'] and Demeter.config['setting']['admin'] > 0: if 'adminInfo' not in Demeter.config['setting'] or ('adminInfo' in Demeter.config['setting'] and Demeter.config['setting']['admin'] != Demeter.config['setting']['adminInfo']): Demeter.config['setting']['adminInfo'] = self.service('common').one('manage_admin', id=Demeter.config['setting']['admin']) if Demeter.config['setting']['adminInfo']: Demeter.config['setting']['roleInfo'] = self.service('common').one('manage_role', id=Demeter.config['setting']['adminInfo']['role_id']) else: #self.redirect('/user/login') return if 'farm' in Demeter.config['setting'] and Demeter.config['setting']['farm'] > 0: if 'farmInfo' not in Demeter.config['setting'] or ('farmInfo' in Demeter.config['setting'] and Demeter.config['setting']['farm'] != Demeter.config['setting']['farmInfo']): Demeter.config['setting']['farmInfo'] = self.service('common').one('farm', id=Demeter.config['setting']['farm']) if not Demeter.config['setting']['farmInfo']: Demeter.config['setting']['farmInfo']['name'] = '默认' if 'menuList' not in Demeter.config['setting']: Demeter.config['setting']['menuList'] = self.menu() if Demeter.config['setting']['adminInfo']['id'] == admin: if 'menu' in Demeter.config['setting']: del Demeter.config['setting']['menu'] else: if 'menu' not in Demeter.config['setting'] and 'roleInfo' in Demeter.config['setting']: Demeter.config['setting']['menu'] = ['_/', '_/login', '_/main', '_/admin/log_update'] menu = Demeter.config['setting']['roleInfo']['auth'] + ',' for i,j in enumerate(Demeter.config['setting']['menuList'][1]): m = 0 for n in j[0]: if n+',' in menu: l = str(i)+'_'+str(n) Demeter.config['setting']['menu'].append(l) Demeter.config['setting']['menu'].append(l + '_update') m = m + 1 Demeter.config['setting']['menu'] = ',' + ",".join(Demeter.config['setting']['menu'])+',' uri = '_' + self.request.uri + ',' if Demeter.config['setting']['admin'] != admin and uri not in Demeter.config['setting']['menu']: self.data['auth'] = False if 'farmList' not in Demeter.config['setting']: Demeter.config['setting']['farmList'] = self.service('common').list('farm') def page(self): Demeter.config['page'] = {} page = self.input('page') if page: Demeter.config['page']['ajax'] = True else: Demeter.config['page']['ajax'] = False page = 1 Demeter.config['page']['current'] = page Demeter.config['page']['total'] = 0 self.data['page'] = Demeter.config['page'] def search(self): data = self.request.arguments self.data['search'] = {} self.data['update'] = {} for key in data: if 'search_' in key: index = key.replace('search_', '') self.data['search'][index] = ",".join(data[key]) if 'update_' in key: index = key.replace('update_', '') self.data['update'][index] = ",".join(data[key]) def input(self, key, value=None): return self.get_argument(key, value) def service(self, name): return Demeter.service(name) def model(self, name): return Demeter.model(name) def common(self, **kwd): self.data['common'] = kwd farm = str(Demeter.config['setting']['farm']) self.data['common']['argvs'] = '&farm=' + farm + '&search_farm_id-select-=' + farm def commonView(self, name): self.view('common/'+name+'.html') def commonList(self, model): self.data['state'] = self.input('state', True) self.data['list'] = self.service('common').list(model, state=self.data['state'], search=self.data['search'], page=True) def commonOne(self, model, **kwd): id = self.input('id') self.data['info'] = {} if id: kwd['id'] = id if kwd: self.data['info'] = self.service('common').one(model, **kwd) if not self.data['info'] and Demeter.config['setting']['farm'] > 0: self.data['info']['farm_id'] = Demeter.config['setting']['farm'] def commonUpdate(self, model, msg='', id=0, **kwd): if not self.data['auth']: self.auth() else: if id <= 0: id = self.input('id') if kwd: info = self.service('common').one(model, **kwd) if id: id = int(id) if info and (id != info['id']): self.out(msg) return state = self.service('common').update(model, id, self.data['update']) self.log(model, 'update', self.data['update']) self.out('yes', {'id':state}) return state def commonDelete(self, model): if not self.data['auth']: self.auth() else: id = self.input('id') state = self.input('state', False) state = self.service('common').delete(model, id, state) self.log(model, 'delete', {id:id, state:state}) self.out('yes', {'state':state}) def log(self, model, method, data): insert = {} insert['admin_id'] = Demeter.config['setting']['admin'] insert['model'] = model insert['method'] = method insert['data'] = json.dumps(data) self.service('common').update('manage_log', None, insert) def view(self, name): if not self.data['auth']: self.auth() else: self.render(name, data=self.data) def auth(self): self.out('您没有权限') def out(self, msg='', data={}, code=0): callback = self.input('callback') function = self.input('function') result = '' send = {} send['status'] = 1 send['msg'] = msg send['data'] = data send['code'] = code if not send['data']: send['status'] = 2 result = json.dumps(send) if callback: result = callback + '(' + result + ')' elif function: result = ''; self.write(result) #self.finish(result) def menu(self): parent = [['农场与设备管理', '网站管理', '基础设置'],['', '', '']] child = [ [['/farm/farm','/device/gateway','/device/info','/work/work','/work/category','/work/land','/origin/batch','/farm/user','/device/printer','/msg/msg'],['农场列表', '网关管理', '设备管理', '农场农事管理', '农场农事方式', '农场地块管理', '溯源批次管理','农场用户管理','农场打印机','消息提醒']] ,[['/content/info','/content/article','/content/category', '/content/set', '/sms/sms'],['首页内容管理', '文章管理', '文章分类', '网站基础设置', '短信通知']] ,[['/device/type','/hardware/type','/msg/type','/admin/admin','/admin/role','/hardware/hardware', '/admin/log'],['设备类型管理','硬件类型管理', '消息类型设置', '管理员设置', '管理权限设置','硬件设备记录', '系统日志']] ] return (parent,child)