forked from osm-fr/osmose-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
byuser.py
102 lines (86 loc) · 2.88 KB
/
byuser.py
1
2
3
4
5
6
7
8
9
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import urllib.parse
from typing import Any, Dict, Union
from asyncpg import Connection
from fastapi import APIRouter, Depends, Request, Response
from fastapi.responses import RedirectResponse
from lxml import etree
from lxml.builder import E # type: ignore
from api.user_utils import _user_count
from modules import utils
from modules.dependencies import commons_params, database, i18n, langs
from modules.utils import LangsNegociation, i10n_select_lang
router = APIRouter()
class XMLResponse(Response):
media_type = "text/xml; charset=utf-8"
def render(self, content: Any) -> bytes:
return etree.tostring(content, pretty_print=True)
class RSSResponse(XMLResponse):
media_type = "application/rss+xml"
@router.get("/byuser")
def byUser() -> RedirectResponse:
return RedirectResponse("byuser/")
@router.get("/byuser/{username}.{format}", response_model=None)
async def user(
username: str,
format: str,
request: Request,
db: Connection = Depends(database.db),
params=Depends(commons_params.params),
langs: LangsNegociation = Depends(langs.langs),
) -> Union[RedirectResponse, Dict[str, Any]]:
if format in ["rss", "gpx", "kml", "josm", "csv"]:
return RedirectResponse(
f"{utils.website}/api/0.3/issues.{format}?{request.url.query}&username={urllib.parse.quote(username)}"
)
return dict(
username=username,
users=username.split(","),
website=utils.website + "/" + i10n_select_lang(langs),
main_website=utils.main_website,
remote_url_read=utils.remote_url_read,
)
@router.get("/byuser_count/{username}.rss", response_class=RSSResponse)
async def user_count(
username: str,
db: Connection = Depends(database.db),
params=Depends(commons_params.params),
_=Depends(i18n.i18n),
) -> RSSResponse:
count = await _user_count(params, db, username)
counts = (
[
E.item(
E.title(
_("Number of level {level} issues: {count}").format(
level=1, count=count[1]
)
)
),
E.item(
E.title(
_("Number of level {level} issues: {count}").format(
level=2, count=count[2]
)
)
),
E.item(
E.title(
_("Number of level {level} issues: {count}").format(
level=3, count=count[3]
)
)
),
]
if count
else []
)
xml = E.rss(
E.channel(
E.title("Osmose - " + username),
E.description(_("Statistics for user {}").format(username)),
E.link("{}/byuser/{}".format(utils.website, username)),
*counts,
),
version="2.0",
)
return RSSResponse(xml)