<commit_before>from datetime import timedelta, datetime

import ckan.authz as authz
from ckan.plugins.toolkit import (
    side_effect_free, ObjectNotFound, get_or_bust, check_access,
    navl_validate, ValidationError)
from ckan.lib.dictization import table_dictize, table_dict_save
from ckan.lib.helpers import _datestamp_to_datetime

from ckanext.inventory.model import InventoryEntry


@side_effect_free
def inventory_entry_list(context, data_dict):
    '''Return a list of inventory entries.

    :param name: organization name
    :type name: string

    :rtype: list of dictionaries
    '''
    # TODO @palcu: define this
    # check_access('inventory_manage', context, data_dict)

    model = context['model']
    name = get_or_bust(data_dict, 'name')
    organization = model.Group.get(name)
    if not organization:
        raise ObjectNotFound('Organization was not found')

    entries = [
        table_dictize(entry, context) for entry in organization.inventory_entries]

    for entry in entries:
        entry['next_deadline_timestamp'] = None
        if entry['last_added_dataset_timestamp']:
            last_added = _datestamp_to_datetime(entry['last_added_dataset_timestamp'])
            delta = timedelta(days=entry['recurring_interval'])
            entry['next_deadline_timestamp'] = last_added + delta
    return entries


@side_effect_free
def inventory_entry_list_for_user(context, data_dict):
    # TODO @palcu: DRY the code below from organization_list_for_user
    model = context['model']
    user = context['user']

    check_access('organization_list_for_user', context, data_dict)
    sysadmin = authz.is_sysadmin(user)

    orgs_q = model.Session.query(InventoryEntry).join(model.Group) \
        .filter(model.Group.is_organization is True) \
        .filter(model.Group.state == 'active')

    if not sysadmin:
        # for non-Sysadmins check they have the required permission

        # NB 'edit_group' doesn't exist so by default this action returns just
        # orgs with admin role
        permission = data_dict.get('permission', 'edit_group')

        roles = authz.get_roles_with_permission(permission)

        if not roles:
            return []
        user_id = authz.get_user_id_for_username(user, allow_none=True)
        if not user_id:
            return []

        q = model.Session.query(model.Member, model.Group) \
            .filter(model.Member.table_name == 'user') \
            .filter(model.Member.capacity.in_(roles)) \
            .filter(model.Member.table_id == user_id) \
            .filter(model.Member.state == 'active') \
            .join(model.Group)

        group_ids = set()
        roles_that_cascade = \
            authz.check_config_permission('roles_that_cascade_to_sub_groups')
        for member, group in q.all():<commit_msg>Hotfix for mysterious condition that is not working<commit_after>from datetime import timedelta, datetime

import ckan.authz as authz
from ckan.plugins.toolkit import (
    side_effect_free, ObjectNotFound, get_or_bust, check_access,
    navl_validate, ValidationError)
from ckan.lib.dictization import table_dictize, table_dict_save
from ckan.lib.helpers import _datestamp_to_datetime

from ckanext.inventory.model import InventoryEntry


@side_effect_free
def inventory_entry_list(context, data_dict):
    '''Return a list of inventory entries.

    :param name: organization name
    :type name: string

    :rtype: list of dictionaries
    '''
    # TODO @palcu: define this
    # check_access('inventory_manage', context, data_dict)

    model = context['model']
    name = get_or_bust(data_dict, 'name')
    organization = model.Group.get(name)
    if not organization:
        raise ObjectNotFound('Organization was not found')

    entries = [
        table_dictize(entry, context) for entry in organization.inventory_entries]

    for entry in entries:
        entry['next_deadline_timestamp'] = None
        if entry['last_added_dataset_timestamp']:
            last_added = _datestamp_to_datetime(entry['last_added_dataset_timestamp'])
            delta = timedelta(days=entry['recurring_interval'])
            entry['next_deadline_timestamp'] = last_added + delta
    return entries


@side_effect_free
def inventory_entry_list_for_user(context, data_dict):
    # TODO @palcu: DRY the code below from organization_list_for_user
    model = context['model']
    user = context['user']

    check_access('organization_list_for_user', context, data_dict)
    sysadmin = authz.is_sysadmin(user)

    orgs_q = model.Session.query(InventoryEntry).join(model.Group) \
        .filter(model.Group.is_organization == True) \
        .filter(model.Group.state == 'active')

    if not sysadmin:
        # for non-Sysadmins check they have the required permission

        # NB 'edit_group' doesn't exist so by default this action returns just
        # orgs with admin role
        permission = data_dict.get('permission', 'edit_group')

        roles = authz.get_roles_with_permission(permission)

        if not roles:
            return []
        user_id = authz.get_user_id_for_username(user, allow_none=True)
        if not user_id:
            return []

        q = model.Session.query(model.Member, model.Group) \
            .filter(model.Member.table_name == 'user') \
            .filter(model.Member.capacity.in_(roles)) \
            .filter(model.Member.table_id == user_id) \
            .filter(model.Member.state == 'active') \
            .join(model.Group)

        group_ids = set()
        roles_that_cascade = \
            authz.check_config_permission('roles_that_cascade_to_sub_groups')
        for member, group in q.all():
{'fraction_non_alphanumeric': 0.09209597741707834, 'fraction_numerical': 0.0, 'mean_word_length': 3.1868537666174297, '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': '2361227', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1654, 'n_words': 381}