Пример #1
0
class DocumentUpdateSchema(Schema):
    title = String(allow_none=True)
    summary = String(allow_none=True)
    countries = List(Country())
    languages = List(Language())
    keywords = List(String(validate=Length(min=1, max=5000)))
    date = PartialDate(allow_none=True)
    authored_at = PartialDate(allow_none=True)
    modified_at = PartialDate(allow_none=True)
    published_at = PartialDate(allow_none=True)
    retrieved_at = PartialDate(allow_none=True)
    file_name = String(allow_none=True)
    author = String(allow_none=True)
    generator = String(allow_none=True)
    mime_type = String(allow_none=True)
    source_url = String(allow_none=True)
Пример #2
0
class CollectionSchema(BaseSchema):
    EXPAND = [
        ('creator', Role, 'creator', RoleReferenceSchema, False),
    ]

    label = String(validate=Length(min=2, max=500), required=True)
    foreign_id = String(missing=None)
    kind = String(dump_only=True)
    casefile = Boolean(missing=None)
    summary = String(allow_none=True)
    publisher = String(allow_none=True)
    publisher_url = Url(allow_none=True)
    data_url = Url(allow_none=True)
    info_url = Url(allow_none=True)
    countries = List(Country())
    languages = List(Language())
    secret = Boolean(dump_only=True)
    category = Category(missing=Collection.DEFAULT)
    creator_id = String(allow_none=True)
    creator = Nested(RoleReferenceSchema(), dump_only=True)
    team = List(Nested(RoleReferenceSchema()), dump_only=True)
    count = Integer(dump_only=True)
    schemata = Dict(dump_only=True, default={})

    @pre_load
    def flatten_collection(self, data):
        flatten_id(data, 'creator_id', 'creator')

    @pre_dump
    def visibility(self, data):
        if not is_mapping(data):
            return
        roles = [int(r) for r in data.get('roles', [])]
        public = Role.public_roles()
        data['secret'] = len(public.intersection(roles)) == 0

    @post_dump
    def hypermedia(self, data):
        pk = str(data.get('id'))
        data['links'] = {
            'self': url_for('collections_api.view', id=pk),
            'ui': collection_url(pk)
        }
        data['writeable'] = request.authz.can_write(pk)
        return data
Пример #3
0
class CollectionSchema(BaseSchema):
    EXPAND = [
        ('creator', Role, 'creator', RoleReferenceSchema, False),
    ]

    label = String(validate=Length(min=2, max=500), required=True)
    foreign_id = String(missing=None)
    kind = String(dump_only=True)
    casefile = Boolean(missing=None)
    summary = String(allow_none=True)
    publisher = String(allow_none=True)
    publisher_url = Url(allow_none=True)
    data_url = Url(allow_none=True)
    info_url = Url(allow_none=True)
    countries = List(Country())
    languages = List(Language())
    secret = Boolean(dump_only=True)
    category = Category(missing=Collection.DEFAULT)
    creator_id = String(allow_none=True)
    creator = Nested(RoleReferenceSchema(), dump_only=True)
    team = List(Nested(RoleReferenceSchema()), dump_only=True)
    count = Integer(dump_only=True)
    schemata = Dict(dump_only=True)

    @pre_load
    def flatten_collection(self, data):
        flatten_id(data, 'creator_id', 'creator')

    @post_dump
    def hypermedia(self, data):
        pk = str(data.get('id'))
        data['links'] = {
            'self': url_for('collections_api.view', id=pk),
            'xref': url_for('xref_api.index', id=pk),
            'xref_csv': url_for('xref_api.csv_export', id=pk, _authorize=True),
            'ui': collection_url(pk)
        }
        data['writeable'] = request.authz.can(pk, request.authz.WRITE)
        return data
