#coding=utf-8
from celery import task
from struts2 import struts_poc
from struts2.models import Info_Struts2
from struts2.views import *
import time

@task
def deal_url(target_url):

    #for target_url in target_url_list:
    result=struts_poc.struts_019(target_url)
    if result[0]==True:
        struts_019_pic=red_pic_dir
        struts_019_be=True
    else:
        struts_019_pic=green_pic_dir
        struts_019_be=False

    struts_target_url=result[2]


    result=struts_poc.struts_033(target_url)
    if result[0]==True:
        struts_033_pic=red_pic_dir
        struts_033_be=True
    else:
        struts_033_pic=green_pic_dir
        struts_033_be=False


    result=struts_poc.struts_005(target_url)
    if result[0]==True:
        struts_005_pic=red_pic_dir
        struts_005_be=True
    else:
        struts_005_pic=green_pic_dir
        struts_005_be=False


    result=struts_poc.struts_016(target_url)
    if result[0]==True:
        struts_016_pic=red_pic_dir
        struts_016_be=True
    else:
        struts_016_pic=green_pic_dir
        struts_016_be=False



    result=struts_poc.struts_032(target_url)
    if result[0]==True:
        struts_032_pic=red_pic_dir
        struts_032_be=True
    else:
        struts_032_pic=green_pic_dir
        struts_032_be=False


    result=struts_poc.struts_037(target_url)
    if result[0]==True:
        struts_037_pic=red_pic_dir
        struts_037_be=True
    else:
        struts_037_pic=green_pic_dir
        struts_037_be=False



    result=struts_poc.struts_045(target_url)
    if result[0]==True:
        struts_045_pic=red_pic_dir
        struts_045_be=True
    else:
        struts_045_pic=green_pic_dir
        struts_045_be=False

    ip=get_ip_from_domain(target_url)

    #判断数据库中是否存在
    #filterResult=Info_Struts2.objects.filter(target_url=struts_target_url)
    #if (len(filterResult)==0):
        #把扫描结果保存到数据库
    obj_Info_Struts2=Info_Struts2(target_url=struts_target_url,struts_005=struts_005_be,struts_016=struts_016_be,struts_019=struts_019_be,struts_032=struts_032_be,struts_033=struts_033_be,struts_037=struts_037_be,struts_045=struts_045_be,ip=ip)
    obj_Info_Struts2.save()

"""
@task
def mon_deal_url(db_start_num,db_task_num):
    while True:
        if get_db_rows()>=db_start_num+db_task_num:
            send_mail()
            break
        time.sleep(2)
"""

@task
def mon_deal_url(celerytask_result):

    while True:
        result=0
        for item in celerytask_result:
            if item.ready():
                result=result+1
        if result==len(celerytask_result):
            send_mail()
            break
        time.sleep(2)




