|
@@ -1,8 +1,10 @@
|
|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
import json
|
|
|
+import logging
|
|
|
import time
|
|
|
|
|
|
+from django.db import transaction
|
|
|
from django.http import StreamingHttpResponse
|
|
|
from django.utils.decorators import method_decorator
|
|
|
from django.views import View
|
|
@@ -114,6 +116,7 @@ class UIDView(View):
|
|
|
return response.json(444)
|
|
|
|
|
|
# 分配UID
|
|
|
+ @transaction.atomic
|
|
|
def do_allot(self, request_dict, response):
|
|
|
username = request_dict.get('username', None)
|
|
|
quantity = int(request_dict.get('quantity', None))
|
|
@@ -141,12 +144,14 @@ class UIDView(View):
|
|
|
|
|
|
updates = []
|
|
|
datas = []
|
|
|
+ count = 0
|
|
|
if uid_qs.exists():
|
|
|
uid_qs = uid_qs[0:quantity]
|
|
|
now_time = int(time.time())
|
|
|
+ savePoint = transaction.savepoint()
|
|
|
for item in uid_qs:
|
|
|
item.status = 1
|
|
|
- item.update_time = now_time
|
|
|
+ item.update_time = time.time()
|
|
|
user_uid = UserUIDModel()
|
|
|
user_uid.uid = item
|
|
|
user_uid.user = allot_user_qs[0]
|
|
@@ -156,15 +161,15 @@ class UIDView(View):
|
|
|
updates.append(item)
|
|
|
|
|
|
if len(datas) % 5000 == 0:
|
|
|
- UserUIDModel.objects.bulk_create(datas)
|
|
|
- UIDModel.objects.bulk_update(updates, fields=['status', 'update_time'])
|
|
|
- datas.clear()
|
|
|
- updates.clear()
|
|
|
+ # count += 1
|
|
|
+ result = self.do_update(datas, updates, savePoint)
|
|
|
+ if result != 0:
|
|
|
+ return response.json(result)
|
|
|
+
|
|
|
if len(datas) > 0:
|
|
|
- UserUIDModel.objects.bulk_create(datas)
|
|
|
- UIDModel.objects.bulk_update(updates, fields=['status', 'update_time'])
|
|
|
- datas.clear()
|
|
|
- updates.clear()
|
|
|
+ result = self.do_update(datas, updates, savePoint)
|
|
|
+ if result != 0:
|
|
|
+ return response.json(result)
|
|
|
redisObject = RedisObject()
|
|
|
values = uid_qs.values('uid')
|
|
|
print(values)
|
|
@@ -175,6 +180,25 @@ class UIDView(View):
|
|
|
else:
|
|
|
return response.json(444)
|
|
|
|
|
|
+ def do_update(self, datas, updates, savePoint, count=0):
|
|
|
+ try:
|
|
|
+ try:
|
|
|
+ if count == 2:
|
|
|
+ raise Exception("Invalid level!")
|
|
|
+ UserUIDModel.objects.bulk_create(datas)
|
|
|
+ UIDModel.objects.bulk_update(updates, fields=['status', 'update_time'])
|
|
|
+ datas.clear()
|
|
|
+ updates.clear()
|
|
|
+ except Exception as e:
|
|
|
+ if savePoint:
|
|
|
+ transaction.savepoint_rollback(savePoint)
|
|
|
+ djangoLogger = logging.getLogger('django')
|
|
|
+ djangoLogger.exception(repr(e))
|
|
|
+ return 174
|
|
|
+ except Exception as e:
|
|
|
+ return 174
|
|
|
+ return 0
|
|
|
+
|
|
|
# 把UID表中的数据移动到HistoryUID表中
|
|
|
def do_remove(self, request_dict, response):
|
|
|
token = TokenObject(request_dict.get('token', None))
|