|
@@ -1,321 +0,0 @@
|
|
|
-#!/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/30 14:13
|
|
|
-@Version: python3.6
|
|
|
-@MODIFY DECORD:ansjer dev
|
|
|
-@file: DeviceMeal.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 Device_Meal, Store_Meal
|
|
|
-import traceback, datetime
|
|
|
-from django.utils import timezone
|
|
|
-from Object.TokenObject import TokenObject
|
|
|
-from Object.ResponseObject import ResponseObject
|
|
|
-
|
|
|
-'''
|
|
|
-http://192.168.136.40:8077/device/meal?operation=add&token=test&uid=UKPAH63V23U4ZHEB111A&channel=1&rank=2&days=1
|
|
|
-http://192.168.136.40:8077/device/meal?operation=query&token=test&page=1&line=10
|
|
|
-http://192.168.136.40:8077/device/meal?operation=delete&token=test&id=1&id=2&id=3&id=4&id=5
|
|
|
-http://192.168.136.45:8077/device/meal?operation=update&token=local&status=1&uid=UKPAH63V23U4ZHEB111A&channel=2&rank=2&days=1&id=6
|
|
|
-用户:
|
|
|
-http://13.56.215.252:82/device/meal?operation=querybyuser&token=test&uid=L59KVYDAEPHR1T6M111A&channel=0
|
|
|
-http://13.56.215.252:82/device/meal?operation=addbyuser&token=test&uid=L59KVYDAEPHR1T6M111A&channel=11&rank=2
|
|
|
-http://13.56.215.252:82/device/meal?operation=updatebyuser&token=test&status=1&uid=L59KVYDAEPHR1T6M111A&channel=0&rank=2
|
|
|
-http://13.56.215.252:82/device/meal?operation=updatestatusbyuser&token=test&status=1&uid=L59KVYDAEPHR1T6M111A&channel=0
|
|
|
-'''
|
|
|
-
|
|
|
-
|
|
|
-class DeviceMeal(View):
|
|
|
- @method_decorator(csrf_exempt)
|
|
|
- def dispatch(self, *args, **kwargs):
|
|
|
- return super(DeviceMeal, 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()
|
|
|
- operation = request_dict.get('operation', None)
|
|
|
- token = request_dict.get('token', None)
|
|
|
- tko = TokenObject(token)
|
|
|
- response.lang = tko.lang
|
|
|
- if tko.code != 0:
|
|
|
- return response.json(tko.code)
|
|
|
- userID = tko.userID
|
|
|
- if userID is not None:
|
|
|
- if operation == 'query':
|
|
|
- return self.query(request_dict, userID, 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 == 'addbyuser':
|
|
|
- return self.addbyuser(request_dict, userID, response)
|
|
|
- elif operation == 'querybyuser':
|
|
|
- return self.querybyuser(request_dict, userID, response)
|
|
|
- elif operation == 'updatebyuser':
|
|
|
- return self.updatebyuser(request_dict, userID, response)
|
|
|
- elif operation == 'updatestatusbyuser':
|
|
|
- return self.updatestatusbyuser(request_dict, userID, response)
|
|
|
- return response.json(444, 'userID,operation')
|
|
|
-
|
|
|
- def add(self, request_dict, userID, response):
|
|
|
- own_perm = ModelService.check_perm(userID=userID, permID=40)
|
|
|
- if own_perm is True:
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
- channel = request_dict.get('channel', None)
|
|
|
- rank = request_dict.get('rank', None)
|
|
|
- param_flag = CommonService.get_param_flag(data=[uid, channel, rank])
|
|
|
- if param_flag is True:
|
|
|
- try:
|
|
|
- store_meal = Store_Meal.objects.get(id=rank)
|
|
|
- dmqs = Device_Meal.objects.filter(uid=uid, channel=channel)
|
|
|
- if dmqs.exists():
|
|
|
- days = store_meal.day
|
|
|
- now_time = timezone.localtime(timezone.now())
|
|
|
- if now_time > dmqs[0].end_time:
|
|
|
- end_time = now_time + datetime.timedelta(days=int(days))
|
|
|
- else:
|
|
|
- end_time = dmqs[0].end_time + datetime.timedelta(days=int(days))
|
|
|
- dmqs.update(end_time=end_time, update_time=now_time)
|
|
|
- return response.json(0)
|
|
|
- else:
|
|
|
- days = store_meal.day
|
|
|
- end_time = datetime.timedelta(days=int(days)) + datetime.datetime.now()
|
|
|
- print(end_time)
|
|
|
- device_meal = Device_Meal(
|
|
|
- status=1,
|
|
|
- uid=uid,
|
|
|
- channel=channel,
|
|
|
- end_time=end_time,
|
|
|
- rank=store_meal,
|
|
|
- )
|
|
|
- device_meal.save()
|
|
|
- except Exception as e:
|
|
|
- return response.json(500, {'details': repr(e)})
|
|
|
- else:
|
|
|
- return response.json(0, {'id': device_meal.id,
|
|
|
- 'status': device_meal.status,
|
|
|
- 'uid': device_meal.uid,
|
|
|
- 'rank': rank,
|
|
|
- 'channel': device_meal.channel,
|
|
|
- 'add_time': str(device_meal.add_time),
|
|
|
- 'update_time': str(device_meal.update_time),
|
|
|
- 'end_time': str(device_meal.end_time)})
|
|
|
-
|
|
|
- return response.json(444)
|
|
|
- else:
|
|
|
- return response.json(404)
|
|
|
-
|
|
|
- def query(self, request_dict, userID, response):
|
|
|
- own_perm = ModelService.check_perm(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 = Device_Meal.objects.all()
|
|
|
- if queryset.exists():
|
|
|
- count = queryset.count()
|
|
|
- res = queryset[(page - 1) * line:page * line]
|
|
|
- send_dict = CommonService.qs_to_dict(res)
|
|
|
- for k, v in enumerate(send_dict["datas"]):
|
|
|
- for val in res:
|
|
|
- if v['pk'] == val.id:
|
|
|
- title = Store_Meal.objects.get(id=v['fields']['rank']).title
|
|
|
- send_dict["datas"][k]['fields']['title'] = title
|
|
|
- send_dict['count'] = count
|
|
|
- return response.json(0, send_dict)
|
|
|
- return response.json(0)
|
|
|
- else:
|
|
|
- return response.json(444)
|
|
|
- else:
|
|
|
- return response.json(404)
|
|
|
-
|
|
|
- def update(self, request_dict, userID, response):
|
|
|
- own_perm = ModelService.check_perm(userID=userID, permID=30)
|
|
|
- if not own_perm:
|
|
|
- return response.json(404)
|
|
|
- id = request_dict.get('id', None)
|
|
|
- status = request_dict.get('status', None)
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
- if not status or not uid or not id:
|
|
|
- return response.json(444, 'status,id,uid')
|
|
|
- try:
|
|
|
- dmqs = Device_Meal.objects.get(id=id)
|
|
|
- dmqs.status = status
|
|
|
- dmqs.uid = uid
|
|
|
- dmqs.save()
|
|
|
- except Exception as e:
|
|
|
- errorInfo = traceback.format_exc()
|
|
|
- print(errorInfo)
|
|
|
- return response.json(424, {'details': repr(e)})
|
|
|
- else:
|
|
|
- return response.json(0, {'update_id': dmqs.id, 'update_time': str(dmqs.update_time),
|
|
|
- 'end_time': str(dmqs.end_time)})
|
|
|
-
|
|
|
- def delete(self, request_dict, userID, response):
|
|
|
- own_perm = ModelService.check_perm(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:
|
|
|
- Device_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 addbyuser(self, request_dict, userID, response):
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
- channel = request_dict.get('channel', None)
|
|
|
- rank = request_dict.get('rank', None)
|
|
|
- param_flag = CommonService.get_param_flag(data=[uid, channel, rank])
|
|
|
- if param_flag is True:
|
|
|
- own_dev = ModelService.check_own_device(userID=userID, UID=uid)
|
|
|
- if own_dev is True:
|
|
|
- is_exist = Device_Meal.objects.filter(uid=uid, channel=channel)
|
|
|
- if is_exist.exists():
|
|
|
- return response.json(0)
|
|
|
- else:
|
|
|
- try:
|
|
|
- store_meal = Store_Meal.objects.get(id=rank)
|
|
|
- days = store_meal.day
|
|
|
- end_time = datetime.timedelta(days=int(days)) + datetime.datetime.now()
|
|
|
- print(end_time)
|
|
|
- device_meal = Device_Meal(
|
|
|
- status=1,
|
|
|
- uid=uid,
|
|
|
- channel=channel,
|
|
|
- end_time=end_time,
|
|
|
- rank=store_meal,
|
|
|
- )
|
|
|
- device_meal.save()
|
|
|
- except Exception as e:
|
|
|
- return response.json(500, {'details': repr(e)})
|
|
|
- else:
|
|
|
- if device_meal.id:
|
|
|
- return response.json(0,
|
|
|
- {
|
|
|
- 'id': device_meal.id,
|
|
|
- 'status': device_meal.status,
|
|
|
- 'uid': device_meal.uid,
|
|
|
- 'rank': rank,
|
|
|
- 'channel': device_meal.channel,
|
|
|
- 'add_time': str(device_meal.add_time),
|
|
|
- 'update_time': str(device_meal.update_time),
|
|
|
- 'end_time': str(device_meal.end_time),
|
|
|
- })
|
|
|
- else:
|
|
|
- return response.json(14)
|
|
|
- return response.json(444)
|
|
|
-
|
|
|
- # 查询设备是否开通
|
|
|
- def querybyuser(self, request_dict, userID, response):
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
- channel = request_dict.get('channel', None)
|
|
|
- param_flag = CommonService.get_param_flag(data=[uid, channel])
|
|
|
- if param_flag is True:
|
|
|
- own_dev = ModelService.check_own_device(userID=userID, UID=uid)
|
|
|
- if own_dev is True:
|
|
|
- queryset = Device_Meal.objects.filter(uid=uid, channel=channel)
|
|
|
- if queryset.exists():
|
|
|
- send_dict = CommonService.qs_to_dict(queryset)
|
|
|
- for k, v in enumerate(send_dict["datas"]):
|
|
|
- for val in queryset:
|
|
|
- if v['pk'] == val.id:
|
|
|
- title = Store_Meal.objects.get(id=v['fields']['rank']).title
|
|
|
- send_dict["datas"][k]['fields']['title'] = title
|
|
|
- return response.json(0, send_dict)
|
|
|
- return response.json(0)
|
|
|
- else:
|
|
|
- return response.json(14)
|
|
|
- else:
|
|
|
- return response.json(444)
|
|
|
-
|
|
|
- def updatebyuser(self, request_dict, userID, response):
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
- channel = request_dict.get('channel', None)
|
|
|
- rank = request_dict.get('rank', None)
|
|
|
- param_flag = CommonService.get_param_flag(data=[uid, channel, rank])
|
|
|
- if param_flag is True:
|
|
|
- own_dev = ModelService.check_own_device(userID=userID, UID=uid)
|
|
|
- if own_dev is True:
|
|
|
- try:
|
|
|
- store_meal = Store_Meal.objects.get(id=rank)
|
|
|
- device_meal = Device_Meal.objects.get(uid=uid, channel=channel)
|
|
|
- days = store_meal.day
|
|
|
- now_time = timezone.localtime(timezone.now())
|
|
|
- if now_time > device_meal.end_time:
|
|
|
- end_time = now_time + datetime.timedelta(days=int(days))
|
|
|
- else:
|
|
|
- end_time = device_meal.end_time + datetime.timedelta(days=int(days))
|
|
|
- except Exception as e:
|
|
|
- errorInfo = traceback.format_exc()
|
|
|
- print(errorInfo)
|
|
|
- return response.json(424, {'details': repr(e)})
|
|
|
- else:
|
|
|
- if device_meal.id:
|
|
|
- device_meal.uid = uid
|
|
|
- device_meal.channel = channel
|
|
|
- device_meal.end_time = end_time
|
|
|
- device_meal.rank = store_meal
|
|
|
- device_meal.save()
|
|
|
- return response.json(0, {'update_id': device_meal.id,
|
|
|
- 'update_time': str(now_time),
|
|
|
- 'end_time': str(end_time)})
|
|
|
- else:
|
|
|
- return response.json(14)
|
|
|
- else:
|
|
|
- return response.json(444)
|
|
|
-
|
|
|
- def updatestatusbyuser(self, request_dict, userID, response):
|
|
|
- uid = request_dict.get('uid', None)
|
|
|
- channel = request_dict.get('channel', None)
|
|
|
- status = request_dict.get('status', None)
|
|
|
- if uid is not None and channel is not None:
|
|
|
- own_dev = ModelService.check_own_device(userID=userID, UID=uid)
|
|
|
- if own_dev is True:
|
|
|
- try:
|
|
|
- device_meal = Device_Meal.objects.get(uid=uid, channel=channel)
|
|
|
- except Exception as e:
|
|
|
- errorInfo = traceback.format_exc()
|
|
|
- print(errorInfo)
|
|
|
- return response.json(424, {'details': repr(e)})
|
|
|
- else:
|
|
|
- if device_meal.id:
|
|
|
- device_meal.status = status
|
|
|
- device_meal.save()
|
|
|
- now_time = timezone.localtime(timezone.now())
|
|
|
- return response.json(0, {'update_id': device_meal.id,
|
|
|
- 'update_time': str(now_time)})
|
|
|
- else:
|
|
|
- return response.json(14)
|
|
|
- else:
|
|
|
- return response.json(444)
|