Skip to content

Commit 2e34c6f

Browse files
committed
minor refactoring for the dashboard reference fields formatting, thanks Nico
1 parent f8b5ddc commit 2e34c6f

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

apps/_dashboard/__init__.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,30 +153,18 @@ def make_grid():
153153
field.readable = True
154154
field.writable = True
155155
# Make reference fields clickable
156-
if field.type.startswith("reference ") or field.type.startswith("big-reference "):
157-
referenced_table_name = field.type.split()[1]
158-
# Create a custom represent function that returns a clickable link
159-
def make_reference_represent(ref_table_name, app_n, db_n):
160-
def represent(value, row):
161-
if not value:
162-
return ""
163-
ref_table = db[ref_table_name]
164-
ref_row = ref_table(value)
165-
if not ref_row:
166-
return f"#{value}(missing)"
167-
# Get display text using table format
168-
if isinstance(ref_table._format, str):
169-
display_text = ref_table._format % ref_row
170-
elif callable(ref_table._format):
171-
display_text = ref_table._format(ref_row)
172-
else:
173-
display_text = str(value)
174-
# Create link to the referenced record's table
175-
link_url = URL("dbadmin", app_n, db_n, ref_table_name, vars=dict(id=value))
176-
return XML(f'<a href="{link_url}">{display_text}</a>')
177-
return represent
178-
field.represent = make_reference_represent(referenced_table_name, app_name, db_name)
179-
156+
if field.type_name in ("reference", "big-reference"):
157+
field.represent = make_admin_reference_represent(
158+
app_name, db_name, db, field
159+
)
160+
elif field.type_name in ("list:reference"):
161+
representer = make_admin_reference_represent(
162+
app_name, db_name, db, field
163+
)
164+
field.represent = lambda values, _: [
165+
representer(item, _) for item in values
166+
]
167+
180168
columns = [
181169
field
182170
for field in table

apps/_dashboard/utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import tarfile
2020
import datetime
2121

22+
from py4web import URL
23+
from yatl.helpers import A
24+
2225
__all__ = (
2326
"safe_join",
2427
"list_dir",
@@ -33,6 +36,7 @@
3336
"get_commits",
3437
"get_branches",
3538
"is_git_repo",
39+
"make_admin_reference_represent",
3640
)
3741

3842

@@ -207,3 +211,39 @@ def get_branches(cwd="."):
207211
def is_git_repo(cwd="."):
208212
"""Checks if the cwd is a git repo"""
209213
return os.path.exists(os.path.join(cwd, ".git/config"))
214+
215+
216+
def fieldformat(table, row):
217+
"""
218+
Given a table and a record, returns a string representation for the record
219+
Compatible with pydal.helpers.methods._fieldformat
220+
"""
221+
format = getattr(table, "_format", None)
222+
if format:
223+
if isinstance(format, str):
224+
return table._format % row
225+
elif callable(table._format):
226+
return table._format(row)
227+
else:
228+
raise NotImplementedError
229+
return str(row.id)
230+
231+
232+
# Create a custom represent function that returns a clickable link
233+
def make_admin_reference_represent(app_name, db_name, db, field):
234+
def represent(value, _):
235+
if not value:
236+
return ""
237+
ref_table = field.referenced_table()
238+
ref_row = ref_table(value)
239+
if not ref_row:
240+
return f"#{value}(missing)"
241+
# Create text to be displayed
242+
display_text = fieldformat(ref_table, ref_row)
243+
# Create link to the referenced record's table
244+
link_url = URL(
245+
"dbadmin", app_name, db_name, ref_table._tablename, vars=dict(id=value)
246+
)
247+
return A(display_text, _href=link_url)
248+
249+
return represent

apps/fadebook/controllers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def friends():
8282
# a search form (simply by first name)
8383
form = Form([Field("name", required=True)])
8484
users = []
85-
if form.accepted:
85+
if form.accepted and form.vars.get("name"):
8686
# select users based on the tokens in the search input
8787
query = None
8888
for token in form.vars.get("name").split():

0 commit comments

Comments
 (0)