Example #1
0
def list_region_products(keycloak: KeycloakClient, region_id, user, filter_):
    region = model.Region.query.get(region_id)
    if not region:
        return problem(404, 'Not Found', f'Region {region_id} does not exist')

    if region.users_group is not None:
        if not keycloak.user_check_role(user, ADMIN_ROLE):
            if not keycloak.user_check_group_any(
                    user, [region.users_group, region.owner_group]):
                raise Forbidden("You don't have access to this region.")

    products_relation = region.products_relation

    if 'name' in filter_:
        products_relation = products_relation.filter(
            model.Region.name.ilike(filter_['enabled']), )

    if 'enabled' in filter_:
        products_relation = products_relation.filter(
            model.Product.enabled == filter_['enabled'], )

    return [{
        'id': r.product_id,
        'region_id': r.region.id,
        'product_id': r.product.id,
        'product': r.product.to_dict(),
        'enabled': r.enabled,
    } | {
        '_href': _region_href(r.region) | _product_href(r.product)
    } for r in products_relation]
Example #2
0
def get_region(keycloak: KeycloakClient, region_id, user):
    region = model.Region.query.get(region_id)
    if not region:
        return problem(404, 'Not Found', f'Region {region_id} does not exist')

    if region.users_group is not None:
        if not keycloak.user_check_role(user, ADMIN_ROLE):
            if not keycloak.user_check_group_any(
                    user, [region.users_group, region.owner_group]):
                raise Forbidden("You don't have access to this region.")

    return region.to_dict() | {'_href': _region_href(region)}