Skip to content
Snippets Groups Projects
Commit 897f58be authored by David Trattnig's avatar David Trattnig
Browse files

List tracks pagination.

parent 5c642669
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
import datetime import datetime
from sqlalchemy import create_engine, Column, DateTime, String, Integer, Boolean from sqlalchemy import create_engine, Column, DateTime, String, Integer, Boolean
from sqlalchemy.event import listen
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow from flask_marshmallow import Marshmallow
...@@ -83,6 +84,28 @@ class PlayLog(db.Model): ...@@ -83,6 +84,28 @@ class PlayLog(db.Model):
return track return track
@staticmethod
def paginate(page, page_size):
"""
Returns a list of entries for a given page.
"""
def q(page=0, page_size=None):
query = db.session.query(PlayLog)
listen(query, 'before_compile', apply_limit(page, page_size), retval=True)
return query
def apply_limit(page, page_size):
def wrapped(query):
if page_size:
query = query.limit(page_size)
if page:
query = query.offset(page * page_size)
return query
return wrapped
return q(page, page_size)
@staticmethod @staticmethod
def select_last_hours(n): def select_last_hours(n):
""" """
...@@ -131,7 +154,7 @@ class PlayLogSchema(ma.SQLAlchemyAutoSchema): ...@@ -131,7 +154,7 @@ class PlayLogSchema(ma.SQLAlchemyAutoSchema):
sqla_session = db.session sqla_session = db.session
class TrackServiceSchema(ma.SQLAlchemySchema): class TrackSchema(ma.SQLAlchemySchema):
""" """
Schema for trackservice entries. Schema for trackservice entries.
""" """
......
import datetime import datetime
import connexion import connexion
import six import six
...@@ -7,8 +6,6 @@ from src.rest.models.play_log_entry import PlayLogEntry # noqa: E501 ...@@ -7,8 +6,6 @@ from src.rest.models.play_log_entry import PlayLogEntry # noqa: E501
from src.rest import util from src.rest import util
from flask import current_app from flask import current_app
def current_track(): # noqa: E501 def current_track(): # noqa: E501
"""Get current track """Get current track
...@@ -21,17 +18,18 @@ def current_track(): # noqa: E501 ...@@ -21,17 +18,18 @@ def current_track(): # noqa: E501
return service.current_track() return service.current_track()
def list_tracks(offset=None, limit=None): # noqa: E501 def list_tracks(page=None, limit=None): # noqa: E501
"""List recent tracks in the play-log """List recent tracks in the play-log
Lists the most recent track-service entries. # noqa: E501 Lists the track-service entries for a given page. # noqa: E501
:param offset: The number of items to skip before starting to collect the result set :param page: The number of items to skip before starting to collect the result set
:type offset: int :type page: int
:param limit: The numbers of items to return :param limit: The numbers of items to return per page
:type limit: int :type limit: int
:rtype: List[PlayLogEntry] :rtype: List[PlayLogEntry]
""" """
service = current_app.config['SERVICE'] service = current_app.config['SERVICE']
return service.list_tracks(offset, limit) return service.list_tracks(offset, limit)
\ No newline at end of file
\ No newline at end of file
...@@ -41,10 +41,10 @@ paths: ...@@ -41,10 +41,10 @@ paths:
- public - public
summary: List recent tracks in the play-log summary: List recent tracks in the play-log
description: | description: |
Lists the most recent track-service entries. Lists the track-service entries for a given page.
operationId: list_tracks operationId: list_tracks
parameters: parameters:
- name: offset - name: page
in: query in: query
description: The number of items to skip before starting to collect the result description: The number of items to skip before starting to collect the result
set set
...@@ -55,7 +55,7 @@ paths: ...@@ -55,7 +55,7 @@ paths:
type: integer type: integer
- name: limit - name: limit
in: query in: query
description: The numbers of items to return description: The numbers of items to return per page
required: false required: false
style: form style: form
explode: true explode: true
......
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
import datetime import datetime
from models import PlayLog, TrackServiceSchema from models import PlayLog, TrackSchema
class ApiService(): class ApiService():
...@@ -44,22 +45,25 @@ class ApiService(): ...@@ -44,22 +45,25 @@ class ApiService():
(PlayLogEntry) (PlayLogEntry)
""" """
track = PlayLog.select_current() track = PlayLog.select_current()
track_schema = TrackServiceSchema() track_schema = TrackSchema()
return track_schema.dump(track) return track_schema.dump(track)
def list_tracks(self, offset=None, limit=None): def list_tracks(self, page=None, size=None):
""" """
Lists the most recent track-service entries. Lists track-service entries with pagination.
Args: Args:
offset (Integer): The number of items to skip before starting to collect the result set page (Integer): The number of the page to return
limit (Integer): The numbers of items to return size (Integer): The numbers of items to return
Returns: Returns:
(List[PlayLogEntry]) (List[PlayLogEntry])
""" """
return "some list of tracks" if not size or size > 50 or size < 1: size = 20
tracklist = PlayLog.paginate(page, size)
tracklist_schema = TrackSchema(many=True)
return tracklist_schema.dump(tracklist)
def clock_info(self): def clock_info(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment