コード例 #1
0
def representation_model_test(api):
    repr = Representation('representation')
    default_includes = current_app.config['FLASK_TAXONOMIES_REPRESENTATION'][
        'representation']['include']
    assert repr.include == set(default_includes)
    assert repr.exclude == set()
    assert repr.select is None

    repr1 = repr.copy()
    assert repr1 is not repr
    assert repr1.include is not repr.include
    assert repr1.exclude is not repr.exclude

    repr1 = repr.extend(include={INCLUDE_ID})
    assert repr != repr1

    assert repr1.include == repr.include | {INCLUDE_ID}
    assert INCLUDE_ID in repr1
    assert INCLUDE_ID not in repr

    repr1 = repr.extend(exclude=default_includes, select=['/a', '/b'])
    for i in default_includes:
        assert i in repr1.include
        assert i not in repr1

    assert repr1.select == {'/a', '/b'}
    repr2 = repr1.extend(select=['/a', '/c'])
    assert repr1.select is not repr2.select
    assert len(repr2.select) == 3
    assert set(repr2.select) == {'/a', '/b', '/c'}
コード例 #2
0
    def _deserialize(self, value, attr, data,
                     **kwargs) -> [Representation, None]:
        if not isinstance(value, (str, bytes)):
            raise self.make_error("invalid")
        try:
            value = utils.ensure_text_type(value)
        except UnicodeDecodeError as error:
            raise self.make_error("invalid_utf8") from error
        value = parse_options_header(value)
        command, _options = value
        if not command.startswith(RETURN_PREFIX):
            return None
        representation = command[len(RETURN_PREFIX):]
        options = {}
        for k, v in _options.items():
            if k in ('include', 'exclude', 'select'):
                options[k] = set((v or '').strip().split())
            else:
                options[k] = (v or '').strip()

        return Representation(representation,
                              include=options.pop('include', None),
                              exclude=options.pop('exclude', None),
                              select=options.pop('select', None),
                              options=options)
コード例 #3
0
def test_get_taxonomy_json_2(app, db, taxonomy_tree):
    paginator = get_taxonomy_json(code="test_taxonomy",
                                  slug="a/b",
                                  prefer=Representation(
                                      "representation",
                                      include=[INCLUDE_DESCENDANTS]))
    res = paginator.paginated_data
    assert "children" in res.keys()
コード例 #4
0
def get_taxonomy_json(code=None,
                      slug=None,
                      prefer: Representation = Representation("taxonomy"),
                      page=None,
                      size=None,
                      status_code=200,
                      q=None,
                      request=None):
    taxonomy = current_flask_taxonomies.get_taxonomy(code)
    prefer = taxonomy.merge_select(prefer)

    if request:
        current_flask_taxonomies.permissions.taxonomy_term_read.enforce(request=request,
                                                                        taxonomy=taxonomy,
                                                                        slug=slug)

    if INCLUDE_DELETED in prefer:
        status_cond = sqlalchemy.sql.true()
    else:
        status_cond = TaxonomyTerm.status == TermStatusEnum.alive

    return_descendants = INCLUDE_DESCENDANTS in prefer

    if return_descendants:
        query = current_flask_taxonomies.descendants_or_self(
            TermIdentification(taxonomy=code, slug=slug),
            levels=prefer.options.get('levels', None),
            status_cond=status_cond,
            return_descendants_count=INCLUDE_DESCENDANTS_COUNT in prefer,
            return_descendants_busy_count=INCLUDE_STATUS in prefer
        )
    else:
        query = current_flask_taxonomies.filter_term(
            TermIdentification(taxonomy=code, slug=slug),
            status_cond=status_cond,
            return_descendants_count=INCLUDE_DESCENDANTS_COUNT in prefer,
            return_descendants_busy_count=INCLUDE_STATUS in prefer
        )
    if q:
        query = current_flask_taxonomies.apply_term_query(query, q, code)
    paginator = Paginator(
        prefer,
        query, page if return_descendants else None,
        size if return_descendants else None,
        json_converter=lambda data:
        build_descendants(data, prefer, root_slug=None),
        allow_empty=INCLUDE_SELF not in prefer, single_result=INCLUDE_SELF in prefer,
        has_query=q is not None
    )
    return paginator
コード例 #5
0
def test_get_taxonomy_json_3(app, db, taxonomy_tree):
    include = [
        INCLUDE_URL, INCLUDE_DESCENDANTS_URL, INCLUDE_DESCENDANTS_COUNT,
        INCLUDE_ANCESTORS_HIERARCHY, INCLUDE_ANCESTORS, INCLUDE_ANCESTOR_LIST,
        INCLUDE_DATA, INCLUDE_ID, INCLUDE_DESCENDANTS, INCLUDE_ENVELOPE,
        INCLUDE_DELETED, INCLUDE_SLUG, INCLUDE_LEVEL, INCLUDE_STATUS,
        INCLUDE_SELF
    ]
    paginator = get_taxonomy_json(code="test_taxonomy",
                                  slug="a/b",
                                  prefer=Representation("representation",
                                                        include=include))
    res = paginator.paginated_data
    pprint(res)