Beispiel #1
0
class OrganisationMetadataSchemaV1(StrictKeysMixin):
    """Schema for the organisation metadata."""

    pid = PersistentIdentifier()
    code = SanitizedUnicode(required=True)
    name = SanitizedUnicode(required=True)
    isShared = fields.Boolean()
    isDedicated = fields.Boolean()
    # When loading, if $schema is not provided, it's retrieved by
    # Record.schema property.
    schema = GenFunction(load_only=True,
                         attribute="$schema",
                         data_key="$schema",
                         deserialize=schema_from_organisation)
    permissions = fields.Dict(dump_only=True)

    @pre_dump
    def add_permissions(self, item):
        """Add permissions to record.

        :param item: Dict representing the record.
        :returns: Modified dict.
        """
        item['permissions'] = {
            'read': OrganisationPermission.read(current_user, item),
            'update': OrganisationPermission.update(current_user, item),
            'delete': OrganisationPermission.delete(current_user, item)
        }

        return item
Beispiel #2
0
class MetadataSchemaBaseV1(StrictKeysMixin):
    """Schema for the record metadata."""

    id = PersistentIdentifier()
    identifiers = Nested(IdentifierSchemaV1, many=True, required=True)
    name = SanitizedUnicode(required=True, validate=validate.Length(min=3))
    status = SanitizedUnicode()
    aliases = fields.List(SanitizedUnicode(), many=True)
    acronyms = fields.List(SanitizedUnicode(), many=True)
    types = fields.List(SanitizedUnicode(), many=True)
    wikipedia_url = fields.Url()
    email_address = fields.Email()
    ip_addresses = fields.List(SanitizedUnicode(), many=True)
    established = fields.Integer()
    onei_registry = fields.Integer()
    exportable = fields.Bool()
    links = fields.List(fields.Url(), many=True)
    labels = Nested(LabelSchemaV1, many=True)
    relationships = Nested(RelationSchemaV1, many=True)
    addresses = Nested(AddressSchemaV1, many=True)
    redirect = IdentifierSchemaV1()

    _schema = GenFunction(
        attribute="$schema",
        data_key="$schema",
        deserialize=schema_from_context,  # to be added only when loading
    )
Beispiel #3
0
    class GeneratedFieldsSchema(schema_to_use):
        """Test schema."""

        if marshmallow_version[0] >= 3:

            class Meta:
                """Meta attributes for the schema."""

                unknown = EXCLUDE

        gen_function = GenFunction(
            serialize=serialize_func,
            deserialize=deserialize_func,
        )

        gen_method = GenMethod(
            serialize='_serialize_gen_method',
            deserialize='_desererialize_gen_method',
            missing='raises-warning',
        )

        def _serialize_gen_method(self, obj):
            # "meth-foo" from context or "meth-bar" from the object
            return self.context.get('meth-foo', obj.get('meth-bar', missing))

        def _desererialize_gen_method(self, value, data):
            # "meth-foo" from context or "meth-bar" from the data
            return self.context.get('meth-foo', data.get('meth-bar', missing))
Beispiel #4
0
class CustomFieldSchema(schema_to_use):
    """Test schema."""

    if marshmallow_version[0] >= 3:

        class Meta:
            """."""

            unknown = EXCLUDE

    date_string_field = DateString(attribute='date_string_field')
    sanitized_html_field = SanitizedHTML(attribute='sanitized_html_field')
    sanitized_unicode_field = SanitizedUnicode(
        attribute='sanitized_unicode_field')
    trimmed_string_field = TrimmedString(attribute='trimmed_string_field')
    gen_function_field = GenFunction(
        lambda o: 'serialize_gen_function_field',
        lambda o: 'deserialize_gen_function_field',
    )
    gen_method_field = GenMethod('serialize_gen_method_field',
                                 'deserialize_gen_method_field')
    persistent_identifier_field = PersistentIdentifier()

    def serialize_gen_method_field(self, obj):
        """Serialize a value for the GenMethod field."""
        return 'serialize_gen_method_field'

    def deserialize_gen_method_field(self, value):
        """Deserialize a value for the GenMethod field."""
        return 'deserialize_gen_method_field'
