Skip to content

Commit 0c2460a

Browse files
committed
v2-Beta12 release
1 parent 534c2cb commit 0c2460a

File tree

5 files changed

+106
-27
lines changed

5 files changed

+106
-27
lines changed

frappe_better_attach_control/api/attachment.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import frappe
88

9-
from .common import parse_json_if_valid
9+
from .common import parse_json_if_valid, send_console_log
1010

1111

1212
_FILE_DOCTYPE_ = "File"
@@ -18,17 +18,46 @@ def remove_files(files):
1818
files = parse_json_if_valid(files)
1919

2020
if not files or not isinstance(files, list):
21+
send_console_log({
22+
"message": "Invalid files list",
23+
"data": files
24+
})
2125
return 0
2226

23-
if (names := frappe.get_all(
24-
_FILE_DOCTYPE_,
25-
fields=["name"],
26-
filters={"file_url": ["in", files]},
27-
pluck="name"
28-
)):
29-
for name in names:
30-
frappe.delete_doc(_FILE_DOCTYPE_, name)
27+
file_urls = []
28+
file_names = []
29+
for file in files:
30+
if file.startswith(("files/", "private/files/")):
31+
file = "/" + file
32+
33+
if file.startswith(("/files/", "/private/files/")):
34+
file_urls.append(file)
35+
else:
36+
file_names.append(file)
3137

32-
return 1
38+
if file_urls or file_names:
39+
or_filters = None
40+
if file_urls:
41+
filters = {"file_url": ["in", file_urls]}
42+
if file_names:
43+
or_filters = {"file_name": ["in", file_names]}
44+
else:
45+
filters = {"file_name": ["in", file_names]}
46+
47+
if (names := frappe.get_all(
48+
_FILE_DOCTYPE_,
49+
fields=["name"],
50+
filters=filters,
51+
or_filters=or_filters,
52+
pluck="name"
53+
)):
54+
for name in names:
55+
frappe.delete_doc(_FILE_DOCTYPE_, name)
56+
57+
return 1
3358

34-
return 0
59+
send_console_log({
60+
"message": "Files not found",
61+
"data": files
62+
})
63+
return 2

frappe_better_attach_control/api/common.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,12 @@ def parse_json_if_valid(data, default=None):
7171
try:
7272
return json.loads(data)
7373
except Exception:
74-
return default
74+
return default
75+
76+
77+
def send_console_log(data):
78+
frappe.publish_realtime(
79+
event="better_attach_console",
80+
message=data,
81+
after_commit=True
82+
)

