Beispiel #1
0
def list_laws(
    include: Optional[ListLawsIncludeOptions] = None,
    page: int = Query(1, gt=0, description=description_page),
    per_page: int = Query(10, gt=0, le=100, description=description_per_page),
):
    """
    Lists all available laws. Use the optional query parameter `include=all_fields` to include all law metadata.
    """
    schema_class = api_schemas.LawBasicFields
    if include == ListLawsIncludeOptions.all_fields:
        schema_class = api_schemas.LawAllFields

    with db.session_scope() as session:
        pagination = db.all_laws_paginated(session, page, per_page)
        data = [schema_class.from_orm_model(law) for law in pagination.items]

    return {
        "data": data,
        "pagination": {
            "total": pagination.total,
            "page": pagination.page,
            "per_page": pagination.per_page
        },
        "links": {
            "prev": urls.list_laws(pagination.prev_page, per_page, include),
            "next": urls.list_laws(pagination.next_page, per_page, include)
        }
    }
Beispiel #2
0
def json_generate(c, law_abbr):
    """
    Update JSON response for a single law in example_json/.
    """
    with db.session_scope() as session:
        law = db.find_law_by_slug(session, law_abbr)
        if not law:
            raise Exception(f'Could not find law by slug "{law_abbr}". Has it been ingested yet?')
        gesetze_im_internet.write_law_json_file(session, law, "example_json")
Beispiel #3
0
def get_article(slug: str = Path(
    ..., description="URL-safe lowercased abbreviation of the law."),
                article_id: str = Path(..., description="The article's ID.")):
    """
    Get data for an individual article within a law.
    """
    with db.session_scope() as session:
        content_item = db.find_content_item_by_id_and_law_slug(
            session, article_id, slug)
        if not content_item:
            raise ApiException(status_code=404,
                               title="Resource not found",
                               detail="Could not find article.")

        return {
            "data":
            api_schemas.ContentItemAllFields.from_orm_model(content_item)
        }
Beispiel #4
0
def get_law(slug: str = Path(
    ..., description="URL-safe lowercased abbreviation of the law."),
            include: GetLawIncludeOptions = Query(
                None,
                description=
                "Whether to include the laws articles & section headings.")):
    """
    Get detailed metadata on a single law. Use the optional query parameter `include=contents` to also include the full text
    and metadata of all articles and section headings that comprise the law.
    """
    with db.session_scope() as session:
        law = db.find_law_by_slug(session, slug)
        if not law:
            raise ApiException(status_code=404,
                               title="Resource not found",
                               detail="Could not find a law for this slug.")

        law_data = api_schemas.LawAllFields.from_orm_model(
            law, include_contents=(include == GetLawIncludeOptions.contents))
        return api_schemas.LawResponse(data=law_data)
Beispiel #5
0
def get_search_results(
        q: str = Query(..., description="Query to search for."),
        type_filter: Optional[SearchTypeOptions] = Query(
            None,
            alias="type",
            description="Only return results of specified type."),
        page: int = Query(1, gt=0),
        per_page: int = Query(10, gt=0, le=100),
):
    """
    Returns laws and articles matching a search query.
    """
    orm_type_to_schema = {
        models.Law: api_schemas.LawBasicFields,
        models.ContentItem: api_schemas.ContentItemBasicFieldsWithLaw
    }
    with db.session_scope() as session:
        type_filter_value = type_filter and type_filter.value
        pagination = db.fulltext_search_laws_content_items(
            session, q, page, per_page, type_filter_value)
        data = [
            orm_type_to_schema[type(item)].from_orm_model(item)
            for item in pagination.items
        ]

    return {
        "data": data,
        "pagination": {
            "total": pagination.total,
            "page": pagination.page,
            "per_page": pagination.per_page
        },
        "links": {
            "prev": urls.search(q, pagination.prev_page, per_page,
                                type_filter),
            "next": urls.search(q, pagination.next_page, per_page, type_filter)
        }
    }
Beispiel #6
0
def ingest_data_from_location(c, data_location):
    """
    Process downloaded laws and store/update them in the DB.
    """
    with db.session_scope() as session:
        gesetze_im_internet.ingest_data_from_location(session, location_from_string(data_location))
Beispiel #7
0
def update_bulk_law_files(c):
    """
    Generate and upload bulk law files.
    """
    with db.session_scope() as session:
        gesetze_im_internet.generate_and_upload_bulk_law_files(session)