Beispiel #5
0
class RecordSchemaV1(StrictKeysMixin):
    """Record schema."""
    metadata = fields.Nested(MetadataSchemaV1)
    created = fields.Str(dump_only=True)
    revision = fields.Integer(dump_only=True)
    updated = fields.Str(dump_only=True)
    links = fields.Dict(dump_only=True)
    id = PersistentIdentifier()
    files = GenFunction(serialize=files_from_context,
                        deserialize=files_from_context)
Beispiel #6
0
class CommunitySchemaMetadataV1(StrictKeysMixin):
    """Community metadata schema."""

    schema_ = fields.Str(attribute="$schema", dump_to="$schema")

    id = GenFunction(
        deserialize=pid_from_context_or_rec,
        serialize=pid_from_context_or_rec  # to be added only when loading
    )
    title = SanitizedUnicode(required=True)
    description = SanitizedHTML()
    curation_policy = SanitizedHTML()
    page = SanitizedHTML()
    type = fields.Str(required=True,
                      validate=validate.OneOf([
                          'organization',
                          'event',
                          'topic',
                          'project',
                      ]))
    alternate_identifiers = fields.List(fields.Raw())
    website = fields.Url()
    funding = fields.List(fields.String())
    domain = fields.List(fields.String())
    verified = fields.Boolean()
    visibility = fields.Str(validate=validate.OneOf([
        'public',
        'private',
        'hidden',
    ]))
    member_policy = fields.Str(validate=validate.OneOf([
        'open',
        'closed',
    ]))
    record_policy = fields.Str(validate=validate.OneOf([
        'open',
        'closed',
        'restricted',
    ]))
    archived = fields.Boolean()
    created_by = GenFunction(deserialize=load_creator,
                             serialize=serialize_creator)
Beispiel #7
0
class UserMetadataSchemaV1(StrictKeysMixin):
    """Schema for the user metadata."""

    pid = PersistentIdentifier()
    full_name = SanitizedUnicode(required=True)
    birth_date = SanitizedUnicode()
    email = SanitizedUnicode(required=True)
    street = SanitizedUnicode()
    postal_code = SanitizedUnicode()
    city = SanitizedUnicode()
    phone = SanitizedUnicode()
    organisation = fields.Dict()
    roles = fields.List(SanitizedUnicode)
    # When loading, if $schema is not provided, it's retrieved by
    # Record.schema property.
    schema = GenFunction(load_only=True,
                         attribute="$schema",
                         data_key="$schema",
                         deserialize=schema_from_user)
    permissions = fields.Dict(dump_only=True)

    @pre_load
    def guess_organisation(self, data, **kwargs):
        """Guess organisation from current logged user.

        :param data: Dict of user data.
        :returns: Modified dict of user data.
        """
        # Organisation already attached to user, we do nothing.
        if data.get('organisation'):
            return data

        # Store current user organisation in new user.
        user = UserRecord.get_user_by_current_user(current_user)
        if user.get('organisation'):
            data['organisation'] = user['organisation']

        return data

    @pre_dump
    def add_permissions(self, item):
        """Add permissions to record.

        :param data: Dict of user data.
        :returns: Modified dict of user data.
        """
        item['permissions'] = {
            'read': UserPermission.read(current_user, item),
            'update': UserPermission.update(current_user, item),
            'delete': UserPermission.delete(current_user, item)
        }

        return item
Beispiel #8
0
class MetadataSchemaV1(StrictKeysMixin):
    """Schema for the record metadata."""

    id = PersistentIdentifier()
    title = SanitizedUnicode(required=True, validate=validate.Length(min=3))
    keywords = fields.List(SanitizedUnicode(), many=True)
    publication_date = DateString()
    contributors = Nested(ContributorSchemaV1, many=True, required=True)
    _schema = GenFunction(
        attribute="$schema",
        data_key="$schema",
        deserialize=schema_from_context,  # to be added only when loading
    )
