<commit_before>from pyramid.security import (
    ALL_PERMISSIONS,
    Allow,
    Authenticated,
    DENY_ALL,
    Everyone,
)
from .download import ItemWithAttachment
from ..contentbase import (
    Collection as BaseCollection,
    location
)
from ..schema_utils import (
    load_schema,
    lookup_resource,
    VALIDATOR_REGISTRY,
)
from pyramid.threadlocal import get_current_request
from pyramid.traversal import (
    find_resource,
    find_root,
)
from urllib import quote_plus
from urlparse import urljoin
import copy
import datetime

ACCESSION_KEYS = [
    {
        'name': 'accession',
        'value': '{accession}',
        '$templated': True,
        '$condition': lambda accession=None, status=None: accession and status != 'replaced'
    },
    {
        'name': 'accession',
        'value': '{accession}',
        '$repeat': 'accession alternate_accessions',
        '$templated': True,
        '$condition': 'alternate_accessions',
    },
]

ALIAS_KEYS = [
    {
        'name': 'alias',
        'value': '{alias}',
        '$repeat': 'alias aliases',
        '$templated': True,
        '$condition': 'aliases',
    },
]


ALLOW_EVERYONE_VIEW = [
    (Allow, Everyone, 'view'),
]

ALLOW_SUBMITTER_ADD = [
    (Allow, 'group.submitter', 'add')
]

ALLOW_AUTHENTICATED_VIEW = [
    (Allow, Authenticated, 'view'),
]

ALLOW_LAB_SUBMITTER_EDIT = [
    (Allow, Authenticated, 'view'),
    (Allow, 'group.admin', 'edit'),
    (Allow, 'role.lab_submitter', 'edit'),
]

ALLOW_CURRENT = [
    (Allow, Everyone, 'view'),
    (Allow, 'group.admin', 'edit'),
]

ONLY_ADMIN_VIEW = [
    (Allow, 'group.admin', ALL_PERMISSIONS),
    (Allow, 'group.read-only-admin', ['traverse', 'view']),
    (Allow, 'remoteuser.EMBED', ['traverse', 'view']),
    (Allow, 'remoteuser.INDEXER', ['traverse', 'view', 'index']),
    DENY_ALL,
]

# Now unused, kept around for upgrade tests.
ENCODE2_AWARDS = frozenset([
    '1a4d6443-8e29-4b4a-99dd-f93e72d42418',
    '1f3cffd4-457f-4105-9b3c-3e9119abfcf0',
    '2528d08a-7e67-48f1-b9ad-dc1c29cb926c',
    '2a27a363-6bb5-43cc-99c4-d58bf06d3d8e',
    '2eb561ab-96de-4286-8afd-5479e7ba909d',
    '2f35506f-0b00-4b56-9d65-6c0be5f304b2',
    '366388ac-685d-415c-b0bb-834ffafdf094',
    '60a6088f-b3c8-46eb-8e27-51ef9cda31e0',
    '7fd6664b-17f5-4bfe-9fdf-ed7481cf4d24',
    '9623f1c0-e619-44bc-b430-8cc7176c766c',
    'a7ba7c03-d93f-4503-8116-63db88a1390c',
    'c68bafc1-dda3-41a9-b889-ec756f0368e1',
    'c7ff037b-05ab-4fe0-bded-dd30999e2b3f',
    'cd51d709-b8d1-4ba6-b756-45adcaa38fb9',
    'dd7fb99a-cb0b-407e-9635-16454f0066c1',
    'df972196-c3c7-4a58-a852-94baa87f9b71',
    # Unattributed
    'f06a0db4-b388-48d9-b414-37d83859cad0',
    # ENCODE2-Mouse
    '95839ec3-9b83-44d9-a98a-026a9fd01bda',
    '124b9d72-22bd-4fcc-8e2b-052677c4e3f2',
    '2cda932c-07d5-4740-a024-d585635f5650',
    '5a009305-4ddc-4dba-bbf3-7327ceda3702',
])

ADD_ACTION = {
    'name': 'add',
    'title': 'Add',
    'profile': '/profiles/{item_type}.json',
    'method': 'GET',
    'href': '{collection_uri}#!add',
    'className': 'btn btn-success',
    '$templated': True,
    '$condition': 'permission:add',
}

EDIT_ACTION = {
    'name': 'edit',
    'title': 'Edit',
    'profile': '/profiles/page.json',
    'method': 'PUT',
    'href': '#!edit',
    'className': 'btn navbar-btn',
}


