<commit_before>import json

from django.conf import settings
from django.db import models
from django.db.models import Count
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.template import Context, loader

from catmaid.control.common import makeJSON_legacy_list
from catmaid.control.project import extend_projects
from catmaid.models import DataView, DataViewType, Project

import re

def get_data_view_type_comment( request ):
    """ Return the comment of a specific data view type.
    """
    requested_id = request.REQUEST["data_view_type_id"]
    if requested_id == "":
        text = "Please select a valid data view type."
    else:
        try:
            data_view_type_id = int(requested_id)
            text = DataViewType.objects.get(pk=data_view_type_id).comment
        except:
            text = "Sorry, the configuration help text couldn't be retrieved."
    result = { 'comment':text }
    return HttpResponse(json.dumps(result), mimetype="text/json")

def dataview_to_dict( dataview ):
    """ Creates a dicitonary of the dataviews' properties.
    """
    return {
        'id': dataview.id,
        'title': dataview.title,
        'code_type': dataview.data_view_type.code_type,
        'config': dataview.config,
        'note': dataview.comment
    }

def get_available_data_views( request ):
    """ Returns a list of all available data views.
    """
    all_views = DataView.objects.order_by("position")
    dataviews = map(dataview_to_dict, all_views)

    return HttpResponse(json.dumps(makeJSON_legacy_list(dataviews)), mimetype="text/json")

def get_default_properties( request ):
    """ Return the properies of the default data view.
    """
    default = DataView.objects.filter(is_default=True)[0]
    default = dataview_to_dict( default )

    return HttpResponse(json.dumps(default), mimetype="text/json")

def get_default_data_view( request ):
    """ Return the data view that is marked as the default. If there
    is more than one view marked as default, the first one is returned.
    """
    default = DataView.objects.filter(is_default=True)[0]

    return get_data_view( request, default.id )

def natural_sort(l, field):
    """ Natural sorting of a list wrt. to its 'title' attribute.
    Based on: http://stackoverflow.com/questions/4836710
    """
    convert = lambda text: int(text) if text.isdigit() else text.lower()
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', getattr(key, field)) ]
    return sorted(l, key = alphanum_key)

def get_data_view( request, data_view_id ):
    """ Returns a rendered template for the given view.
    """
    # Load the template
    dv = get_object_or_404(DataView, pk=data_view_id)
    code_type = dv.data_view_type.code_type
    template = loader.get_template( "catmaid/" + code_type + ".html" )
    # Get project information and pass all to the template context
    config = json.loads( dv.config )

    # If requested, filter projects by tags. Otherwise, get all.
    if "filter_tags" in config:
        filter_tags = config["filter_tags"]
        # Only get projects that have all the filter tags set
        projects = Project.objects.filter( tags__name__in=filter_tags ).annotate(
            repeat_count=Count("id") ).filter( repeat_count=len(filter_tags) )
    else:
        projects = Project.objects.all()

    # Extend the project list with additional information like editabilty
    projects = extend_projects( request, projects )

    # Sort by default
    if "sort" not in config or config["sort"] == True:
        projects = natural_sort( projects, "title" )

    context = Context({
        'data_view': dv,
        'projects': projects,
        'config': config
    })

    return HttpResponse( template.render( context ) );<commit_msg>Data views: give templates access to settings<commit_after>import json

from django.conf import settings
from django.db import models
from django.db.models import Count
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.template import Context, loader

from catmaid.control.common import makeJSON_legacy_list
from catmaid.control.project import extend_projects
from catmaid.models import DataView, DataViewType, Project

import re

def get_data_view_type_comment( request ):
    """ Return the comment of a specific data view type.
    """
    requested_id = request.REQUEST["data_view_type_id"]
    if requested_id == "":
        text = "Please select a valid data view type."
    else:
        try:
            data_view_type_id = int(requested_id)
            text = DataViewType.objects.get(pk=data_view_type_id).comment
        except:
            text = "Sorry, the configuration help text couldn't be retrieved."
    result = { 'comment':text }
    return HttpResponse(json.dumps(result), mimetype="text/json")

def dataview_to_dict( dataview ):
    """ Creates a dicitonary of the dataviews' properties.
    """
    return {
        'id': dataview.id,
        'title': dataview.title,
        'code_type': dataview.data_view_type.code_type,
        'config': dataview.config,
        'note': dataview.comment
    }

def get_available_data_views( request ):
    """ Returns a list of all available data views.
    """
    all_views = DataView.objects.order_by("position")
    dataviews = map(dataview_to_dict, all_views)

    return HttpResponse(json.dumps(makeJSON_legacy_list(dataviews)), mimetype="text/json")

def get_default_properties( request ):
    """ Return the properies of the default data view.
    """
    default = DataView.objects.filter(is_default=True)[0]
    default = dataview_to_dict( default )

    return HttpResponse(json.dumps(default), mimetype="text/json")

def get_default_data_view( request ):
    """ Return the data view that is marked as the default. If there
    is more than one view marked as default, the first one is returned.
    """
    default = DataView.objects.filter(is_default=True)[0]

    return get_data_view( request, default.id )

def natural_sort(l, field):
    """ Natural sorting of a list wrt. to its 'title' attribute.
    Based on: http://stackoverflow.com/questions/4836710
    """
    convert = lambda text: int(text) if text.isdigit() else text.lower()
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', getattr(key, field)) ]
    return sorted(l, key = alphanum_key)

def get_data_view( request, data_view_id ):
    """ Returns a rendered template for the given view.
    """
    # Load the template
    dv = get_object_or_404(DataView, pk=data_view_id)
    code_type = dv.data_view_type.code_type
    template = loader.get_template( "catmaid/" + code_type + ".html" )
    # Get project information and pass all to the template context
    config = json.loads( dv.config )

    # If requested, filter projects by tags. Otherwise, get all.
    if "filter_tags" in config:
        filter_tags = config["filter_tags"]
        # Only get projects that have all the filter tags set
        projects = Project.objects.filter( tags__name__in=filter_tags ).annotate(
            repeat_count=Count("id") ).filter( repeat_count=len(filter_tags) )
    else:
        projects = Project.objects.all()

    # Extend the project list with additional information like editabilty
    projects = extend_projects( request, projects )

    # Sort by default
    if "sort" not in config or config["sort"] == True:
        projects = natural_sort( projects, "title" )

    context = Context({
        'data_view': dv,
        'projects': projects,
        'config': config,
        'settings': settings
    })

    return HttpResponse( template.render( context ) );
{'fraction_non_alphanumeric': 0.09968806862490252, 'fraction_numerical': 0.004419027813880946, 'mean_word_length': 3.3946316390633924, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3741695', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2260, 'n_words': 672}