frappe_better_attach_control/public/js/controls/attach.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
265265
this._allow_remove = true;
266266
this._display_ready = false;
267267
this._unprocessed_files = [];
268+
frappe.realtime.on('better_attach_console', function(ret) {
269+
console.log(ret);
270+
});
268271
}
269272
_update_options() {
270273
if (
@@ -585,7 +588,8 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
585588
_add_list_file(file, idx) {
586589
// Check if allowed multiple files or not
587590
if (!this._allow_multiple || !this._$list) return;
588-
let meta = '';
591+
let meta = '',
592+
rem = !this._allow_remove ? ' ba-hidden' : '';
589593
if (file.size && file.size_str) {
590594
meta = '<div class="ba-meta">' + file.size_str + '</div>';
591595
}
@@ -602,7 +606,7 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
602606
+ '</div>'
603607
+ '</div>'
604608
+ '<div class="col-auto ba-actions">'
605-
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0">'
609+
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0' + rem + '">'
606610
+ '<span class="fa fa-times fa-fw"></span>'
607611
+ '</button>'
608612
+ '</div>'
@@ -611,6 +615,7 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
611615
));
612616
}
613617
_remove_files(data, callback, error) {
618+
if (!isArray(data)) data = [data];
614619
request('remove_files', {files: data}, callback, error);
615620
}
616621
_remove_file_by_idx(idx) {
@@ -628,13 +633,12 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
628633
this._remove_file_by_url(url);
629634
}
630635
_remove_file_by_url(url) {
631-
if (!this.frm) {
632-
this._remove_files([url], function(ret) {
636+
if (!this.frm || !this.frm.attachments) {
637+
this._remove_files(url, function(ret) {
633638
if (!cint(ret)) error('Unable to remove the uploaded attachment ({0}).', [url]);
634639
});
635640
return;
636641
}
637-
if (!this.frm.attachments) return;
638642
var me = this;
639643
this.frm.attachments.remove_attachment_by_filename(
640644
url,
@@ -661,6 +665,16 @@ frappe.ui.form.ControlAttach = class ControlAttach extends frappe.ui.form.Contro
661665
+ '</div>'
662666
).appendTo(this.input_area);
663667
this._$list_group = this._$list.find('ul.list-group');
668+
this._$list_group.click('.ba-remove', function() {
669+
let $el = $(this);
670+
if (!$el.hasClass('ba-remove')) return;
671+
let $parent = $el.parents('.ba-attachment');
672+
if (!$parent.length) return;
673+
let idx = $parent.attr('data-file-idx');
674+
if (!idx || !/[0-9]+/.test('' + idx)) return;
675+
idx = cint(idx);
676+
if (idx >= 0) _remove_file_by_idx(idx);
677+
});
664678
}
665679
_destroy_list() {
666680
if (this._$list) {

frappe_better_attach_control/public/js/controls/v12/attach.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
227227
this._allow_remove = true;
228228
this._display_ready = false;
229229
this._unprocessed_files = [];
230+
frappe.realtime.on('better_attach_console', function(ret) {
231+
console.log(ret);
232+
});
230233
},
231234
_update_options: function() {
232235
if (
@@ -539,7 +542,8 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
539542
_add_list_file: function(file, idx) {
540543
// Check if allowed multiple files or not
541544
if (!this._allow_multiple || !this._$list) return;
542-
var meta = '';
545+
var meta = '',
546+
rem = !this._allow_remove ? ' ba-hidden' : '';
543547
if (file.size && file.size_str) {
544548
meta = '<div class="ba-meta">' + file.size_str + '</div>';
545549
}
@@ -556,7 +560,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
556560
+ '</div>'
557561
+ '</div>'
558562
+ '<div class="col-auto ba-actions">'
559-
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0">'
563+
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0' + rem + '">'
560564
+ '<span class="fa fa-times fa-fw"></span>'
561565
+ '</button>'
562566
+ '</div>'
@@ -565,6 +569,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
565569
));
566570
},
567571
_remove_files: function(data, callback, error) {
572+
if (!isArray(data)) data = [data];
568573
request('remove_files', {files: data}, callback, error);
569574
},
570575
_remove_file_by_idx: function(idx) {
@@ -582,13 +587,12 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
582587
this._remove_file_by_url(url);
583588
},
584589
_remove_file_by_url: function(url) {
585-
if (!this.frm) {
586-
this._remove_files([url], function(ret) {
590+
if (!this.frm || !this.frm.attachments) {
591+
this._remove_files(url, function(ret) {
587592
if (!cint(ret)) error('Unable to remove the uploaded attachment ({0}).', [url]);
588593
});
589594
return;
590595
}
591-
if (!this.frm.attachments) return;
592596
var me = this;
593597
this.frm.attachments.remove_attachment_by_filename(
594598
url,
@@ -613,6 +617,16 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
613617
+ '</div>'
614618
).appendTo(this.input_area);
615619
this._$list_group = this._$list.find('ul.list-group');
620+
this._$list_group.click('.ba-remove', function() {
621+
var $el = $(this);
622+
if (!$el.hasClass('ba-remove')) return;
623+
var $parent = $el.parents('.ba-attachment');
624+
if (!$parent.length) return;
625+
var idx = $parent.attr('data-file-idx');
626+
if (!idx || !/[0-9]+/.test('' + idx)) return;
627+
idx = cint(idx);
628+
if (idx >= 0) _remove_file_by_idx(idx);
629+
});
616630
},
617631
_destroy_list: function() {
618632
if (this._$list) {

frappe_better_attach_control/public/js/controls/v13/attach.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
248248
this._allow_remove = true;
249249
this._display_ready = false;
250250
this._unprocessed_files = [];
251+
frappe.realtime.on('better_attach_console', function(ret) {
252+
console.log(ret);
253+
});
251254
},
252255
_update_options: function() {
253256
if (
@@ -565,7 +568,8 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
565568
_add_list_file: function(file, idx) {
566569
// Check if allowed multiple files or not
567570
if (!this._allow_multiple || !this._$list) return;
568-
var meta = '';
571+
var meta = '',
572+
rem = !this._allow_remove ? ' ba-hidden' : '';
569573
if (file.size && file.size_str) {
570574
meta = '<div class="ba-meta">' + file.size_str + '</div>';
571575
}
@@ -582,7 +586,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
582586
+ '</div>'
583587
+ '</div>'
584588
+ '<div class="col-auto ba-actions">'
585-
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0">'
589+
+ '<button type="button" class="ba-remove btn btn-danger btn-xs mx-0' + rem + '">'
586590
+ '<span class="fa fa-times fa-fw"></span>'
587591
+ '</button>'
588592
+ '</div>'
@@ -591,6 +595,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
591595
));
592596
},
593597
_remove_files: function(data, callback, error) {
598+
if (!isArray(data)) data = [data];
594599
request('remove_files', {files: data}, callback, error);
595600
},
596601
_remove_file_by_idx: function(idx) {
@@ -608,13 +613,12 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
608613
this._remove_file_by_url(url);
609614
},
610615
_remove_file_by_url: function(url) {
611-
if (!this.frm) {
612-
this._remove_files([url], function(ret) {
616+
if (!this.frm || !this.frm.attachments) {
617+
this._remove_files(url, function(ret) {
613618
if (!cint(ret)) error('Unable to remove the uploaded attachment ({0}).', [url]);
614619
});
615620
return;
616621
}
617-
if (!this.frm.attachments) return;
618622
var me = this;
619623
this.frm.attachments.remove_attachment_by_filename(
620624
url,
@@ -639,6 +643,16 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlAttach.extend({
639643
+ '</div>'
640644
).appendTo(this.input_area);
641645
this._$list_group = this._$list.find('ul.list-group');
646+
this._$list_group.click('.ba-remove', function() {
647+
var $el = $(this);
648+
if (!$el.hasClass('ba-remove')) return;
649+
var $parent = $el.parents('.ba-attachment');
650+
if (!$parent.length) return;
651+
var idx = $parent.attr('data-file-idx');
652+
if (!idx || !/[0-9]+/.test('' + idx)) return;
653+
idx = cint(idx);
654+
if (idx >= 0) _remove_file_by_idx(idx);
655+
});
642656
},
643657
_destroy_list: function() {
644658
if (this._$list) {

0 commit comments

Comments
 (0)