origin.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web page
  5. name:origin.py
  6. author:rabin
  7. """
  8. from __load__ import *
  9. #import qrcode
  10. from datetime import *
  11. import os
  12. import short_url
  13. import uuid
  14. import random
  15. class Code(object):
  16. @staticmethod
  17. def get(self, value):
  18. cdate = Demeter.date(value['cdate'], '%Y%m%d%H%M%S')
  19. #number = Demeter.compressUuid(value['id']).upper()
  20. min = 1000
  21. max = 9999
  22. number = 'XN' + str(value['farm_id'] + 10000) + '-' + str(value['cdate']) + str(random.randint(min, max))
  23. info = self.service('common').one('origin_batch', number=number)
  24. if info and info['id'] != value['id']:
  25. return Code.get(self, value)
  26. #url = Demeter.config['front']['qrcode'] + self.data['setting']['web'] + 'origin/view?qrcode=' + self.data['list'][key]['number']
  27. update = {}
  28. update['number'] = number
  29. state = self.service('common').update('origin_batch', value['id'], update)
  30. return number
  31. class index_path(Load):
  32. @Web.auth
  33. @Web.setting
  34. def get(self):
  35. mobile = Demeter.checkMobile(self.request)
  36. if mobile:
  37. self.redirect('/device')
  38. return
  39. self.data['list'] = self.service('common').list('origin_batch', search={'farm_id':self.data['setting']['farm']})
  40. if self.data['list']:
  41. for key, value in enumerate(self.data['list']):
  42. self.data['list'][key]['cdates'] = Demeter.date(value['cdate'], '%Y%m%d')
  43. self.data['list'][key]['zzdates'] = Demeter.date(value['zzdate'], '%Y-%m-%d')
  44. self.data['list'][key]['csdates'] = Demeter.date(value['csdate'], '%Y-%m-%d')
  45. product = self.service('common').one('farm_product', id=value['product_id'])
  46. if product:
  47. self.data['list'][key]['product'] = product['name']
  48. else:
  49. self.data['list'][key]['product'] = ''
  50. if not value['number']:
  51. self.data['list'][key]['number'] = Code.get(self, value)
  52. value['number'] = self.data['list'][key]['number']
  53. self.data['list'][key]['url'] = '/origin/view?id=' + value['id']
  54. self.view("origin/index.html")
  55. class view_path(Load):
  56. @Web.auth
  57. @Web.setting
  58. def get(self):
  59. id = self.input('id')
  60. self.data['info'] = self.service('common').one('origin_batch', id=id)
  61. #self.data['url'] = self.data['setting']['web'] + 'origin?id=' + id + '&code=' + self.data['info']['number']
  62. self.data['url'] = self.data['setting']['originurl'] + id + '&code=' + self.data['info']['number']
  63. self.data['qrcode'] = Demeter.config['front']['qrcode'] + self.data['url']
  64. self.view("origin/view.html")
  65. class update_path(Load):
  66. @Web.auth
  67. @Web.setting
  68. def get(self):
  69. id = self.input('id')
  70. copy_id = self.input('copy_id')
  71. if copy_id:
  72. id = copy_id
  73. self.data['product'] = self.service('common').list('farm_product', search={'status':True,'farm_id':self.data['setting']['farm']})
  74. self.data['land'] = self.service('common').list('farm_work_land', search={'farm_id':self.data['setting']['farm']})
  75. self.data['gateway'] = self.service('common').list('device_gateway', search={'farm_id':self.data['setting']['farm']})
  76. self.data['device'] = Device.getByGateway(self, search={'hardware_type' : 2,'farm_id':self.data['setting']['farm']})
  77. self.data['camera'] = self.service('common').list('device_info', search={'hardware_type': 6,'farm_id':self.data['setting']['farm']})
  78. self.data['info'] = {}
  79. self.data['growth'] = []
  80. if id:
  81. self.data['growth'] = self.service('common').list('origin_growth', search={'batch_id': id})
  82. if self.data['growth']:
  83. for key, value in enumerate(self.data['growth']):
  84. self.data['growth'][key]['date'] = Demeter.date(value['date'], '%Y-%m-%d')
  85. self.data['info'] = self.service('common').one('origin_batch', id=id)
  86. if self.data['info']:
  87. self.data['info']['zzdate'] = Demeter.date(self.data['info']['zzdate'], '%Y-%m-%d')
  88. self.data['info']['csdate'] = Demeter.date(self.data['info']['csdate'], '%Y-%m-%d')
  89. if copy_id:
  90. self.data['info']['id'] = None
  91. self.view("origin/update.html")
  92. @Web.auth
  93. @Web.setting
  94. def post(self):
  95. id = self.input('id')
  96. update = {}
  97. update['devices'] = self.inputs('update_devices')
  98. update['amount'] = self.input('update_amount')
  99. update['product_id'] = self.input('update_product_id')
  100. update['device_camera_id'] = self.input('update_device_camera_id')
  101. update['land_id'] = self.input('update_land_id')
  102. update['name'] = self.input('update_name')
  103. update['zzdate'] = self.input('update_zzdate')
  104. update['csdate'] = self.input('update_csdate')
  105. growth = {}
  106. growth['name'] = self.inputs('update_growth_name')
  107. growth['date'] = self.inputs('update_growth_date')
  108. growth['pic'] = self.inputs('update_growth_pic')
  109. growth['id'] = self.inputs('update_growth_id')
  110. if not update['name']:
  111. self.out(u'请输入批次名称')
  112. return
  113. if not update['product_id']:
  114. self.out(u'请选择产品')
  115. return
  116. if not update['land_id']:
  117. self.out(u'请选择地块')
  118. return
  119. if not update['devices']:
  120. self.out(u'请选择设备')
  121. return
  122. '''
  123. if not update['device_camera_id']:
  124. self.out(u'请选择摄像头')
  125. return
  126. '''
  127. if not update['zzdate'] or not update['csdate']:
  128. self.out(u'请选择生长周期')
  129. return
  130. if not growth['name'] or not growth['date'] or not growth['pic']:
  131. self.out(u'请添加生长阶段')
  132. return
  133. if not update['amount']:
  134. self.out(u'请输入本批产量')
  135. return
  136. update['devices'] = str(update['devices'])
  137. state = self.service('common').update('origin_batch', id, update)
  138. if state:
  139. model = Demeter.model('origin_growth')
  140. model.batch_id = state
  141. model.delete()
  142. for key,value in enumerate(growth['name']):
  143. if value and growth['date'][key]:
  144. gid = growth['id'][key]
  145. gupdate = {}
  146. gupdate['name'] = value
  147. gupdate['date'] = growth['date'][key]
  148. gupdate['pic'] = growth['pic'][key]
  149. gupdate['batch_id'] = state
  150. self.service('common').update('origin_growth', gid, gupdate)
  151. self.out('yes', {'state':state})
  152. @Web.auth
  153. @Web.setting
  154. def delete(self):
  155. id = self.input('id')
  156. if not id:
  157. self.out('no')
  158. return
  159. state = self.input('state', False)
  160. state = self.service('common').delete('origin_batch', id, state)
  161. self.out('yes', {'state':state})
  162. class printer_path(Load):
  163. @Web.auth
  164. @Web.setting
  165. def get(self):
  166. id = self.input('id')
  167. table_id = self.input('table_id');
  168. self.data['info'] = {}
  169. self.data['printer'] = self.service('common').list('device_printer', search={'farm_id':self.data['setting']['farm']})
  170. self.data['table_id'] = table_id
  171. if id:
  172. self.data['info'] = self.service('common').one('device_printer_log', id=id)
  173. self.view("origin/printer.html")
  174. @Web.auth
  175. @Web.setting
  176. def post(self):
  177. id = self.input('id')
  178. update = {}
  179. update['printer_id'] = self.input('update_printer_id')
  180. update['num'] = self.input('update_num')
  181. if not update['printer_id']:
  182. self.out(u'请选择打印机')
  183. return
  184. if not update['num']:
  185. self.out(u'请输入张数')
  186. return
  187. update['uid'] = self.data['setting']['userInfo']['id']
  188. update['table_name'] = 'origin_batch'
  189. update['table_id'] = self.input('update_table_id')
  190. if not update['table_id']:
  191. self.out(u'错误的来源')
  192. return
  193. info = self.service('common').one(update['table_name'], id=update['table_id'])
  194. update['content'] = '打印' + info['name'] + '溯源码:' + str(update['num']) + '张'
  195. state = self.service('common').update('device_printer_log', id, update)
  196. self.out('已发送至打印机队列')