From 6e79fc2302660e29b6642a766012f1e0a6549fbb Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 4 Apr 2024 22:05:51 +0200 Subject: add endpoint for json summary --- fietsboek/routes.py | 1 + fietsboek/views/profile.py | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/fietsboek/routes.py b/fietsboek/routes.py index 480094c..6b18c82 100644 --- a/fietsboek/routes.py +++ b/fietsboek/routes.py @@ -59,6 +59,7 @@ def includeme(config): config.add_route("delete-friend", "/me/delete-friend") config.add_route("accept-friend", "/me/accept-friend") config.add_route("json-friends", "/me/friends.json") + config.add_route("json-summary", "/me/summary.json") config.add_route("toggle-favourite", "/me/toggle-favourite") config.add_route("force-logout", "/me/force-logout") diff --git a/fietsboek/views/profile.py b/fietsboek/views/profile.py index b353abd..b8c5477 100644 --- a/fietsboek/views/profile.py +++ b/fietsboek/views/profile.py @@ -1,6 +1,7 @@ """Endpoints for the user profile pages.""" import datetime +import logging import sqlite3 import urllib.parse from dataclasses import dataclass @@ -16,6 +17,7 @@ from sqlalchemy.orm import aliased from .. import models, util from ..data import UserDataDir from ..models.track import TrackType, TrackWithMetadata +from ..summaries import Summary # A well-made transparent tile is actually pretty small (only 116 bytes), which # is even smaller than our HTTP 404 page. So not only is it more efficient @@ -42,6 +44,8 @@ EMPTY_TILE = bytes([ ]) # fmt: on +LOGGER = logging.getLogger(__name__) + @dataclass class CumulativeStats: @@ -247,4 +251,26 @@ def user_tile(request: Request) -> Response: return Response(result[0], content_type="image/png") -__all__ = ["profile", "user_tile"] +@view_config( + route_name="json-summary", request_method="GET", permission="profile.view", renderer="json" +) +def json_summary(request: Request) -> Response: + """Returns the per-month summary as JSON. + + :param request: The pyramid request. + :return: The response, rendered as JSON. + """ + query = request.identity.all_tracks_query() + query = select(aliased(models.Track, query)).where(query.c.type == TrackType.ORGANIC) + summary = Summary() + + for track in request.dbsession.execute(query).scalars(): + if track.cache is None: + LOGGER.debug("Skipping track %d as it has no cached metadata", track.id) + continue + summary.add(TrackWithMetadata(track, request.data_manager)) + + return {y.year: {m.month: m.total_length for m in y} for y in summary} + + +__all__ = ["profile", "user_tile", "json_summary"] -- cgit v1.2.3