view函数

from django.shortcuts import render
import datetime
import json
from django.forms.models import model_to_dict
from django.http import HttpResponse,HttpResponseRedirect
from django.db import transaction
from .models import Project,Batch,Sub_Batch,Package,MyLog
from .models import MyTask
from user.models import Account
from category.models import Category
import numpy as np
import os
import io
import xlwt
import shutil
import hashlib
import time
import math
import random
from PIL import Image
from celery import task as task1
from project.tasks import upload,label_input_db_1
from django.core.paginator import Paginator
from dwebsocket.decorators import accept_websocket,require_websocket

#编辑距离计算
def edit_distance(word1, word2):
    len1 = len(word1);
    len2 = len(word2);
    dp = np.zeros((len1 + 1, len2 + 1))
    for i in range(len1 + 1):
        dp[i][0] = i;
    for j in range(len2 + 1):
        dp[0][j] = j;

    for i in range(1, len1 + 1):
        for j in range(1, len2 + 1):
            delta = 0 if word1[i - 1] == word2[j - 1] else 1
            dp[i][j] = min(dp[i - 1][j - 1] + delta, min(dp[i - 1][j] + 1, dp[i][j - 1] + 1))
    return dp[len1][len2]

#项目相关
def project_list(request):#获取搜索/完整项目列表,并转向项目列表页面
    p_keyword = request.GET.get('project_keyword')
    n_keyword = request.GET.get('nickname_keyword')
    is_p_search = 0
    is_n_search = 0
    page_num = request.GET.get('currentPage')
    if page_num == '' or page_num == None:
        current_page = 1
    else:
        current_page = int(page_num)
    response = list(Project.objects.all().values())
    dict = {}
    if p_keyword == None:
        p_keyword = ''
    else:
        is_p_search = 1
    dict['p_keyword'] = p_keyword
    if n_keyword == None:
        n_keyword = ''
    else:
        is_n_search = 1
    dict['n_keyword'] = n_keyword
    final_output = []
    output = []
    if is_p_search and not is_n_search:
        for project in response:
            #后续更新编辑距离
            if p_keyword in project['project_name'] and n_keyword in project['nick_name']:
                total = MyTask.objects.filter(project_id=project['id']).count()
                if total > 0:
                    project['complete_rate'] = round(MyTask.objects.filter(project_id=project['id'],qa_pass=1).count()/total*100,2)
                else:
                    project['complete_rate'] = round(0,2)
                output.append((edit_distance(p_keyword,project['project_name']),project))
        output = sorted(output, key=lambda x: x[0])
        final_output = [x[-1] for x in output]
    elif not is_p_search and is_n_search:
        for project in response:
            #后续更新编辑距离
            if p_keyword in project['project_name'] and n_keyword in project['nick_name']:
                total = MyTask.objects.filter(project_id=project['id']).count()
                if total > 0:
                    project['complete_rate'] = round(MyTask.objects.filter(project_id=project['id'],qa_pass=1).count()/total*100,2)
                else:
                    project['complete_rate'] = round(0,2)
                output.append((edit_distance(n_keyword,project['nick_name']),project))
        output = sorted(output, key=lambda x: x[0])
        final_output = [x[-1] for x in output]
    elif is_p_search and is_n_search:
        for project in response:
            #后续更新编辑距离
            if p_keyword in project['project_name'] and n_keyword in project['nick_name']:
                total = MyTask.objects.filter(project_id=project['id']).count()
                if total > 0:
                    project['complete_rate'] = round(MyTask.objects.filter(project_id=project['id'],qa_pass=1).count()/total*100,2)
                else:
                    project['complete_rate'] = round(0,2)
                output.append((edit_distance(p_keyword,project['project_name']),edit_distance(n_keyword,project['nick_name']),project))
        output = sorted(output, key=lambda x: x[0:2])
        final_output = [x[-1] for x in output]
    else:
        for project in response:
            #后续更新编辑距离
            #if p_keyword in project['project_name'] and n_keyword in project['nick_name']:
            total = MyTask.objects.filter(project_id=project['id']).count()
            if total > 0:
                project['complete_rate'] = round(MyTask.objects.filter(project_id=project['id'],qa_pass=1).count()/total*100,2)
            else:
                project['complete_rate'] = round(0,2)
            final_output.append((project))
    paginator = Paginator(final_output, 20)
    output = paginator.page(current_page)
    dict['project_list'] = output
    dict['currentPage'] = current_page
    dict['page_range'] = paginator.page_range
    return render(request, 'project_list.html', dict)

def add_project(request):#转向添加项目页面
    output = []
    categorys = Category.objects.filter(ishiddent=0)
    for category in categorys:
        output.append(category.root)
    return render(request, 'add_project.html',{'category_list':output})

def project_exists(request):#判断项目是否存在
    projectname = request.GET.get('projectname')
    try:
        Project.objects.get(project_name=projectname)
        return HttpResponse('1')
    except:
        return HttpResponse('0')

def add_one_project(request):#执行添加单个项目
    status = 1
    projectname = request.POST.getlist('projectname')[0]
    categorys = request.POST.getlist('categorys')
    nick_name = request.POST.getlist('nickname')[0]
    percentage_fstc = int(request.POST.getlist('p_fstc')[0])
    percentage_secc = int(request.POST.getlist('p_secc')[0])
    task_amount = int(request.POST.getlist('task_amount')[0])
    now = datetime.datetime.now()
    category = ','.join(categorys)
    try:
        Project.objects.get(project_name=projectname)
        return HttpResponse('0')
    except:
        Project.objects.create(project_name=projectname,category=category,nick_name=nick_name,percentage_fstc=percentage_fstc,percentage_secc=percentage_secc,created_date=now,task_amount=task_amount)
        return HttpResponse(str(status))

