<commit_before>from copy import deepcopy
import json
import re
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib import messages
from django.http import StreamingHttpResponse
from django.http import HttpResponse, JsonResponse
from models import Cohort, Workbook, Worksheet, Worksheet_comment, Worksheet_variable, Worksheet_gene, Worksheet_cohort, Worksheet_plot, Worksheet_plot_cohort
from variables.models import VariableFavorite, Variable
from genes.models import GeneFavorite
from analysis.models import Analysis
from projects.models import Project
from sharing.service import create_share
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
debug = settings.DEBUG
if settings.DEBUG :
    import sys

@login_required
def workbook_list(request):
    template  = 'workbooks/workbook_list.html',

    userWorkbooks = request.user.workbook_set.all()
    sharedWorkbooks = Workbook.objects.filter(shared__matched_user=request.user, shared__active=True, active=True)

    workbooks = userWorkbooks | sharedWorkbooks
    workbooks = workbooks.distinct()

    return render(request, template, {'workbooks' : workbooks})

def workbook_samples(request):
    template = 'workbooks/workbook_samples.html'
    return render(request, template, {
        'workbooks': Workbook.objects.all().filter(is_public=True, active=True)
    })


#TODO secure this url
@login_required
def workbook_create_with_cohort(request):
    cohort_id       = request.POST.get('cohort_id')
    cohort          = Cohort.objects.get(id=cohort_id)
    workbook_model  = Workbook.create(name="Untitled Workbook", description="This workbook was created with cohort \"" + cohort.name + "\" added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
    worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)
    worksheet_model.add_cohort(cohort=cohort)

    redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
    return redirect(redirect_url)

@login_required
def workbook_create_with_cohort_list(request):
    cohort_ids = json.loads(request.body)['cohorts']
    if len(cohort_ids) > 0 :
        workbook_model  = Workbook.create(name="Untitled Workbook", description="This is a workbook created with cohorts added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
        worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)
        for id in cohort_ids :
            cohort = Cohort.objects.get(id=id)
            worksheet_model.add_cohort(cohort=cohort)

        result = {'workbook_id'  : workbook_model.id,
                  'worksheet_id' : worksheet_model.id}
    else :
        result = {'error' : 'parameters are not correct'}

    return HttpResponse(json.dumps(result), status=200)

#TODO not complete
@login_required
def workbook_create_with_project(request):
    project_id = request.POST.get('project_id')
    project_model = Project.get(id=project_id)

    workbook_model = Workbook.create(name="Untitled Workbook", description="this is an untitled workbook with all variables of project \"" + project_model.name + "\" added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
    worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)

    #add every variable within the model
    for study in project_model.study_set.all().filter(active=True) :
        for var in study.user_feature_definitions_set.all() :
            work_var = Worksheet_variable.objects.create(worksheet_id = worksheet_model.id,
                                              name         = var.feature_name,
                                              url_code     = var.bq_map_id,
                                              feature_id   = var.id)
            work_var.save()

    redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
    return redirect(redirect_url)

@login_required
def workbook_create_with_variables(request):
    var_list_id = request.POST.get('variable_list_id')
    var_list_model = VariableFavorite.objects.get(id=var_list_id)

    workbook_model = Workbook.create(name="Untitled Workbook", description="this is an untitled workbook with all variables of variable favorite list \"" + var_list_model.name + "\" added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
    worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)

    for var in var_list_model.get_variables() :
        work_var = Worksheet_variable.objects.create(worksheet_id = worksheet_model.id,
                                          name         = var.name,
                                          url_code     = var.code,
                                          feature_id   = var.feature_id)

        work_var.save()

    redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
    return redirect(redirect_url)

@login_required
def workbook_create_with_analysis(request):
    analysis_type   = request.POST.get('analysis')

    allowed_types = Analysis.get_types()
    redirect_url = reverse('sample_analyses')
    for type in allowed_types :
        if analysis_type == type['name'] :
            workbook_model  = Workbook.create(name="Untitled Workbook", description="this is an untitled workbook with a \"" + analysis_type + "\" plot added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
            worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)
            worksheet_model.set_plot(type=analysis_type)
            redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
            break

    return redirect(redirect_url)
