@ -11,19 +11,12 @@ from tildes.enums import TopicSortOption
from tildes.schemas.fields import Enum , ID36 , Ltree , ShortTimePeriod
from tildes.schemas.fields import Enum , ID36 , Ltree , ShortTimePeriod
class TopicListingSchema ( Schema ) :
""" Marshmallow schema to validate arguments for a topic listing page. """
DEFAULT_TOPICS_PER_PAGE = 50
class PaginatedListingSchema ( Schema ) :
""" Marshmallow schema to validate arguments for a paginated listing page. """
order = Enum ( TopicSortOption )
period = ShortTimePeriod ( allow_none = True )
after = ID36 ( )
after = ID36 ( )
before = ID36 ( )
before = ID36 ( )
per_page = Integer ( validate = Range ( min = 1 , max = 100 ) , missing = DEFAULT_TOPICS_PER_PAGE )
rank_start = Integer ( load_from = " n " , validate = Range ( min = 1 ) , missing = None )
tag = Ltree ( missing = None )
unfiltered = Boolean ( missing = False )
per_page = Integer ( validate = Range ( min = 1 , max = 100 ) , missing = 50 )
@validates_schema
@validates_schema
def either_after_or_before ( self , data : dict ) - > None :
def either_after_or_before ( self , data : dict ) - > None :
@ -31,6 +24,21 @@ class TopicListingSchema(Schema):
if data . get ( " after " ) and data . get ( " before " ) :
if data . get ( " after " ) and data . get ( " before " ) :
raise ValidationError ( " Can ' t specify both after and before. " )
raise ValidationError ( " Can ' t specify both after and before. " )
class Meta :
""" Always use strict checking so error handlers are invoked. """
strict = True
class TopicListingSchema ( PaginatedListingSchema ) :
""" Marshmallow schema to validate arguments for a topic listing page. """
period = ShortTimePeriod ( allow_none = True )
order = Enum ( TopicSortOption )
tag = Ltree ( missing = None )
unfiltered = Boolean ( missing = False )
rank_start = Integer ( load_from = " n " , validate = Range ( min = 1 ) , missing = None )
@pre_load
@pre_load
def reset_rank_start_on_first_page ( self , data : dict ) - > dict :
def reset_rank_start_on_first_page ( self , data : dict ) - > dict :
""" Reset rank_start to 1 if this is a first page (no before/after). """
""" Reset rank_start to 1 if this is a first page (no before/after). """
@ -38,8 +46,3 @@ class TopicListingSchema(Schema):
data [ " rank_start " ] = 1
data [ " rank_start " ] = 1
return data
return data
class Meta :
""" Always use strict checking so error handlers are invoked. """
strict = True