def project_search(request):#用于项目的自动补全搜索
    response = list(Project.objects.all().values())
    output = []
    for project in response:
        output.append(project['project_name'])
    return HttpResponse(json.dumps({
        "result": output
    }))

def change_nickname(request,project):#转向对内名称修改页
    return render(request, 'change_pnickname.html',{"projectname":project});

def pnickname_update(request):#对内名称修改处理
    project_name = request.GET.get('projectname')
    nick_name = request.GET.get('nickname')
    try:
        username =request.session['user']['username']
        role = request.session['user']['role']
    except:
        return render(request, 'home.html', {})
    project = Project.objects.get(project_name=project_name)
    project.nick_name = nick_name
    project.save()
    now = datetime.datetime.utcnow()
    MyLog.objects.create(log_date=now,action='修改',initiate_user=username,initiate_user_role=role,target_project=project,reason='将项目的对内名称修改为'+nick_name)
    return HttpResponse('1')
def file_counts(request):
    files = request.GET.get('img')
    projectname = request.GET.get('project')
    batchname = request.GET.get('batchname')
    print(projectname,batchname)
    try:
        project = Project.objects.get(project_name=projectname)
    except:
        return HttpResponse(json.dumps({"count": '2',}))
    exts = 'bmp jpg tiff jpeg png tif gif pdf'
    src = str(files)
    dest = r'C:\Users\Administrator\Desktop\图新'
    fp = {}
    set_1 = set()
    extss = exts.lower().split()
    try:
        Batch.objects.get(batch_name=batchname)
        return HttpResponse(json.dumps({"count": '3',}))
    except:
        pass
    for dn, dns, fns in os.walk(src):
        # print(dn)
        for fl in fns:
            if os.path.splitext(fl.lower())[1][1:] in extss:
                if dn not in fp.keys():
                    fp[dn] = []
                    # print(dn)
                fp[dn].append(fl)

    file_count = 0
    for v in fp.values():
        for f in v:
            file_count += 1
    if file_count == 0:
        return HttpResponse(json.dumps({"count": '0',}))
    #批次建表
    created_date = datetime.datetime.now()
    project = Project.objects.get(project_name=projectname)
    Batch.objects.create(project=project, batch_name=batchname, created_date=created_date, files_num=file_count)
    return HttpResponse(json.dumps({
                "count": '#'+str(file_count),
            }))
#def pm(request, body):
#    res = body.get('result')
#    if body.get('status') == 'PROGRESS':
#        a = res.get('p')
        #print(a)
#        return int(a)
#    else:
#        pass
        #print('end')

#批次相关

@require_websocket
def uploadImg(request):#上传批次
    #for message in request.websocket:
    for message in request.websocket:
        files,projectname,batchname = message.decode('utf-8').split('#')
        break
    dest = r'/root/myplatform/common_static/picture_server'
    try:
        project = Project.objects.get(project_name=projectname)
    except:
        request.websocket.send('#2')
    result = upload.apply_async((files,projectname,batchname,dest,),queue='for_task_A',routing_key="task_a")
    #result = upload.apply_async((files,projectname,batchname,dest,))
    task_id = result.id
    the_task = upload.AsyncResult(task_id)
    while the_task.state != 'SUCCESS':
        if the_task.state == 'PENDING':
           pass#iprint('waiting')
        else:
           try:
               request.websocket.send(str(int(the_task.result.get('p'))))
               if int(the_task.result.get('p')) == 100:
                   request.websocket.send('100')  
           except:
               print(the_task.result)
               request.websocket.send('end')
    #request.websocket.send(int(the_task.result.get('p')))
    #b = result.get(on_message=pm(request),propagate=False)
    #print(b)
    if result.get() == '3':
        request.websocket.send('#3')
    elif result.get() == '0':
        request.websocket.send('#0')
        # 文件批量建表
        # MyTask.objects.bulk_create(mytask_list_to_insert)
        # 批次建表
        # Batch.objects.create(project=project, batch_name=batchname, created_date=created_date, files_num=files_num)
def add_batch_for_project(request):#转向批次导入界面
    return render(request, 'uploadImg.html')

def addBatch(request,project):#转向特定项目的批次导入界面
    return render(request, 'uploadImg.html',{'projectname':project})

def showImg(request):
    imgs = MyTask.objects.all() # 从数据库中取出所有的图片路径
    context = {
        'imgs' : imgs
    }
    return render(request, 'showImg.html', context)

def allow_batch(request):
    batch_id = int(request.GET.get('batch_id'))
    batch = Batch.objects.get(id=batch_id)
    if batch.issetup == 0:
        batch.issetup = 1
        batch.save()
        try:
            username = request.session['user']['username']
            role = request.session['user']['role']
        except:
            return render(request, 'home.html', {})
        now = datetime.datetime.utcnow()
        sub_batches = list(Sub_Batch.objects.filter(batch_name=batch.batch_name))
        allow_sub_batchs(sub_batches,username,role,now)
        packages = list(Package.objects.filter(batch_name=batch.batch_name))
        allow_packages(packages,username,role,now)
        MyLog.objects.create(log_date=now,action='启用',initiate_user=username,initiate_user_role=role,target_batch=batch,reason='启用了批次'+batch.batch_name)
    return HttpResponse('1')