Beispiel #9
0
class DepositMetadataSchemaV1(StrictKeysMixin):
    """Schema for the deposit metadata."""

    pid = PersistentIdentifier()
    contributors = fields.List(fields.Dict())
    diffusion = fields.Dict()
    document = fields.Dict()
    logs = fields.List(fields.Dict())
    metadata = fields.Dict()
    status = SanitizedUnicode()
    step = SanitizedUnicode()
    user = fields.Dict()
    projects = fields.List(fields.Dict())
    _files = fields.List(fields.Dict())
    _bucket = SanitizedUnicode()
    # When loading, if $schema is not provided, it's retrieved by
    # Record.schema property.
    schema = GenFunction(load_only=True,
                         attribute="$schema",
                         data_key="$schema",
                         deserialize=schema_from_deposit)
    permissions = fields.Dict(dump_only=True)

    @pre_dump
    def add_permissions(self, item, **kwargs):
        """Add permissions to record.

        :param item: Dict representing the record
        :returns: Dict of modified record.
        """
        item['permissions'] = {
            'read': DepositPermission.read(current_user_record, item),
            'update': DepositPermission.update(current_user_record, item),
            'delete': DepositPermission.delete(current_user_record, item)
        }

        return item

    @pre_load
    def remove_fields(self, data, **kwargs):
        """Removes computed fields.

        :param data: Dict of record data.
        :returns: Modified data.
        """
        data.pop('permissions', None)

        return data
Beispiel #10
0
class OrganisationMetadataSchemaV1(StrictKeysMixin):
    """Schema for the organisation metadata."""

    pid = PersistentIdentifier()
    code = SanitizedUnicode(required=True)
    name = SanitizedUnicode(required=True)
    description = SanitizedUnicode()
    isShared = fields.Boolean(validate=can_activate_mode)
    isDedicated = fields.Boolean(validate=can_activate_mode)
    allowedIps = SanitizedUnicode()
    # When loading, if $schema is not provided, it's retrieved by
    # Record.schema property.
    schema = GenFunction(load_only=True,
                         attribute="$schema",
                         data_key="$schema",
                         deserialize=schema_from_organisation)
    permissions = fields.Dict(dump_only=True)
    _files = fields.List(fields.Dict())
    _bucket = SanitizedUnicode()

    @pre_dump
    def add_permissions(self, item, **kwargs):
        """Add permissions to record.

        :param item: Dict representing the record.
        :returns: Modified dict.
        """
        item['permissions'] = {
            'read': OrganisationPermission.read(current_user, item),
            'update': OrganisationPermission.update(current_user, item),
            'delete': OrganisationPermission.delete(current_user, item)
        }

        return item

    @pre_load
    def remove_fields(self, data, **kwargs):
        """Removes computed fields.

        :param data: Dict of record data.
        :returns: Modified data.
        """
        data.pop('permissions', None)

        return data
Beispiel #11
0
class MetadataSchemaV1(StrictKeysMixin):
    """Schema for the record metadata."""

    id = PersistentIdentifier()
    title = SanitizedUnicode()
    creator = SanitizedUnicode()
    fileName = SanitizedUnicode()
    fileType = SanitizedUnicode()
    owner = fields.Integer()
    metadata = Nested(MetaDataSchemaX)
    keywords = fields.List(SanitizedUnicode(), many=True)
    publication_date = DateString()
    contributors = Nested(ContributorSchemaV1, many=True)
    doi = fields.Str()
    _schema = GenFunction(
        attribute="$schema",
        data_key="$schema",
        deserialize=schema_from_context,  # to be added only when loading
    )
    _oai = Nested(OaiSchemaV1)

    class Meta:
        unknown = INCLUDE
        class GeneratedFieldsSchema(StrictKeysMixin):
            """Test schema."""

            gen_function = GenFunction(
                serialize=serialize_func,
                deserialize=deserialize_func,
            )

            gen_method = GenMethod(
                serialize='_serialize_gen_method',
                deserialize='_desererialize_gen_method',
                missing='raises-warning',
            )

            def _serialize_gen_method(self, obj):
                # "meth-foo" from context or "meth-bar" from the object
                return self.context.get('meth-foo',
                                        obj.get('meth-bar', missing))

            def _desererialize_gen_method(self, value, data):
                # "meth-foo" from context or "meth-bar" from the data
                return self.context.get('meth-foo',
                                        data.get('meth-bar', missing))