class Collection(BaseCollection):
    def __init__(self, parent, name):
        super(Collection, self).__init__(parent, name)
        if hasattr(self, '__acl__'):
            return
        if 'lab' in self.schema['properties']:
            self.__acl__ = ALLOW_SUBMITTER_ADD

    class Item(BaseCollection.Item):
        STATUS_ACL = {
            # standard_status
            'released': ALLOW_CURRENT,
            'deleted': ONLY_ADMIN_VIEW,
            'replaced': ONLY_ADMIN_VIEW,

            # shared_status
            'current': ALLOW_CURRENT,
            'disabled': ONLY_ADMIN_VIEW,

            # file
            'obsolete': ONLY_ADMIN_VIEW,

            # antibody_characterization
            'compliant': ALLOW_CURRENT,
            'not compliant': ALLOW_CURRENT,
            'not reviewed': ALLOW_CURRENT,
            'not submitted for review by lab': ALLOW_CURRENT,

            # antibody_lot
            'eligible for new data': ALLOW_CURRENT,
            'not eligible for new data': ALLOW_CURRENT,
            'not pursued': ALLOW_CURRENT,

            # dataset / experiment
            'release ready': ALLOW_AUTHENTICATED_VIEW,
            'revoked': ALLOW_CURRENT,

            # publication
            'published': ALLOW_CURRENT,
        }
        actions = [EDIT_ACTION]

        @property
        def __name__(self):
            if self.name_key is None:
                return self.uuid
            properties = self.upgrade_properties(finalize=False)
            if properties.get('status') == 'replaced':
                return self.uuid
            return properties.get(self.name_key, None) or self.uuid

        def __acl__(self):
            # Don't finalize to avoid validation here.
            properties = self.upgrade_properties(finalize=False).copy()
            ns = self.template_namespace(properties)
            properties.update(ns)
            status = ns.get('status')
            return self.STATUS_ACL.get(status, ALLOW_LAB_SUBMITTER_EDIT)

        def __ac_local_roles__(self):
            roles = {}
            properties = self.upgrade_properties(finalize=False).copy()
            ns = self.template_namespace(properties)
            properties.update(ns)
            if 'lab' in properties:
                lab_submitters = 'submits_for.%s' % properties['lab']
                roles[lab_submitters] = 'role.lab_submitter'
            return roles


@location('labs')
class Lab(Collection):
    item_type = 'lab'
    schema = load_schema('lab.json')
    __acl__ = ALLOW_EVERYONE_VIEW
    properties = {
        'title': 'Labs',
        'description': 'Listing of ENCODE DCC labs',
    }
    item_name_key = 'name'
    unique_key = 'lab:name'
    item_keys = [
        {'name': '{item_type}:name', 'value': '{name}', '$templated': True},
        {'name': '{item_type}:name', 'value': '{title}', '$templated': True},
    ]


@location('awards')
class Award(Collection):
    item_type = 'award'
    schema = load_schema('award.json')
    __acl__ = ALLOW_EVERYONE_VIEW
    properties = {
        'title': 'Awards (Grants)',
        'description': 'Listing of awards (aka grants)',
    }
    item_name_key = 'name'
    unique_key = 'award:name'
    item_keys = ['name']


@location('antibody-lots')
class AntibodyLot(Collection):
    item_type = 'antibody_lot'
    schema = load_schema('antibody_lot.json')
    properties = {
        'title': 'Antibodies Registry',
        'description': 'Listing of ENCODE antibodies',
    }

    class Item(Collection.Item):
        template = {
            'lot_reviews': [
                {'$value': lambda lot_review: lot_review, '$repeat': 'lot_review lot_reviews', '$templated': True}
            ],
            'title': {'$value': '{accession}', '$templated': True},
        }
        name_key = 'accession'

        keys = ACCESSION_KEYS + ALIAS_KEYS + [
            {
                'name': '{item_type}:source_product_lot',
                'value': '{source}/{product_id}/{lot_id}',
                '$templated': True,
            },
            {
                'name': '{item_type}:source_product_lot',
                'value': '{source}/{product_id}/{alias}',
                '$repeat': 'alias lot_id_alias',
                '$templated': True,
            },
        ]

        rev = {
            'characterizations': ('antibody_characterization', 'characterizes'),
        }

        embedded = set([
            'source',
            'host_organism',
            'characterizations.award',
            'characterizations.lab',
            'characterizations.submitted_by',
            'characterizations.target.organism',
            'lot_reviews.targets'
        ])

        def template_namespace(self, properties, request=None):
            ns = super(AntibodyLot.Item, self).template_namespace(properties, request)
            if request is None:
                return ns
            if ns['characterizations']:
                compliant_secondary = False
                not_compliant_secondary = False
                pending_secondary = False
                has_lane_review = False
                not_reviewed = False
                histone_mod_target = False
                lab_not_reviewed_chars = 0<commit_msg>Embed organisms into lot_reviews<commit_after>from pyramid.security import (
    ALL_PERMISSIONS,
    Allow,
    Authenticated,
    DENY_ALL,
    Everyone,
)
from .download import ItemWithAttachment
from ..contentbase import (
    Collection as BaseCollection,
    location
)
from ..schema_utils import (
    load_schema,
    lookup_resource,
    VALIDATOR_REGISTRY,
)
from pyramid.threadlocal import get_current_request
from pyramid.traversal import (
    find_resource,
    find_root,
)
from urllib import quote_plus
from urlparse import urljoin
import copy
import datetime