@transaction.atomic
def allow_sub_batchs(sub_batches,username,role,now):
    for sub_batch in sub_batches:
        if sub_batch.issetup == 0:
            sub_batch.status = sub_batch.last_status
            sub_batch.last_status = 0
            sub_batch.issetup = 1
            sub_batch.save()
            MyLog.objects.create(log_date=now,action='启用',initiate_user=username,initiate_user_role=role,target_sub_batch=sub_batch,reason='启用了子批次'+str(sub_batch.id))    

@transaction.atomic
def allow_packages(packages,username,role,now):
    for package in packages:
        if package.status == 400:
            package.status = package.last_status
            package.last_status = 0
            package.save()
            MyLog.objects.create(log_date=now,action='启用',initiate_user=username,initiate_user_role=role,target_package=package,reason='启用了子批次'+str(package.id))
            
def ban_batch(request):
    batch_id = int(request.GET.get('batch_id'))
    batch = Batch.objects.get(id=batch_id)
    if batch.issetup == 1:
        batch.issetup = 0
        batch.save()
        username = request.session['user']['username']
        role = request.session['user']['role']
        now = datetime.datetime.utcnow()
        sub_batches = list(Sub_Batch.objects.filter(batch_name=batch.batch_name))
        ban_sub_batchs(sub_batches,username,role,now)
        packages = list(Package.objects.filter(batch_name=batch.batch_name))
        ban_packages(packages,username,role,now)
        MyLog.objects.create(log_date=now,action='禁用',initiate_user=username,initiate_user_role=role,target_batch=batch,reason='因某些原因禁用了批次'+batch.batch_name)
    return HttpResponse('1')

@transaction.atomic
def ban_sub_batchs(sub_batches,username,role,now):
    for sub_batch in sub_batches:
        if sub_batch.issetup == 1:
            sub_batch.last_status = sub_batch.status
            sub_batch.status = 400
            sub_batch.issetup = 0
            sub_batch.save()
            MyLog.objects.create(log_date=now,action='禁用',initiate_user=username,initiate_user_role=role,target_sub_batch=sub_batch,reason='因某些原因禁用了子批次'+str(sub_batch.id))

@transaction.atomic
def ban_packages(packages,username,role,now):
    for package in packages:
        if package.status != 400:
            package.last_status = package.status
            package.status = 400
            package.save()
            MyLog.objects.create(log_date=now,action='禁用',initiate_user=username,initiate_user_role=role,target_package=package,reason='因某些原因禁用了子批次'+str(package.id))

def batch_list(request):#获取搜索/完整批次列表,并转向批次列表页面
    project_keyword = request.GET.get('project_keyword')
    batch_keyword = request.GET.get('batch_keyword')
    page_num = request.GET.get('currentPage')
    is_p_search = 0
    is_b_search = 0
    dict = {}
    if page_num == '' or page_num == None:
        current_page = 1
    else:
        current_page = int(page_num)
    if project_keyword == None:
        project_keyword = ''
    else:
        is_p_search = 1
    dict['p_keyword'] = project_keyword
    if batch_keyword == None:
        batch_keyword = ''
    else:
        is_b_search = 1
    dict['b_keyword'] = batch_keyword
    response = list(Batch.objects.all())
    final_output = []
    batch_output = []
    if is_p_search and not is_b_search:
        for batch in response:
            #后续更新编辑距离
            #project_name = Project.objects.get(id=batch.project_id).project_name
            project_name = batch.project.project_name
            if batch_keyword in batch.batch_name and project_keyword in project_name:
                batch_dict = model_to_dict(batch)
                batch_dict['project'] = project_name
                try:
                    files_rest = list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], sub_batch_id=None)).count()
                except TypeError:
                    files_rest = len(list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], sub_batch_id=None)))
                batch_dict['files_to_allocate'] = files_rest
                try:
                    files_complete = list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], qa_pass=1)).count()
                except TypeError:
                    files_complete = len(list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], qa_pass=1)))
                batch_dict['complete_rate'] = round(files_complete/batch_dict['files_num']*100,2)
                batch_output.append((edit_distance(project_keyword,project_name),batch_dict))
        final_output = [x[-1] for x in sorted(batch_output,key=lambda x: x[0])]
    elif not is_p_search and is_b_search:
        for batch in response:
            #后续更新编辑距离
            #project_name = Project.objects.get(id=batch.project_id).project_name
            project_name = batch.project.project_name
            if batch_keyword in batch.batch_name and project_keyword in project_name:
                batch_dict = model_to_dict(batch)
                batch_dict['project'] = project_name
                try:
                    files_rest = list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], sub_batch_id=None)).count()
                except TypeError:
                    files_rest = len(list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], sub_batch_id=None)))
                batch_dict['files_to_allocate'] = files_rest
                try:
                    files_complete = list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], qa_pass=1)).count()
                except TypeError:
                    files_complete = len(list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], qa_pass=1)))
                batch_dict['complete_rate'] = round(files_complete/batch_dict['files_num']*100,2)
                batch_output.append((edit_distance(batch_keyword,batch.batch_name),batch_dict))
        final_output = [x[-1] for x in sorted(batch_output,key=lambda x: x[0])]
    elif is_p_search and is_b_search:
        for batch in response:
            #后续更新编辑距离
            #project_name = Project.objects.get(id=batch.project_id).project_name
            project_name = batch.project.project_name
            if batch_keyword in batch.batch_name and project_keyword in project_name:
                batch_dict = model_to_dict(batch)
                batch_dict['project'] = project_name
                try:
                    files_rest = list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], sub_batch_id=None)).count()
                except TypeError:
                    files_rest = len(list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], sub_batch_id=None)))
                batch_dict['files_to_allocate'] = files_rest
                try:
                    files_complete = list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], qa_pass=1)).count()
                except TypeError:
                    files_complete = len(list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], qa_pass=1)))
                batch_dict['complete_rate'] = round(files_complete/batch_dict['files_num']*100,2)
                batch_output.append((edit_distance(project_keyword,project_name),edit_distance(batch_keyword,batch.batch_name),batch_dict))
        final_output = [x[-1] for x in sorted(batch_output,key=lambda x: x[0:2])]
    else:
        for batch in response:
            #后续更新编辑距离
            #project_name = Project.objects.get(id=batch.project_id).project_name
            project_name = batch.project.project_name
            batch_dict = model_to_dict(batch)
            batch_dict['project'] = project_name
            try:
                files_rest = list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], sub_batch_id=None)).count()
            except TypeError:
                files_rest = len(list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], sub_batch_id=None)))
            batch_dict['files_to_allocate'] = files_rest
            try:
                files_complete = list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], qa_pass=1)).count()
            except TypeError:
                files_complete = len(list(MyTask.objects.filter(batch_name=batch_dict['batch_name'], qa_pass=1)))
            batch_dict['complete_rate'] = round(files_complete/batch_dict['files_num']*100,2)
            final_output.append(batch_dict)
    paginator = Paginator(final_output, 20)
    output = paginator.page(current_page)
    dict['batch_list'] = output
    dict['currentPage'] = current_page
    dict['page_range'] = paginator.page_range
    return render(request, 'batch_list.html', dict)



