# Path: utils/file_operator.py
# def file_upload_response(request):
#     f = file_upload_save_process(request)
#     data = [{'name': f.title,
#              'id': f.fid,
#              'type': f.file_type}]
# 
#     response = JSONResponse(data, {}, response_minetype(request))
#     response["Content-Dispostion"] = "inline; filename=files.json"
#     return response
# 
# Path: chemistry/util.py
# def singletask_details(pid):
#     from chemistry.tasks import has_temperature
#     single_task = get_object_or_404(SingleTask, pid=pid)
#     if not has_temperature(single_task.model.desc):
#         single_task.temperature = '--'
#     single_task.result_value, single_task.hi, single_task.hx, single_task.degrade = get_singletask_area(single_task.results)
#     try:
#         local_search_id = single_task.file_obj.local_search_id
#         if local_search_id:
#             local_search = ChemInfoLocal.objects.get(id=local_search_id)
#         else:
#             local_search = None
#     except Exception:
#         chemistry_logger.exception('failed to get cheminfo by local_search_id')
#         local_search = None
# 
#     return dict(singletask=single_task,
#                 search_engine=local_search)
# 
# def suitetask_details(sid):
#     from chemistry.tasks import has_temperature
#     suitetask = get_object_or_404(SuiteTask, sid=sid)
#     single_lists = SingleTask.objects.filter(sid=sid, is_hide=False)
# 
#     for s in single_lists:
#         if not has_temperature(s.model.desc):
#             s.temperature = '--'
#         s.result_value, s.hi, s.hx, s.degrade = get_singletask_area(s.results)
# 
#     return dict(suitetask=suitetask,
#                 single_lists=single_lists)
# 
# def get_models_selector(models_str):
#     """get models name and color flag"""
#     colors = ("label-success", "label-warning", "label-primary",
#               "label-info", "label-danger", "label-default")
# 
#     models_list = models_str.split(MODEL_SPLITS)
#     result = []
#     for i in range(0, len(models_list)):
#         e = {}
#         e["color"] = colors[i % len(colors)]
#         e["value"] = models_list[i]
#         result.append(e)
# 
#     return result


from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect
from utils import is_client, chemistry_logger
from utils.file_operator import file_upload_response
from chemistry.util import (singletask_details, suitetask_details,
                            get_models_selector)
from chemistry.models import SuiteTask, SingleTask
#coding: utf-8




@csrf_exempt
@login_required
def submit(request):
    if request.method == "POST" and request.FILES:
        return file_upload_response(request)
    return render(request, "newtask.html")


@login_required
def history(request):
    #TODO: Add pagination
    results = SuiteTask.objects.filter(user__user=request.user,
            is_hide=False).order_by('-start_time')

    #show_all = request.META.get('show_all', '0') == '1'
    #if not show_all:
    #    pass
        #results = results.filter(is_hide=True)

    for r in results:
        r.models_str_list = get_models_selector(r.models_str)

# Path: utils/file_operator.py
# def file_upload_response(request):
#     f = file_upload_save_process(request)
#     data = [{'name': f.title,
#              'id': f.fid,
#              'type': f.file_type}]
# 
#     response = JSONResponse(data, {}, response_minetype(request))
#     response["Content-Dispostion"] = "inline; filename=files.json"
#     return response
# 
# Path: chemistry/util.py
# def singletask_details(pid):
#     from chemistry.tasks import has_temperature
#     single_task = get_object_or_404(SingleTask, pid=pid)
#     if not has_temperature(single_task.model.desc):
#         single_task.temperature = '--'
#     single_task.result_value, single_task.hi, single_task.hx, single_task.degrade = get_singletask_area(single_task.results)
#     try:
#         local_search_id = single_task.file_obj.local_search_id
#         if local_search_id:
#             local_search = ChemInfoLocal.objects.get(id=local_search_id)
#         else:
#             local_search = None
#     except Exception:
#         chemistry_logger.exception('failed to get cheminfo by local_search_id')
#         local_search = None
# 
#     return dict(singletask=single_task,
#                 search_engine=local_search)
# 
# def suitetask_details(sid):
#     from chemistry.tasks import has_temperature
#     suitetask = get_object_or_404(SuiteTask, sid=sid)
#     single_lists = SingleTask.objects.filter(sid=sid, is_hide=False)
# 
#     for s in single_lists:
#         if not has_temperature(s.model.desc):
#             s.temperature = '--'
#         s.result_value, s.hi, s.hx, s.degrade = get_singletask_area(s.results)
# 
#     return dict(suitetask=suitetask,
#                 single_lists=single_lists)
# 
# def get_models_selector(models_str):
#     """get models name and color flag"""
#     colors = ("label-success", "label-warning", "label-primary",
#               "label-info", "label-danger", "label-default")
# 
#     models_list = models_str.split(MODEL_SPLITS)
#     result = []
#     for i in range(0, len(models_list)):
#         e = {}
#         e["color"] = colors[i % len(colors)]
#         e["value"] = models_list[i]
#         result.append(e)
# 
#     return result


# Path: chemistry/views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseRedirect
from utils import is_client, chemistry_logger
from utils.file_operator import file_upload_response
from chemistry.util import (singletask_details, suitetask_details,
                            get_models_selector)
from chemistry.models import SuiteTask, SingleTask

#coding: utf-8




@csrf_exempt
@login_required
def submit(request):
    if request.method == "POST" and request.FILES:
        return file_upload_response(request)
    return render(request, "newtask.html")


@login_required
def history(request):
    #TODO: Add pagination
    results = SuiteTask.objects.filter(user__user=request.user,
            is_hide=False).order_by('-start_time')

    #show_all = request.META.get('show_all', '0') == '1'
    #if not show_all:
    #    pass
        #results = results.filter(is_hide=True)

    for r in results:
        r.models_str_list = get_models_selector(r.models_str)

        r.models_category_str_list = get_models_selector(r.models_category_str)