Skip to content

Commit

Permalink
v2-Beta12 release
Browse files Browse the repository at this point in the history
  • Loading branch information
kid1194 committed May 3, 2023
1 parent 534c2cb commit 0c2460a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 27 deletions.
51 changes: 40 additions & 11 deletions frappe_better_attach_control/api/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import frappe

from .common import parse_json_if_valid
from .common import parse_json_if_valid, send_console_log


_FILE_DOCTYPE_ = "File"
Expand All @@ -18,17 +18,46 @@ def remove_files(files):
files = parse_json_if_valid(files)

if not files or not isinstance(files, list):
send_console_log({
"message": "Invalid files list",
"data": files
})
return 0

if (names := frappe.get_all(
_FILE_DOCTYPE_,
fields=["name"],
filters={"file_url": ["in", files]},
pluck="name"
)):
for name in names:
frappe.delete_doc(_FILE_DOCTYPE_, name)
file_urls = []
file_names = []
for file in files:
if file.startswith(("files/", "private/files/")):
file = "/" + file

if file.startswith(("/files/", "/private/files/")):
file_urls.append(file)
else:
file_names.append(file)

return 1
if file_urls or file_names:
or_filters = None
if file_urls:
filters = {"file_url": ["in", file_urls]}
if file_names:
or_filters = {"file_name": ["in", file_names]}
else:
filters = {"file_name": ["in", file_names]}

if (names := frappe.get_all(
_FILE_DOCTYPE_,
fields=["name"],
filters=filters,
or_filters=or_filters,
pluck="name"
)):
for name in names:
frappe.delete_doc(_FILE_DOCTYPE_, name)

return 1

return 0
send_console_log({
"message": "Files not found",
"data": files
})
return 2
10 changes: 9 additions & 1 deletion frappe_better_attach_control/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ def parse_json_if_valid(data, default=None):
try:
return json.loads(data)
except Exception:
return default
return default