Пример #4
0
class CollectionSchema(BaseSchema):
    EXPAND = [
        ('creator', Role, 'creator', RoleReferenceSchema, False),
    ]

    label = String(validate=Length(min=2, max=500), required=True)
    foreign_id = String()
    summary = String(allow_none=True)
    countries = List(Country())
    lanaguages = List(Language())
    managed = Boolean()
    secret = Boolean(dump_only=True)
    category = Category(required=True)
    creator_id = String(allow_none=True)
    creator = Nested(RoleReferenceSchema(), dump_only=True)
    count = Integer(dump_only=True)
    schemata = Dict(dump_only=True, default={})

    @pre_load()
    def flatten_collection(self, data):
        flatten_id(data, 'creator_id', 'creator')

    @pre_dump()
    def visibility(self, data):
        if not is_mapping(data):
            return
        roles = data.get('roles', [])
        public = Role.public_roles()
        data['secret'] = public.intersection(roles) < 0

    @post_dump
    def transient(self, data):
        pk = str(data.get('id'))
        data['links'] = {
            'self': url_for('collections_api.view', id=pk),
            'ui': collection_url(pk)
        }
        data['writeable'] = request.authz.can_write(pk)
        return data
Пример #5
0
class ShallowCombinedSchema(BaseSchema):
    collection_id = String()

    # Joint entity/document attributes
    collection = Nested(CollectionSchema())
    schema = SchemaName()
    schemata = List(SchemaName())
    names = List(String())
    addresses = List(String())
    phones = List(String())
    emails = List(String())
    identifiers = List(String())
    countries = List(Country())
    dates = List(PartialDate())
    bulk = Boolean()

    # Entity attributes
    foreign_id = String()
    name = String()
    entities = List(String())
    properties = Dict()

    # Document attributes
    status = String()
    content_hash = String()
    uploader_id = String()
    uploader = Nested(RoleReferenceSchema())
    error_message = String()
    title = String()
    summary = String()
    languages = List(Language())
    keywords = List(String())
    date = PartialDate()
    authored_at = PartialDate()
    modified_at = PartialDate()
    published_at = PartialDate()
    retrieved_at = PartialDate()
    file_name = String()
    file_size = Integer()
    author = String()
    generator = String()
    mime_type = String()
    extension = String()
    encoding = String()
    source_url = String()
    pdf_version = String()
    columns = List(String())
    headers = Dict()
    children = Integer()

    # TODO: is this a separate endpoint?
    text = String()
    html = String()

    def document_links(self, data, pk, schemata):
        links = {
            'self': url_for('documents_api.view', document_id=pk),
            'tags': url_for('entities_api.tags', id=pk),
            'ui': document_url(pk)
        }
        if data.get('content_hash'):
            links['file'] = url_for('documents_api.file',
                                    document_id=pk,
                                    _authorize=True)
        if schemata.intersection([Document.SCHEMA_PDF]):
            links['pdf'] = url_for('documents_api.pdf',
                                   document_id=pk,
                                   _authorize=True)
        if schemata.intersection([Document.SCHEMA_PDF, Document.SCHEMA_TABLE]):
            links['records'] = url_for('documents_api.records', document_id=pk)
        if schemata.intersection([Document.SCHEMA_FOLDER]):
            query = (('filter:parent.id', pk),)
            links['children'] = url_for('documents_api.index', _query=query)
        return links

    def entity_links(self, data, pk, schemata):
        return {
            'self': url_for('entities_api.view', id=pk),
            # 'similar': url_for('entities_api.similar', id=pk),
            # 'documents': url_for('entities_api.documents', id=pk),
            'references': url_for('entities_api.references', id=pk),
            'tags': url_for('entities_api.tags', id=pk),
            'ui': entity_url(pk)
        }

    @post_dump()
    def hypermedia(self, data):
        pk = str(data.get('id'))
        collection = data.get('collection', {})
        collection_id = collection.get('id')
        collection_id = collection_id or data.get('collection_id')
        schemata = set(data.get('schemata', []))
        if Document.SCHEMA in schemata:
            data['links'] = self.document_links(data, pk, schemata)
        else:
            data['links'] = self.entity_links(data, pk, schemata)

        if data.get('bulk'):
            data['writeable'] = False
        else:
            data['writeable'] = request.authz.can_write(collection_id)
        return data