MealManage.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. import traceback
  15. from django.utils import timezone
  16. from django.utils.decorators import method_decorator
  17. from django.views.decorators.csrf import csrf_exempt
  18. from django.views.generic.base import View
  19. from Model.models import Store_Meal, VodBucketModel, Pay_Type
  20. from Object.ResponseObject import ResponseObject
  21. from Object.TokenObject import TokenObject
  22. from Service.CommonService import CommonService
  23. from Service.ModelService import ModelService
  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. response.lang = tko.lang
  48. if tko.code != 0:
  49. return response.json(tko.code)
  50. userID = tko.userID
  51. if userID is None:
  52. return response.json(309)
  53. if operation == 'query':
  54. return self.query(request_dict, response)
  55. elif operation == 'add':
  56. return self.add(request_dict, userID, response)
  57. elif operation == 'update':
  58. return self.update(request_dict, userID, response)
  59. elif operation == 'delete':
  60. return self.delete(request_dict, userID, response)
  61. elif operation == 'find':
  62. return self.find(request_dict, userID, response)
  63. else:
  64. return response.json(444, 'operation')
  65. def add(self, request_dict, userID, response):
  66. title = request_dict.get('title', None)
  67. id = request_dict.get('id', None)
  68. price = request_dict.get('price', None)
  69. content = request_dict.get('content', None)
  70. day = request_dict.get('day', None)
  71. currency = request_dict.get('currency', None)
  72. bucketID = request_dict.get('bucketID', None)
  73. paytype = request_dict.get('paytype', None)
  74. virtual_price = request_dict.get('virtual_price', None)
  75. is_discounts = request_dict.get('is_discounts', None)
  76. discount_price = request_dict.get('discount_price', None)
  77. discount_content = request_dict.get('discount_content', None)
  78. expire = request_dict.get('expire', None)
  79. if not title or not id or not price or not day or not content:
  80. return response.json(444, 'title,id,price,content,day,bucketID')
  81. own_perm = ModelService.check_perm(userID=userID, permID=40)
  82. if own_perm is not True:
  83. return response.json(404)
  84. try:
  85. bucketQs = VodBucketModel.objects.filter(id=bucketID)
  86. if Store_Meal.objects.filter(id=id):
  87. return response.json(10, '已存在')
  88. store_meal = Store_Meal(id=id, title=title, price=price, content=content, day=day, bucket_id=bucketID,
  89. currency=currency, virtual_price=virtual_price, is_discounts=is_discounts,
  90. discount_price=discount_price, discount_content=discount_content, expire=expire)
  91. store_meal.save()
  92. paytype = paytype.split(',')
  93. if len(paytype) > 0:
  94. Store_Meal.objects.get(id=id).pay_type.set(paytype)
  95. except Exception:
  96. errorInfo = traceback.format_exc()
  97. print(errorInfo)
  98. return response.json(500, {'details': errorInfo})
  99. else:
  100. if store_meal.id:
  101. return response.json(0, {
  102. 'bucket__bucket': bucketQs[0].bucket,
  103. 'bucket__storeDay': bucketQs[0].storeDay,
  104. 'id': id,
  105. 'title': title,
  106. 'price': price,
  107. 'content': content,
  108. 'currency': currency,
  109. 'day': day,
  110. 'add_time': str(store_meal.add_time),
  111. 'update_time': str(store_meal.update_time)})
  112. def query(self, request_dict, response):
  113. page = int(request_dict.get('page', None))
  114. line = int(request_dict.get('line', None))
  115. if page is None or line is None:
  116. return response.json(444)
  117. qs = Store_Meal.objects.values("id", "title", "price", "content", "day", "add_time", "update_time", "currency"
  118. , "bucket_id", "bucket__bucket", "bucket__area", "commodity_type", "commodity_code",
  119. "bucket__storeDay", "virtual_price", "is_discounts", "discount_price", "discount_content", "expire")
  120. res = {}
  121. if qs.exists():
  122. ql = list(qs)
  123. from operator import itemgetter
  124. from itertools import groupby
  125. ql.sort(key=itemgetter('bucket__area'))
  126. ql=CommonService.qs_to_list(ql[(page - 1) * line:page * line])
  127. for area, items in groupby(ql, key=itemgetter('bucket__area')):
  128. items_list = list(items)
  129. for key, val in enumerate(items_list):
  130. pay_types = Pay_Type.objects.filter(store_meal=items_list[key]['id']).values("id", "payment")
  131. items_list[key]['pay_type'] = list(pay_types)
  132. res['count'] = len(ql)
  133. res['data'] = items_list
  134. return response.json(0, res)
  135. def update(self, request_dict, userID, response):
  136. id = request_dict.get('id', None)
  137. title = request_dict.get('title', None)
  138. price = request_dict.get('price', None)
  139. day = request_dict.get('day', None)
  140. content = request_dict.get('content', None)
  141. currency = request_dict.get('currency', None)
  142. bucketID = request_dict.get('bucketID', None)
  143. commodity_type = request_dict.get('commodity_type', None)
  144. commodity_code = request_dict.get('commodity_code', None)
  145. virtual_price = request_dict.get('virtual_price', None)
  146. is_discounts = request_dict.get('is_discounts', None)
  147. discount_price = request_dict.get('discount_price', None)
  148. discount_content = request_dict.get('discount_content', None)
  149. expire = request_dict.get('expire', None)
  150. type = request_dict.get('type', None)
  151. if not id or not title or not price or not content or not day or not type:
  152. return response.json(444, 'id, title, price, content, day,type')
  153. own_perm = ModelService.check_perm(userID=userID, permID=30)
  154. if own_perm is not True:
  155. return response.json(404)
  156. try:
  157. store_meal = Store_Meal.objects.get(id=id)
  158. now_time = timezone.localtime(timezone.now())
  159. print(now_time)
  160. store_meal.title = title
  161. store_meal.price = price
  162. store_meal.content = content
  163. store_meal.commodity_type = commodity_type
  164. store_meal.commodity_code = commodity_code
  165. store_meal.virtual_price = virtual_price
  166. store_meal.is_discounts = is_discounts
  167. store_meal.discount_price = discount_price
  168. store_meal.discount_content = discount_content
  169. store_meal.expire = expire
  170. store_meal.day = day
  171. if bucketID:
  172. store_meal.bucket_id = bucketID
  173. if currency:
  174. store_meal.currency = currency
  175. store_meal.save()
  176. type = type.split(',')
  177. if len(type) > 0:
  178. Store_Meal.objects.get(id=id).pay_type.set(type)
  179. else:
  180. Store_Meal.objects.get(id=id).pay_type.clear()
  181. except Exception:
  182. errorInfo = traceback.format_exc()
  183. print(errorInfo)
  184. return response.json(424, {'details': errorInfo})
  185. else:
  186. return response.json(0, {'update_id': store_meal.id, 'update_time': str(now_time)})
  187. def delete(self, request_dict, userID, response):
  188. id_list = request_dict.getlist('id', None)
  189. if not id_list:
  190. return response.json(444, 'id')
  191. own_perm = ModelService.check_perm(userID=userID, permID=10)
  192. if own_perm is not True:
  193. return response.json(404)
  194. try:
  195. for id in id_list:
  196. Store_Meal.objects.filter(id=id).delete()
  197. except Exception as e:
  198. errorInfo = traceback.format_exc()
  199. print(errorInfo)
  200. return response.json(424, {'details': repr(e)})
  201. else:
  202. return response.json(0)
  203. def find(self, request_dict, userID, response):
  204. page = int(request_dict.get('page', None))
  205. line = int(request_dict.get('line', None))
  206. if not page or not line:
  207. return response.json(444, 'page,line')
  208. own_perm = ModelService.check_perm(userID=userID, permID=30)
  209. if own_perm is not True:
  210. return response.json(404)
  211. qs = Store_Meal.objects.all()
  212. if qs.exists():
  213. count = qs.count()
  214. res = qs[(page - 1) * line:page * line]
  215. send_json = CommonService.qs_to_dict(res)
  216. send_json['count'] = count
  217. return response.json(0, send_json)
  218. return response.json(0)
  219. '''
  220. 用户获取全部套餐信息
  221. http://192.168.136.40:8077/meal/list?token=local
  222. '''
  223. class MealView(View):
  224. @method_decorator(csrf_exempt)
  225. def dispatch(self, *args, **kwargs):
  226. return super(MealView, self).dispatch(*args, **kwargs)
  227. def get(self, request, *args, **kwargs):
  228. request.encoding = 'utf-8'
  229. operation = kwargs.get('operation')
  230. return self.validation(request.GET, request, operation)
  231. def post(self, request, *args, **kwargs):
  232. request.encoding = 'utf-8'
  233. operation = kwargs.get('operation')
  234. return self.validation(request.POST, request, operation)
  235. def validation(self, request_dict, request, operation):
  236. response = ResponseObject()
  237. if operation is None:
  238. return response.json(444, 'error path')
  239. token = request_dict.get('token', None)
  240. # 设备主键uid
  241. tko = TokenObject(token)
  242. response.lang = tko.lang
  243. if tko.code != 0:
  244. return response.json(tko.code)
  245. elif operation == 'query':
  246. return self.do_query(request_dict, response)
  247. else:
  248. return response.json(414)
  249. def do_query(self, request_dict, response):
  250. mold = request_dict.get('request_dict', None)
  251. if mold:
  252. qs = Store_Meal.objects.filter(bucket__mold=1). \
  253. values("id", "title", "content", "price", "day", "currency",
  254. "bucket__storeDay", "bucket__bucket", "bucket__area",
  255. "type")
  256. else:
  257. qs = Store_Meal.objects.all(). \
  258. values("id", "title", "content", "price", "day", "currency",
  259. "bucket__storeDay", "bucket__bucket", "bucket__area",
  260. "type")
  261. if qs.exists():
  262. ql = list(qs)
  263. from operator import itemgetter
  264. from itertools import groupby
  265. ql.sort(key=itemgetter('bucket__area'))
  266. res = []
  267. for area, items in groupby(ql, key=itemgetter('bucket__area')):
  268. res_c = {'area': area, 'items': list(items)}
  269. res.append(res_c)
  270. result = {
  271. 'meals': res,
  272. 'extra':
  273. {
  274. 'cloud_banner': 'https://www.dvema.com/web/images/cloud_cn_banner.png',
  275. 'cloud_en_baner': 'https://www.dvema.com/web/images/cloud_en_banner.png'
  276. }
  277. }
  278. return response.json(0, result)
  279. else:
  280. return response.json(0)