# Path: ui/tools.py
# def error(message, jump_url='.', msg_title="error", status=0, wait_second=3):
#     json_data = dict()
#     json_data['msg_title'] = msg_title
#     json_data['info'] = str(message)
#     json_data['url'] = jump_url
#     json_data['status'] = status
#     json_data['wait_second'] = wait_second
#     return JsonResponse(json_data)
# 
# def success(message, jump_url='.', msg_title="success", status=1, wait_second=1):
#     json_data = dict()
#     json_data['msg_title'] = msg_title
#     json_data['info'] = message
#     json_data['url'] = jump_url
#     json_data['status'] = status
#     json_data['wait_second'] = wait_second
#     return JsonResponse(json_data)
# 
# def page_info(page_model, page):
#     try:
#         items = page_model.page(page)
#     except PageNotAnInteger:
#         items = page_model.page(1)
#     except EmptyPage:
#         items = page_model.page(page_model.num_pages)
#     return items
# 
# Path: worker/bases.py
# def get_config(section, key, const=0):
#     """
#     Get a configuration
#     :param section: string, the config insides which section
#     :param key: string
#     :param const: int, 0 means to look up user's conf, 1 means to look up bioqueue's conf
#     :return:
#     """
#     try:
#         config = config_init(const)
#         return config.get(section, key)
#     except:
#         return None
# 
# Path: QueueDB/models.py
# class Experiment(models.Model):
#     name = models.CharField(max_length=300)
#     required_fields = models.TextField()
#     file_support = models.CharField(null=True, blank=True, max_length=500)
# 
#     def __str__(self):
#         return self.name
# 
# class Sample(_OwnerModel):
#     name = models.CharField(max_length=500)
#     file_path = models.TextField()  # real file path with uploads as prefix
#     inner_path = models.TextField()  # file path without uploads as prefix
#     experiment = models.ForeignKey(Experiment, on_delete=models.PROTECT, related_name="%(app_label)s_%(class)s_related")
#     attribute = models.TextField(blank=True, null=True)
#     create_time = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return self.name


from django.contrib.auth.decorators import login_required, permission_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render
from django.db.models import Q
from django.template.loader import render_to_string
from functools import reduce
from ..tools import error, success, page_info
from worker.bases import get_config
from ..forms import *
from QueueDB.models import Experiment, Sample
import operator
import os
import base64
import time
    import time
            real_path = ";".join(map(lambda x: "uploads/" + os.path.split(base64.b64decode(x).decode())[1],
                                     sample.inner_path.split(";")))
        else:
            real_path = file_names
        translated_samples.append({
            "id": sample.pk,
            "name": sample.name,
            "create_time": sample.create_time,
            "trace": sample.file_path,
            "inner": sample.inner_path,
            "raw": file_names,
            "rp": real_path,
            "attribute": sample.attribute,
            "file_support": experiment_cache[sample.experiment.id],
            "exp": sample.experiment.name,
        })
    return translated_samples


@login_required
@permission_required("QueueDB.view_sample", raise_exception=True)
def query_job_samples(request):
    if request.method == "POST":
        query_form = SearchJobAssociatedForm(request.POST)
        if query_form.is_valid():
            cd = query_form.cleaned_data
            try:
                if len(cd["files"]) > 0:
                    query_files = [base64.b64decode(qf.encode()).decode() for qf in set(cd["files"].split(";")) if
                                   qf != ""]

# Path: ui/tools.py
# def error(message, jump_url='.', msg_title="error", status=0, wait_second=3):
#     json_data = dict()
#     json_data['msg_title'] = msg_title
#     json_data['info'] = str(message)
#     json_data['url'] = jump_url
#     json_data['status'] = status
#     json_data['wait_second'] = wait_second
#     return JsonResponse(json_data)
# 
# def success(message, jump_url='.', msg_title="success", status=1, wait_second=1):
#     json_data = dict()
#     json_data['msg_title'] = msg_title
#     json_data['info'] = message
#     json_data['url'] = jump_url
#     json_data['status'] = status
#     json_data['wait_second'] = wait_second
#     return JsonResponse(json_data)
# 
# def page_info(page_model, page):
#     try:
#         items = page_model.page(page)
#     except PageNotAnInteger:
#         items = page_model.page(1)
#     except EmptyPage:
#         items = page_model.page(page_model.num_pages)
#     return items
# 
# Path: worker/bases.py
# def get_config(section, key, const=0):
#     """
#     Get a configuration
#     :param section: string, the config insides which section
#     :param key: string
#     :param const: int, 0 means to look up user's conf, 1 means to look up bioqueue's conf
#     :return:
#     """
#     try:
#         config = config_init(const)
#         return config.get(section, key)
#     except:
#         return None
# 
# Path: QueueDB/models.py
# class Experiment(models.Model):
#     name = models.CharField(max_length=300)
#     required_fields = models.TextField()
#     file_support = models.CharField(null=True, blank=True, max_length=500)
# 
#     def __str__(self):
#         return self.name
# 
# class Sample(_OwnerModel):
#     name = models.CharField(max_length=500)
#     file_path = models.TextField()  # real file path with uploads as prefix
#     inner_path = models.TextField()  # file path without uploads as prefix
#     experiment = models.ForeignKey(Experiment, on_delete=models.PROTECT, related_name="%(app_label)s_%(class)s_related")
#     attribute = models.TextField(blank=True, null=True)
#     create_time = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return self.name


# Path: ui/views/Samples.py
from django.contrib.auth.decorators import login_required, permission_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render
from django.db.models import Q
from django.template.loader import render_to_string
from functools import reduce
from ..tools import error, success, page_info
from worker.bases import get_config
from ..forms import *
from QueueDB.models import Experiment, Sample
import operator
import os
import base64
import time
    import time

            real_path = ";".join(map(lambda x: "uploads/" + os.path.split(base64.b64decode(x).decode())[1],
                                     sample.inner_path.split(";")))
        else:
            real_path = file_names
        translated_samples.append({
            "id": sample.pk,
            "name": sample.name,
            "create_time": sample.create_time,
            "trace": sample.file_path,
            "inner": sample.inner_path,
            "raw": file_names,
            "rp": real_path,
            "attribute": sample.attribute,
            "file_support": experiment_cache[sample.experiment.id],
            "exp": sample.experiment.name,
        })
    return translated_samples


@login_required
@permission_required("QueueDB.view_sample", raise_exception=True)
def query_job_samples(request):
    if request.method == "POST":
        query_form = SearchJobAssociatedForm(request.POST)
        if query_form.is_valid():
            cd = query_form.cleaned_data
            try:
                if len(cd["files"]) > 0:
                    query_files = [base64.b64decode(qf.encode()).decode() for qf in set(cd["files"].split(";")) if
                                   qf != ""]

                    samples = Sample.objects.filter(user=request.user.queuedb_profile_related.delegate)