def send_console_log(data):
frappe.publish_realtime(
event="better_attach_console",
message=data,
after_commit=True
)
24 changes: 19 additions & 5 deletions frappe_better_attach_control/public/js/controls/attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
this._allow_remove = true;
this._display_ready = false;
this._unprocessed_files = [];
frappe.realtime.on('better_attach_console', function(ret) {
console.log(ret);
});
}
_update_options() {
if (
Expand Down Expand Up @@ -585,7 +588,8 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
_add_list_file(file, idx) {
// Check if allowed multiple files or not
if (!this._allow_multiple || !this._$list) return;
let meta = '';
let meta = '',
rem = !this._allow_remove ? ' ba-hidden' : '';
if (file.size && file.size_str) {
meta = '<div class="ba-meta">' + file.size_str + '</div>';
}
Expand All @@ -602,7 +606,7 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
+ '</div>'
+ '</div>'
+ '<div class="col-auto ba-actions">'
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0">'
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0' + rem + '">'
+ '<span class="fa fa-times fa-fw"></span>'
+ '</button>'
+ '</div>'
Expand All @@ -611,6 +615,7 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
));
}
_remove_files(data, callback, error) {
if (!isArray(data)) data = [data];
request('remove_files', {files: data}, callback, error);
}
_remove_file_by_idx(idx) {
Expand All @@ -628,13 +633,12 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
this._remove_file_by_url(url);
}
_remove_file_by_url(url) {
if (!this.frm) {
this._remove_files([url], function(ret) {
if (!this.frm || !this.frm.attachments) {
this._remove_files(url, function(ret) {
if (!cint(ret)) error('Unable to remove the uploaded attachment ({0}).', [url]);
});
return;
}
if (!this.frm.attachments) return;
var me = this;
this.frm.attachments.remove_attachment_by_filename(
url,
Expand All @@ -661,6 +665,16 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
+ '</div>'
).appendTo(this.input_area);
this._$list_group = this._$list.find('ul.list-group');
this._$list_group.click('.ba-remove', function() {
let $el = $(this);
if (!$el.hasClass('ba-remove')) return;
let $parent = $el.parents('.ba-attachment');
if (!$parent.length) return;
let idx = $parent.attr('data-file-idx');
if (!idx || !/[0-9]+/.test('' + idx)) return;
idx = cint(idx);
if (idx >= 0) _remove_file_by_idx(idx);
});
}
_destroy_list() {
if (this._$list) {
Expand Down
24 changes: 19 additions & 5 deletions frappe_better_attach_control/public/js/controls/v12/attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
this._allow_remove = true;
this._display_ready = false;
this._unprocessed_files = [];
frappe.realtime.on('better_attach_console', function(ret) {
console.log(ret);
});
},
_update_options: function() {
if (
Expand Down Expand Up @@ -539,7 +542,8 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
_add_list_file: function(file, idx) {
// Check if allowed multiple files or not
if (!this._allow_multiple || !this._$list) return;
var meta = '';
var meta = '',
rem = !this._allow_remove ? ' ba-hidden' : '';
if (file.size && file.size_str) {
meta = '<div class="ba-meta">' + file.size_str + '</div>';
}
Expand All @@ -556,7 +560,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
+ '</div>'
+ '</div>'
+ '<div class="col-auto ba-actions">'
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0">'
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0' + rem + '">'
+ '<span class="fa fa-times fa-fw"></span>'
+ '</button>'
+ '</div>'
Expand All @@ -565,6 +569,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
));
},
_remove_files: function(data, callback, error) {
if (!isArray(data)) data = [data];
request('remove_files', {files: data}, callback, error);
},
_remove_file_by_idx: function(idx) {
Expand All @@ -582,13 +587,12 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
this._remove_file_by_url(url);
},
_remove_file_by_url: function(url) {
if (!this.frm) {
this._remove_files([url], function(ret) {
if (!this.frm || !this.frm.attachments) {
this._remove_files(url, function(ret) {
if (!cint(ret)) error('Unable to remove the uploaded attachment ({0}).', [url]);
});
return;
}
if (!this.frm.attachments) return;
var me = this;
this.frm.attachments.remove_attachment_by_filename(
url,
Expand All @@ -613,6 +617,16 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
+ '</div>'
).appendTo(this.input_area);
this._$list_group = this._$list.find('ul.list-group');
this._$list_group.click('.ba-remove', function() {
var $el = $(this);
if (!$el.hasClass('ba-remove')) return;
var $parent = $el.parents('.ba-attachment');
if (!$parent.length) return;
var idx = $parent.attr('data-file-idx');
if (!idx || !/[0-9]+/.test('' + idx)) return;
idx = cint(idx);
if (idx >= 0) _remove_file_by_idx(idx);
});
},
_destroy_list: function() {
if (this._$list) {
Expand Down
24 changes: 19 additions & 5 deletions frappe_better_attach_control/public/js/controls/v13/attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
this._allow_remove = true;
this._display_ready = false;
this._unprocessed_files = [];
frappe.realtime.on('better_attach_console', function(ret) {
console.log(ret);
});
},
_update_options: function() {
if (
Expand Down Expand Up @@ -565,7 +568,8 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
_add_list_file: function(file, idx) {
// Check if allowed multiple files or not
if (!this._allow_multiple || !this._$list) return;
var meta = '';
var meta = '',
rem = !this._allow_remove ? ' ba-hidden' : '';
if (file.size && file.size_str) {
meta = '<div class="ba-meta">' + file.size_str + '</div>';
}
Expand All @@ -582,7 +586,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
+ '</div>'
+ '</div>'
+ '<div class="col-auto ba-actions">'
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0">'
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0' + rem + '">'
+ '<span class="fa fa-times fa-fw"></span>'
+ '</button>'
+ '</div>'
Expand All @@ -591,6 +595,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
));
},
_remove_files: function(data, callback, error) {
if (!isArray(data)) data = [data];
request('remove_files', {files: data}, callback, error);
},
_remove_file_by_idx: function(idx) {
Expand All @@ -608,13 +613,12 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
this._remove_file_by_url(url);
},
_remove_file_by_url: function(url) {
if (!this.frm) {
this._remove_files([url], function(ret) {
if (!this.frm || !this.frm.attachments) {
this._remove_files(url, function(ret) {
if (!cint(ret)) error('Unable to remove the uploaded attachment ({0}).', [url]);
});
return;
}
if (!this.frm.attachments) return;
var me = this;
this.frm.attachments.remove_attachment_by_filename(
url,
Expand All @@ -639,6 +643,16 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
+ '</div>'
).appendTo(this.input_area);
this._$list_group = this._$list.find('ul.list-group');
this._$list_group.click('.ba-remove', function() {
var $el = $(this);
if (!$el.hasClass('ba-remove')) return;
var $parent = $el.parents('.ba-attachment');
if (!$parent.length) return;
var idx = $parent.attr('data-file-idx');
if (!idx || !/[0-9]+/.test('' + idx)) return;
idx = cint(idx);
if (idx >= 0) _remove_file_by_idx(idx);
});
},
_destroy_list: function() {
if (this._$list) {
Expand Down

0 comments on commit 0c2460a

Please sign in to comment.