class CustomFieldSchema(StrictKeysMixin):
    """Test schema."""

    date_string_field = DateString(attribute='date_string_field')
    sanitized_html_field = SanitizedHTML(attribute='sanitized_html_field')
    sanitized_unicode_field = SanitizedUnicode(
        attribute='sanitized_unicode_field')
    trimmed_string_field = TrimmedString(attribute='trimmed_string_field')
    gen_function_field = GenFunction(
        lambda o: 'serialize_gen_function_field',
        lambda o: 'deserialize_gen_function_field',
    )
    gen_method_field = GenMethod('serialize_gen_method_field',
                                 'deserialize_gen_method_field')
    persistent_identifier_field = PersistentIdentifier()

    def serialize_gen_method_field(self, obj):
        """Serialize a value for the GenMethod field."""
        return 'serialize_gen_method_field'

    def deserialize_gen_method_field(self, value):
        """Deserialize a value for the GenMethod field."""
        return 'deserialize_gen_method_field'
Beispiel #14
0
class OrganisationMetadataSchemaV1(StrictKeysMixin):
    """Schema for the organisation metadata."""

    pid = PersistentIdentifier()
    code = SanitizedUnicode(required=True)
    name = SanitizedUnicode(required=True)
    description = fields.List(fields.Dict())
    footer = fields.List(fields.Dict())
    isShared = fields.Boolean()
    isDedicated = fields.Boolean()
    allowedIps = SanitizedUnicode()
    platformName = SanitizedUnicode()
    documentsCustomField1 = fields.Dict()
    documentsCustomField2 = fields.Dict()
    documentsCustomField3 = fields.Dict()
    publicDocumentFacets = fields.List(fields.String())
    # When loading, if $schema is not provided, it's retrieved by
    # Record.schema property.
    schema = GenFunction(load_only=True,
                         attribute="$schema",
                         data_key="$schema",
                         deserialize=schema_from_organisation)
    permissions = fields.Dict(dump_only=True)
    _files = fields.List(fields.Dict())
    _bucket = SanitizedUnicode()

    @pre_dump
    def add_permissions(self, item, **kwargs):
        """Add permissions to record.

        :param item: Dict representing the record.
        :returns: Modified dict.
        """
        item['permissions'] = {
            'read': OrganisationPermission.read(current_user_record, item),
            'update': OrganisationPermission.update(current_user_record, item),
            'delete': OrganisationPermission.delete(current_user_record, item)
        }

        return item

    @pre_load
    def remove_fields(self, data, **kwargs):
        """Removes computed fields.

        :param data: Dict of record data.
        :returns: Modified data.
        """
        data.pop('permissions', None)

        return data

    @pre_load
    def override_modes(self, data, **kwargs):
        """Override organisation's modes.

        For non super users, the modes (isShared, isDedicated) is not saved
        by posting the data, but guessed from user's organisation.
        """
        if not has_superuser_access():
            data['isShared'] = current_organisation.get('isShared', False)
            data['isDedicated'] = current_organisation.get(
                'isDedicated', False)
        return data
