2022-09-15 13:14:55 +02:00
|
|
|
import time
|
2021-02-20 21:37:31 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import humanize
|
2023-10-02 18:57:53 +02:00
|
|
|
from sqlalchemy.sql import text
|
2021-02-20 21:37:31 +01:00
|
|
|
|
2022-09-15 13:14:55 +02:00
|
|
|
from fittrackee import db
|
|
|
|
|
2021-02-20 21:37:31 +01:00
|
|
|
|
|
|
|
def get_readable_duration(duration: int, locale: Optional[str] = None) -> str:
|
|
|
|
"""
|
|
|
|
Return readable and localized duration from duration in seconds
|
|
|
|
"""
|
|
|
|
if locale is None:
|
|
|
|
locale = 'en'
|
|
|
|
if locale != 'en':
|
|
|
|
try:
|
|
|
|
_t = humanize.i18n.activate(locale) # noqa
|
|
|
|
except FileNotFoundError:
|
|
|
|
locale = 'en'
|
|
|
|
readable_duration = humanize.naturaldelta(timedelta(seconds=duration))
|
|
|
|
if locale != 'en':
|
|
|
|
humanize.i18n.deactivate()
|
|
|
|
return readable_duration
|
2022-09-15 13:14:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
def clean(sql: str, days: int) -> int:
|
|
|
|
limit = int(time.time()) - (days * 86400)
|
2023-10-02 18:57:53 +02:00
|
|
|
result = db.session.execute(text(sql), {'limit': limit})
|
|
|
|
# DELETE statement returns CursorResult with the rowcount attribute
|
|
|
|
return result.rowcount # type: ignore
|