<commit_before># oxy.io
# File: oxyio/web/views/objects.py
# Desc: list & add objects

from flask import abort, request, g
from jinja2 import TemplateNotFound
from sqlalchemy.orm import joinedload

from oxyio.data import get_object_class_or_404, get_objects, get_object

from oxyio.web.route import html_api_route
from oxyio.web.request import in_request_args, get_request_data
from oxyio.web.response import render_or_jsonify, redirect_or_jsonify
from oxyio.web.user import (
    get_own_objects,
    login_required, get_current_user,
    has_own_objects_permission, has_any_objects_permission,
    has_global_objects_permission
)


def _filter_field(objects, obj, field, value):
    # Multiple values, do an IN query
    if isinstance(value, list):
        objects = objects.filter(getattr(obj, field).in_(value))

    # Single value, simple filter
    else:
        objects = objects.filter(getattr(obj, field) == value)

    return objects


def _contains_field(objects, obj, field, value):
    return objects.filter(getattr(obj, field).contains(value))


def _do_list_objects(module_name, objects_type, get_objects_func, is_all=False):
    g.module = module_name
    g.object = objects_type

    object_class = get_object_class_or_404(module_name, objects_type)
    objects = get_objects_func(module_name, objects_type)

    # Join-load any relations
    objects = objects.options(*[
        joinedload(field)
        for field, _, _ in object_class.LIST_RELATIONS
    ])

    # Join-load any multi relations
    objects = objects.options(*[
        joinedload(field)
        for field, _, _ in object_class.LIST_MRELATIONS
    ])

    # Join-load any list fields which ask for it
    for field, options in object_class.LIST_FIELDS:
        if options.get('join'):
            objects = objects.options(joinedload(field))

    # Apply any filters
    filtered = False

    # Name filter
    if in_request_args('name'):
        filtered = True
        objects = objects.filter(
            object_class.name.like('%{0}%'.format(request.args['name']))
        )

    # Field filters
    for field, _ in object_class.FILTER_FIELDS:
        if in_request_args(field):
            filtered = True
            objects = _filter_field(
                objects, object_class, field,
                request.args[field],
            )

    # Relation filters
    for field, (module_name, objects_type), options in object_class.FILTER_RELATIONS:
        if in_request_args(field):
            filtered = True
            objects = _filter_field(
                objects, object_class, field,
                get_object(module_name, objects_type, request.args[field]),
            )

    # Multi relation filters
    for field, (module_name, objects_type), options in object_class.FILTER_MRELATIONS:
        if in_request_args(field):
            filtered = True
            objects = _contains_field(
                objects, object_class, field,
                get_object(module_name, objects_type, request.args[field]),
            )

    # Fetch the objects
    objects = list(objects)

    # Build filter form<commit_msg>Add ordering to list views (backend only).<commit_after># oxy.io
# File: oxyio/web/views/objects.py
# Desc: list & add objects

from flask import abort, request, g
from jinja2 import TemplateNotFound
from sqlalchemy import desc, asc
from sqlalchemy.orm import joinedload

from oxyio.data import get_object_class_or_404, get_objects, get_object

from oxyio.web.route import html_api_route
from oxyio.web.request import in_request_args, get_request_data
from oxyio.web.response import render_or_jsonify, redirect_or_jsonify
from oxyio.web.user import (
    get_own_objects,
    login_required, get_current_user,
    has_own_objects_permission, has_any_objects_permission,
    has_global_objects_permission
)


def _filter_field(objects, obj, field, value):
    # Multiple values, do an IN query
    if isinstance(value, list):
        objects = objects.filter(getattr(obj, field).in_(value))

    # Single value, simple filter
    else:
        objects = objects.filter(getattr(obj, field) == value)

    return objects


def _contains_field(objects, obj, field, value):
    return objects.filter(getattr(obj, field).contains(value))


def _do_list_objects(module_name, objects_type, get_objects_func, is_all=False):
    g.module = module_name
    g.object = objects_type

    object_class = get_object_class_or_404(module_name, objects_type)
    objects = get_objects_func(module_name, objects_type)

    # Join-load any relations
    objects = objects.options(*[
        joinedload(field)
        for field, _, _ in object_class.LIST_RELATIONS
    ])

    # Join-load any multi relations
    objects = objects.options(*[
        joinedload(field)
        for field, _, _ in object_class.LIST_MRELATIONS
    ])

    # Join-load any list fields which ask for it
    for field, options in object_class.LIST_FIELDS:
        if options.get('join'):
            objects = objects.options(joinedload(field))

    # Apply any filters
    filtered = False

    # Name filter
    if in_request_args('name'):
        filtered = True
        objects = objects.filter(
            object_class.name.like('%{0}%'.format(request.args['name']))
        )

    # Field filters
    for field, _ in object_class.FILTER_FIELDS:
        if in_request_args(field):
            filtered = True
            objects = _filter_field(
                objects, object_class, field,
                request.args[field],
            )

    # Relation filters
    for field, (module_name, objects_type), options in object_class.FILTER_RELATIONS:
        if in_request_args(field):
            filtered = True
            objects = _filter_field(
                objects, object_class, field,
                get_object(module_name, objects_type, request.args[field]),
            )

    # Multi relation filters
    for field, (module_name, objects_type), options in object_class.FILTER_MRELATIONS:
        if in_request_args(field):
            filtered = True
            objects = _contains_field(
                objects, object_class, field,
                get_object(module_name, objects_type, request.args[field]),
            )

    # Ordering
    order_func = (
        asc
        if in_request_args('order') and request.args['order'] == 'asc'
        else desc
    )

    order_field = object_class.id
    if in_request_args('order_by') and (
        request.args['order_by'] == 'name'
        or request.args['order_by'] in object_class.ORDER_FIELDS
    ):
        order_field = getattr(object_class, request.args['order_by'])

    objects = objects.order_by(order_func(order_field))

    # Fetch the objects
    objects = list(objects)

    # Build filter form
{'fraction_non_alphanumeric': 0.08456534594914251, 'fraction_numerical': 0.002365464222353637, 'mean_word_length': 2.874570446735395, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4522996', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1964, 'n_words': 478}