Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/api/routes/resource_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from dateutil import parser
from flask import redirect, request
from sqlalchemy import func, or_
from sqlalchemy import func, or_, text

from app import utils as utils
from app.api import bp
Expand Down Expand Up @@ -86,6 +86,17 @@ def get_resources():
paidAsBool = paid.lower() == 'true'
q = q.filter(Resource.paid == paidAsBool)

# Order by "getting started" category
if not languages and not category and paid is None:
show_first = Category.query.filter(Category.name == "Getting Started").first()
clause = (
f" CASE resource.category_id"
f" WHEN {show_first.id} THEN 1"
f" ELSE 2"
f" END, id"
)
q = q.order_by(text(clause))

try:
paginated_resources = resource_paginator.paginated_data(q)
if not paginated_resources:
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/test_routes/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ def test_paginator(module_client, module_db):
response = client.get('api/v1/resources?page_size=100')
assert (len(response.json['resources']) == 100)

# Test pages different and sequential
# Test first category is 'Getting Started'
first_page_resource = response.json['resources'][0]
assert (first_page_resource.get('id') == 1)
assert (first_page_resource.get('category') == 'Getting Started')

# Test pages are different
response = client.get('api/v1/resources?page_size=100&page=2')
second_page_resource = response.json['resources'][0]
assert (second_page_resource.get('id') == 101)
assert (second_page_resource.get('id') != first_page_resource.get('id'))
response = client.get('api/v1/resources?page_size=100&page=3')
third_page_resource = response.json['resources'][0]
assert (third_page_resource.get('id') == 201)
assert (second_page_resource.get('id') != third_page_resource.get('id'))
assert (third_page_resource.get('id') != first_page_resource.get('id'))

# Test bigger than max page size
too_long = PaginatorConfig.max_page_size + 1
Expand Down