|
2 | 2 | import re
|
3 | 3 |
|
4 | 4 | from django import forms
|
| 5 | +from django.contrib.postgres.forms import SimpleArrayField |
5 | 6 | from django.utils.safestring import mark_safe
|
6 | 7 | from django.utils.translation import gettext_lazy as _
|
7 | 8 |
|
|
21 | 22 | )
|
22 | 23 | from utilities.forms.rendering import FieldSet, ObjectAttribute
|
23 | 24 | from utilities.forms.widgets import ChoicesWidget, HTMXSelect
|
| 25 | +from utilities.tables import get_table_for_model |
24 | 26 | from virtualization.models import Cluster, ClusterGroup, ClusterType
|
25 | 27 |
|
26 | 28 | __all__ = (
|
|
37 | 39 | 'NotificationGroupForm',
|
38 | 40 | 'SavedFilterForm',
|
39 | 41 | 'SubscriptionForm',
|
| 42 | + 'TableConfigForm', |
40 | 43 | 'TagForm',
|
41 | 44 | 'WebhookForm',
|
42 | 45 | )
|
@@ -301,6 +304,65 @@ def __init__(self, *args, initial=None, **kwargs):
|
301 | 304 | super().__init__(*args, initial=initial, **kwargs)
|
302 | 305 |
|
303 | 306 |
|
| 307 | +class TableConfigForm(forms.ModelForm): |
| 308 | + object_type = ContentTypeChoiceField( |
| 309 | + label=_('Object type'), |
| 310 | + queryset=ObjectType.objects.all() |
| 311 | + ) |
| 312 | + ordering = SimpleArrayField( |
| 313 | + base_field=forms.CharField(), |
| 314 | + required=False, |
| 315 | + label=_('Ordering'), |
| 316 | + help_text=_( |
| 317 | + "Enter a comma-separated list of column names. Prepend a name with a hyphen to reverse the order." |
| 318 | + ) |
| 319 | + ) |
| 320 | + available_columns = SimpleArrayField( |
| 321 | + base_field=forms.CharField(), |
| 322 | + required=False, |
| 323 | + widget=forms.SelectMultiple( |
| 324 | + attrs={'size': 10, 'class': 'form-select'} |
| 325 | + ), |
| 326 | + label=_('Available Columns') |
| 327 | + ) |
| 328 | + columns = SimpleArrayField( |
| 329 | + base_field=forms.CharField(), |
| 330 | + widget=forms.SelectMultiple( |
| 331 | + attrs={'size': 10, 'class': 'form-select select-all'} |
| 332 | + ), |
| 333 | + label=_('Selected Columns') |
| 334 | + ) |
| 335 | + |
| 336 | + class Meta: |
| 337 | + model = TableConfig |
| 338 | + exclude = ('user',) |
| 339 | + |
| 340 | + def __init__(self, data=None, *args, **kwargs): |
| 341 | + super().__init__(data, *args, **kwargs) |
| 342 | + |
| 343 | + object_type = ObjectType.objects.get(pk=get_field_value(self, 'object_type')) |
| 344 | + model = object_type.model_class() |
| 345 | + table_name = get_field_value(self, 'table') |
| 346 | + table_class = get_table_for_model(model, table_name) |
| 347 | + table = table_class([]) |
| 348 | + |
| 349 | + if columns := self._get_columns(): |
| 350 | + table._set_columns(columns) |
| 351 | + |
| 352 | + # Initialize columns field based on table attributes |
| 353 | + self.fields['available_columns'].widget.choices = table.available_columns |
| 354 | + self.fields['columns'].widget.choices = table.selected_columns |
| 355 | + |
| 356 | + def _get_columns(self): |
| 357 | + if self.is_bound and (columns := self.data.getlist('columns')): |
| 358 | + return columns |
| 359 | + if 'columns' in self.initial: |
| 360 | + columns = self.get_initial_for_field(self.fields['columns'], 'columns') |
| 361 | + return columns.split(',') if type(columns) is str else columns |
| 362 | + if self.instance is not None: |
| 363 | + return self.instance.columns |
| 364 | + |
| 365 | + |
304 | 366 | class BookmarkForm(forms.ModelForm):
|
305 | 367 | object_type = ContentTypeChoiceField(
|
306 | 368 | label=_('Object type'),
|
|
0 commit comments