Skip to content

Commit b4d281c

Browse files
authored
fixes #338 (#341)
* fixes #338 * fixed test and linting
1 parent 9117f0f commit b4d281c

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

app/api/routes/resource_retrieval.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from dateutil import parser
44
from flask import redirect, request
5-
from sqlalchemy import func, or_
5+
from sqlalchemy import func, or_, text
66

77
from app import utils as utils
88
from app.api import bp
@@ -86,6 +86,17 @@ def get_resources():
8686
paidAsBool = paid.lower() == 'true'
8787
q = q.filter(Resource.paid == paidAsBool)
8888

89+
# Order by "getting started" category
90+
if not languages and not category and paid is None:
91+
show_first = Category.query.filter(Category.name == "Getting Started").first()
92+
clause = (
93+
f" CASE resource.category_id"
94+
f" WHEN {show_first.id} THEN 1"
95+
f" ELSE 2"
96+
f" END, id"
97+
)
98+
q = q.order_by(text(clause))
99+
89100
try:
90101
paginated_resources = resource_paginator.paginated_data(q)
91102
if not paginated_resources:

tests/unit/test_routes/test_utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ def test_paginator(module_client, module_db):
4646
response = client.get('api/v1/resources?page_size=100')
4747
assert (len(response.json['resources']) == 100)
4848

49-
# Test pages different and sequential
49+
# Test first category is 'Getting Started'
5050
first_page_resource = response.json['resources'][0]
51-
assert (first_page_resource.get('id') == 1)
51+
assert (first_page_resource.get('category') == 'Getting Started')
52+
53+
# Test pages are different
5254
response = client.get('api/v1/resources?page_size=100&page=2')
5355
second_page_resource = response.json['resources'][0]
54-
assert (second_page_resource.get('id') == 101)
56+
assert (second_page_resource.get('id') != first_page_resource.get('id'))
5557
response = client.get('api/v1/resources?page_size=100&page=3')
5658
third_page_resource = response.json['resources'][0]
57-
assert (third_page_resource.get('id') == 201)
59+
assert (second_page_resource.get('id') != third_page_resource.get('id'))
60+
assert (third_page_resource.get('id') != first_page_resource.get('id'))
5861

5962
# Test bigger than max page size
6063
too_long = PaginatorConfig.max_page_size + 1

0 commit comments

Comments
 (0)