| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 | #!/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 Viewfrom django.utils.decorators import method_decoratorfrom django.views.decorators.csrf import csrf_exemptfrom Service.ModelService import ModelServicefrom Service.CommonService import CommonServicefrom Model.models import Store_Meal, VodBucketModelimport tracebackfrom django.utils import timezonefrom Object.TokenObject import TokenObjectfrom 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=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)        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)        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')        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):        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)            content = request_dict.get('content', None)            currency = request_dict.get('currency', None)            bucketID = request_dict.get('bucketID', 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)                    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)})            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)        if operation == 'list':            return self.do_query_list(response)        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():            res = {}            for q in qs:                if q['bucket__area'] not in res:                    res[q['bucket__area']] = []                res[q['bucket__area']].append(q)            # res['cloudpng'] = 'https://www.dvema.com/web/images/cloud_banner.png'            return response.json(0,res)        else:            return response.json(0, [])    def do_query_list(self, response):        qs = Store_Meal.objects.all().values("id", "title", "content", "price", "day", "currency","bucket__storeDay",                                             "bucket__bucket")        if qs.exists():            # res = CommonService.qs_to_list(qs)            return response.json(0, list(qs))        else:            return response.json(0, [])
 |