Skip to content

Commit

Permalink
chore: update files manager
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jun 27, 2024
1 parent ecb5e01 commit 2018b09
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 104 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ include README.rst
include LICENSE
include requirements.txt
recursive-include ckanext/files *.html *.json *.js *.less *.css *.mo *.yml *.config *.yaml
recursive-include ckanext/file_manager *.html *.json *.js *.less *.css *.mo *.yml *.config *.yaml
recursive-include ckanext/files_manager *.html *.json *.js *.less *.css *.mo *.yml *.config *.yaml
recursive-include ckanext/files/migration *.ini *.py *.mako
32 changes: 0 additions & 32 deletions ckanext/file_manager/templates/file_manager/record.html

This file was deleted.

4 changes: 3 additions & 1 deletion ckanext/files/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def _streaming_file(
) -> Response | None:
if storage.supports(shared.Capability.STREAM):
resp = streaming_response(storage.stream(data), data.content_type)
if not utils.is_supported_type(item.content_type, shared.config.inline_types()):
if utils.is_supported_type(item.content_type, shared.config.inline_types()):
resp.headers["content-disposition"] = f"inline; filename={item.name}"
else:
resp.headers["content-disposition"] = f"attachment; filename={item.name}"

item.touch()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ckan.module("fm-htmx", function ($) {
},

_onHTMXconfirm: function (evt) {
if (evt.detail.path.includes("/file_manager/delete")) {
if (evt.detail.path.includes("/files_manager/delete")) {
evt.preventDefault();

swal({
Expand All @@ -29,7 +29,7 @@ ckan.module("fm-htmx", function ($) {
},

_onAfterRequest: function (evt) {
if (evt.detail.pathInfo.requestPath.includes("/file_manager/delete/")) {
if (evt.detail.pathInfo.requestPath.includes("/files_manager/delete/")) {
htmx.trigger(`#${this.options.formId}`, "change");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
file-manager-js:
files-manager-js:
filter: rjsmin
output: ckanext-admin_panel/%(version)s-file-manager.js
output: ckanext-admin_panel/%(version)s-files-manager.js
contents:
- js/fm-htmx.js
extra:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,25 @@ def file_row_dictizer(serializer: ApHtmxTableSerializer[Any], row: File):
return data


class FileManagerCollection(ApCollection):
class FilesManagerCollection(ApCollection):
SerializerFactory = ApHtmxTableSerializer.with_attributes(
record_template="file_manager/record.html",
record_template="files_manager/record.html",
row_dictizer=file_row_dictizer,
)

ColumnsFactory = ApColumns.with_attributes(
names=[
"bulk-action",
"name",
"path",
"kind",
"uploaded_at",
"last_access",
"extras",
"location",
"size",
"content_type",
"storage",
"ctime",
"storage_data",
"row_actions",
],
sortable={"name", "kind", "uploaded_at", "last_access"},
sortable={"name", "storage", "content_type", "size", "ctime"},
searchable={"name"},
labels={
"bulk-action": tk.literal(
Expand All @@ -57,18 +58,16 @@ class FileManagerCollection(ApCollection):
),
),
"name": "Name",
"path": "Path",
"kind": "Type",
"uploaded_at": "Uploaded At",
"last_access": "Last Access",
"extras": "Extras",
"location": "Location",
"storage": "Storage",
"ctime": "Uploaded At",
"storage_data": "Extras",
"row_actions": "Actions",
},
width={"name": "20%", "path": "20%"},
width={"name": "20%", "location": "20%"},
serializers={
"uploaded_at": [("date", {})],
"extras": [("json_display", {})],
"last_access": [("day_passed", {})],
# "ctime": [("date", {})],
"storage_data": [("json_display", {})],
},
)

Expand All @@ -91,19 +90,6 @@ class FileManagerCollection(ApCollection):
],
},
),
RowAction(
name="edit",
type="row_action",
options={
"endpoint": "",
"label": "",
"icon": "fa fa-pencil",
"params": {
"data-module-path": "$id",
"view": "edit",
},
},
),
RowAction(
name="view",
type="row_action",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from ckan.types import SignalMapping

from ckanext.collection.interfaces import CollectionFactory, ICollection
from ckanext.file_manager.collection import FileManagerCollection
from ckanext.files_manager.collection import FilesManagerCollection


@tk.blanket.blueprints
class FileManagerPlugin(p.SingletonPlugin):
class FilesManagerPlugin(p.SingletonPlugin):
p.implements(p.IConfigurer)
p.implements(p.ISignal)
p.implements(ICollection, inherit=True)
Expand All @@ -22,7 +22,7 @@ class FileManagerPlugin(p.SingletonPlugin):
def update_config(self, config_: CKANConfig):
tk.add_template_directory(config_, "templates")
tk.add_public_directory(config_, "public")
tk.add_resource("assets", "file_manager")
tk.add_resource("assets", "files_manager")

# ISignal

Expand All @@ -36,7 +36,7 @@ def get_signal_subscriptions(self) -> SignalMapping:
# ICollection

def get_collection_factories(self) -> dict[str, CollectionFactory]:
return {"file-manager": FileManagerCollection}
return {"files-manager": FilesManagerCollection}


def collect_config_sections_subs(sender: None) -> dict[str, Any]:
Expand All @@ -45,7 +45,7 @@ def collect_config_sections_subs(sender: None) -> dict[str, Any]:
"configs": [
{
"name": "File manager",
"blueprint": "file_manager.list",
"blueprint": "files_manager.list",
"info": "Manage uploaded files",
},
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{% extends 'admin_panel/base.html' %}

{% block ap_main_class %} file-manager-list {% endblock %}
{% block ap_main_class %} files-manager-list {% endblock %}

{% block breadcrumb_content %}
<li>{% link_for _("File manager"), request.endpoint %}</li>
{% endblock breadcrumb_content %}

{% block ap_content %}
<div class="file-manager--manage mb-2" data-module="fm-htmx" data-module-form-id="{{ collection.serializer.form_id }}">
<div class="files-manager--manage mb-2" data-module="fm-htmx" data-module-form-id="{{ collection.serializer.form_id }}">
<button
type="button"
class="btn btn-success"
Expand All @@ -17,8 +17,8 @@
</button>
</div>

<form action="{{ h.url_for('file_manager.upload') }}" method="POST" id="ap-cron-add" enctype="multipart/form-data">
{% snippet 'file_manager/upload_file_modal.html' %}
<form action="{{ h.url_for('files_manager.upload') }}" method="POST" id="ap-cron-add" enctype="multipart/form-data">
{% snippet 'files_manager/upload_file_modal.html' %}
</form>

<div class="row g-3">
Expand All @@ -35,6 +35,6 @@


{% block scripts %}
{% asset 'file_manager/file-manager-js' %}
{% asset 'files_manager/files-manager-js' %}
{{ super() }}
{% endblock %}
36 changes: 36 additions & 0 deletions ckanext/files_manager/templates/files_manager/record.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends "collection/serialize/ap_htmx_table/record.html" %}

{% block value %}
{% if column == "row_actions" %}
{% set info = h.files_link_details(data.id) %}
{% set url = info and info.href %}

{% if url %}
<a
data-module="ap-tooltip ap-copy-to-clipboard"
data-module-content="{{ url }}"
title="{{ _('Copy file URL')}}"
class="btn btn-black">
<i class="fas fa-copy"></i>
</a>

<a
href="{{ url }}"
download
data-module="ap-tooltip"
title="{{ _('Download file')}}"
class="btn btn-primary">
<i class="fas fa-download"></i>
</a>
{% endif %}
<a
data-module="ap-tooltip" title="{{ _('Remove a file') }}"
class="btn btn-danger"
href="{{ h.url_for('files_manager.delete', file_id=data.id) }}"
hx-swap="none" hx-trigger="click" hx-post="{{ h.url_for('files_manager.delete', file_id=data.id) }}">
<i class="fa fa-trash-alt"></i>
</a>
{% else %}
{{ super() }}
{% endif %}
{% endblock value %}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h5 class="modal-title" id="upload-file-label">{{ _("Upload file")}}</h5>
</div> <!-- .modal-header -->

<div class="modal-body">
{% snippet 'file_manager/upload_file_modal_form.html', data={} %}
{% snippet 'files_manager/upload_file_modal_form.html', data={} %}
</div> <!-- .modal-body -->

<div class="modal-footer">
Expand Down
49 changes: 26 additions & 23 deletions ckanext/file_manager/views.py → ckanext/files_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
from ckanext.collection.shared import get_collection

log = logging.getLogger(__name__)
file_manager = Blueprint(
"file_manager",
files_manager = Blueprint(
"files_manager",
__name__,
url_prefix="/admin-panel/file_manager",
url_prefix="/admin-panel/files_manager",
)
file_manager.before_request(ap_before_request)
files_manager.before_request(ap_before_request)


class FileManagerView(MethodView):
class FilesManagerView(MethodView):
def get(self) -> str | Response:
return tk.render(
"file_manager/list.html",
"files_manager/list.html",
extra_vars={
"collection": get_collection(
"file-manager",
"files-manager",
parse_params(tk.request.args),
),
},
Expand All @@ -41,15 +41,15 @@ def post(self) -> Response:

if not action_func:
tk.h.flash_error(tk._("The bulk action is not implemented"))
return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")

for file_id in file_ids:
try:
action_func(file_id)
except tk.ValidationError as e:
tk.h.flash_error(str(e))

return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")

def _get_bulk_action(self, value: str) -> Callable[[str], None] | None:
return {
Expand All @@ -63,13 +63,13 @@ def _remove_file(self, file_id: str):
)


class FileManagerUploadView(MethodView):
class FilesManagerUploadView(MethodView):
def post(self):
file = tk.request.files.get("upload")

if not file:
tk.h.flash_error(tk._("Missing file object"))
return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")

try:
tk.get_action("files_file_create")(
Expand All @@ -81,35 +81,38 @@ def post(self):
)
except tk.ValidationError as e:
tk.h.flash_error(str(e.error_summary))
return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")
except OSError as e:
tk.h.flash_error(str(e))
return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")

tk.h.flash_success(tk._("File has been uploaded!"))
return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")


class FileManagerDeleteView(MethodView):
class FilesManagerDeleteView(MethodView):
def post(self, file_id: str):
try:
tk.get_action("files_file_delete")({"ignore_auth": True}, {"id": file_id})
except tk.ValidationError as e:
tk.h.flash_error(str(e.error_summary))
return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")
except OSError as e:
tk.h.flash_error(str(e))
return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")

tk.h.flash_success(tk._("File has been deleted!"))
return tk.redirect_to("file_manager.list")
return tk.redirect_to("files_manager.list")


file_manager.add_url_rule("/manage", view_func=FileManagerView.as_view("list"))
file_manager.add_url_rule("/upload", view_func=FileManagerUploadView.as_view("upload"))
file_manager.add_url_rule(
files_manager.add_url_rule("/manage", view_func=FilesManagerView.as_view("list"))
files_manager.add_url_rule(
"/upload",
view_func=FilesManagerUploadView.as_view("upload"),
)
files_manager.add_url_rule(
"/delete/<file_id>",
view_func=FileManagerDeleteView.as_view("delete"),
view_func=FilesManagerDeleteView.as_view("delete"),
)

blueprints = [file_manager]
blueprints = [files_manager]
Loading

0 comments on commit 2018b09

Please sign in to comment.