# Path: core/config.py

# 
# Path: core/api/crud.py
# def get(collection, criteria_projection_order):
# def remove(collection, criterias = []):
# def add(collection, documents_list):
# def update(collection, document):
# 
# Path: core/validation/validation.py
# class TSValidationError(Exception):
# def recursive_merge(container, container_override):
# def recursive_replace(container, replace_function):
# def _replace_function_sanitize_objectify_json(container):
# def sanitize_objectify_json(json_in):
# def _replace_function_stringify_objectid_json(container):
# def stringify_objectid_cursor(cursor_in):
# def _replace_function_stringify_objectid_list(container):
# def stringify_objectid_list(list_in):
# def validate_json_list(collection, list_in):
# def validate_request(collection, json_in):
# def update_password_salt_user_list(collection, list_in):
# def update_password_salt_user_json(json_in):
# def get_password_salt(password_in):
# 
# Path: core/validation/permissions.py
# def check_datamine_permissions(action, document):
#     # Get datamine permissions
#     
#     try:
#         restrictions = cherrypy.session['_ts_user']['restrictions'][action]
#     except (KeyError, TypeError):
#         restrictions = None
#         
#     if not restrictions:
#         raise TSValidationError("Access to '%s' is restricted for current user" % (action))
#     elif restrictions != True:
#         validator(restrictions, format_checker=jsonschema.FormatChecker()).validate(document)


import tempfile, os, shutil, cherrypy, time
import logging
from core.config import conf_uploads
from core.api.crud import db
from core.validation.validation import TSValidationError, validate_request, cgi, ObjectId, sanitize_objectify_json
from core.validation.permissions import check_datamine_permissions
    
    # Check permissions  
    check_datamine_permissions('file_download', cherrypy.request.params)
    
    upload_id = ObjectId(upload_id)
    
    json_found = db.upload.find_one({ '_id' : upload_id }, { 'content_type' : 1})
    file_path = os.path.abspath(os.path.join(conf_uploads['folder'], str(upload_id)))
    
    cherrypy.log('%s' % (upload_id), context = 'TS.FILE.DOWNLOAD.upload_id', severity = db_log_severity)
    
    if not json_found:
        raise TSValidationError("File id not found")
    
    return file_path, json_found['content_type']


def remove(upload_ids):

    check_datamine_permissions('file_remove', cherrypy.request.params)
    
    # Sanify upload_id (to match with sanified documents)
    # TODO: check why does not objectify lists
    #upload_ids = sanitize_objectify_json(upload_ids)
    
    cherrypy.log('%s' % (str(upload_ids)), context = 'TS.FILE.REMOVE.upload_ids', severity = db_log_severity)
    
    # Requests
    for upload_id in upload_ids:
        

# Path: core/config.py

# 
# Path: core/api/crud.py
# def get(collection, criteria_projection_order):
# def remove(collection, criterias = []):
# def add(collection, documents_list):
# def update(collection, document):
# 
# Path: core/validation/validation.py
# class TSValidationError(Exception):
# def recursive_merge(container, container_override):
# def recursive_replace(container, replace_function):
# def _replace_function_sanitize_objectify_json(container):
# def sanitize_objectify_json(json_in):
# def _replace_function_stringify_objectid_json(container):
# def stringify_objectid_cursor(cursor_in):
# def _replace_function_stringify_objectid_list(container):
# def stringify_objectid_list(list_in):
# def validate_json_list(collection, list_in):
# def validate_request(collection, json_in):
# def update_password_salt_user_list(collection, list_in):
# def update_password_salt_user_json(json_in):
# def get_password_salt(password_in):
# 
# Path: core/validation/permissions.py
# def check_datamine_permissions(action, document):
#     # Get datamine permissions
#     
#     try:
#         restrictions = cherrypy.session['_ts_user']['restrictions'][action]
#     except (KeyError, TypeError):
#         restrictions = None
#         
#     if not restrictions:
#         raise TSValidationError("Access to '%s' is restricted for current user" % (action))
#     elif restrictions != True:
#         validator(restrictions, format_checker=jsonschema.FormatChecker()).validate(document)


# Path: core/api/uploads.py
import tempfile, os, shutil, cherrypy, time
import logging
from core.config import conf_uploads
from core.api.crud import db
from core.validation.validation import TSValidationError, validate_request, cgi, ObjectId, sanitize_objectify_json
from core.validation.permissions import check_datamine_permissions

    
    # Check permissions  
    check_datamine_permissions('file_download', cherrypy.request.params)
    
    upload_id = ObjectId(upload_id)
    
    json_found = db.upload.find_one({ '_id' : upload_id }, { 'content_type' : 1})
    file_path = os.path.abspath(os.path.join(conf_uploads['folder'], str(upload_id)))
    
    cherrypy.log('%s' % (upload_id), context = 'TS.FILE.DOWNLOAD.upload_id', severity = db_log_severity)
    
    if not json_found:
        raise TSValidationError("File id not found")
    
    return file_path, json_found['content_type']


def remove(upload_ids):

    check_datamine_permissions('file_remove', cherrypy.request.params)
    
    # Sanify upload_id (to match with sanified documents)
    # TODO: check why does not objectify lists
    #upload_ids = sanitize_objectify_json(upload_ids)
    
    cherrypy.log('%s' % (str(upload_ids)), context = 'TS.FILE.REMOVE.upload_ids', severity = db_log_severity)
    
    # Requests
    for upload_id in upload_ids:
        

        db.upload.remove({ '_id' : ObjectId(upload_id) })