Skip to content

Commit

Permalink
feat: param to sort and place files list at end
Browse files Browse the repository at this point in the history
Reviewed-by: andriacap
  • Loading branch information
andriacap committed Feb 26, 2024
1 parent 5a918b1 commit 0484641
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
20 changes: 19 additions & 1 deletion atlas/atlasRoutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ def ficheEspece(cd_nom):

organisms = vmOrganismsRepository.getListOrganism(connection, cd_ref)

sort_medias_last = current_app.config["MEDIAS_TOOLS"]["SORT_LAST_MEDIAS"]
if sort_medias_last:
pattern_for_last_medias = current_app.config["MEDIAS_TOOLS"]["SORT_LAST_MEDIAS_PATTERN"]
# ReOrder articles based on pattern
matching_paths = []
non_matching_paths = []

# Separate paths based on whether they match the regex pattern
for item in articles:
if utils.regex_filter(item['path'],pattern_for_last_medias):
matching_paths.append(item)
else:
non_matching_paths.append(item)

# Concatenate the two lists to reorder them
final_list_articles = non_matching_paths + matching_paths
else:
final_list_articles = articles
connection.close()
db_session.close()

Expand All @@ -280,7 +298,7 @@ def ficheEspece(cd_nom):
firstPhoto=firstPhoto,
photoCarousel=photoCarousel,
videoAudio=videoAudio,
articles=articles,
articles=final_list_articles,
taxonDescription=taxonDescription,
observers=observers,
organisms=organisms,
Expand Down
18 changes: 18 additions & 0 deletions atlas/configuration/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
},
}

MEDIAS_TOOLS = {
'SORT_COL':'date_media',
'SORT_ORDER': 'DESC',
'SORT_LAST_MEDIAS':False,
'SORT_LAST_MEDIAS_PATTERN':r"xxxxx"
}


class SecretSchemaConf(Schema):
class Meta:
Expand Down Expand Up @@ -208,6 +215,8 @@ class Meta:
# (no need to restart the atlas service when updating templates)
# Defaults to False to have the best performance in production
TEMPLATES_AUTO_RELOAD = fields.Boolean(load_default=False)
MEDIAS_TOOLS = fields.Dict(load_default=MEDIAS_TOOLS)


@validates_schema
def validate_url_taxhub(self, data, **kwargs):
Expand All @@ -218,3 +227,12 @@ def validate_url_taxhub(self, data, **kwargs):
raise ValidationError(
{"Le champ TAXHUB_URL doit être rempli si REDIMENSIONNEMENT_IMAGE = True"}
)

@validates_schema
def validate_sort_dir(self, data, **kwargs):
sort_order = data['MEDIAS_TOOLS']['SORT_ORDER']
if sort_order not in ['ASC','DESC']:
raise ValidationError(
f"The specified sort order '{sort_order}' is not possible . You should use 'ASC' or 'DESC'."
)

8 changes: 7 additions & 1 deletion atlas/modeles/repositories/vmMedias.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,18 @@ def getVideo_and_audio(connection, cd_ref, id5, id6, id7, id8, id9):


def getLinks_and_articles(connection, cd_ref, id3, id4):
schema_name = 'atlas'
view_name = 'vm_medias'
sql = """
SELECT *
FROM atlas.vm_medias
WHERE id_type IN (:id3, :id4) AND cd_ref = :thiscdref
ORDER BY date_media DESC
"""
order_by_col = current_app.config["MEDIAS_TOOLS"]["SORT_COL"]
sort_order = current_app.config["MEDIAS_TOOLS"]["SORT_ORDER"]
utils.check_database_column_existence(connection,order_by_col,schema_name,view_name)
# Add ORDER BY clause
sql += f" ORDER BY {order_by_col} {sort_order}"
req = connection.execute(text(sql), thiscdref=cd_ref, id3=id3, id4=id4)
return [_format_media(r) for r in req]

Expand Down
20 changes: 19 additions & 1 deletion atlas/modeles/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding:utf-8 -*-

import unicodedata

from sqlalchemy.sql import text

def deleteAccent(string):
if string is None:
Expand All @@ -16,3 +16,21 @@ def findPath(row):
return row.chemin
else:
return row.url

def check_database_column_existence(connection, col_name,schema_name, view_name):
# with app.app_context():
sql = f"""
SELECT *
FROM {schema_name}.{view_name}
LIMIT 1
"""
# connection = db.engine.connect()
result = connection.execute(text(sql), schema_name=schema_name, view_name=view_name)
columns = result.keys()
cols = [col for col in columns ]
if col_name in cols :
return
else:
raise KeyError(
f"The specified sort column '{col_name}' does not exist in view '{schema_name}.{view_name}', default column used to sort medias is 'date_media'."
)
6 changes: 5 additions & 1 deletion atlas/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding:utf-8 -*-

import re
from sqlalchemy import MetaData
from sqlalchemy import create_engine
from sqlalchemy.pool import QueuePool
Expand Down Expand Up @@ -84,3 +84,7 @@ def as_dict(self, data, columns=None):
fprops = self.serialize_columns

return {item: _serializer(getattr(data, item)) for item, _serializer in fprops}


def regex_filter(value, pattern):
return re.search(pattern, value)

0 comments on commit 0484641

Please sign in to comment.