packages.py 2.58 KB
Newer Older
Leonidas Spyropoulos's avatar
WIP  
Leonidas Spyropoulos committed
1
2
3
4
5
6
7
8
from http import HTTPStatus

from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import HTMLResponse

from aurweb import db
from aurweb.models.package import Package
from aurweb.models.package_base import PackageBase
Leonidas Spyropoulos's avatar
WIP2    
Leonidas Spyropoulos committed
9
from aurweb.models.package_keyword import PackageKeyword
Leonidas Spyropoulos's avatar
WIP  
Leonidas Spyropoulos committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from aurweb.templates import make_variable_context, render_template

router = APIRouter()

ITEMS_PER_PAGE = 50  # Paged table size.


async def package_template(request: Request,
                           coff: int = 0,  # current offset
                           poff: int = 0):
    context = await make_variable_context(request, "Packages")
    current_off, past_off = coff, poff
    context["pp"] = pp = ITEMS_PER_PAGE

    packages = db.query(Package).join(PackageBase).order_by(PackageBase.Popularity.desc())

    context["pkglist_count"] = packages.count()
    context["pkglist"] = list(packages.limit(pp).offset(current_off))
    context["current_off"] = current_off
    context["q"] = '&'.join([f"coff={current_off}", f"poff={past_off}"])

    return render_template(request, "package/packages.html", context)


@router.get("/packages", response_class=HTMLResponse)
async def package(request: Request,
                  coff: int = 0,  # current offset
                  poff: int = 0):  # past offset
    return await package_template(request, coff, poff)


@router.get("/packages/{package}")
async def package_single(request: Request, package: str,
                         coff: int = 0,  # current offset
                         poff: int = 0):  # past offset

    context = await make_variable_context(request, "Packages")
    current_off, past_off = coff, poff

    pkg = db.query(Package).join(PackageBase).filter(PackageBase.Name == package).first()
    if not pkg:
        raise HTTPException(status_code=int(HTTPStatus.NOT_FOUND))

    context["package"] = pkg
    context["current_off"] = current_off
    context["q"] = '&'.join([f"coff={current_off}", f"poff={past_off}"])

    return render_template(request, "package/packages.html", context)
Leonidas Spyropoulos's avatar
WIP2    
Leonidas Spyropoulos committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72


@router.get("/pkgbase/{package}")
async def package_base(request: Request, package: str):

    context = await make_variable_context(request, "Packages")
    packages = db.query(Package).join(PackageBase).filter(PackageBase.Name == package).outerjoin(PackageKeyword)
    if not packages:
        raise HTTPException(status_code=int(HTTPStatus.NOT_FOUND))

    context["pkgbase"] = packages.first().PackageBase
    context["packages_count"] = packages.count()
    context["packages"] = packages.all()

    return render_template(request, "package/packages.html", context)