MealManage.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/5/29 17:07
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: MealManage.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. from django.views.generic.base import View
  15. from django.utils.decorators import method_decorator
  16. from django.views.decorators.csrf import csrf_exempt
  17. from Service.ModelService import ModelService
  18. from Service.CommonService import CommonService
  19. from Model.models import Store_Meal
  20. import traceback
  21. from django.utils import timezone
  22. from Object.TokenObject import TokenObject
  23. from Object.ResponseObject import ResponseObject
  24. '''
  25. http://192.168.136.40:8077/meal/manage?operation=add&token=local&title=套餐A&price=$199&content=存7天&day=7&id=1
  26. http://192.168.136.45:8077/meal/manage?operation=update&token=test&id=1&title=套餐A&price=$199&content=存3天&day=7
  27. http://192.168.136.40:8077/meal/manage?operation=query&token=test&page=1&line=10
  28. http://192.168.136.40:8077/meal/manage?operation=delete&token=test&id=1&id=2&id=3&id=4&id=5
  29. '''
  30. class MealManage(View):
  31. @method_decorator(csrf_exempt)
  32. def dispatch(self, *args, **kwargs):
  33. return super(MealManage, self).dispatch(*args, **kwargs)
  34. def get(self, request, *args, **kwargs):
  35. request.encoding = 'utf-8'
  36. return self.validation(request_dict=request.GET)
  37. def post(self, request, *args, **kwargs):
  38. request.encoding = 'utf-8'
  39. return self.validation(request_dict=request.POST)
  40. def validation(self, request_dict, *args, **kwargs):
  41. response = ResponseObject()
  42. token = request_dict.get('token', None)
  43. operation = request_dict.get('operation', None)
  44. if token is None:
  45. return response.json(309)
  46. tko = TokenObject(token)
  47. tko.valid()
  48. response.lang = tko.lang
  49. if tko.code != 0:
  50. return response.json(tko.code)
  51. userID = tko.userID
  52. if userID is None:
  53. return response.json(309)
  54. if operation == 'query':
  55. return self.query(request_dict, response)
  56. elif operation == 'add':
  57. return self.add(request_dict, userID, response)
  58. elif operation == 'update':
  59. return self.update(request_dict, userID, response)
  60. elif operation == 'delete':
  61. return self.delete(request_dict, userID, response)
  62. elif operation == 'find':
  63. return self.find(request_dict, userID, response)
  64. else:
  65. return response.json(444, 'operation')
  66. def add(self, request_dict, userID, response):
  67. own_perm = ModelService.check_permission(userID=userID, permID=40)
  68. if own_perm is not True:
  69. return response.json(404)
  70. title = request_dict.get('title', None)
  71. id = request_dict.get('id', None)
  72. price = request_dict.get('price', None)
  73. content = request_dict.get('content', None)
  74. day = request_dict.get('day', None)
  75. storeDay = request_dict.get('storeDay', None)
  76. param_flag = CommonService.get_param_flag(data=[title, price, content])
  77. if param_flag is not True:
  78. return response.json(444,'title,id,price,content,day,storeDay')
  79. try:
  80. store_meal = Store_Meal(id=id,title=title,price=price,content=content,day=day,storeDay=storeDay)
  81. store_meal.save()
  82. except Exception:
  83. errorInfo = traceback.format_exc()
  84. print(errorInfo)
  85. return response.json(500, {'details': errorInfo})
  86. else:
  87. if store_meal.id:
  88. return response.json(0, {
  89. 'id': store_meal.id,
  90. 'title': store_meal.title,
  91. 'price': store_meal.price,
  92. 'content': store_meal.content,
  93. 'day': store_meal.day,
  94. 'add_time': str(store_meal.add_time),
  95. 'update_time': str(store_meal.update_time),
  96. 'storeDay':storeDay})
  97. def query(self, request_dict, response):
  98. page = int(request_dict.get('page', None))
  99. line = int(request_dict.get('line', None))
  100. if page is None or line is None:
  101. return response.json(444)
  102. queryset = Store_Meal.objects.all()
  103. if queryset.exists():
  104. count = queryset.count()
  105. res = queryset[(page - 1) * line:page * line]
  106. send_json = CommonService.qs_to_dict(res)
  107. send_json['count'] = count
  108. return response.json(0, send_json)
  109. return response.json(0)
  110. def update(self, request_dict, userID, response):
  111. own_perm = ModelService.check_permission(userID=userID, permID=30)
  112. if own_perm is True:
  113. id = request_dict.get('id', None)
  114. title = request_dict.get('title', None)
  115. price = request_dict.get('price', None)
  116. day = request_dict.get('day', None)
  117. storeDay = request_dict.get('storeDay', None)
  118. content = request_dict.get('content', None)
  119. param_flag = CommonService.get_param_flag(
  120. data=[id, title, price, content, day])
  121. if param_flag is True:
  122. try:
  123. store_meal = Store_Meal.objects.get(id=id)
  124. except Exception:
  125. errorInfo = traceback.format_exc()
  126. print(errorInfo)
  127. return response.json(424, {'details': errorInfo})
  128. else:
  129. if store_meal.id:
  130. now_time = timezone.localtime(timezone.now())
  131. print(now_time)
  132. store_meal.title = title
  133. store_meal.price = price
  134. store_meal.content = content
  135. store_meal.storeDay = storeDay
  136. store_meal.day = day
  137. store_meal.save()
  138. return response.json(0, {'update_id': store_meal.id,
  139. 'update_time': str(now_time)})
  140. else:
  141. return response.json(444)
  142. else:
  143. return response.json(404)
  144. def delete(self, request_dict, userID, response):
  145. own_perm = ModelService.check_permission(userID=userID, permID=10)
  146. if own_perm is True:
  147. id_list = request_dict.getlist('id', None)
  148. param_flag = CommonService.get_param_flag(data=[id_list])
  149. if param_flag is True:
  150. try:
  151. for id in id_list:
  152. Store_Meal.objects.filter(id=id).delete()
  153. except Exception as e:
  154. errorInfo = traceback.format_exc()
  155. print(errorInfo)
  156. return response.json(424, {'details': repr(e)})
  157. else:
  158. return response.json(0)
  159. else:
  160. return response.json(444)
  161. else:
  162. return response.json(404)
  163. def find(self, request_dict, userID, response):
  164. own_perm = ModelService.check_permission(userID=userID, permID=30)
  165. if own_perm is True:
  166. page = int(request_dict.get('page', None))
  167. line = int(request_dict.get('line', None))
  168. param_flag = CommonService.get_param_flag(data=[page, line])
  169. if param_flag is True:
  170. queryset = Store_Meal.objects.all()
  171. if queryset.exists():
  172. count = queryset.count()
  173. res = queryset[(page - 1) * line:page * line]
  174. send_json = CommonService.qs_to_dict(res)
  175. send_json['count'] = count
  176. return response.json(0, send_json)
  177. return response.json(0)
  178. else:
  179. return response.json(444)
  180. else:
  181. return response.json(404)
  182. '''
  183. 用户获取全部套餐信息
  184. http://192.168.136.40:8077/meal/list?token=local
  185. '''
  186. class MealView(View):
  187. @method_decorator(csrf_exempt)
  188. def dispatch(self, *args, **kwargs):
  189. return super(MealView, self).dispatch(*args, **kwargs)
  190. def get(self, request, *args, **kwargs):
  191. request.encoding = 'utf-8'
  192. operation = kwargs.get('operation')
  193. return self.validation(request.GET, request, operation)
  194. def post(self, request, *args, **kwargs):
  195. request.encoding = 'utf-8'
  196. operation = kwargs.get('operation')
  197. return self.validation(request.POST, request, operation)
  198. def validation(self, request_dict, request, operation):
  199. response = ResponseObject()
  200. if operation is None:
  201. return response.json(444, 'error path')
  202. token = request_dict.get('token', None)
  203. # 设备主键uid
  204. tko = TokenObject(token)
  205. tko.valid()
  206. response.lang = tko.lang
  207. if tko.code != 0:
  208. return response.json(tko.code)
  209. userID = tko.userID
  210. if operation == 'list':
  211. return self.do_query_list(response)
  212. else:
  213. return response.json(414)
  214. def do_query_list(self, response):
  215. qs = Store_Meal.objects.all().values("id","title","content","price","day","storeDay")
  216. if qs.exists():
  217. # res = CommonService.qs_to_list(qs)
  218. return response.json(0, list(qs))
  219. else:
  220. return response.json(0, [])