Exemplo n.º 1
0
async def get_sheets(
        request: Request,
        current_user: UserInDB = Depends(get_current_active_user),
        page: int = Query(1),
        sort: str = Query("piece"),
        direction: int = Query(1),
):
    limit = int(os.getenv("SHEETS_PER_PAGE", 20))
    sheet_cursor = await crud.get_user_sheets(current_user.email, page, sort,
                                              direction, limit)
    prev_page, next_page = get_next_prev_page_urls(request.url, page)
    if not await crud.user_sheets_has_next(current_user.email, page, limit):
        next_page = None
    user_sheets = [
        models.SheetOut.parse_obj(sheet) async for sheet in sheet_cursor
    ]
    sort_links = get_sort_links(request.url, sort, direction)
    return templates.TemplateResponse(
        "list.html",
        {
            "request": request,
            "page": page,
            "sort": sort,
            "direction": direction,
            "sheets": user_sheets,
            "prev_page": prev_page,
            "next_page": next_page,
            "title": "All Sheets",
            "sort_links": sort_links,
        },
    )
Exemplo n.º 2
0
async def get_single_instrument(
        request: Request,
        instrument_name: str,
        current_user: UserInDB = Depends(get_current_active_user),
        page: int = Query(1),
        sort: str = Query("piece"),
        direction: int = Query(1),
):
    limit = int(os.getenv("SHEETS_PER_PAGE", 20))
    prev_page, next_page = util.get_next_prev_page_urls(request.url, page)
    sheets = await crud.get_instrument_sheets(current_user.email,
                                              instrument_name, limit, page,
                                              sort, direction)
    if not await crud.instrument_sheets_has_next(current_user.email,
                                                 instrument_name, limit, page):
        next_page = None
    return templates.TemplateResponse(
        "list.html",
        {
            "request": request,
            "page": page,
            "sort": sort,
            "direction": direction,
            "sheets": sheets,
            "next_page": next_page,
            "prev_page": prev_page,
            "sort_links": util.get_sort_links(request.url, sort, direction),
            "title": instrument_name,
        },
    )
Exemplo n.º 3
0
async def get_related(
        request: Request,
        sheet_id: str,
        field: str = Query(...),
        page: int = Query(1),
        sort: str = Query("piece"),
        direction: int = Query(1),
        current_user: UserInDB = Depends(get_current_active_user),
):
    sheet_id = uuid.UUID(sheet_id)
    limit = int(os.getenv("SHEETS_PER_PAGE", 20))
    sheet = await crud.get_sheet_by_id(current_user.email, sheet_id)
    prev_page, next_page = get_next_prev_page_urls(request.url, page)
    if field in models.Sheet.allowed_related_fields():
        sheets = await crud.find_related(sheet,
                                         field,
                                         limit,
                                         page,
                                         sort,
                                         direction,
                                         exclude=False)
        if not await crud.related_has_next(sheet, field, page, limit):
            next_page = None
    else:
        sheets = []
    title_text = getattr(sheet, field)
    if isinstance(title_text, list):
        title_text = ", ".join(title_text)
    return templates.TemplateResponse(
        "list.html",
        {
            "request": request,
            "page": page,
            "sort": sort,
            "direction": direction,
            "sheets": sheets,
            "next_page": next_page,
            "prev_page": prev_page,
            "title": f"Related to {title_text}",
            "sort_links": get_sort_links(request.url, sort, direction),
        },
    )
Exemplo n.º 4
0
async def search_sheets(
        request: Request,
        current_user: UserInDB = Depends(get_current_active_user),
        search: str = Query(...),
        page: int = Query(1),
        sort: str = Query("piece"),
        direction: int = Query(1),
):
    limit = int(os.getenv("SHEETS_PER_PAGE", 20))
    found = crud.find_sheet_from_text(
        current_user.email,
        search,
        page=page,
        sort=sort,
        direction=direction,
        limit=limit,
    )
    sheets = [models.SheetOut.parse_obj(item) async for item in found]
    sort_links = get_sort_links(request.url, sort, direction)
    prev_page, next_page = get_next_prev_page_urls(request.url, page)
    if not await crud.sheet_search_has_next(current_user.email, search, page,
                                            limit):
        next_page = None
    return templates.TemplateResponse(
        "list.html",
        {
            "request": request,
            "page": page,
            "sort": sort,
            "direction": direction,
            "sheets": sheets,
            "prev_page": prev_page,
            "next_page": next_page,
            "title": "Found Sheets",
            "sort_links": sort_links,
        },
    )