| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 | #!/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"""import tracebackfrom django.utils import timezonefrom django.utils.decorators import method_decoratorfrom django.views.decorators.csrf import csrf_exemptfrom django.views.generic.base import Viewfrom Model.models import Store_Meal, VodBucketModelfrom Object.ResponseObject import ResponseObjectfrom Object.TokenObject import TokenObjectfrom Service.CommonService import CommonServicefrom Service.ModelService import ModelService'''http://192.168.136.40:8077/meal/manage?operation=add&token=local&title=套餐A&price=$199&content=存7天&day=7&id=1http://192.168.136.45:8077/meal/manage?operation=update&token=test&id=1&title=套餐A&price=$199&content=存3天&day=7http://192.168.136.40:8077/meal/manage?operation=query&token=test&page=1&line=10http://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)        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):        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)        currency = request_dict.get('currency', None)        bucketID = request_dict.get('bucketID', None)        if not title or not id or not price or not day or not content:            return response.json(444, 'title,id,price,content,day,bucketID')        own_perm = ModelService.check_perm(userID=userID, permID=40)        if own_perm is not True:            return response.json(404)        try:            bucketQs = VodBucketModel.objects.filter(id=bucketID)            if Store_Meal.objects.filter(id=id):                return response.json(10, '已存在')            store_meal = Store_Meal(id=id, title=title, price=price, content=content, day=day, bucket_id=bucketID,                                    currency=currency)            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, {                    'bucket__bucket': bucketQs[0].bucket,                    'bucket__storeDay': bucketQs[0].storeDay,                    'id': id,                    'title': title,                    'price': price,                    'content': content,                    'currency': currency,                    'day': day,                    'add_time': str(store_meal.add_time),                    'update_time': str(store_meal.update_time)})    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)        qs = Store_Meal.objects.values("id", "title", "price", "content", "day", "add_time", "update_time", "currency",                                       "bucket_id",                                       "bucket__bucket", "bucket__storeDay")        res = {}        if qs.exists():            res['count'] = qs.count()            res['data'] = CommonService.qs_to_list(qs[(page - 1) * line:page * line])        return response.json(0, res)    def update(self, request_dict, userID, response):        id = request_dict.get('id', None)        title = request_dict.get('title', None)        price = request_dict.get('price', None)        day = request_dict.get('day', None)        content = request_dict.get('content', None)        currency = request_dict.get('currency', None)        bucketID = request_dict.get('bucketID', None)        if not id or not title or not price or not content or not day:            return response.json(444, 'id, title, price, content, day')        own_perm = ModelService.check_perm(userID=userID, permID=30)        if own_perm is not True:            return response.json(404)        try:            store_meal = Store_Meal.objects.get(id=id)            now_time = timezone.localtime(timezone.now())            print(now_time)            store_meal.title = title            store_meal.price = price            store_meal.content = content            store_meal.day = day            if bucketID:                store_meal.bucket_id = bucketID            if currency:                store_meal.currency = currency            store_meal.save()        except Exception:            errorInfo = traceback.format_exc()            print(errorInfo)            return response.json(424, {'details': errorInfo})        else:            return response.json(0, {'update_id': store_meal.id, 'update_time': str(now_time)})    def delete(self, request_dict, userID, response):        id_list = request_dict.getlist('id', None)        if not id_list:            return response.json(444, 'id')        own_perm = ModelService.check_perm(userID=userID, permID=10)        if own_perm is not True:            return response.json(404)        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)    def find(self, request_dict, userID, response):        page = int(request_dict.get('page', None))        line = int(request_dict.get('line', None))        if not page or not line:            return response.json(444, 'page,line')        own_perm = ModelService.check_perm(userID=userID, permID=30)        if own_perm is not True:            return response.json(404)        qs = Store_Meal.objects.all()        if qs.exists():            count = qs.count()            res = qs[(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)'''用户获取全部套餐信息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)        response.lang = tko.lang        if tko.code != 0:            return response.json(tko.code)        elif operation == 'query':            return self.do_query(response)        else:            return response.json(414)    def do_query(self, response):        qs = Store_Meal.objects.all().values("id", "title", "content", "price", "day", "currency", "bucket__storeDay",                                             "bucket__bucket", "bucket__area")        if qs.exists():            ql = list(qs)            from operator import itemgetter            from itertools import groupby            ql.sort(key=itemgetter('bucket__area'))            res = []            for area, items in groupby(ql, key=itemgetter('bucket__area')):                res_c = {'area': area, 'items': list(items)}                res.append(res_c)            result = {'meals': res, 'extra': {                'cloud_banner': 'https://www.dvema.com/web/images/cloud_cn_banner.png',                'cloud_en_baner':'https://www.dvema.com/web/images/cloud_en_banner.png'            }}            return response.json(0, result)        else:            return response.json(0)
 |