def edit_note(request,batchid):#转向备注修改页
    return render(request, 'edit_note.html',{"batchid":batchid});

def rule(request):#保密协议
    return render(request, 'rule.html')
    

def note_update(request):#备注修改处理
    batch_id = int(request.GET.get('batchid'))
    note = request.GET.get('note')
    batch = Batch.objects.get(id=batch_id)
    batch.note = note.strip()
    batch.save()
    try:
        username = request.session['user']['username']
        role = request.session['user']['role']
    except:
        return render(request, 'home.html', {})
    now = datetime.datetime.utcnow()
    MyLog.objects.create(log_date=now,action='修改',initiate_user=username,initiate_user_role=role,target_batch=batch,reason='将批次'+batch.batch_name+'的备注修改为['+note.strip()+']')
    return HttpResponse('1')

def batchname_exists(request):#判断批次名是否已存在
    batchname = request.GET.get('batchname')
    try:
        Batch.objects.get(batch_name=batchname)
        return HttpResponse('1')
    except:
        return HttpResponse('0')

#子批次相关
def allocate_sub_batch(request):#转向分配子批次页面
    batchid = request.GET.get('batchid')
    batch = Batch.objects.get(id=batchid)
    #获取批次名称
    batchname = batch.batch_name
    #获取批未分配文件数
    files_rest = list(MyTask.objects.filter(batch_name=batchname,sub_batch_id=None).values())
    files_rest_num = len(files_rest)
    return render(request, 'allocate_sub_batch.html', {"batchname": batchname,"files_rest_num":files_rest_num})

def sub_batch_allocate(request):#执行子批次分配操作
    outs_username = request.GET.get('outs_username')
    batchname = request.GET.get('batchname')
    sub_batch_num = int(request.GET.get('sub_batch_num'))
    sub_batch_size = int(request.GET.get('sub_batch_size'))
    batch = Batch.objects.get(batch_name=batchname)
    project = batch.project
    files_rest = list(MyTask.objects.filter(batch_name=batchname, sub_batch_id=None).order_by('id'))
    total = len(files_rest)
    index = 0
    task_amount = project.task_amount
    package = None
    try:
        username = request.session['user']['username']
        role = request.session['user']['role']
    except:
        return render(request, 'home.html', {})
    for i in range(sub_batch_num):
        #建立子批次
        try:
            created_date = datetime.datetime.now()
            deadline_default = created_date+datetime.timedelta(days=2)
            if deadline_default.minute > 0 or deadline_default.second > 0 or deadline_default.microsecond > 0:
                deadline_default = deadline_default.replace(hour=deadline_default.hour+1)
                deadline_default = deadline_default.replace(minute=0,second=0)
            Sub_Batch.objects.create(project=project,batch_name=batchname,created_date=created_date,
                                     outs_account=outs_username,files_num=sub_batch_size,deadline=deadline_default)
            sub_batch = Sub_Batch.objects.last()
            now = datetime.datetime.utcnow()
            MyLog.objects.create(log_date=now,action='创建',initiate_user=username,initiate_user_role=role,target_user=outs_username,target_user_role='outs',target_sub_batch=sub_batch,reason='创建子批次'+str(sub_batch.id)+',并分配文件')
        except Exception as e:
            print(e)
            return HttpResponse('0')
        #分桶版本
        buckets_num = math.ceil(sub_batch_size/task_amount)
        sub_batch.package_num = buckets_num
        sub_batch.save()
        file_for_bucket = files_rest[i*sub_batch_size:min((i+1)*sub_batch_size,total)]
        for j in range(buckets_num):
            Package.objects.create(batch_name=batchname, project=project, sub_batch=sub_batch)
            package = Package.objects.last()
            files_rest_bucket = file_for_bucket[j*task_amount:min((j+1)*task_amount,sub_batch_size)]
            package_allocate(package, sub_batch, files_rest_bucket)
    return HttpResponse('1')

