<commit_before>from django.shortcuts import render_to_response
from django.template import RequestContext
import requests, json
from django.conf import settings
from django.http import HttpResponse
from bii_webapp.settings import common
from django.shortcuts import redirect
from django.contrib.auth import decorators, views
from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache
import re

cache.clear()
cache_time=25102040

@csrf_exempt
@decorators.login_required(login_url=views.login)
def deleteInvestigation(request):
    url = settings.WEBSERVICES_URL + 'update/delete'
    try:
        r = requests.post(url, data=request.body, timeout=20);
    except requests.exceptions.Timeout:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Service timeout", "total": 1}}))
    except Exception:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Server error", "total": 1}}))

    loaded = json.loads(r.content)
    if 'ERROR' in loaded:
        return HttpResponse(json.dumps(loaded))

    print 'investigation deleted'
    cache.delete('browse')
    loaded = json.loads(request.body)
    inv = cache.get(loaded['pk'])
    studies = inv['i_studies']
    for study in studies:
        assays = study['s_assays']
        for assay in assays:
            cache.delete((str)(study['s_id']) + "_" + (str)(assay['measurement']) + "_" + (str)(assay['technology'
                                                                                                      '']))
        cache.delete(study['s_id'])

    cache.delete(loaded['pk'])

    return HttpResponse(json.dumps(loaded))


@csrf_exempt
@decorators.login_required(login_url=views.login)
def deleteStudy(request):
    url = settings.WEBSERVICES_URL + 'update/delete'
    try:
        r = requests.post(url, data=request.body, timeout=20);
    except requests.exceptions.Timeout:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Service timeout", "total": 1}}))
    except Exception:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Server error", "total": 1}}))

    loaded = json.loads(r.content)
    if 'ERROR' in loaded:
        return HttpResponse(json.dumps(loaded))

    print 'study deleted'
    cache.delete('browse')
    loaded = json.loads(request.body)

    study = cache.get(loaded['pk'])
    assays = study['s_assays']
    for assay in assays:
        cache.delete((str)(study['s_id']) + "_" + (str)(assay['measurement']) + "_" + (str)(assay['technology']))
    cache.delete(loaded['pk'])

    return HttpResponse(json.dumps(loaded))


@csrf_exempt
@decorators.login_required(login_url=views.login)
def updateInvestigation(request):
    data = request.POST.copy()
    data['type'] = 'investigation';
    url = settings.WEBSERVICES_URL + 'update'
    try:
        r = requests.post(url, data=json.dumps(data),timeout=20);
    except requests.exceptions.Timeout:
        return HttpResponse(json.dumps({"ERROR":{"messages":"Web Service timeout","total":1}}))
    except Exception:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Server error", "total": 1}}))

    loaded = json.loads(r.content)
    if 'ERROR' in loaded:
        return HttpResponse(json.dumps(loaded))

    loaded['field'] = field = data['name']

    pk = request.POST['pk']

    investigation = cache.get(pk)
    if (investigation != None):
        investigation[loaded['field']] = data['value']
        cache.set(pk, investigation)

    return HttpResponse(json.dumps(loaded))


def updateBrowseCache(pk, field, value):
    browse = cache.get('browse')
    if browse == None:
        return

    found = False
    results = json.loads(browse['results'])
    if 'studies' in results:
        studies = results['studies']
        for s in studies:
            if s['s_id'] == pk:
                found = True
                s[field] = value
                break
        if not found:
            investigations = results['investigations']
            for i in investigations:
                for s in i['i_studies']:
                    if s['s_id'] == pk:
                        s[field] = value
                        break

    browse['results'] = json.dumps(results)
    cache.set('browse', browse)


@csrf_exempt
@decorators.login_required(login_url=views.login)
def updateStudy(request):
    data = request.POST.copy()
    data['type'] = 'study';
    url = settings.WEBSERVICES_URL + 'update'
    url2 = settings.WEBSERVICES_URL + 'rollback'
    try:
        r = requests.post(url, data=json.dumps(data),timeout=20)
    except requests.exceptions.Timeout:
        return HttpResponse(json.dumps({"ERROR":{"messages":"Web Service timeout","total":1}}))
    except Exception:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Server error", "total": 1}}))

    loaded = json.loads(r.content)
    if 'ERROR' in loaded:
        return HttpResponse(json.dumps(loaded))

    loaded['field'] = field = data['name']

    pk = request.POST['pk']
    updateBrowseCache(pk, field, data['value'])

    study = cache.get(pk)
    if (study != None):
        study[loaded['field']] = data['value']
        cache.set(pk, study)

    return HttpResponse(json.dumps(loaded))


