Skip to content

Commit 018aa43

Browse files
committed
adopt Finder to be usable with Cascade 3
1 parent 55af0b1 commit 018aa43

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

client/scss/finder-select.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ finder-file-select {
55
input.finder-file-field {
66
width: 0 !important;
77
height: 0 !important;
8+
border: none !important;
9+
outline: none !important;
810
position: absolute;
911
bottom: 0;
10-
padding: 0;
12+
padding: 0 !important;
1113
}
1214
}

finder/admin/file.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from finder.forms.file import FileForm
1010
from finder.models.file import FileModel
1111
from finder.models.inode import InodeModel
12+
from finder.models.label import Label
1213
from finder.admin.inode import InodeAdmin
1314

1415

@@ -84,6 +85,12 @@ def replace_file(self, request, file_id):
8485
file_obj.save()
8586
return HttpResponse(f"Replaced content of {file_obj.name} successfully.")
8687

88+
def get_fields(self, request, obj=None):
89+
fields = list(super().get_fields(request, obj))
90+
if 'labels' in fields and not Label.objects.exists():
91+
fields.remove('labels')
92+
return fields
93+
8794
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
8895
has_editable_inline_admin_formsets = False
8996
for inline in context['inline_admin_formsets']:

finder/browser/views.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ def structure(self, request, slug):
7373
# in addition to that, also open all ancestors of the last opened folder
7474
open_folders = set(request.session['finder.open_folders'])
7575
try:
76-
open_folders.update(
77-
map(str, FolderModel.objects.get(id=last_folder_id).ancestors.values_list('id', flat=True))
78-
)
76+
ancestors = FolderModel.objects.get(id=last_folder_id).ancestors
7977
except FolderModel.DoesNotExist:
8078
pass
79+
if isinstance(ancestors, QuerySet):
80+
open_folders.update(map(str, ancestors.values_list('id', flat=True)))
81+
else: # django-cte not installed
82+
open_folders.update(str(ancestor.id) for ancestor in ancestors)
8183
children = self._get_children(open_folders, realm.root_folder)
8284
else:
8385
children = None

finder/forms/fields.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import uuid
2-
31
from django.forms.fields import UUIDField
42
from django.forms.models import ModelMultipleChoiceField
53

@@ -9,15 +7,6 @@
97
class FinderFileField(UUIDField):
108
widget = FinderFileSelect
119

12-
def prepare_value(self, value):
13-
if isinstance(value, uuid.UUID):
14-
return str(value)
15-
return value
16-
17-
def to_python(self, value):
18-
value = super().to_python(value)
19-
return value
20-
2110

2211
class LabelsChoiceField(ModelMultipleChoiceField):
2312
def prepare_value(self, values):

finder/forms/widgets.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import json
2+
import uuid
23

34
from django.core.serializers.json import DjangoJSONEncoder
45
from django.forms.widgets import TextInput
56
from django.templatetags.static import static
67
from django.urls import reverse
78
from django.utils.html import format_html
89

9-
from finder.models.file import AbstractFileModel
10+
from finder.models.file import AbstractFileModel, FileModel
1011

1112

1213
class FinderFileSelect(TextInput):
@@ -30,11 +31,19 @@ def get_context(self, name, value, attrs):
3031
realm='admin',
3132
style_url=static('finder/css/finder-browser.css'),
3233
)
34+
if isinstance(value, str):
35+
# file reference has not been stored using a `finder.models.fields.FinderFileField`
36+
try:
37+
value = FileModel.objects.get_inode(id=uuid.UUID(value), is_folder=False)
38+
except (ValueError, FileModel.DoesNotExist):
39+
pass
3340
if isinstance(value, AbstractFileModel):
3441
context['selected_file'] = json.dumps(value.as_dict, cls=DjangoJSONEncoder)
3542
return context
3643

3744
def format_value(self, value):
3845
if value == "" or value is None:
3946
return None
40-
return value.id
47+
if isinstance(value, AbstractFileModel):
48+
return value.id
49+
return value

0 commit comments

Comments
 (0)