<commit_before>from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required

from catmaid.models import *
from catmaid.control.authentication import *
from catmaid.control.common import *
from catmaid.transaction import *

def get_stack_info(project_id=None, stack_id=None, user=None):
    """ Returns a dictionary with relevant information for stacks.
    Depending on the tile_source_type, get information from database
    or from tile server directly
    """
    p = get_object_or_404(Project, pk=project_id)
    s = get_object_or_404(Stack, pk=stack_id)
    ps_all = ProjectStack.objects.filter(project=project_id, stack=stack_id)
    if len(ps_all) != 1:
        return {'error': 'Multiple project - stack associations, but should only be one.'}
    ps=ps_all[0]
    can_edit = user.has_perm('can_administer', p) or user.has_perm('can_annotate', p)

    # https://github.com/acardona/CATMAID/wiki/Convention-for-Stack-Image-Sources
    if int(s.tile_source_type) == 2:
        # request appropriate stack metadata from tile source
        url=s.image_base.rstrip('/').lstrip('http://')
        # Important: Do not use localhost, but 127.0.0.1 instead
        # to prevent an namespace lookup error (gaierror)
        # Important2: Do not put http:// in front!
        conn = httplib.HTTPConnection(url)
        conn.request('GET', '/metadata')
        response = conn.getresponse()
        # read JSON response according to metadata convention
        # Tornado reponse is escaped JSON string
        read_response = response.read()
        # convert it back to dictionary str->dict
        return json.loads(read_response)
    else:
        broken_slices_qs = BrokenSlice.objects.filter(stack=stack_id)
        broken_slices = {}
        for ele in broken_slices_qs:
            broken_slices[ele.index] = 1
        overlays = []
        overlays_qs = Overlay.objects.filter(stack=stack_id)
        for ele in overlays_qs:
            overlays.append( {
                'id': ele.id,
                'title': ele.title,
                'image_base': ele.image_base,
                'default_opacity': ele.default_opacity,
                'tile_width': ele.tile_width,
                'tile_height': ele.tile_height,
                'tile_source_type': ele.tile_source_type,
                'file_extension': ele.file_extension
                } )
        result={
            'sid': int(s.id),
            'pid': int(p.id),
            'ptitle': p.title,
            'stitle': s.title,
            'image_base': s.image_base,
            'num_zoom_levels': int(s.num_zoom_levels),
            'file_extension': s.file_extension,
            'editable': can_edit,
            'translation': {
                'x': ps.translation.x,
                'y': ps.translation.y,
                'z': ps.translation.z
            },
            'resolution': {
                'x': float(s.resolution.x),
                'y': float(s.resolution.y),
                'z': float(s.resolution.z)
            },
            'dimension': {
                'x': int(s.dimension.x),
                'y': int(s.dimension.y),
                'z': int(s.dimension.z)
            },
            'tile_height': int(s.tile_height),
            'tile_width': int(s.tile_width),
            'tile_source_type': int(s.tile_source_type),
            'metadata' : s.metadata,
            'broken_slices': broken_slices,
            'trakem2_project': int(s.trakem2_project),
            'overlay': overlays
        }

    return result

@requires_user_role([UserRole.Annotate, UserRole.Browse])
def stack_info(request, project_id=None, stack_id=None):
    result=get_stack_info(project_id, stack_id, request.user)
    return HttpResponse(json.dumps(result, sort_keys=True, indent=4), mimetype="text/json")
<commit_msg>Stack information fetchable for all users<commit_after>from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import login_required

from catmaid.models import *
from catmaid.control.authentication import *
from catmaid.control.common import *
from catmaid.transaction import *

def get_stack_info(project_id=None, stack_id=None, user=None):
    """ Returns a dictionary with relevant information for stacks.
    Depending on the tile_source_type, get information from database
    or from tile server directly
    """
    p = get_object_or_404(Project, pk=project_id)
    s = get_object_or_404(Stack, pk=stack_id)
    ps_all = ProjectStack.objects.filter(project=project_id, stack=stack_id)
    if len(ps_all) != 1:
        return {'error': 'Multiple project - stack associations, but should only be one.'}
    ps=ps_all[0]
    can_edit = user.has_perm('can_administer', p) or user.has_perm('can_annotate', p)

    # https://github.com/acardona/CATMAID/wiki/Convention-for-Stack-Image-Sources
    if int(s.tile_source_type) == 2:
        # request appropriate stack metadata from tile source
        url=s.image_base.rstrip('/').lstrip('http://')
        # Important: Do not use localhost, but 127.0.0.1 instead
        # to prevent an namespace lookup error (gaierror)
        # Important2: Do not put http:// in front!
        conn = httplib.HTTPConnection(url)
        conn.request('GET', '/metadata')
        response = conn.getresponse()
        # read JSON response according to metadata convention
        # Tornado reponse is escaped JSON string
        read_response = response.read()
        # convert it back to dictionary str->dict
        return json.loads(read_response)
    else:
        broken_slices_qs = BrokenSlice.objects.filter(stack=stack_id)
        broken_slices = {}
        for ele in broken_slices_qs:
            broken_slices[ele.index] = 1
        overlays = []
        overlays_qs = Overlay.objects.filter(stack=stack_id)
        for ele in overlays_qs:
            overlays.append( {
                'id': ele.id,
                'title': ele.title,
                'image_base': ele.image_base,
                'default_opacity': ele.default_opacity,
                'tile_width': ele.tile_width,
                'tile_height': ele.tile_height,
                'tile_source_type': ele.tile_source_type,
                'file_extension': ele.file_extension
                } )
        result={
            'sid': int(s.id),
            'pid': int(p.id),
            'ptitle': p.title,
            'stitle': s.title,
            'image_base': s.image_base,
            'num_zoom_levels': int(s.num_zoom_levels),
            'file_extension': s.file_extension,
            'editable': can_edit,
            'translation': {
                'x': ps.translation.x,
                'y': ps.translation.y,
                'z': ps.translation.z
            },
            'resolution': {
                'x': float(s.resolution.x),
                'y': float(s.resolution.y),
                'z': float(s.resolution.z)
            },
            'dimension': {
                'x': int(s.dimension.x),
                'y': int(s.dimension.y),
                'z': int(s.dimension.z)
            },
            'tile_height': int(s.tile_height),
            'tile_width': int(s.tile_width),
            'tile_source_type': int(s.tile_source_type),
            'metadata' : s.metadata,
            'broken_slices': broken_slices,
            'trakem2_project': int(s.trakem2_project),
            'overlay': overlays
        }

    return result

def stack_info(request, project_id=None, stack_id=None):
    result=get_stack_info(project_id, stack_id, request.user)
    return HttpResponse(json.dumps(result, sort_keys=True, indent=4), mimetype="text/json")

{'fraction_non_alphanumeric': 0.1126797385620915, 'fraction_numerical': 0.006013071895424837, 'mean_word_length': 2.220117845117845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1031', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2276, 'n_words': 483}