allow year relative filtering

This commit is contained in:
ducklet 2021-06-22 09:59:48 +02:00
parent d09880438d
commit a6adfefdd8
3 changed files with 21 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
*.pyc
/.cache
/.pytest_cache
/data/*
/requirements.txt

View file

@ -2,7 +2,7 @@ import logging
import re
from dataclasses import fields
from pathlib import Path
from typing import Optional, Type, TypeVar
from typing import Literal, Optional, Type, TypeVar
import sqlalchemy
from databases import Database
@ -155,7 +155,7 @@ async def find_ratings(
media_type: str = None,
ignore_tv_episodes: bool = False,
include_unrated: bool = False,
year: int = None,
yearcomp: tuple[Literal["<", "=", ">"], int] = None,
limit_rows=10,
):
values = {
@ -176,9 +176,11 @@ async def find_ratings(
"""
)
if year:
if yearcomp:
op, year = yearcomp
assert op in "<=>"
values["year"] = year
conditions.append(f"{Movie._table}.release_year=:year")
conditions.append(f"{Movie._table}.release_year{op}:year")
if media_type:
values["media_type"] = media_type

View file

@ -1,6 +1,7 @@
import base64
import binascii
import logging
from typing import Literal
from starlette.applications import Starlette
from starlette.authentication import (
@ -50,6 +51,18 @@ def truthy(s: str):
return bool(s) and s.lower() in {"1", "yes", "true"}
def yearcomp(s: str):
if not s:
return
comp: Literal["<", "=", ">"] = "="
if (prefix := s[0]) in "<=>":
comp = prefix # type: ignore
s = s[len(prefix) :]
return comp, int(s)
async def ratings(request):
params = request.query_params
rows = await find_ratings(
@ -57,7 +70,7 @@ async def ratings(request):
media_type=params.get("media_type"),
ignore_tv_episodes=truthy(params.get("ignore_tv_episodes")),
include_unrated=truthy(params.get("include_unrated")),
year=int(params["year"]) if "year" in params else None,
yearcomp=yearcomp(params["year"]) if "year" in params else None,
)
aggr = {}