Beispiel #15
0
class ProjectMetadataSchemaV1(StrictKeysMixin):
    """Schema for the project metadata."""

    pid = PersistentIdentifier()
    name = SanitizedUnicode(required=True)
    description = SanitizedUnicode()
    startDate = SanitizedUnicode()
    endDate = SanitizedUnicode()
    identifiedBy = fields.Dict()
    investigators = fields.List(fields.Dict())
    funding_organisations = fields.List(fields.Dict())
    organisation = fields.Dict()
    user = fields.Dict()
    documents = fields.List(fields.Dict(), dump_only=True)
    # When loading, if $schema is not provided, it's retrieved by
    # Record.schema property.
    schema = GenFunction(load_only=True,
                         attribute="$schema",
                         data_key="$schema",
                         deserialize=schema_from_project)
    permissions = fields.Dict(dump_only=True)

    def dump(self, obj, *args, **kwargs):
        """Dump object.

        Override the parent method to add the documents linked to projects.
        It was not possible to use the `pre_dump` decorator, because
        `add_permission` need this property and we cannot be sure that this
        hook will be executed first.
        """
        obj['documents'] = DocumentRecord.get_documents_by_project(obj['pid'])
        return super(ProjectMetadataSchemaV1, self).dump(obj, *args, **kwargs)

    @pre_dump
    def add_permissions(self, item, **kwargs):
        """Add permissions to record.

        :param item: Dict representing the record.
        :returns: Modified dict.
        """
        item['permissions'] = {
            'read': ProjectPermission.read(current_user_record, item),
            'update': ProjectPermission.update(current_user_record, item),
            'delete': ProjectPermission.delete(current_user_record, item)
        }

        return item

    @pre_load
    def remove_fields(self, data, **kwargs):
        """Removes computed fields.

        :param data: Dict of record data.
        :returns: Modified data.
        """
        data.pop('permissions', None)
        data.pop('documents', None)

        return data

    @pre_load
    def guess_organisation(self, data, **kwargs):
        """Guess organisation from current logged user.

        :param data: Dict of record data.
        :returns: Modified dict of record data.
        """
        # Organisation already attached to document, we do nothing.
        if data.get('organisation'):
            return data

        # Store current user organisation in new document.
        if current_user_record.get('organisation'):
            data['organisation'] = current_user_record['organisation']

        return data

    @pre_load
    def guess_user(self, data, **kwargs):
        """Guess user.

        :param data: Dict of record data.
        :returns: Modified dict of record data.
        """
        # If user is already set, we don't set it.
        if data.get('user'):
            return data

        # Store current user in project.
        data['user'] = {
            '$ref':
            current_user_record.get_ref_link('users',
                                             current_user_record['pid'])
        }

        return data
Beispiel #16
0
class RecordMetadataSchema(StrictKeysMixin):
    """Schema for record metadata."""

    pid = PersistentIdentifier()
    name = fields.List(fields.Dict(), required=True)
    organisation = fields.Dict()
    permissions = fields.Dict(dump_only=True)
    label = fields.Method('get_label')
    # When loading, if $schema is not provided, it's retrieved by
    # Record.schema property.
    schema = GenFunction(load_only=True,
                         attribute="$schema",
                         data_key="$schema",
                         deserialize=schema_from_record)

    def get_label(self, obj):
        """Get label."""
        return get_language_value(obj['name'])

    @pre_load
    def remove_fields(self, data, **kwargs):
        """Removes computed fields.

        :param dict data: Record data
        :return: Modified data
        :rtype: dict
        """
        data.pop('permissions', None)
        data.pop('label', None)

        return data

    @pre_load
    def guess_organisation(self, data, **kwargs):
        """Guess organisation from current logged user.

        :param dict data: Record data
        :return: Modified data
        :rtype: dict
        """
        # Organisation already attached to project, we do nothing.
        if data.get('organisation'):
            return data

        # Store current user organisation in new project.
        if current_user_record.get('organisation'):
            data['organisation'] = current_user_record['organisation']

        return data

    @pre_dump
    def add_permissions(self, item, **kwargs):
        """Add permissions to record.

        :param dict item: Record data
        :return: Modified item
        :rtype: dict
        """
        item['permissions'] = {
            'read': RecordPermission.read(current_user_record, item),
            'update': RecordPermission.update(current_user_record, item),
            'delete': RecordPermission.delete(current_user_record, item)
        }

        return item
