-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into file-manager
- Loading branch information
Showing
14 changed files
with
227 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import click | ||
|
||
from ckan.plugins import toolkit as tk | ||
|
||
import ckanext.files.config as files_conf | ||
|
||
__all__ = [ | ||
"files", | ||
] | ||
|
||
|
||
@click.group(short_help="ckanext-files CLI commands") | ||
def files(): | ||
pass | ||
|
||
|
||
@files.command() | ||
@click.option("--delete", "-d", is_flag=True, help="Delete orphaned datasets.") | ||
@click.argument("threshold", required=False, type=int) | ||
def remove_unused_files(delete: bool, threshold: int): | ||
"""Remove files that are not used for N days. The unused threshold is specified | ||
in a config""" | ||
threshold = ( | ||
threshold | ||
if threshold is not None | ||
else files_conf.get_unused_threshold() | ||
) | ||
|
||
files = tk.get_action("files_get_unused_files")( | ||
{"ignore_auth": True}, {"threshold": threshold} | ||
) | ||
|
||
if not files: | ||
return click.secho("No unused files", fg="blue") | ||
|
||
click.secho( | ||
f"Found unused files that were unused more than {threshold} days:", fg="green" | ||
) | ||
|
||
for file in files: | ||
click.echo(f"File path={file['path']}") | ||
|
||
if delete: | ||
tk.get_action("files_file_delete")( | ||
{"ignore_auth": True}, {"id": file["id"]} | ||
) | ||
click.echo(f"File was deleted", fg="red") | ||
|
||
if not delete: | ||
click.secho( | ||
"If you want to delete unused files, add `--delete` flag", | ||
fg="red", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ckan.plugins.toolkit as tk | ||
|
||
CONF_UNUSED_THRESHOLD = "ckanext.files.unused_threshold" | ||
|
||
|
||
def get_unused_threshold() -> int: | ||
return tk.config[CONF_UNUSED_THRESHOLD] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: 1 | ||
groups: | ||
- annotation: ckanext-admin_panel | ||
options: | ||
- key: ckanext.files.<KIND>.max_size | ||
type: dynamic | ||
description: File max size in MB for a specific file type | ||
default: 2 | ||
|
||
- key: ckanext.files.unused_threshold | ||
type: int | ||
description: Сonsider the file unused if there was no request for access within N days | ||
default: 180 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
ckanext/files/migration/files/versions/2c5f1f90888c_add_file_last_access_field.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Add file last_access field | ||
Revision ID: 2c5f1f90888c | ||
Revises: cc1a832108c5 | ||
Create Date: 2024-01-27 12:47:35.568291 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.sql import func | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "2c5f1f90888c" | ||
down_revision = "cc1a832108c5" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.add_column( | ||
"files_file", | ||
sa.Column( | ||
"last_access", | ||
sa.DateTime(), | ||
nullable=False, | ||
server_default=func.now(), | ||
), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column("files_file", "last_access") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import logging | ||
|
||
from flask import Blueprint | ||
from flask.views import MethodView | ||
|
||
import ckan.plugins.toolkit as tk | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
files = Blueprint("files", __name__) | ||
|
||
|
||
class FilesGetFileView(MethodView): | ||
"""This view is designed for serving files while also updating | ||
the 'last_access' field in the database for the corresponding file object. | ||
The `last_access` field is updated inside `files_file_show` action. | ||
""" | ||
|
||
def get(self, file_id: str): | ||
try: | ||
file_data = tk.get_action("files_file_show")( | ||
{ | ||
"user": tk.current_user.name, | ||
"auth_user_obj": tk.current_user, | ||
}, | ||
{"id": file_id}, | ||
) | ||
except (tk.ValidationError, OSError): | ||
return | ||
|
||
return tk.redirect_to( | ||
tk.h.url_for_static(file_data["path"], qualified=True) | ||
) | ||
|
||
|
||
files.add_url_rule( | ||
"/files/get_url/<file_id>", | ||
view_func=FilesGetFileView.as_view("get_file"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters