__load__.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter load
  5. author:rabin
  6. """
  7. import tornado.web
  8. from demeter import *
  9. import json
  10. class Load(tornado.web.RequestHandler):
  11. KEYS = ('farm', 'admin')
  12. def initialize(self):
  13. self.data = {}
  14. self.setting()
  15. def setting(self):
  16. self.page()
  17. self.search()
  18. self.cookie()
  19. self.admin()
  20. self.assign()
  21. def get_current_user(self):
  22. return self.get_secure_cookie(self.KEYS[1])
  23. def assign(self):
  24. self.data['setting'] = Demeter.config['setting']
  25. def cookie(self):
  26. for key in self.KEYS:
  27. cookie = None
  28. if key in Demeter.config['base']:
  29. cookie = Demeter.config['base'][key]
  30. if not cookie:
  31. cookie = self.get_secure_cookie(key)
  32. #cookie = self.get_cookie(key)
  33. if not cookie:
  34. value = self.input(key)
  35. if value:
  36. #self.set_secure_cookie(key, value)
  37. Demeter.config['setting'][key] = value
  38. else:
  39. Demeter.config['setting'][key] = 0
  40. else:
  41. Demeter.config['setting'][key] = cookie
  42. def admin(self):
  43. admin = 1
  44. self.data['auth'] = True
  45. if 'admin' in Demeter.config['setting'] and Demeter.config['setting']['admin'] > 0:
  46. if 'adminInfo' not in Demeter.config['setting'] or ('adminInfo' in Demeter.config['setting'] and Demeter.config['setting']['admin'] != Demeter.config['setting']['adminInfo']):
  47. Demeter.config['setting']['adminInfo'] = self.service('common').one('manage_admin', id=Demeter.config['setting']['admin'])
  48. if Demeter.config['setting']['adminInfo']:
  49. Demeter.config['setting']['roleInfo'] = self.service('common').one('manage_role', id=Demeter.config['setting']['adminInfo']['role_id'])
  50. else:
  51. #self.redirect('/user/login')
  52. return
  53. if 'farm' in Demeter.config['setting'] and Demeter.config['setting']['farm'] > 0:
  54. if 'farmInfo' not in Demeter.config['setting'] or ('farmInfo' in Demeter.config['setting'] and Demeter.config['setting']['farm'] != Demeter.config['setting']['farmInfo']):
  55. Demeter.config['setting']['farmInfo'] = self.service('common').one('farm', id=Demeter.config['setting']['farm'])
  56. if not Demeter.config['setting']['farmInfo']:
  57. Demeter.config['setting']['farmInfo']['name'] = '默认'
  58. if 'menuList' not in Demeter.config['setting']:
  59. Demeter.config['setting']['menuList'] = self.menu()
  60. if Demeter.config['setting']['adminInfo']['id'] == admin:
  61. if 'menu' in Demeter.config['setting']:
  62. del Demeter.config['setting']['menu']
  63. else:
  64. if 'menu' not in Demeter.config['setting'] and 'roleInfo' in Demeter.config['setting']:
  65. Demeter.config['setting']['menu'] = ['_/', '_/login', '_/main', '_/admin/log_update']
  66. menu = Demeter.config['setting']['roleInfo']['auth'] + ','
  67. for i,j in enumerate(Demeter.config['setting']['menuList'][1]):
  68. m = 0
  69. for n in j[0]:
  70. if n+',' in menu:
  71. l = str(i)+'_'+str(n)
  72. Demeter.config['setting']['menu'].append(l)
  73. Demeter.config['setting']['menu'].append(l + '_update')
  74. m = m + 1
  75. Demeter.config['setting']['menu'] = ',' + ",".join(Demeter.config['setting']['menu'])+','
  76. uri = '_' + self.request.uri + ','
  77. if Demeter.config['setting']['admin'] != admin and uri not in Demeter.config['setting']['menu']:
  78. self.data['auth'] = False
  79. if 'farmList' not in Demeter.config['setting']:
  80. Demeter.config['setting']['farmList'] = self.service('common').list('farm')
  81. def page(self):
  82. Demeter.config['page'] = {}
  83. page = self.input('page')
  84. if page:
  85. Demeter.config['page']['ajax'] = True
  86. else:
  87. Demeter.config['page']['ajax'] = False
  88. page = 1
  89. Demeter.config['page']['current'] = page
  90. Demeter.config['page']['total'] = 0
  91. self.data['page'] = Demeter.config['page']
  92. def search(self):
  93. data = self.request.arguments
  94. self.data['search'] = {}
  95. self.data['update'] = {}
  96. for key in data:
  97. if 'search_' in key:
  98. index = key.replace('search_', '')
  99. self.data['search'][index] = ",".join(data[key])
  100. if 'update_' in key:
  101. index = key.replace('update_', '')
  102. self.data['update'][index] = ",".join(data[key])
  103. def input(self, key, value=None):
  104. return self.get_argument(key, value)
  105. def service(self, name):
  106. return Demeter.service(name)
  107. def model(self, name):
  108. return Demeter.model(name)
  109. def common(self, **kwd):
  110. self.data['common'] = kwd
  111. farm = str(Demeter.config['setting']['farm'])
  112. self.data['common']['argvs'] = '&farm=' + farm + '&search_farm_id-select-=' + farm
  113. def commonView(self, name):
  114. self.view('common/'+name+'.html')
  115. def commonList(self, model):
  116. self.data['state'] = self.input('state', True)
  117. self.data['list'] = self.service('common').list(model, state=self.data['state'], search=self.data['search'], page=True)
  118. def commonOne(self, model, **kwd):
  119. id = self.input('id')
  120. self.data['info'] = {}
  121. if id:
  122. kwd['id'] = id
  123. if kwd:
  124. self.data['info'] = self.service('common').one(model, **kwd)
  125. if not self.data['info'] and Demeter.config['setting']['farm'] > 0:
  126. self.data['info']['farm_id'] = Demeter.config['setting']['farm']
  127. def commonUpdate(self, model, msg='', id=0, **kwd):
  128. if not self.data['auth']:
  129. self.auth()
  130. else:
  131. if id <= 0:
  132. id = self.input('id')
  133. if kwd:
  134. info = self.service('common').one(model, **kwd)
  135. if id:
  136. id = int(id)
  137. if info and (id != info['id']):
  138. self.out(msg)
  139. return
  140. state = self.service('common').update(model, id, self.data['update'])
  141. self.log(model, 'update', self.data['update'])
  142. self.out('yes', {'id':state})
  143. return state
  144. def commonDelete(self, model):
  145. if not self.data['auth']:
  146. self.auth()
  147. else:
  148. id = self.input('id')
  149. state = self.input('state', False)
  150. state = self.service('common').delete(model, id, state)
  151. self.log(model, 'delete', {id:id, state:state})
  152. self.out('yes', {'state':state})
  153. def log(self, model, method, data):
  154. insert = {}
  155. insert['admin_id'] = Demeter.config['setting']['admin']
  156. insert['model'] = model
  157. insert['method'] = method
  158. insert['data'] = json.dumps(data)
  159. self.service('common').update('manage_log', None, insert)
  160. def view(self, name):
  161. if not self.data['auth']:
  162. self.auth()
  163. else:
  164. self.render(name, data=self.data)
  165. def auth(self):
  166. self.out('您没有权限')
  167. def out(self, msg='', data={}, code=0):
  168. callback = self.input('callback')
  169. function = self.input('function')
  170. result = ''
  171. send = {}
  172. send['status'] = 1
  173. send['msg'] = msg
  174. send['data'] = data
  175. send['code'] = code
  176. if not send['data']:
  177. send['status'] = 2
  178. result = json.dumps(send)
  179. if callback:
  180. result = callback + '(' + result + ')'
  181. elif function:
  182. result = '<script>parent.' + function + '(' + result + ')' + '</script>';
  183. self.write(result)
  184. #self.finish(result)
  185. def menu(self):
  186. parent = [['农场与设备管理', '网站管理', '基础设置'],['&#xe62e;', '&#xe62a;', '&#xe614;']]
  187. child = [
  188. [['/farm/farm','/device/gateway','/device/info','/work/work','/work/category','/work/land','/origin/batch','/farm/user','/device/printer','/msg/msg'],['农场列表', '网关管理', '设备管理', '农场农事管理', '农场农事方式', '农场地块管理', '溯源批次管理','农场用户管理','农场打印机','消息提醒']]
  189. ,[['/content/info','/content/article','/content/category', '/content/set', '/sms/sms'],['首页内容管理', '文章管理', '文章分类', '网站基础设置', '短信通知']]
  190. ,[['/device/type','/hardware/type','/msg/type','/admin/admin','/admin/role','/hardware/hardware', '/admin/log'],['设备类型管理','硬件类型管理', '消息类型设置', '管理员设置', '管理权限设置','硬件设备记录', '系统日志']]
  191. ]
  192. return (parent,child)