MealManage.py 13 KB

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