django-smartbase-admin 0.2.100__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,8 @@ import math
4
4
  from django.core.paginator import Paginator
5
5
  from django.db.models import Q
6
6
  from django.utils import timezone
7
+ from django.utils.html import escape
8
+ from django.utils.safestring import SafeString
7
9
  from django.utils.text import smart_split, unescape_string_literal
8
10
 
9
11
  from django_smartbase_admin.engine.const import (
@@ -406,11 +408,11 @@ class SBAdminListAction(SBAdminAction):
406
408
 
407
409
  def process_final_data(self, final_data):
408
410
  visible_columns = self.get_visible_column_fields()
409
- fields_with_methods_to_call = [
410
- field
411
+ fields_with_methods_to_call_by_field_key = {
412
+ field.field: field
411
413
  for field in visible_columns
412
414
  if field.view_method or field.python_formatter
413
- ]
415
+ }
414
416
  for row in final_data:
415
417
  additional_data = {}
416
418
  if self.view.sbadmin_list_display_data:
@@ -418,18 +420,23 @@ class SBAdminListAction(SBAdminAction):
418
420
  data: row.get(data, None)
419
421
  for data in self.view.sbadmin_list_display_data
420
422
  }
421
- for field in fields_with_methods_to_call:
422
- object_id = row.get(self.get_pk_field().name, None)
423
- value = row.get(field.field, None)
424
- processed_value = value
425
- if field.view_method:
426
- processed_value = field.view_method(
427
- object_id, value, **additional_data
428
- )
429
- formatted_value = processed_value
430
- if field.python_formatter:
431
- formatted_value = field.python_formatter(object_id, processed_value)
432
- row[field.field] = formatted_value
423
+ for field_key, value in row.items():
424
+ if field_key in fields_with_methods_to_call_by_field_key:
425
+ field = fields_with_methods_to_call_by_field_key[field_key]
426
+ object_id = row.get(self.get_pk_field().name, None)
427
+ processed_value = value
428
+ if field.view_method:
429
+ processed_value = field.view_method(
430
+ object_id, value, **additional_data
431
+ )
432
+ formatted_value = processed_value
433
+ if field.python_formatter:
434
+ formatted_value = field.python_formatter(
435
+ object_id, processed_value
436
+ )
437
+ row[field_key] = formatted_value
438
+ if isinstance(value, str) and not isinstance(value, SafeString):
439
+ row[field_key] = escape(value)
433
440
 
434
441
  def get_json_data(self):
435
442
  return self.get_data()
@@ -215,11 +215,16 @@ class SBAdminFormFieldWidgetsMixin:
215
215
  ) # remove origin classes to prevent override our custom widget class
216
216
  kwargs = {}
217
217
  if isinstance(form_field, RichTextFormField):
218
- kwargs["config_name"] = getattr(db_field, "config_name", "default")
218
+ kwargs["config_name"] = getattr(
219
+ form_field.widget, "config_name", None
220
+ ) or getattr(db_field, "config_name", "default")
221
+
219
222
  kwargs["external_plugin_resources"] = getattr(
220
- db_field, "external_plugin_resources", []
221
- )
222
- kwargs["extra_plugins"] = getattr(db_field, "extra_plugins", [])
223
+ form_field.widget, "external_plugin_resources", None
224
+ ) or getattr(db_field, "external_plugin_resources", [])
225
+ kwargs["extra_plugins"] = getattr(
226
+ form_field.widget, "extra_plugins", None
227
+ ) or getattr(db_field, "extra_plugins", [])
223
228
  form_field.widget = widget(form_field=form_field, attrs=widget_attrs, **kwargs)
224
229
  return form_field
225
230
 
@@ -100,7 +100,7 @@ class SBAdminField(JSONSerializableMixin):
100
100
  list_visible=None,
101
101
  list_collapsed=None,
102
102
  auto_created=None,
103
- formatter: Formatter = None,
103
+ formatter: Formatter = Formatter.HTML.value,
104
104
  tabulator_editor=None,
105
105
  python_formatter=None,
106
106
  tabulator_options: "TabulatorFieldOptions" = None,
@@ -185,7 +185,6 @@ class SBAdminField(JSONSerializableMixin):
185
185
  if self.model_field and not self.annotate:
186
186
  self.annotate = F(field_name)
187
187
  self.filter_field = self.filter_field or field_name
188
- self.formatter = "html"
189
188
  if self.view.model and not self.model_field:
190
189
  self.model_field = self.get_model_field_from_model(self.name)
191
190
  if (
@@ -225,8 +224,6 @@ class SBAdminField(JSONSerializableMixin):
225
224
  self.python_formatter = datetime_formatter
226
225
  if isinstance(self.model_field, BooleanField):
227
226
  self.python_formatter = boolean_formatter
228
- if self.python_formatter and not self.formatter:
229
- self.formatter = "html"
230
227
  self.filter_field = self.filter_field or self.field
231
228
  self.init_filter_for_field(configuration)
232
229
  self.initialized = True
@@ -1,6 +1,7 @@
1
1
  from enum import Enum
2
2
 
3
3
  from django.template.defaultfilters import date, time
4
+ from django.utils.safestring import mark_safe
4
5
  from django.utils.translation import gettext_lazy as _
5
6
  from django.utils import timezone
6
7
 
@@ -38,8 +39,10 @@ def datetime_formatter_with_format(date_format=None, time_format=None):
38
39
 
39
40
  def boolean_formatter(object_id, value):
40
41
  if value:
41
- return f'<span class="badge badge-simple badge-positive">{_("Yes")}</span>'
42
- return f'<span class="badge badge-simple badge-neutral">{_("No")}</span>'
42
+ return mark_safe(
43
+ f'<span class="badge badge-simple badge-positive">{_("Yes")}</span>'
44
+ )
45
+ return mark_safe(f'<span class="badge badge-simple badge-neutral">{_("No")}</span>')
43
46
 
44
47
 
45
48
  def format_array(value_list, separator="", badge_type: BadgeType = BadgeType.NOTICE):
@@ -50,7 +53,7 @@ def format_array(value_list, separator="", badge_type: BadgeType = BadgeType.NOT
50
53
  if not value:
51
54
  continue
52
55
  result += f'<span class="badge badge-simple badge-{badge_type.value} mr-4">{value}</span>{separator}'
53
- return result
56
+ return mark_safe(result)
54
57
 
55
58
 
56
59
  def array_badge_formatter(object_id, value_list):
@@ -62,8 +65,10 @@ def newline_separated_array_badge_formatter(object_id, value_list):
62
65
 
63
66
 
64
67
  def rich_text_formatter(object_id, value):
65
- return f'<div style="max-width: 500px; white-space: normal;">{value}</div>'
68
+ return mark_safe(
69
+ f'<div style="max-width: 500px; white-space: normal;">{value}</div>'
70
+ )
66
71
 
67
72
 
68
73
  def link_formatter(object_id, value):
69
- return f'<a href="{value}">{value}</a>'
74
+ return mark_safe(f'<a href="{value}">{value}</a>')