diff --git a/README.md b/README.md index 1de4be1..9c6a8f0 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,7 @@ You can also filter the assets by: - `tag`: Filter the assets by a specific tag - `q`: A query string to filter the assets by filename, e.g. `q=ubuntu` will return all the assets with `ubuntu` in their filename - `type`: The type of the asset, e.g. `type=png` will return all the assets with the `png` extension +- `include_deprecated`: Whether to include or not the deprecated assets. Default is `false` ##### Pagination @@ -227,4 +228,4 @@ As the server only uses a basic token for authentication, it is paramount that i ## Caching -The server is intended to be run in production behind a caching layer (e.g. [squid cache](http://www.squid-cache.org/)). And as the server stores assets by default with a unique hash corresponding to the file's contents (e.g. a2f56da4-some-image.png), the cache expiration time should be as long as possible to maximise performance. +The server is intended to be run in production behind a caching layer (e.g. [squid cache](http://www.squid-cache.org/)). And as the server stores assets by default with a unique hash corresponding to the file's contents (e.g. ``a2f56da4``-some-image.png``), the cache expiration time should be as long as possible to maximise performance. diff --git a/templates/_asset-list.html b/templates/_asset-list.html index ffff6d7..59abd46 100644 --- a/templates/_asset-list.html +++ b/templates/_asset-list.html @@ -1,4 +1,10 @@ -
Found {{ total_assets }} assets
+
+ + {% for asset in assets %} {% include '_asset.html' %} {% endfor %} diff --git a/templates/_pagination.html b/templates/_pagination.html index b9c0f72..362c783 100644 --- a/templates/_pagination.html +++ b/templates/_pagination.html @@ -18,7 +18,7 @@ {% if page > 3 %}
  • {% endif %} {% for p in range(page - 1, page + max_pagination_items) %} - {% if p > 1 and p <= total_pages %} + {% if p > 1 and p < total_pages %}
  • {{ p }} @@ -34,7 +34,7 @@ href="{{ add_query_param('page', total_pages) }}">{{ total_pages }}
  • {% endif %} - {% if page <= total_pages %} + {% if page < total_pages %}
  • diff --git a/templates/index.html b/templates/index.html index c23c4ed..83a1728 100644 --- a/templates/index.html +++ b/templates/index.html @@ -22,14 +22,24 @@
    - + + {% for t in tags %} + {% endfor %}
    +
    +
    + +
    +
    diff --git a/webapp/routes.py b/webapp/routes.py index bee8f6a..13bb95d 100644 --- a/webapp/routes.py +++ b/webapp/routes.py @@ -70,6 +70,9 @@ def home(): per_page = request.values.get("per_page", type=int) order_by = request.values.get("order_by", type=str) order_dir = request.values.get("order_dir", type=str, default="desc") + include_deprecated = request.values.get( + "include_deprecated", type=bool, default=False + ) if not per_page or per_page < 1 or per_page > 100: per_page = 20 @@ -86,6 +89,7 @@ def home(): per_page=per_page, order_by=asset_service.order_by_fields()[order_by], desc_order=order_dir == "desc", + include_deprecated=include_deprecated, ) else: assets = [] @@ -105,6 +109,7 @@ def home(): order_by=order_by, order_dir=order_dir, order_by_fields=asset_service.order_by_fields(), + include_deprecated=include_deprecated, ) diff --git a/webapp/services.py b/webapp/services.py index 2f62fc0..86f37c8 100644 --- a/webapp/services.py +++ b/webapp/services.py @@ -5,6 +5,7 @@ from typing import Tuple # Packages +from sqlalchemy import func from sqlalchemy.sql.expression import or_ from sqlalchemy.sql.sqltypes import Text from wand.image import Image @@ -29,6 +30,7 @@ def find_assets( per_page=10, order_by=Asset.created, desc_order=True, + include_deprecated=False, ) -> Tuple[list, int]: """ Find assets that matches the given criterions @@ -47,14 +49,25 @@ def find_assets( if tag: tag_condition = Asset.tags.any(Tag.name == tag) conditions.append(tag_condition) - assets = ( + + if not include_deprecated: + conditions.append(Asset.deprecated.is_(False)) + + if order_by == Asset.file_path: + # Example: "86293d6f-FortyCloud.png" -> "FortyCloud.png" + order_col = func.split_part(Asset.file_path, "-", 2) + else: + order_col = order_by + + assets_query = ( db_session.query(Asset) .filter(*conditions) - .order_by(order_by.desc() if desc_order else order_by) - .offset((max(page, 1) - 1) * max(per_page, 10)) - .limit(per_page) - .all() + .order_by(order_col.desc() if desc_order else order_col) + .offset((page - 1) * per_page) ) + + assets = assets_query.limit(per_page).all() + total = db_session.query(Asset).filter(*conditions).count() return assets, total @@ -215,7 +228,6 @@ def order_by_fields(): return { "Creation date": Asset.created, "File name": Asset.file_path, - "Deprecated": Asset.deprecated, } diff --git a/webapp/views.py b/webapp/views.py index 6eff73c..ac5efc8 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -162,14 +162,20 @@ def get_assets(): file_type = request.values.get("type", "") page = request.values.get("page", type=int) per_page = request.values.get("per_page", type=int) - + include_deprecated = ( + strtobool(request.values.get("include_deprecated", "false")) == 1 + ) page = 1 if not page or page < 1 else page per_page = ( 20 if not per_page or per_page < 1 or per_page > 100 else per_page ) assets, total = asset_service.find_assets( - query=query, file_type=file_type, page=page, per_page=per_page + query=query, + file_type=file_type, + page=page, + per_page=per_page, + include_deprecated=include_deprecated, ) return jsonify(