<commit_before>"""
    filebrowser.views
    
"""
import logging
import re
import json
from datetime import datetime
from django.http import HttpResponse
from django.shortcuts import render_to_response, render
from django.template import RequestContext, loader
from django.template.loader import get_template
from django.conf import settings
from django.utils.cache import patch_response_headers
#from django.core.urlresolvers import reverse
#from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from .utils import get_rucio_file, get_rucio_pfns_from_guids, fetch_file, get_filebrowser_vo, \
get_filebrowser_hostname

from core.common.models import Filestable4
from core.views import getCacheEntry, setCacheEntry, DateEncoder

_logger = logging.getLogger('bigpandamon-filebrowser')


def index(request):
    """
        index -- filebrowser's default page
        
        :param request: Django's HTTP request 
        :type request: django.http.HttpRequest
        
    """

    # set cache time to store - 10 days
    cacheTime = 10*24*60
    # Here we try to get cached data
    data = getCacheEntry(request, "filebrowser")
    if data is not None:
        data = json.loads(data)
        data['request'] = request
        return render_to_response('filebrowser/filebrowser_index.html', data, content_type='text/html')

    errors = {}

    ### check that all expected parameters are in URL
    expectedFields = ['guid', 'site', 'scope', 'lfn']
    for expectedField in expectedFields:
        try:
            request.GET[expectedField]
        except:
            msg = 'Missing expected GET parameter %s. ' % expectedField
            _logger.error(msg)
            if 'missingparameter' not in errors.keys():
                errors['missingparameter'] = ''
            errors['missingparameter'] += msg

    ### if all expected GET parameters are present, execute file lookup
    pfns = []
    scope = ''
    lfn = ''
    guid = ''
    site = ''
    pattern_string='^[a-zA-Z0-9.\-_]+$'
    pattern_site = '^[a-zA-Z0-9.,\-_]+$'
    pattern_guid='^(\{){0,1}[0-9a-zA-Z]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}(\}){0,1}$'
    try:
        guid = request.GET['guid']
        if re.match(pattern_guid, guid) is None:
            guid = None
            if 'improperformat' not in errors.keys():
                errors['improperformat'] = ''
            errors['improperformat'] += 'guid: %s ' % (request.GET['guid'])
    except:
        pass
    try:
        site = request.GET['site']
        if re.match(pattern_site, site) is None:
            site = None
            if 'improperformat' not in errors.keys():
                errors['improperformat'] = ''
            errors['improperformat'] += 'site: %s ' % (request.GET['site'])
    except:
        pass
    try:
        lfn = request.GET['lfn']
        if re.match(pattern_string, lfn) is None:
            lfn = None
            if 'improperformat' not in errors.keys():
                errors['improperformat'] = ''
            errors['improperformat'] += 'lfn: %s ' % (request.GET['lfn'])
    except:
        pass
    try:
        scope = request.GET['scope']
        if re.match(pattern_string, scope) is None:
            scope = None
            if 'improperformat' not in errors.keys():
                errors['improperformat'] = ''
            errors['improperformat'] += 'scope: %s ' % (request.GET['scope'])
    except:
        pass

    ### download the file
    files = []
    dirprefix = ''
    tardir = ''
    files, errtxt, dirprefix, tardir = get_rucio_file(scope,lfn, guid)
    if not len(files):
        msg = 'File download failed. [guid=%s, site=%s, scope=%s, lfn=%s]' % \
            (guid, site, scope, lfn)
        _logger.warning(msg)
        errors['download'] = msg
    if len(errtxt):
        if 'download' not in errors:
            errors['download'] = ''
        errors['download'] += errtxt

    ### return the file page


    ### set request response data
    data = { \
        'errors': errors, \
        'pfns': pfns, \
        'files': files, \
        'dirprefix': dirprefix, \
        'tardir': tardir, \
        'scope': scope, \
        'lfn': lfn, \
        'site': site, \
        'guid': guid, \
        'MEDIA_URL': settings.MEDIA_URL, \
        'viewParams' : {'MON_VO': str(get_filebrowser_vo()).upper()}, \
        'HOSTNAME': get_filebrowser_hostname() \
#        , 'new_contents': new_contents
    }

    if 'json' not in request.GET:
        return render_to_response('filebrowser/filebrowser_index.html', data, RequestContext(request))
    else:
        setCacheEntry(request, "filebrowser", json.dumps(data, cls=DateEncoder), 60 * cacheTime)
        return HttpResponse(json.dumps(data), content_type='text/html')



def api_single_pandaid(request):
    """
        api_single_pandaid -- return log file URL for a single PanDA job
        
        :param request: Django's HTTP request 
        :type request: django.http.HttpRequest
        
    """
    errors = {}

    ### check that all expected parameters are in URL
#    expectedFields = ['guid', 'site', 'scope', 'lfn']<commit_msg>small fix<commit_after>"""
    filebrowser.views
    
