<commit_before>"""Metadata Sheet."""
from logging import getLogger

from pyramid.registry import Registry
import colander

from adhocracy_core.interfaces import IResource
from adhocracy_core.interfaces import ISheet
from adhocracy_core.interfaces import SheetToSheet
from adhocracy_core.sheets import add_sheet_to_registry
from adhocracy_core.sheets import AttributeResourceSheet
from adhocracy_core.sheets import sheet_meta
from adhocracy_core.sheets.principal import IUserBasic
from adhocracy_core.schema import Boolean
from adhocracy_core.schema import DateTime
from adhocracy_core.schema import Reference
from adhocracy_core.utils import get_sheet


logger = getLogger(__name__)


class IMetadata(ISheet):

    """Market interface for the metadata sheet."""


class MetadataCreatorsReference(SheetToSheet):

    """Metadata sheet creators reference."""

    source_isheet = IMetadata
    source_isheet_field = 'creator'
    target_isheet = IUserBasic


class MetadataModifiedByReference(SheetToSheet):

    """Points to the last person who modified a resource."""

    source_isheet = IMetadata
    source_isheet_field = 'modified_by'
    target_isheet = IUserBasic


@colander.deferred
def deferred_validate_hidden(node, kw):
    """Check hide_permission."""
    context = kw['context']
    request = kw.get('request', None)
    if request is None:
        return

    def check_hide_permisison(node, cstruct):
        if not request.has_permission('hide', context):
            raise colander.Invalid(node, 'Changing this field is not allowed')
    return check_hide_permisison


class MetadataSchema(colander.MappingSchema):

    """Metadata sheet data structure.

    `creation_date`: Creation date of this resource. defaults to now.
    `item_creation_date`: Equals creation date for ISimple/IPool, equals
                          the item creation date for
                          :class:`adhocracy_core.interfaces.IItemVersion`.
                          This exists to ease the frontend end development.
                          This may go away if we have a high level API
                          to make :class:`adhocracy_core.interfaces.Item`
                         /`IItemVersion` one `thing`.
                         defaults to now.
    `creator`: creator (user resource) of this resource.
    `modified_by`: the last person (user resources) who modified a resource,
                   initally the creator
    `modification_date`: Modification date of this resource. defaults to now.
    `deleted`: whether the resource is marked as deleted (only shown to those
               that specifically ask for it)
    `hidden`: whether the resource is marked as hidden (only shown to those
               that have special permissions and ask for it)
    """

    creator = Reference(reftype=MetadataCreatorsReference, readonly=True)
    creation_date = DateTime(missing=colander.drop, readonly=True)
    item_creation_date = DateTime(missing=colander.drop, readonly=True)
    modified_by = Reference(reftype=MetadataModifiedByReference, readonly=True)
    modification_date = DateTime(missing=colander.drop, readonly=True)
    deleted = Boolean()<commit_msg>Fix docstring formatting / display<commit_after>"""Metadata Sheet."""
from logging import getLogger

from pyramid.registry import Registry
import colander

from adhocracy_core.interfaces import IResource
from adhocracy_core.interfaces import ISheet
from adhocracy_core.interfaces import SheetToSheet
from adhocracy_core.sheets import add_sheet_to_registry
from adhocracy_core.sheets import AttributeResourceSheet
from adhocracy_core.sheets import sheet_meta
from adhocracy_core.sheets.principal import IUserBasic
from adhocracy_core.schema import Boolean
from adhocracy_core.schema import DateTime
from adhocracy_core.schema import Reference
from adhocracy_core.utils import get_sheet


logger = getLogger(__name__)


class IMetadata(ISheet):

    """Market interface for the metadata sheet."""


class MetadataCreatorsReference(SheetToSheet):

    """Metadata sheet creators reference."""

    source_isheet = IMetadata
    source_isheet_field = 'creator'
    target_isheet = IUserBasic


class MetadataModifiedByReference(SheetToSheet):

    """Points to the last person who modified a resource."""

    source_isheet = IMetadata
    source_isheet_field = 'modified_by'
    target_isheet = IUserBasic


@colander.deferred
def deferred_validate_hidden(node, kw):
    """Check hide_permission."""
    context = kw['context']
    request = kw.get('request', None)
    if request is None:
        return

    def check_hide_permisison(node, cstruct):
        if not request.has_permission('hide', context):
            raise colander.Invalid(node, 'Changing this field is not allowed')
    return check_hide_permisison


class MetadataSchema(colander.MappingSchema):

    """Metadata sheet data structure.

    `creation_date`: Creation date of this resource. defaults to now.

    `item_creation_date`: Equals creation date for ISimple/IPool,
    equals the item creation date for
    :class:`adhocracy_core.interfaces.IItemVersion`. This exists to
    ease the frontend end development. This may go away if we have a
    high level API to make :class:`adhocracy_core.interfaces.Item` /
    `IItemVersion` one `thing`. Defaults to now.

    `creator`: creator (user resource) of this resource.

    `modified_by`: the last person (user resources) who modified a
    resource, initially the creator

    `modification_date`: Modification date of this resource. defaults to now.

    `deleted`: whether the resource is marked as deleted (only shown to those
    that specifically ask for it)

    `hidden`: whether the resource is marked as hidden (only shown to those
    that have special permissions and ask for it)

    """

    creator = Reference(reftype=MetadataCreatorsReference, readonly=True)
    creation_date = DateTime(missing=colander.drop, readonly=True)
    item_creation_date = DateTime(missing=colander.drop, readonly=True)
    modified_by = Reference(reftype=MetadataModifiedByReference, readonly=True)
    modification_date = DateTime(missing=colander.drop, readonly=True)
    deleted = Boolean()
{'fraction_non_alphanumeric': 0.06927175843694494, 'fraction_numerical': 0.0, 'mean_word_length': 3.8964426877470357, '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': '671804', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1617, 'n_words': 536}