Beispiel #17
0
class DocumentMetadataSchemaV1(StrictKeysMixin):
    """Schema for the document metadata."""

    pid = PersistentIdentifier()
    ark = SanitizedUnicode()
    documentType = SanitizedUnicode()
    title = fields.List(fields.Dict())
    partOf = fields.List(fields.Dict())
    abstracts = fields.List(fields.Dict())
    contribution = fields.List(fields.Dict())
    organisation = fields.List(fields.Dict())
    language = fields.List(fields.Dict())
    copyrightDate = fields.List(fields.String())
    editionStatement = fields.Dict()
    provisionActivity = fields.List(fields.Dict())
    extent = SanitizedUnicode()
    otherMaterialCharacteristics = SanitizedUnicode()
    formats = fields.List(SanitizedUnicode())
    additionalMaterials = SanitizedUnicode()
    series = fields.List(fields.Dict())
    notes = fields.List(fields.String())
    identifiedBy = fields.List(fields.Dict())
    subjects = fields.List(fields.Dict())
    classification = fields.List(fields.Dict())
    collections = fields.List(fields.Dict())
    dissertation = fields.Dict()
    otherEdition = fields.List(fields.Dict())
    usageAndAccessPolicy = fields.Dict()
    projects = fields.List(fields.Dict())
    oa_status = SanitizedUnicode()
    subdivisions = fields.List(fields.Dict())
    harvested = fields.Boolean()
    contentNote = fields.List(SanitizedUnicode())
    customField1 = fields.List(fields.String(validate=validate.Length(min=1)))
    customField2 = fields.List(fields.String(validate=validate.Length(min=1)))
    customField3 = fields.List(fields.String(validate=validate.Length(min=1)))
    masked = SanitizedUnicode()
    _bucket = SanitizedUnicode()
    _files = Nested(FileSchemaV1, many=True)
    _oai = fields.Dict()
    # When loading, if $schema is not provided, it's retrieved by
    # Record.schema property.
    schema = GenFunction(load_only=True,
                         attribute="$schema",
                         data_key="$schema",
                         deserialize=schema_from_document)
    permissions = fields.Dict(dump_only=True)
    permalink = SanitizedUnicode(dump_only=True)

    @pre_dump
    def populate_files_properties(self, item, **kwargs):
        """Add some customs properties to file before dumping it.

        :param item: Item object to process
        :returns: Modified item
        """
        if not item.get('_files'):
            return item

        # Check if organisation record forces to point file to an external url
        item['external_url'] = has_external_urls_for_files(item)

        # Add restriction, link and thumbnail to files
        populate_files_properties(item)

        # Sort files to have the main file in first position
        item['_files'] = sorted(item['_files'],
                                key=lambda file: file.get('order', 100))

        return item

    @pre_dump
    def add_permissions(self, item, **kwargs):
        """Add permissions to record.

        :param item: Dict representing the record.
        :returns: Modified dict.
        """
        # For public views, no check for permissions
        if request.args.get('view'):
            return item

        item['permissions'] = {
            'read': DocumentPermission.read(current_user_record, item),
            'update': DocumentPermission.update(current_user_record, item),
            'delete': DocumentPermission.delete(current_user_record, item)
        }

        return item

    @pre_dump
    def add_permalink(self, item, **kwargs):
        """Add permanent link to document."""
        item['permalink'] = DocumentRecord.get_permanent_link(
            request.host_url, item['pid'])
        return item

    @pre_dump
    def add_formatted_texts(self, item, **kwargs):
        """Add formatted texts for objects which are processing in backend.

        :param item: Dict of record data.
        :returns: Modified data.
        """
        # Provision activity processing
        for index, provision_activity in enumerate(
                item.get('provisionActivity', [])):
            item['provisionActivity'][index][
                'text'] = create_publication_statement(provision_activity)

        # Part of proccessing
        for index, part_of in enumerate(item.get('partOf', [])):
            item['partOf'][index]['text'] = part_of_format(part_of)

        # Contribution
        for index, contribution in enumerate(item.get('contribution', [])):
            item['contribution'][index]['text'] = contribution_text(
                contribution)

        if item.get('dissertation'):
            item['dissertation']['text'] = dissertation(item)

        return item

    @pre_load
    def guess_organisation(self, data, **kwargs):
        """Guess organisation from current logged user.

        :param data: Dict of record data.
        :returns: Modified dict of record data.
        """
        # Organisation already attached to document, we do nothing.
        if data.get('organisation'):
            return data

        # Store current user organisation in new document.
        if current_user_record.get('organisation'):
            data['organisation'] = [current_user_record['organisation']]

        return data

    @pre_load
    def remove_fields(self, data, **kwargs):
        """Removes computed fields.

        :param data: Dict of record data.
        :returns: Modified data.
        """
        for provision_activity in data.get('provisionActivity', []):
            provision_activity.pop('text', None)

        for part_of in data.get('partOf', []):
            part_of.pop('text', None)

        for contribution in data.get('contribution', []):
            contribution.pop('text', None)

        data.get('dissertation', {}).pop('text', None)

        data.pop('permalink', None)
        data.pop('permissions', None)

        return data