"""
import logging
import re
import json
from datetime import datetime
from django.http import HttpResponse
from django.shortcuts import render_to_response, render
from django.template import RequestContext, loader
from django.template.loader import get_template
from django.conf import settings
from django.utils.cache import patch_response_headers
#from django.core.urlresolvers import reverse
#from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from .utils import get_rucio_file, get_rucio_pfns_from_guids, fetch_file, get_filebrowser_vo, \
get_filebrowser_hostname

from core.common.models import Filestable4
from core.views import getCacheEntry, setCacheEntry, DateEncoder

_logger = logging.getLogger('bigpandamon-filebrowser')


def index(request):
    """
        index -- filebrowser's default page
        
        :param request: Django's HTTP request 
        :type request: django.http.HttpRequest
        
    """

    # set cache time to store - 10 days
    cacheTime = 10*24*60
    # Here we try to get cached data
    data = getCacheEntry(request, "filebrowser")
    if data is not None:
        data = json.loads(data)
        data['request'] = request
        return render_to_response('filebrowser/filebrowser_index.html', data, content_type='text/html')

    errors = {}

    ### check that all expected parameters are in URL
    expectedFields = ['guid', 'site', 'scope', 'lfn']
    for expectedField in expectedFields:
        try:
            request.GET[expectedField]
        except:
            msg = 'Missing expected GET parameter %s. ' % expectedField
            _logger.error(msg)
            if 'missingparameter' not in errors.keys():
                errors['missingparameter'] = ''
            errors['missingparameter'] += msg

    ### if all expected GET parameters are present, execute file lookup
    pfns = []
    scope = ''
    lfn = ''
    guid = ''
    site = ''
    pattern_string='^[a-zA-Z0-9.\-_]+$'
    pattern_site = '^[a-zA-Z0-9.,\-_]+$'
    pattern_guid='^(\{){0,1}[0-9a-zA-Z]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}(\}){0,1}$'
    try:
        guid = request.GET['guid']
        if re.match(pattern_guid, guid) is None:
            guid = None
            if 'improperformat' not in errors.keys():
                errors['improperformat'] = ''
            errors['improperformat'] += 'guid: %s ' % (request.GET['guid'])
    except:
        pass
    try:
        site = request.GET['site']
        if re.match(pattern_site, site) is None:
            site = None
            if 'improperformat' not in errors.keys():
                errors['improperformat'] = ''
            errors['improperformat'] += 'site: %s ' % (request.GET['site'])
    except:
        pass
    try:
        lfn = request.GET['lfn']
        if re.match(pattern_string, lfn) is None:
            lfn = None
            if 'improperformat' not in errors.keys():
                errors['improperformat'] = ''
            errors['improperformat'] += 'lfn: %s ' % (request.GET['lfn'])
    except:
        pass
    try:
        scope = request.GET['scope']
        if re.match(pattern_string, scope) is None:
            scope = None
            if 'improperformat' not in errors.keys():
                errors['improperformat'] = ''
            errors['improperformat'] += 'scope: %s ' % (request.GET['scope'])
    except:
        pass

    ### download the file
    files = []
    dirprefix = ''
    tardir = ''
    files, errtxt, dirprefix, tardir = get_rucio_file(scope,lfn, guid)
    if not len(files):
        msg = 'File download failed. [guid=%s, site=%s, scope=%s, lfn=%s]' % \
            (guid, site, scope, lfn)
        _logger.warning(msg)
        errors['download'] = msg
    if len(errtxt):
        if 'download' not in errors:
            errors['download'] = ''
        errors['download'] += errtxt

    ### return the file page


    ### set request response data
    data = { \
        'errors': errors, \
        'pfns': pfns, \
        'files': files, \
        'dirprefix': dirprefix, \
        'tardir': tardir, \
        'scope': scope, \
        'lfn': lfn, \
        'site': site, \
        'guid': guid, \
        'MEDIA_URL': settings.MEDIA_URL, \
        'viewParams' : {'MON_VO': str(get_filebrowser_vo()).upper()}, \
        'HOSTNAME': get_filebrowser_hostname() \
#        , 'new_contents': new_contents
    }

    if 'json' not in request.GET:
        setCacheEntry(request, "filebrowser", json.dumps(data, cls=DateEncoder), 60 * cacheTime)
        return render_to_response('filebrowser/filebrowser_index.html', data, RequestContext(request))
    else:
        return HttpResponse(json.dumps(data), content_type='text/html')



def api_single_pandaid(request):
    """
        api_single_pandaid -- return log file URL for a single PanDA job
        
        :param request: Django's HTTP request 
        :type request: django.http.HttpRequest
        
    """
    errors = {}

    ### check that all expected parameters are in URL
#    expectedFields = ['guid', 'site', 'scope', 'lfn']
{'fraction_non_alphanumeric': 0.13275024295432458, 'fraction_numerical': 0.006802721088435374, 'mean_word_length': 2.4053606882859033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4194707', 'n_tokens_mistral': 3186, 'n_tokens_neox': 3095, 'n_words': 772}