123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
- @AUTHOR: ASJRD018
- @NAME: Ansjer
- @software: PyCharm
- @DATE: 2018/5/29 17:07
- @Version: python3.6
- @MODIFY DECORD:ansjer dev
- @file: MealManage.py
- @Contact: chanjunkai@163.com
- """
- from django.views.generic.base import View
- from django.utils.decorators import method_decorator
- from django.views.decorators.csrf import csrf_exempt
- from Service.ModelService import ModelService
- from Service.CommonService import CommonService
- from Model.models import Store_Meal
- import traceback
- from django.utils import timezone
- from Object.TokenObject import TokenObject
- from Object.ResponseObject import ResponseObject
- '''
- http://192.168.136.40:8077/meal/manage?operation=add&token=local&title=套餐A&price=$199&content=存7天&day=7&id=1
- http://192.168.136.45:8077/meal/manage?operation=update&token=test&id=1&title=套餐A&price=$199&content=存3天&day=7
- http://192.168.136.40:8077/meal/manage?operation=query&token=test&page=1&line=10
- http://192.168.136.40:8077/meal/manage?operation=delete&token=test&id=1&id=2&id=3&id=4&id=5
- '''
- class MealManage(View):
- @method_decorator(csrf_exempt)
- def dispatch(self, *args, **kwargs):
- return super(MealManage, self).dispatch(*args, **kwargs)
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- return self.validation(request_dict=request.GET)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- return self.validation(request_dict=request.POST)
- def validation(self, request_dict, *args, **kwargs):
- response = ResponseObject()
- token = request_dict.get('token', None)
- operation = request_dict.get('operation', None)
- if token is None:
- return response.json(309)
- tko = TokenObject(token)
- tko.valid()
- response.lang = tko.lang
- if tko.code != 0:
- return response.json(tko.code)
- userID = tko.userID
- if userID is None:
- return response.json(309)
- if operation == 'query':
- return self.query(request_dict, response)
- elif operation == 'add':
- return self.add(request_dict, userID, response)
- elif operation == 'update':
- return self.update(request_dict, userID, response)
- elif operation == 'delete':
- return self.delete(request_dict, userID, response)
- elif operation == 'find':
- return self.find(request_dict, userID, response)
- else:
- return response.json(444, 'operation')
- def add(self, request_dict, userID, response):
- own_perm = ModelService.check_permission(userID=userID, permID=40)
- if own_perm is not True:
- return response.json(404)
- title = request_dict.get('title', None)
- id = request_dict.get('id', None)
- price = request_dict.get('price', None)
- content = request_dict.get('content', None)
- day = request_dict.get('day', None)
- storeDay = request_dict.get('storeDay', None)
- param_flag = CommonService.get_param_flag(data=[title, price, content])
- if param_flag is not True:
- return response.json(444,'title,id,price,content,day,storeDay')
- try:
- store_meal = Store_Meal(id=id,title=title,price=price,content=content,day=day,storeDay=storeDay)
- store_meal.save()
- except Exception:
- errorInfo = traceback.format_exc()
- print(errorInfo)
- return response.json(500, {'details': errorInfo})
- else:
- if store_meal.id:
- return response.json(0, {
- 'id': store_meal.id,
- 'title': store_meal.title,
- 'price': store_meal.price,
- 'content': store_meal.content,
- 'day': store_meal.day,
- 'add_time': str(store_meal.add_time),
- 'update_time': str(store_meal.update_time),
- 'storeDay':storeDay})
- def query(self, request_dict, response):
- page = int(request_dict.get('page', None))
- line = int(request_dict.get('line', None))
- if page is None or line is None:
- return response.json(444)
- queryset = Store_Meal.objects.all()
- if queryset.exists():
- count = queryset.count()
- res = queryset[(page - 1) * line:page * line]
- send_json = CommonService.qs_to_dict(res)
- send_json['count'] = count
- return response.json(0, send_json)
- return response.json(0)
- def update(self, request_dict, userID, response):
- own_perm = ModelService.check_permission(userID=userID, permID=30)
- if own_perm is True:
- id = request_dict.get('id', None)
- title = request_dict.get('title', None)
- price = request_dict.get('price', None)
- day = request_dict.get('day', None)
- storeDay = request_dict.get('storeDay', None)
- content = request_dict.get('content', None)
- param_flag = CommonService.get_param_flag(
- data=[id, title, price, content, day])
- if param_flag is True:
- try:
- store_meal = Store_Meal.objects.get(id=id)
- except Exception:
- errorInfo = traceback.format_exc()
- print(errorInfo)
- return response.json(424, {'details': errorInfo})
- else:
- if store_meal.id:
- now_time = timezone.localtime(timezone.now())
- print(now_time)
- store_meal.title = title
- store_meal.price = price
- store_meal.content = content
- store_meal.storeDay = storeDay
- store_meal.day = day
- store_meal.save()
- return response.json(0, {'update_id': store_meal.id,
- 'update_time': str(now_time)})
- else:
- return response.json(444)
- else:
- return response.json(404)
- def delete(self, request_dict, userID, response):
- own_perm = ModelService.check_permission(userID=userID, permID=10)
- if own_perm is True:
- id_list = request_dict.getlist('id', None)
- param_flag = CommonService.get_param_flag(data=[id_list])
- if param_flag is True:
- try:
- for id in id_list:
- Store_Meal.objects.filter(id=id).delete()
- except Exception as e:
- errorInfo = traceback.format_exc()
- print(errorInfo)
- return response.json(424, {'details': repr(e)})
- else:
- return response.json(0)
- else:
- return response.json(444)
- else:
- return response.json(404)
- def find(self, request_dict, userID, response):
- own_perm = ModelService.check_permission(userID=userID, permID=30)
- if own_perm is True:
- page = int(request_dict.get('page', None))
- line = int(request_dict.get('line', None))
- param_flag = CommonService.get_param_flag(data=[page, line])
- if param_flag is True:
- queryset = Store_Meal.objects.all()
- if queryset.exists():
- count = queryset.count()
- res = queryset[(page - 1) * line:page * line]
- send_json = CommonService.qs_to_dict(res)
- send_json['count'] = count
- return response.json(0, send_json)
- return response.json(0)
- else:
- return response.json(444)
- else:
- return response.json(404)
- '''
- 用户获取全部套餐信息
- http://192.168.136.40:8077/meal/list?token=local
- '''
- class MealView(View):
- @method_decorator(csrf_exempt)
- def dispatch(self, *args, **kwargs):
- return super(MealView, self).dispatch(*args, **kwargs)
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.GET, request, operation)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- return self.validation(request.POST, request, operation)
- def validation(self, request_dict, request, operation):
- response = ResponseObject()
- if operation is None:
- return response.json(444, 'error path')
- token = request_dict.get('token', None)
- # 设备主键uid
- tko = TokenObject(token)
- tko.valid()
- response.lang = tko.lang
- if tko.code != 0:
- return response.json(tko.code)
- userID = tko.userID
- if operation == 'list':
- return self.do_query_list(response)
- else:
- return response.json(414)
- def do_query_list(self, response):
- qs = Store_Meal.objects.all().values("id","title","content","price","day","storeDay")
- if qs.exists():
- # res = CommonService.qs_to_list(qs)
- return response.json(0, list(qs))
- else:
- return response.json(0, [])
|