ACCESSION_KEYS = [
    {
        'name': 'accession',
        'value': '{accession}',
        '$templated': True,
        '$condition': lambda accession=None, status=None: accession and status != 'replaced'
    },
    {
        'name': 'accession',
        'value': '{accession}',
        '$repeat': 'accession alternate_accessions',
        '$templated': True,
        '$condition': 'alternate_accessions',
    },
]

ALIAS_KEYS = [
    {
        'name': 'alias',
        'value': '{alias}',
        '$repeat': 'alias aliases',
        '$templated': True,
        '$condition': 'aliases',
    },
]


ALLOW_EVERYONE_VIEW = [
    (Allow, Everyone, 'view'),
]

ALLOW_SUBMITTER_ADD = [
    (Allow, 'group.submitter', 'add')
]

ALLOW_AUTHENTICATED_VIEW = [
    (Allow, Authenticated, 'view'),
]

ALLOW_LAB_SUBMITTER_EDIT = [
    (Allow, Authenticated, 'view'),
    (Allow, 'group.admin', 'edit'),
    (Allow, 'role.lab_submitter', 'edit'),
]

ALLOW_CURRENT = [
    (Allow, Everyone, 'view'),
    (Allow, 'group.admin', 'edit'),
]

ONLY_ADMIN_VIEW = [
    (Allow, 'group.admin', ALL_PERMISSIONS),
    (Allow, 'group.read-only-admin', ['traverse', 'view']),
    (Allow, 'remoteuser.EMBED', ['traverse', 'view']),
    (Allow, 'remoteuser.INDEXER', ['traverse', 'view', 'index']),
    DENY_ALL,
]

# Now unused, kept around for upgrade tests.
ENCODE2_AWARDS = frozenset([
    '1a4d6443-8e29-4b4a-99dd-f93e72d42418',
    '1f3cffd4-457f-4105-9b3c-3e9119abfcf0',
    '2528d08a-7e67-48f1-b9ad-dc1c29cb926c',
    '2a27a363-6bb5-43cc-99c4-d58bf06d3d8e',
    '2eb561ab-96de-4286-8afd-5479e7ba909d',
    '2f35506f-0b00-4b56-9d65-6c0be5f304b2',
    '366388ac-685d-415c-b0bb-834ffafdf094',
    '60a6088f-b3c8-46eb-8e27-51ef9cda31e0',
    '7fd6664b-17f5-4bfe-9fdf-ed7481cf4d24',
    '9623f1c0-e619-44bc-b430-8cc7176c766c',
    'a7ba7c03-d93f-4503-8116-63db88a1390c',
    'c68bafc1-dda3-41a9-b889-ec756f0368e1',
    'c7ff037b-05ab-4fe0-bded-dd30999e2b3f',
    'cd51d709-b8d1-4ba6-b756-45adcaa38fb9',
    'dd7fb99a-cb0b-407e-9635-16454f0066c1',
    'df972196-c3c7-4a58-a852-94baa87f9b71',
    # Unattributed
    'f06a0db4-b388-48d9-b414-37d83859cad0',
    # ENCODE2-Mouse
    '95839ec3-9b83-44d9-a98a-026a9fd01bda',
    '124b9d72-22bd-4fcc-8e2b-052677c4e3f2',
    '2cda932c-07d5-4740-a024-d585635f5650',
    '5a009305-4ddc-4dba-bbf3-7327ceda3702',
])

ADD_ACTION = {
    'name': 'add',
    'title': 'Add',
    'profile': '/profiles/{item_type}.json',
    'method': 'GET',
    'href': '{collection_uri}#!add',
    'className': 'btn btn-success',
    '$templated': True,
    '$condition': 'permission:add',
}

EDIT_ACTION = {
    'name': 'edit',
    'title': 'Edit',
    'profile': '/profiles/page.json',
    'method': 'PUT',
    'href': '#!edit',
    'className': 'btn navbar-btn',
}


