MealManage.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.shortcuts import HttpResponse
  15. from django.views.generic.base import View
  16. from django.utils.decorators import method_decorator
  17. from django.views.decorators.csrf import csrf_exempt
  18. from Service.TokenManager import JSONTokenManager
  19. from Service.ModelService import ModelService
  20. from Service.CommonService import CommonService
  21. from Model.models import Store_Meal
  22. import traceback
  23. from django.utils import timezone
  24. from Service.ResponseService import ResponseJSON
  25. '''
  26. http://192.168.136.40:8077/meal/manage?operation=add&token=test&title=套餐A&price=$199&content=存7天&day=7&id=1
  27. http://192.168.136.45:8077/meal/manage?operation=update&token=test&id=1&title=套餐A&price=$199&content=存3天&day=7
  28. http://192.168.136.40:8077/meal/manage?operation=query&token=test&page=1&line=10
  29. http://192.168.136.40:8077/meal/manage?operation=delete&token=test&id=1&id=2&id=3&id=4&id=5
  30. '''
  31. class MealManage(View):
  32. @method_decorator(csrf_exempt)
  33. def dispatch(self, *args, **kwargs):
  34. return super(MealManage, self).dispatch(*args, **kwargs)
  35. def get(self, request, *args, **kwargs):
  36. request.encoding = 'utf-8'
  37. return self.validation(request_dict=request.GET)
  38. def post(self, request, *args, **kwargs):
  39. request.encoding = 'utf-8'
  40. return self.validation(request_dict=request.POST)
  41. def validation(self, request_dict, *args, **kwargs):
  42. operation = request_dict.get('operation', None)
  43. if operation is not None:
  44. token = request_dict.get('token', None)
  45. if token is not None:
  46. tokenManager = JSONTokenManager()
  47. error_code = tokenManager.verify_AToken(token)
  48. if error_code == 0:
  49. userID = tokenManager.accessDict.get('userID', None)
  50. param_flag = CommonService.get_param_flag(data=[userID])
  51. if param_flag is True:
  52. if operation == 'query':
  53. return self.query(request_dict=request_dict)
  54. elif operation == 'add':
  55. return self.add(request_dict=request_dict, userID=userID)
  56. elif operation == 'update':
  57. return self.update(request_dict=request_dict, userID=userID)
  58. elif operation == 'delete':
  59. return self.delete(request_dict=request_dict, userID=userID)
  60. elif operation == 'find':
  61. return self.find(request_dict=request_dict, userID=userID)
  62. return ResponseJSON(444)
  63. else:
  64. return HttpResponse(tokenManager.errorCodeInfo(error_code))
  65. else:
  66. return ResponseJSON(311)
  67. else:
  68. return ResponseJSON(444)
  69. def add(self, request_dict, userID):
  70. own_perm = ModelService.check_permission(userID=userID, permID=40)
  71. if own_perm is True:
  72. title = request_dict.get('title', None)
  73. id = request_dict.get('id', None)
  74. price = request_dict.get('price', None)
  75. content = request_dict.get('content', None)
  76. day = request_dict.get('day', None)
  77. param_flag = CommonService.get_param_flag(data=[title, price, content])
  78. if param_flag is True:
  79. try:
  80. store_meal = Store_Meal(
  81. id = id,
  82. title=title,
  83. price=price,
  84. content=content,
  85. day=day,
  86. )
  87. store_meal.save()
  88. except Exception:
  89. errorInfo = traceback.format_exc()
  90. print(errorInfo)
  91. return ResponseJSON(500, {'details': errorInfo})
  92. else:
  93. if store_meal.id:
  94. return ResponseJSON(0,
  95. {
  96. 'id': store_meal.id,
  97. 'title': store_meal.title,
  98. 'price': store_meal.price,
  99. 'content': store_meal.content,
  100. 'day': store_meal.day,
  101. 'add_time': str(store_meal.add_time),
  102. 'update_time': str(store_meal.update_time),
  103. })
  104. return ResponseJSON(444)
  105. else:
  106. return ResponseJSON(404)
  107. def query(self, request_dict):
  108. page = int(request_dict.get('page', None))
  109. line = int(request_dict.get('line', None))
  110. param_flag = CommonService.get_param_flag(data=[page, line])
  111. if param_flag is True:
  112. queryset = Store_Meal.objects.all()
  113. if queryset.exists():
  114. count = queryset.count()
  115. res = queryset[(page - 1) * line:page * line]
  116. send_json = CommonService.query_set_to_dict(res)
  117. send_json['count'] = count
  118. return ResponseJSON(0, send_json)
  119. return ResponseJSON(0)
  120. else:
  121. return ResponseJSON(444)
  122. def update(self, request_dict, userID):
  123. own_perm = ModelService.check_permission(userID=userID, permID=30)
  124. if own_perm is True:
  125. id = request_dict.get('id', None)
  126. title = request_dict.get('title', None)
  127. price = request_dict.get('price', None)
  128. day = request_dict.get('day', None)
  129. content = request_dict.get('content', None)
  130. param_flag = CommonService.get_param_flag(
  131. data=[id, title, price, content,day])
  132. if param_flag is True:
  133. try:
  134. store_meal = Store_Meal.objects.get(id=id)
  135. except Exception:
  136. errorInfo = traceback.format_exc()
  137. print(errorInfo)
  138. return ResponseJSON(424, {'details': errorInfo})
  139. else:
  140. if store_meal.id:
  141. now_time = timezone.localtime(timezone.now())
  142. print(now_time)
  143. store_meal.title = title
  144. store_meal.price = price
  145. store_meal.content = content
  146. store_meal.day = day
  147. store_meal.save()
  148. return ResponseJSON(0, {'update_id': store_meal.id,
  149. 'update_time': str(now_time)})
  150. else:
  151. return ResponseJSON(444)
  152. else:
  153. return ResponseJSON(404)
  154. def delete(self, request_dict, userID):
  155. own_perm = ModelService.check_permission(userID=userID, permID=10)
  156. if own_perm is True:
  157. id_list = request_dict.getlist('id', None)
  158. param_flag = CommonService.get_param_flag(data=[id_list])
  159. if param_flag is True:
  160. try:
  161. for id in id_list:
  162. Store_Meal.objects.filter(id=id).delete()
  163. except Exception as e:
  164. errorInfo = traceback.format_exc()
  165. print(errorInfo)
  166. return ResponseJSON(424, {'details': repr(e)})
  167. else:
  168. return ResponseJSON(0)
  169. else:
  170. return ResponseJSON(444)
  171. else:
  172. return ResponseJSON(404)
  173. def find(self, request_dict, userID):
  174. own_perm = ModelService.check_permission(userID=userID, permID=30)
  175. if own_perm is True:
  176. page = int(request_dict.get('page', None))
  177. line = int(request_dict.get('line', None))
  178. param_flag = CommonService.get_param_flag(data=[page, line])
  179. if param_flag is True:
  180. queryset = Store_Meal.objects.all()
  181. if queryset.exists():
  182. count = queryset.count()
  183. res = queryset[(page - 1) * line:page * line]
  184. send_json = CommonService.query_set_to_dict(res)
  185. send_json['count'] = count
  186. return ResponseJSON(0, send_json)
  187. return ResponseJSON(0)
  188. else:
  189. return ResponseJSON(444)
  190. else:
  191. return ResponseJSON(404)