# 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
# 
# def rand_sig():
#     """
#     Generate a random signature
#     :return: string
#     """
#     import datetime
#     import hashlib
#     sig = hashlib.md5(str(datetime.datetime.now()).encode())
#     return sig.hexdigest()
# 
# def get_user_folder_size(user):
#     kb_to_mb = 1073741824
#     user_path = os.path.join(get_config('env', 'workspace'), str(user))
#     return round(get_folder_size(user_path)/kb_to_mb)


from django.http import JsonResponse, StreamingHttpResponse
from worker.bases import get_config, rand_sig, get_user_folder_size
from django.core.paginator import EmptyPage, PageNotAnInteger
    from django.contrib.auth.models import User
import os
    import json
    import os
    import os
    import platform


def build_json_protocol(protocol):
    """
    response = StreamingHttpResponse(json.dumps(protocol))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(protocol['name']+'.txt')
    return response
    """
    return json.dumps(protocol)


def build_json_reference(ref):
    result = list()
    for value in ref:
        result.append('{'+value+'}')
    return JsonResponse(','.join(result), safe=False)


def check_user_existence(username):
    try:
        u = User.objects.get(username=username)
        return u.id
    except Exception as e:
        return 0


def check_disk_quota_lock(user):

# 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
# 
# def rand_sig():
#     """
#     Generate a random signature
#     :return: string
#     """
#     import datetime
#     import hashlib
#     sig = hashlib.md5(str(datetime.datetime.now()).encode())
#     return sig.hexdigest()
# 
# def get_user_folder_size(user):
#     kb_to_mb = 1073741824
#     user_path = os.path.join(get_config('env', 'workspace'), str(user))
#     return round(get_folder_size(user_path)/kb_to_mb)


# Path: ui/tools.py
from django.http import JsonResponse, StreamingHttpResponse
from worker.bases import get_config, rand_sig, get_user_folder_size
from django.core.paginator import EmptyPage, PageNotAnInteger
    from django.contrib.auth.models import User
import os
    import json
    import os
    import os
    import platform



def build_json_protocol(protocol):
    """
    response = StreamingHttpResponse(json.dumps(protocol))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(protocol['name']+'.txt')
    return response
    """
    return json.dumps(protocol)


def build_json_reference(ref):
    result = list()
    for value in ref:
        result.append('{'+value+'}')
    return JsonResponse(','.join(result), safe=False)


def check_user_existence(username):
    try:
        u = User.objects.get(username=username)
        return u.id
    except Exception as e:
        return 0


def check_disk_quota_lock(user):

    disk_limit = get_config('env', 'disk_quota')