@transaction.atomic
def package_allocate(package,sub_batch,files_rest):
    for file in files_rest:
        file.package = package
        file.sub_batch = sub_batch
        file.save()

def allocate_qa(request):#转向分配QA页面
    sub_batch_id = request.GET.get('sub_batch_id')
    sub_batch = Sub_Batch.objects.get(id=sub_batch_id)
    #获取批未分配文件数
    files_rest_num = 0
    files_rest = MyTask.objects.filter(sub_batch_id=sub_batch_id)
    for file in files_rest:
        if file.qa_account == None:
            files_rest_num += 1
    return render(request, 'allocate_qa.html', {"sub_batch": sub_batch,"files_rest_num":files_rest_num,"total_num":files_rest.count()})

def qa_allocate(request):#执行QA分配操作
    qa_username = request.GET.get('qa_username')
    rest = int(request.GET.get('rest'))
    total = int(request.GET.get('total'))
    try:
        pqaad = float(request.GET.get('pqaad'))
    except ValueError:
        return HttpResponse('0')
    allocate_num = math.ceil(pqaad * total/100)
    if allocate_num > rest:
        return HttpResponse('0')
    sub_batch_id = int(request.GET.get('sub_batch_id'))
    MyTask.objects.filter(sub_batch_id=sub_batch_id, qa_account=qa_username).update(qa_pass=0,qa_status=0,qa_account=None)
    files_rest = list(MyTask.objects.filter(sub_batch_id=sub_batch_id,qa_account=None).order_by('id'))
    qa_files_allocate(files_rest, allocate_num, qa_username)
    sub_batch = Sub_Batch.objects.get(id=sub_batch_id)
    if sub_batch.qa_account == '':
        sub_batch.qa_account = qa_username
    else:
        qa_account_list = sub_batch.qa_account.split(',')
        if qa_username not in qa_account_list:
            qa_account_list.append(qa_username)
        sub_batch.qa_account = ','.join(qa_account_list)
    sub_batch.save()
    try:
        username = request.session['user']['username']
        role = request.session['user']['role']
    except:
        return render(request, 'home.html', {})
    now = datetime.datetime.utcnow()
    MyLog.objects.create(log_date=now,action='分配',initiate_user=username,initiate_user_role=role,target_user=qa_username,target_user_role='qaad',target_sub_batch=sub_batch,reason='给QA管理员'+qa_username+'分配了'+str(allocate_num)+'个文件进行QA验收')
    return HttpResponse('1')

@transaction.atomic
def qa_files_allocate(files,allocate_num, qa_username):
    for i in range(allocate_num):
        file = files[i]
        file.qa_account = qa_username
        file.save()

def sub_batch_recycle(request):#回收子批次
    sub_batch_id = request.POST.getlist('sub_batch_id')[0]
    batch_name = request.POST.getlist('batch_name')[0]
    #先清空文件
    #files = list(MyTask.objects.filter(sub_batch_id=sub_batch_id))
    #reset_mytask(files)
    tasks = MyTask.objects.filter(sub_batch_id=sub_batch_id)
    tasks.update(
        fstc_status=0,
        secc_status = 0,
        qa_status = 0,
        qa_account = None,
        isdeal = 0,
        fstc_selected = 0,
        secc_selected = 0,
        fstc_pass = 0,
        secc_pass = 0,
        qa_pass = 0,
    )
    try:
        username = request.session['user']['username']
        role = request.session['user']['role']
    except:
        return render(request, 'home.html', {})
    now = datetime.datetime.utcnow()
    #删除任务包
    packages = Package.objects.filter(sub_batch_id=sub_batch_id)
    for package in packages:
        if package.lab_account != '':
             MyLog.objects.create(log_date=now,action='回收',initiate_user=username,initiate_user_role=role,target_user=package.lab_account,target_user_role='lab',target_package=package,reason='回收任务包'+str(package.id)+'并将所有文件重置到原始状态')
        if package.fstc_account != '':
            MyLog.objects.create(log_date=now,action='回收',initiate_user=username,initiate_user_role=role,target_user=package.fstc_account,target_user_role='fstc',target_package=package,reason='回收任务包'+str(package.id)+'并将所有文件重置到原始状态')
        if package.secc_account != '':
            MyLog.objects.create(log_date=now,action='回收',initiate_user=username,initiate_user_role=role,target_user=package.secc_account,target_user_role='secc',target_package=package,reason='回收任务包'+str(package.id)+'并将所有文件重置到原始状态')
    packages.delete()
    #删除子批次并输出日志
    sub_batches = Sub_Batch.objects.filter(id=sub_batch_id)
    MyLog.objects.create(log_date=now,action='回收',initiate_user=username,initiate_user_role=role,target_user=sub_batches[0].outs_account,target_user_role='outs',reason='回收了子批次'+str(sub_batch_id)+'并将所有文件重置到原始状态')
    sub_batches.delete()
    return HttpResponse('1')

@transaction.atomic
def reset_mytask(files):
    for file in range(files):
        #file.sub_batch = None
        #file.package = None
        file.fstc_status = 0
        file.secc_status = 0
        file.qa_status = 0
        file.qa_account = None
        file.isdeal = 0
        file.fstc_selected = 0
        file.secc_selected = 0
        file.fstc_pass = 0
        file.secc_pass = 0
        file.qa_pass = 0
        file.save()


