|
@@ -6,7 +6,7 @@ import os
|
|
|
import time
|
|
|
import math
|
|
|
from django.views.generic.base import View
|
|
|
-from Model.models import CouponModel,Device_User
|
|
|
+from Model.models import CouponModel, Device_User, CouponConfigModel, CouponLang
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from django.db.models import Q, F, Count
|
|
@@ -41,59 +41,79 @@ class CouponView(View):
|
|
|
if tko.code != 0:
|
|
|
return response.json(tko.code)
|
|
|
userID = tko.userID
|
|
|
- if operation == 'UserCoupon': #用户优惠券
|
|
|
+ if operation == 'UserCoupon': # 用户优惠券
|
|
|
return self.query_user_coupon(request_dict, userID, response)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
|
- def generate_coupon(self,request_dict,response):
|
|
|
+ def generate_coupon(self, request_dict, response):
|
|
|
username = request_dict.get('username', None)
|
|
|
num = request_dict.get('num', None)
|
|
|
userID = Device_User.objects.filter(username=username).values('userID')[0]['userID']
|
|
|
|
|
|
try:
|
|
|
data = []
|
|
|
- for i in range(int(num)):
|
|
|
- if i % 2 == 0:
|
|
|
- data.append(CouponModel(
|
|
|
- userID_id=userID,
|
|
|
- use_status=0,
|
|
|
- type=1,
|
|
|
- coupon_discount=7,
|
|
|
- distributeTime=int(time.time()),
|
|
|
- valid_time=int(time.time()) + 8640000,
|
|
|
- addTime=int(time.time())
|
|
|
- ))
|
|
|
- else:
|
|
|
- data.append(CouponModel(
|
|
|
- userID_id=userID,
|
|
|
- use_status=0,
|
|
|
- type=2,
|
|
|
- coupon_discount=0.01,
|
|
|
- distributeTime=int(time.time()),
|
|
|
- valid_time=int(time.time()) + 8640000,
|
|
|
- addTime=int(time.time())
|
|
|
- ))
|
|
|
- CouponModel.objects.bulk_create(data)
|
|
|
+ # CouponConfigModel.objects.create(type=1, use_range=1, coupon_discount=8)
|
|
|
+ # CouponLang.objects.create(
|
|
|
+ # lang='en',
|
|
|
+ # instruction='for the first month of the Auto-renewal plan',
|
|
|
+ # quota='20.0%',
|
|
|
+ # unit='off',
|
|
|
+ # remark='This coupon can be used on each device once only.'
|
|
|
+ # )
|
|
|
+ # CouponConfigModel.objects.get(id=1).lang.add(1)
|
|
|
+ now_time = int(time.time())
|
|
|
+ CouponModel.objects.create(
|
|
|
+ use_status=0,
|
|
|
+ distribute_time=now_time,
|
|
|
+ valid_time=now_time,
|
|
|
+ userID=userID,
|
|
|
+ coupon_config_id=1,
|
|
|
+ update_time=now_time,
|
|
|
+ create_time=now_time
|
|
|
+ )
|
|
|
return HttpResponse('success')
|
|
|
except Exception as e:
|
|
|
- return HttpResponse(repr(e))
|
|
|
+ return HttpResponse(
|
|
|
+ "错误行数:{errLine}, 错误信息: {errmsg}".format(errLine=e.__traceback__.tb_lineno, errmsg=repr(e)))
|
|
|
|
|
|
+ def query_user_coupon(self, request_dict, userID, response): # 用户优惠券列表
|
|
|
+ now_time = int(time.time())
|
|
|
+ lang = request_dict.get('lang', 'en')
|
|
|
+ # couponObj = CouponModel.objects.filter(userID_id=userID, use_status=0, distributeTime__lte=now_time,
|
|
|
+ # valid_time__gt=now_time).annotate(coupon_id=F('id')).values(
|
|
|
+ # "coupon_id", "type", "coupon_discount", "valid_time")
|
|
|
|
|
|
+ coupon_obj = CouponModel.objects.filter(
|
|
|
+ userID=userID,
|
|
|
+ use_status=0,
|
|
|
+ distribute_time__lte=now_time,
|
|
|
+ valid_time__gt=now_time,
|
|
|
+ coupon_config__lang__lang=lang,
|
|
|
+ ).annotate(
|
|
|
+ coupon_id=F('id'),
|
|
|
+ type=F('coupon_config__type'),
|
|
|
+ coupon_discount=F('coupon_config__coupon_discount'),
|
|
|
+ instruction=F('coupon_config__lang__instruction'),
|
|
|
+ remark=F('coupon_config__lang__remark'),
|
|
|
+ quota=F('coupon_config__lang__quota'),
|
|
|
+ unit=F('coupon_config__lang__unit'),
|
|
|
+ ).values(
|
|
|
+ "coupon_id",
|
|
|
+ "type",
|
|
|
+ "coupon_discount",
|
|
|
+ "valid_time",
|
|
|
+ "instruction",
|
|
|
+ "remark",
|
|
|
+ "quota",
|
|
|
+ "unit",
|
|
|
+ )
|
|
|
|
|
|
+ for couponList in coupon_obj:
|
|
|
+ couponList['valid_time'] = CommonService.timestamp_to_str(couponList['valid_time'])
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- def query_user_coupon(self, request_dict, userID, response): #用户优惠券列表
|
|
|
- nowTime = int(time.time())
|
|
|
- couponObj = CouponModel.objects.filter(userID_id=userID,use_status=0,distributeTime__lte=nowTime,
|
|
|
- valid_time__gt=nowTime).annotate(coupon_id=F('id')).values(
|
|
|
- "coupon_id","type","coupon_discount","valid_time")
|
|
|
- for couponList in couponObj:
|
|
|
- couponList['valid_time'] = CommonService.timestamp_to_str(couponList['valid_time'])
|
|
|
result = {
|
|
|
- 'count':couponObj.count(),
|
|
|
- 'couponList':list(couponObj),
|
|
|
+ 'count': coupon_obj.count(),
|
|
|
+ 'couponList': list(coupon_obj),
|
|
|
}
|
|
|
return response.json(0, result)
|