def browse(request, page=1, msg=None):
    loaded = cache.get('browse')
    try:
        if loaded == None or loaded['page'] != page:
            username = "default"
            if request.user:
                username = request.user.username

            r = requests.post(settings.WEBSERVICES_URL + 'retrieve/browse',
                              data=json.dumps({'username': username, 'page': page}),
                              headers={'Cache-Control': 'no-cache'})

            if r.status_code != 200:
                return render_to_response("browse.html",
                                          {"data": {"ERROR": {"messages": "Web Service failed", "total": 1}},
                                           'number_of_pages': 0, 'current_page': 0,
                                           'pageNotice': 'This page shows the accessible studies for your account, click on each to get more details'},
                                          context_instance=RequestContext(request))

            loaded = json.loads(r.content)

            if 'ERROR' in loaded:
                if (int)(page) != 1:
                    return redirect(browse, 1)
                else:
                    return render_to_response("browse.html", {"data": loaded, 'number_of_pages': 0, 'current_page': 0,
                                                              'pageNotice': 'This page shows the accessible studies for your account, click on each to get more details'},
                                              context_instance=RequestContext(request))

            if 'INFO' in loaded:
                return render_to_response("browse.html", {"data": loaded, 'number_of_pages': 0, 'current_page': 0,
                                                          'pageNotice': 'This page shows the accessible studies for your account, click on each to get more details'},<commit_msg>Changed info for browse view<commit_after>from django.shortcuts import render_to_response
from django.template import RequestContext
import requests, json
from django.conf import settings
from django.http import HttpResponse
from bii_webapp.settings import common
from django.shortcuts import redirect
from django.contrib.auth import decorators, views
from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache
import re

cache.clear()
cache_time=25102040

@csrf_exempt
@decorators.login_required(login_url=views.login)
def deleteInvestigation(request):
    url = settings.WEBSERVICES_URL + 'update/delete'
    try:
        r = requests.post(url, data=request.body, timeout=20);
    except requests.exceptions.Timeout:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Service timeout", "total": 1}}))
    except Exception:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Server error", "total": 1}}))

    loaded = json.loads(r.content)
    if 'ERROR' in loaded:
        return HttpResponse(json.dumps(loaded))

    print 'investigation deleted'
    cache.delete('browse')
    loaded = json.loads(request.body)
    inv = cache.get(loaded['pk'])
    studies = inv['i_studies']
    for study in studies:
        assays = study['s_assays']
        for assay in assays:
            cache.delete((str)(study['s_id']) + "_" + (str)(assay['measurement']) + "_" + (str)(assay['technology'
                                                                                                      '']))
        cache.delete(study['s_id'])

    cache.delete(loaded['pk'])

    return HttpResponse(json.dumps(loaded))


@csrf_exempt
@decorators.login_required(login_url=views.login)
def deleteStudy(request):
    url = settings.WEBSERVICES_URL + 'update/delete'
    try:
        r = requests.post(url, data=request.body, timeout=20);
    except requests.exceptions.Timeout:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Service timeout", "total": 1}}))
    except Exception:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Server error", "total": 1}}))

    loaded = json.loads(r.content)
    if 'ERROR' in loaded:
        return HttpResponse(json.dumps(loaded))

    print 'study deleted'
    cache.delete('browse')
    loaded = json.loads(request.body)

    study = cache.get(loaded['pk'])
    assays = study['s_assays']
    for assay in assays:
        cache.delete((str)(study['s_id']) + "_" + (str)(assay['measurement']) + "_" + (str)(assay['technology']))
    cache.delete(loaded['pk'])

    return HttpResponse(json.dumps(loaded))


@csrf_exempt
@decorators.login_required(login_url=views.login)
def updateInvestigation(request):
    data = request.POST.copy()
    data['type'] = 'investigation';
    url = settings.WEBSERVICES_URL + 'update'
    try:
        r = requests.post(url, data=json.dumps(data),timeout=20);
    except requests.exceptions.Timeout:
        return HttpResponse(json.dumps({"ERROR":{"messages":"Web Service timeout","total":1}}))
    except Exception:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Server error", "total": 1}}))

    loaded = json.loads(r.content)
    if 'ERROR' in loaded:
        return HttpResponse(json.dumps(loaded))

    loaded['field'] = field = data['name']

    pk = request.POST['pk']

    investigation = cache.get(pk)
    if (investigation != None):
        investigation[loaded['field']] = data['value']
        cache.set(pk, investigation)

    return HttpResponse(json.dumps(loaded))


def updateBrowseCache(pk, field, value):
    browse = cache.get('browse')
    if browse == None:
        return

    found = False
    results = json.loads(browse['results'])
    if 'studies' in results:
        studies = results['studies']
        for s in studies:
            if s['s_id'] == pk:
                found = True
                s[field] = value
                break
        if not found:
            investigations = results['investigations']
            for i in investigations:
                for s in i['i_studies']:
                    if s['s_id'] == pk:
                        s[field] = value
                        break

    browse['results'] = json.dumps(results)
    cache.set('browse', browse)


@csrf_exempt
@decorators.login_required(login_url=views.login)
def updateStudy(request):
    data = request.POST.copy()
    data['type'] = 'study';
    url = settings.WEBSERVICES_URL + 'update'
    url2 = settings.WEBSERVICES_URL + 'rollback'
    try:
        r = requests.post(url, data=json.dumps(data),timeout=20)
    except requests.exceptions.Timeout:
        return HttpResponse(json.dumps({"ERROR":{"messages":"Web Service timeout","total":1}}))
    except Exception:
        return HttpResponse(json.dumps({"ERROR": {"messages": "Web Server error", "total": 1}}))

    loaded = json.loads(r.content)
    if 'ERROR' in loaded:
        return HttpResponse(json.dumps(loaded))

    loaded['field'] = field = data['name']

    pk = request.POST['pk']
    updateBrowseCache(pk, field, data['value'])

    study = cache.get(pk)
    if (study != None):
        study[loaded['field']] = data['value']
        cache.set(pk, study)

    return HttpResponse(json.dumps(loaded))


def browse(request, page=1, msg=None):
    loaded = cache.get('browse')
    try:
        if loaded == None or loaded['page'] != page:
            username = "default"
            if request.user:
                username = request.user.username

            r = requests.post(settings.WEBSERVICES_URL + 'retrieve/browse',
                              data=json.dumps({'username': username, 'page': page}),
                              headers={'Cache-Control': 'no-cache'})

            if r.status_code != 200:
                if username == "default":
                    text = 'This page shows the publicly accessible ISA-TAB datasets, click on each investigation/study/assay to get more details'
                else:
                    text = 'This page shows publicly accessible ISA-TAB datasets and those accessible for your account, click on each investigation/study/assay to get more details'
                return render_to_response("browse.html",
                                          {"data": {"ERROR": {"messages": "Web Service failed", "total": 1}},
                                           'number_of_pages': 0, 'current_page': 0,
                                           'pageNotice': text},
                                          context_instance=RequestContext(request))


            loaded = json.loads(r.content)

            if 'ERROR' in loaded:
                if (int)(page) != 1:
                    return redirect(browse, 1)
                else:
                    return render_to_response("browse.html", {"data": loaded, 'number_of_pages': 0, 'current_page': 0,
                                                              'pageNotice': 'This page shows the accessible studies for your account, click on each to get more details'},
                                              context_instance=RequestContext(request))

            if 'INFO' in loaded:
                return render_to_response("browse.html", {"data": loaded, 'number_of_pages': 0, 'current_page': 0,
                                                          'pageNotice': 'This page shows the accessible studies for your account, click on each to get more details'},
{'fraction_non_alphanumeric': 0.12861846618191797, 'fraction_numerical': 0.005213335162573741, 'mean_word_length': 2.424712238665727, 'pattern_counts': {'":': 61, '<': 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': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5144223', 'n_tokens_mistral': 4209, 'n_tokens_neox': 3978, 'n_words': 977}