origin.py 7.4 KB

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