MealManage.py 15 KB

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