def sub_batch_list(request):#获取搜索/完整子批次列表,并转向子批次列表页面
    batch_keyword = request.GET.get('batch_keyword')
    sub_batch_id = request.GET.get('sub_batch_id')
    sub_batch_status = request.GET.get('sub_batch_status')
    outs_account = request.GET.get('outs_account')
    try:
        role = request.session['user']['role']
        username = request.session['user']['username']
        superior = request.session['user']['superior']
    except:
        return render(request, 'home.html', {})
    page_num = request.GET.get('currentPage')
    if page_num == '' or page_num == None:
        current_page = 1
    else:
        current_page = int(page_num)
    output_dict = {}
    dict = {}
    is_b_search = 0
    if batch_keyword != None and batch_keyword != '':
        dict['batch_name__contains'] = batch_keyword
        output_dict['batch_keyword'] = batch_keyword
        is_b_search = 1
    if sub_batch_id != None and sub_batch_id != '':
        sub_batch_id = int(sub_batch_id)
        output_dict['sub_batch_id'] = sub_batch_id
        dict['id'] = sub_batch_id
    if sub_batch_status != None and sub_batch_status != '':
        sub_batch_status = int(sub_batch_status)
        output_dict['sub_batch_id'] = sub_batch_id
        dict['status'] = sub_batch_status
    if outs_account != None and outs_account != '':
        output_dict['outs_account'] = outs_account
        dict['outs_account'] = outs_account
    final_output = []
    now = datetime.datetime.utcnow()
    if role == 'ad':
        response = list(Sub_Batch.objects.filter(**dict))
        output = []
        if is_b_search:
            for sub_batch in response:
                files_total = MyTask.objects.filter(sub_batch_id=sub_batch.id).count()
                files_complete = MyTask.objects.filter(sub_batch_id=sub_batch.id, qa_status=1)
                project_name = sub_batch.project.project_name
                if files_complete.count() == files_total and sub_batch.status < 2:
                    sub_batch.status = 2
                    sub_batch.save()
                    MyLog.objects.create(log_date=now,action='通过',initiate_user_role='ad',target_sub_batch=sub_batch,reason='子批次'+str(sub_batch.id)+'已通过QA验收')
                    sub_batch = model_to_dict(sub_batch)
                    files_pass = 0
                    for file in files_complete:
                        if file.qa_pass == 1:
                            files_pass += 1
                    sub_batch['qa_rate'] = str(round(files_pass / files_complete.count() * 100, 2))+'%'
                else:
                    sub_batch = model_to_dict(sub_batch)
                sub_batch['project_name'] = project_name
                sub_batch['compelete_rate'] = str(round(files_complete.count() / files_total * 100, 2)) + '%'
                output.append((edit_distance(batch_keyword,sub_batch['batch_name']),sub_batch))
            final_output = [x[-1] for x in sorted(output,key=lambda x: x[0])]
        else:
            for sub_batch in response:
                files_total = MyTask.objects.filter(sub_batch_id=sub_batch.id).count()
                files_complete = MyTask.objects.filter(sub_batch_id=sub_batch.id, qa_status=1)
                project_name = sub_batch.project.project_name
                if files_complete.count() == files_total and sub_batch.status < 2:
                    sub_batch.status = 2
                    sub_batch.save()
                    MyLog.objects.create(log_date=now,action='通过',initiate_user_role='ad',target_sub_batch=sub_batch,reason='子批次'+str(sub_batch.id)+'已通过QA验收')
                    sub_batch = model_to_dict(sub_batch)
                    files_pass = 0
                    for file in files_complete:
                        if file.qa_pass == 1:
                            files_pass += 1
                    sub_batch['qa_rate'] = str(round(files_pass / files_complete.count() * 100, 2))+'%'
                else:
                    sub_batch = model_to_dict(sub_batch)
                sub_batch['project_name'] = project_name
                if files_total == 0:
                    sub_batch['compelete_rate'] = str(0)
                else:
                    sub_batch['compelete_rate'] = str(round(files_complete.count() / files_total * 100, 2)) + '%'
                final_output.append(sub_batch)
        # 后续更新编辑距离
        paginator = Paginator(final_output, 20)
        output = paginator.page(current_page)
        output_dict['sub_batch_list'] = output
        output_dict['currentPage'] = current_page
        output_dict['page_range'] = paginator.page_range
        return render(request, 'sub_batch_list.html', output_dict)
    elif role == 'outs':
        dict['outs_account'] = username
        response = list(Sub_Batch.objects.filter(**dict).values())
        output = []
        if is_b_search:
            for sub_batch in response:
                sub_batch['unlabelled'] = 0
                sub_batch['labelling'] = 0
                sub_batch['unfstc'] = 0
                sub_batch['fstcing'] = 0
                sub_batch['unsecc'] = 0
                sub_batch['seccing'] = 0
                sub_batch['seccdone'] = 0
                packages = list(Package.objects.filter(sub_batch_id=sub_batch['id']))
                for package in packages:
                    if package.status == 0:
                        sub_batch['unlabelled'] += 1
                    elif package.status == 2:
                        sub_batch['labelling'] += 1
                    elif package.status == 3:
                        sub_batch['unfstc'] += 1
                    elif package.status == 5:
                        sub_batch['fstcing'] += 1
                    elif package.status == 6:
                        sub_batch['unsecc'] += 1
                    elif package.status == 8:
                        sub_batch['seccing'] += 1
                    elif package.status == 9:
                        sub_batch['seccdone'] += 1
                output.append((edit_distance(batch_keyword,sub_batch['batch_name']),sub_batch))
            final_output = [x[-1] for x in sorted(output, key=lambda x: x[0])]
        else:
            for sub_batch in response:
                sub_batch['unlabelled'] = 0
                sub_batch['labelling'] = 0
                sub_batch['unfstc'] = 0
                sub_batch['fstcing'] = 0
                sub_batch['unsecc'] = 0
                sub_batch['seccing'] = 0
                sub_batch['seccdone'] = 0
                packages = list(Package.objects.filter(sub_batch_id=sub_batch['id']))
                for package in packages:
                    if package.status == 0:
                        sub_batch['unlabelled'] += 1
                    elif package.status == 2:
                        sub_batch['labelling'] += 1
                    elif package.status == 3:
                        sub_batch['unfstc'] += 1
                    elif package.status == 5:
                        sub_batch['fstcing'] += 1
                    elif package.status == 6:
                        sub_batch['unsecc'] += 1
                    elif package.status == 8:
                        sub_batch['seccing'] += 1
                    elif package.status == 9:
                        sub_batch['seccdone'] += 1
                final_output.append(sub_batch)
        paginator = Paginator(final_output, 20)
        output = paginator.page(current_page)
        output_dict['sub_batch_list'] = output
        output_dict['currentPage'] = current_page
        output_dict['page_range'] = paginator.page_range
        return render(request, 'sub_batch_list_outs.html', output_dict)
    elif role == 'qaad':
        dict['status__gt'] = 0
        response = list(Sub_Batch.objects.filter(**dict).values())
        output = []
        if is_b_search:
            for sub_batch in response:
                qausers = sub_batch['qa_account'].split(',')
                files_to_qa = MyTask.objects.filter(sub_batch_id=sub_batch['id'],qa_account=username).count()
                sub_batch['files_to_qa'] = files_to_qa
                files_complete = MyTask.objects.filter(sub_batch_id=sub_batch['id'],qa_account=username,qa_status=1)
                if files_complete.count() > 0 and files_complete.count() == files_to_qa:
                    files_pass = 0
                    for file in files_complete:
                        if file.qa_pass == 1:
                            files_pass += 1
                    sub_batch['qa_pass_rate'] = round(files_pass/files_complete.count()*100,2)
                if username in qausers:
                    output.append((edit_distance(batch_keyword,sub_batch['batch_name']),sub_batch))
            final_output = [x[-1] for x in sorted(output, key=lambda x: x[0])]
        else:
            for sub_batch in response:
                qausers = sub_batch['qa_account'].split(',')
                files_to_qa = MyTask.objects.filter(sub_batch_id=sub_batch['id'],qa_account=username).count()
                sub_batch['files_to_qa'] = files_to_qa
                files_complete = MyTask.objects.filter(sub_batch_id=sub_batch['id'],qa_account=username,qa_status=1)
                if files_complete.count() > 0 and files_complete.count() == files_to_qa:
                    files_pass = 0
                    for file in files_complete:
                        if file.qa_pass == 1:
                            files_pass += 1
                    sub_batch['qa_pass_rate'] = round(files_pass/files_complete.count()*100,2)
                if username in qausers:
                    final_output.append(sub_batch)
        # 后续更新编辑距离
        paginator = Paginator(final_output, 20)
        output = paginator.page(current_page)
        output_dict['sub_batch_list'] = output
        output_dict['currentPage'] = current_page
        output_dict['page_range'] = paginator.page_range
        return render(request, 'sub_batch_list_qa.html', output_dict)
    else:
        dict['outs_account'] = superior
    response = list(Sub_Batch.objects.filter(**dict))
    output = []
    for res in response:
        dict_temp = {'sub_batch_id': res.id}
        if role == 'lab':
            dict_temp['status']=0
        elif role == 'fstc':
            dict_temp['status']=3
        elif role == 'secc':
            dict_temp['status']=6
        rest_package = Package.objects.filter(**dict_temp).count()
        files_per_package = MyTask.objects.filter(sub_batch_id=res.id).count()
        res_dict = model_to_dict(res)
        res_dict['rest_package'] = rest_package
        res_dict['files_per_package'] = files_per_package
        output.append(res_dict)
    # 后续更新编辑距离
    paginator = Paginator(output, 20)
    output = paginator.page(current_page)
    output_dict['sub_batch_list'] = output
    output_dict['currentPage'] = current_page
    output_dict['page_range'] = paginator.page_range
    return render(request, 'sub_batch_list_lab.html', output_dict)

