origin.py 7.5 KB

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