class Collection(BaseCollection):
    def __init__(self, parent, name):
        super(Collection, self).__init__(parent, name)
        if hasattr(self, '__acl__'):
            return
        if 'lab' in self.schema['properties']:
            self.__acl__ = ALLOW_SUBMITTER_ADD

    class Item(BaseCollection.Item):
        STATUS_ACL = {
            # standard_status
            'released': ALLOW_CURRENT,
            'deleted': ONLY_ADMIN_VIEW,
            'replaced': ONLY_ADMIN_VIEW,

            # shared_status
            'current': ALLOW_CURRENT,
            'disabled': ONLY_ADMIN_VIEW,

            # file
            'obsolete': ONLY_ADMIN_VIEW,

            # antibody_characterization
            'compliant': ALLOW_CURRENT,
            'not compliant': ALLOW_CURRENT,
            'not reviewed': ALLOW_CURRENT,
            'not submitted for review by lab': ALLOW_CURRENT,

            # antibody_lot
            'eligible for new data': ALLOW_CURRENT,
            'not eligible for new data': ALLOW_CURRENT,
            'not pursued': ALLOW_CURRENT,

            # dataset / experiment
            'release ready': ALLOW_AUTHENTICATED_VIEW,
            'revoked': ALLOW_CURRENT,

            # publication
            'published': ALLOW_CURRENT,
        }
        actions = [EDIT_ACTION]

        @property
        def __name__(self):
            if self.name_key is None:
                return self.uuid
            properties = self.upgrade_properties(finalize=False)
            if properties.get('status') == 'replaced':
                return self.uuid
            return properties.get(self.name_key, None) or self.uuid

        def __acl__(self):
            # Don't finalize to avoid validation here.
            properties = self.upgrade_properties(finalize=False).copy()
            ns = self.template_namespace(properties)
            properties.update(ns)
            status = ns.get('status')
            return self.STATUS_ACL.get(status, ALLOW_LAB_SUBMITTER_EDIT)

        def __ac_local_roles__(self):
            roles = {}
            properties = self.upgrade_properties(finalize=False).copy()
            ns = self.template_namespace(properties)
            properties.update(ns)
            if 'lab' in properties:
                lab_submitters = 'submits_for.%s' % properties['lab']
                roles[lab_submitters] = 'role.lab_submitter'
            return roles


@location('labs')
class Lab(Collection):
    item_type = 'lab'
    schema = load_schema('lab.json')
    __acl__ = ALLOW_EVERYONE_VIEW
    properties = {
        'title': 'Labs',
        'description': 'Listing of ENCODE DCC labs',
    }
    item_name_key = 'name'
    unique_key = 'lab:name'
    item_keys = [
        {'name': '{item_type}:name', 'value': '{name}', '$templated': True},
        {'name': '{item_type}:name', 'value': '{title}', '$templated': True},
    ]


@location('awards')
class Award(Collection):
    item_type = 'award'
    schema = load_schema('award.json')
    __acl__ = ALLOW_EVERYONE_VIEW
    properties = {
        'title': 'Awards (Grants)',
        'description': 'Listing of awards (aka grants)',
    }
    item_name_key = 'name'
    unique_key = 'award:name'
    item_keys = ['name']


@location('antibody-lots')
class AntibodyLot(Collection):
    item_type = 'antibody_lot'
    schema = load_schema('antibody_lot.json')
    properties = {
        'title': 'Antibodies Registry',
        'description': 'Listing of ENCODE antibodies',
    }

    class Item(Collection.Item):
        template = {
            'lot_reviews': [
                {'$value': lambda lot_review: lot_review, '$repeat': 'lot_review lot_reviews', '$templated': True}
            ],
            'title': {'$value': '{accession}', '$templated': True},
        }
        name_key = 'accession'

        keys = ACCESSION_KEYS + ALIAS_KEYS + [
            {
                'name': '{item_type}:source_product_lot',
                'value': '{source}/{product_id}/{lot_id}',
                '$templated': True,
            },
            {
                'name': '{item_type}:source_product_lot',
                'value': '{source}/{product_id}/{alias}',
                '$repeat': 'alias lot_id_alias',
                '$templated': True,
            },
        ]

        rev = {
            'characterizations': ('antibody_characterization', 'characterizes'),
        }

        embedded = set([
            'source',
            'host_organism',
            'characterizations.award',
            'characterizations.lab',
            'characterizations.submitted_by',
            'characterizations.target.organism',
            'lot_reviews.targets',
            'lot_reviews.organisms'
        ])

        def template_namespace(self, properties, request=None):
            ns = super(AntibodyLot.Item, self).template_namespace(properties, request)
            if request is None:
                return ns
            if ns['characterizations']:
                compliant_secondary = False
                not_compliant_secondary = False
                pending_secondary = False
                has_lane_review = False
                not_reviewed = False
                histone_mod_target = False
                lab_not_reviewed_chars = 0
{'fraction_non_alphanumeric': 0.1331233228328083, 'fraction_numerical': 0.0485357601213394, 'mean_word_length': 2.361372549019608, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2103371', 'n_tokens_mistral': 6238, 'n_tokens_neox': 5728, 'n_words': 983}