def ban_sub_batch(request):
    sub_batch_id = int(request.GET.get('sub_batch_id'))
    sub_batch = Sub_Batch.objects.get(id=sub_batch_id)
    if sub_batch.issetup == 1:
        sub_batch.issetup = 0
        sub_batch.last_status = sub_batch.status
        sub_batch.status = 400
        sub_batch.save()
        packages = list(Package.objects.filter(sub_batch_id=sub_batch_id))
        try:
            username = request.session['user']['username']
            role = request.session['user']['role']
        except:
            return render(request, 'home.html', {})
        now = datetime.datetime.utcnow()
        ban_packages(packages,username,role,now)
        MyLog.objects.create(log_date=now,action='禁用',initiate_user=username,initiate_user_role=role,target_sub_batch=sub_batch,reason='因某些原因禁用了子批次'+str(sub_batch.id))
    return HttpResponse('1')

def allow_sub_batch(request):
    sub_batch_id = int(request.GET.get('sub_batch_id'))
    sub_batch = Sub_Batch.objects.get(id=sub_batch_id)
    if sub_batch.issetup == 0:
        sub_batch.issetup = 1
        sub_batch.status = sub_batch.last_status
        sub_batch.last_status = 0
        sub_batch.save()
        packages = list(Package.objects.filter(sub_batch_id=sub_batch_id))
        try:
            username = request.session['user']['username']
            role = request.session['user']['role']
        except:
            return render(request, 'home.html', {})
        now = datetime.datetime.utcnow()        
        allow_packages(packages,username,role,now)
        MyLog.objects.create(log_date=now,action='启用',initiate_user=username,initiate_user_role=role,target_sub_batch=sub_batch,reason='启用了子批次'+str(sub_batch.id))
    return HttpResponse('1')