<commit_msg>fixed #135 change the name of the post id accepted in the view function for creating workbooks from variable lists<commit_after>from copy import deepcopy
import json
import re
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib import messages
from django.http import StreamingHttpResponse
from django.http import HttpResponse, JsonResponse
from models import Cohort, Workbook, Worksheet, Worksheet_comment, Worksheet_variable, Worksheet_gene, Worksheet_cohort, Worksheet_plot, Worksheet_plot_cohort
from variables.models import VariableFavorite, Variable
from genes.models import GeneFavorite
from analysis.models import Analysis
from projects.models import Project
from sharing.service import create_share
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
debug = settings.DEBUG
if settings.DEBUG :
    import sys

@login_required
def workbook_list(request):
    template  = 'workbooks/workbook_list.html',

    userWorkbooks = request.user.workbook_set.all()
    sharedWorkbooks = Workbook.objects.filter(shared__matched_user=request.user, shared__active=True, active=True)

    workbooks = userWorkbooks | sharedWorkbooks
    workbooks = workbooks.distinct()

    return render(request, template, {'workbooks' : workbooks})

def workbook_samples(request):
    template = 'workbooks/workbook_samples.html'
    return render(request, template, {
        'workbooks': Workbook.objects.all().filter(is_public=True, active=True)
    })


#TODO secure this url
@login_required
def workbook_create_with_cohort(request):
    cohort_id       = request.POST.get('cohort_id')
    cohort          = Cohort.objects.get(id=cohort_id)
    workbook_model  = Workbook.create(name="Untitled Workbook", description="This workbook was created with cohort \"" + cohort.name + "\" added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
    worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)
    worksheet_model.add_cohort(cohort=cohort)

    redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
    return redirect(redirect_url)

@login_required
def workbook_create_with_cohort_list(request):
    cohort_ids = json.loads(request.body)['cohorts']
    if len(cohort_ids) > 0 :
        workbook_model  = Workbook.create(name="Untitled Workbook", description="This is a workbook created with cohorts added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
        worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)
        for id in cohort_ids :
            cohort = Cohort.objects.get(id=id)
            worksheet_model.add_cohort(cohort=cohort)

        result = {'workbook_id'  : workbook_model.id,
                  'worksheet_id' : worksheet_model.id}
    else :
        result = {'error' : 'parameters are not correct'}

    return HttpResponse(json.dumps(result), status=200)

#TODO not complete
@login_required
def workbook_create_with_project(request):
    project_id = request.POST.get('project_id')
    project_model = Project.get(id=project_id)

    workbook_model = Workbook.create(name="Untitled Workbook", description="this is an untitled workbook with all variables of project \"" + project_model.name + "\" added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
    worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)

    #add every variable within the model
    for study in project_model.study_set.all().filter(active=True) :
        for var in study.user_feature_definitions_set.all() :
            work_var = Worksheet_variable.objects.create(worksheet_id = worksheet_model.id,
                                              name         = var.feature_name,
                                              url_code     = var.bq_map_id,
                                              feature_id   = var.id)
            work_var.save()

    redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
    return redirect(redirect_url)

@login_required
def workbook_create_with_variables(request):
    var_list_id    = request.POST.get('variable_id')
    var_list_model = VariableFavorite.objects.get(id=var_list_id)

    workbook_model = Workbook.create(name=request.POST.get('name'), description="this is an untitled workbook with all variables of variable favorite list \"" + var_list_model.name + "\" added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
    worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)

    for var in var_list_model.get_variables() :
        work_var = Worksheet_variable.objects.create(worksheet_id = worksheet_model.id,
                                          name         = var.name,
                                          url_code     = var.code,
                                          feature_id   = var.feature_id)

        work_var.save()

    redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
    return redirect(redirect_url)

@login_required
def workbook_create_with_analysis(request):
    analysis_type   = request.POST.get('analysis')

    allowed_types = Analysis.get_types()
    redirect_url = reverse('sample_analyses')
    for type in allowed_types :
        if analysis_type == type['name'] :
            workbook_model  = Workbook.create(name="Untitled Workbook", description="this is an untitled workbook with a \"" + analysis_type + "\" plot added to the first worksheet. Click Edit Details to change your workbook title and description.", user=request.user)
            worksheet_model = Worksheet.objects.create(name="worksheet 1", description="", workbook=workbook_model)
            worksheet_model.set_plot(type=analysis_type)
            redirect_url = reverse('workbook_detail', kwargs={'workbook_id':workbook_model.id})
            break

    return redirect(redirect_url)

{'fraction_non_alphanumeric': 0.08782608695652173, 'fraction_numerical': 0.0016600790513833991, 'mean_word_length': 3.7205223880597016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1831346', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3452, 'n_words': 854}