Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
goulinkh committed Dec 10, 2024
1 parent 5da0bae commit c8a5f4c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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. <code><b>a2f56da4</b>-some-image.png</code>), 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. `<code><b>`a2f56da4`</b>`-some-image.png`</code>`), the cache expiration time should be as long as possible to maximise performance.
8 changes: 7 additions & 1 deletion templates/_asset-list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<h5>Found {{ total_assets }} assets</h5>
<h5 id="assets_count"></h5>
<script>
// local format number
const formattedNumber = new Intl.NumberFormat('en-US').format({{ total_assets }});
document.getElementById('assets_count').innerText = `Found ${formattedNumber} assets`;
</script>

{% for asset in assets %}
{% include '_asset.html' %}
{% endfor %}
4 changes: 2 additions & 2 deletions templates/_pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</li>
{% if page > 3 %}<li class="p-pagination__item p-pagination__item--truncation u-hide--small"></li>{% 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 %}
<li class="p-pagination__item">
<a class="p-pagination__link {{ 'is-active' if page == p }}"
href="{{ add_query_param('page', p) }}">{{ p }}</a>
Expand All @@ -34,7 +34,7 @@
href="{{ add_query_param('page', total_pages) }}">{{ total_pages }}</a>
</li>
{% endif %}
{% if page <= total_pages %}
{% if page < total_pages %}
<li class="p-pagination__item">
<a class="p-pagination__link--next {{ 'is-active' if page == total_pages }}"
href="{{ add_query_param('page', page + 1) }}">
Expand Down
18 changes: 14 additions & 4 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@
</div>
<div class="p-form__group">
<div class="p-form__control u-clearfix">
<select name="type" onchange="this.form.submit()">
<option {% if not type %}selected{% endif %} value="">any</option>
{% for t in available_extensions %}
<option {% if t == type %}selected{% endif %} value="{{ t }}">{{ t }}</option>
<select name="tag" onchange="this.form.submit()">
<option {% if not tag %}selected{% endif %} value="">any</option>
{% for t in tags %}
<option {% if t == tag %}selected{% endif %} value="{{ t }}">{{ t }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="p-form__group">
<div class="p-form__control u-clearfix">
<label class="p-checkbox">
<input type="checkbox" name="include_deprecated" id="include_deprecated" class="p-checkbox__input" aria-labelledby="deprecatedLabel"
{% if include_deprecated %}checked{% endif %}
>
<span class="p-checkbox__label" id="deprecatedLabel">Include deprecated</span>
</label>
</div>
</div>
<div class="p-form__group">
<label for="order_by" style="margin-right: 10px;">Order by:</label>
<div class="p-form__control u-clearfix">
Expand Down
5 changes: 5 additions & 0 deletions webapp/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand All @@ -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,
)


Expand Down
24 changes: 18 additions & 6 deletions webapp/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -215,7 +228,6 @@ def order_by_fields():
return {
"Creation date": Asset.created,
"File name": Asset.file_path,
"Deprecated": Asset.deprecated,
}


Expand Down
10 changes: 8 additions & 2 deletions webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c8a5f4c

Please sign in to comment.