def sub_batch_setting(request):#转向子批次详细设置页面
    sub_batch_id = request.GET.get('sub_batch_id')
    sub_batch = Sub_Batch.objects.get(id=sub_batch_id)
    #获取当前超时
    deadline = time.mktime(sub_batch.deadline.timetuple())
    created_date = time.mktime(sub_batch.created_date.timetuple())
    dif = int((deadline-created_date)/3600)
    #dif = sub_batch.deadline
    #获取当前超次
    temp_collect_num = sub_batch.max_collect_num
    return render(request, 'sub_batch_setting.html', {"sub_batch_id": sub_batch_id,"deadline":dif,"collect_num":temp_collect_num})

def set_sub_batch(request):#设置子批次超时超次
    sub_batch_id = int(request.GET.get('sub_batch_id'))
    sub_batch_period = request.GET.get('sub_batch_period')
    sub_batch_collect_num = request.GET.get('sub_batch_collect_num')
    sub_batch = Sub_Batch.objects.get(id=sub_batch_id)
    sentence_period = ''
    sentence_time = ''
    if sub_batch_period != '':
        new_deadline = sub_batch.created_date + datetime.timedelta(hours=int(sub_batch_period))
        sub_batch.deadline = new_deadline
        #sub_batch.deadline = int(sub_batch_period)
        sentence_period = '将时限修改为'+sub_batch_period+'小时'
    if sub_batch_collect_num != '':
        sub_batch.max_collect_num = int(sub_batch_collect_num)
        sentence_time = '将子批次最大提交次数修改为'+sub_batch_collect_num
    if sub_batch_collect_num != '' and sub_batch_period != '':
        split_token = ','
    else:
        split_token = ''
    #这里要添加一个查表判断是否超次进行回收判断
    sub_batch.save()
    try:
        username = request.session['user']['username']
        role = request.session['user']['role']
    except:
        return render(request, 'home.html', {})
    now = datetime.datetime.utcnow()        
    MyLog.objects.create(log_date=now,action='修改',initiate_user=username,initiate_user_role=role,target_sub_batch=sub_batch,reason=sentence_period+split_token+sentence_time)
    return HttpResponse('1')

def reject_sub_batch(request):
    sub_batch_id = request.POST.getlist('sub_batch_id')[0]
    try:
        username = request.session['user']['username']
        role = request.session['user']['role']
    except:
        return render(request, 'home.html', {})
    now = datetime.datetime.utcnow()
    try:
        MyTask.objects.filter(sub_batch_id=sub_batch_id).update(qa_pass=0,qa_status=0,qa_account = None)
    except:
        mytask = MyTask.objects.get(sub_batch_id=sub_batch_id)
        mytask.qa_pass = 0
        mytask.qa_status = 0
        mytask.qa_account = None
        mytask.save()
    try:
        packages = Package.objects.filter(sub_batch_id=sub_batch_id)
        pacakges.update(status=8)
        for package in packages:
            if package.lab_account != '':
                MyLog.objects.create(log_date=now,action='打回',initiate_user=username,initiate_user_role=role,target_user=package.lab_account,target_user_role='lab',target_package=package,reason='QA未通过,打回任务包'+str(package.id))
            if package.fstc_account != '':
                MyLog.objects.create(log_date=now,action='打回',initiate_user=username,initiate_user_role=role,target_user=package.fstc_account,target_user_role='fstc',target_package=package,reason='QA未通过,打回任务包'+str(package.id))
            if package.secc_account != '':
                MyLog.objects.create(log_date=now,action='打回',initiate_user=username,initiate_user_role=role,target_user=package.secc_account,target_user_role='secc',target_package=package,reason='QA未通过,打回任务包'+str(package.id))    
    except:
        package = Package.objects.get(sub_batch_id=sub_batch_id)
        package.status = 8
        package.save()
        if package.lab_account != '':
            MyLog.objects.create(log_date=now,action='打回',initiate_user=username,initiate_user_role=role,target_user=package.lab_account,target_user_role='lab',target_package=package,reason='QA未通过,打回任务包'+str(package.id))
        if package.fstc_account != '':
            MyLog.objects.create(log_date=now,action='打回',initiate_user=username,initiate_user_role=role,target_user=package.fstc_account,target_user_role='fstc',target_package=package,reason='QA未通过,打回任务包'+str(package.id))
        if package.secc_account != '':
            MyLog.objects.create(log_date=now,action='打回',initiate_user=username,initiate_user_role=role,target_user=package.secc_account,target_user_role='secc',target_package=package,reason='QA未通过,打回任务包'+str(package.id))
    sub_batch = Sub_Batch.objects.get(id=sub_batch_id)
    sub_batch.status = 0
    sub_batch.qa_account = ''
    sub_batch.save()
    MyLog.objects.create(log_date=now,action='打回',initiate_user=username,initiate_user_role=role,target_user=sub_batch.outs_account,target_user_role='outs',target_sub_batch=sub_batch,reason='QA未通过,将子批次'+sub_batch.id+'打回')
    return HttpResponse('1')

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值