|
54 | 54 |
|
55 | 55 | from uds.core.module import Module |
56 | 56 |
|
| 57 | +if typing.TYPE_CHECKING: |
| 58 | + from django.db.models.query import QuerySet |
| 59 | + |
57 | 60 | logger = logging.getLogger(__name__) |
58 | 61 |
|
59 | 62 |
|
@@ -110,9 +113,9 @@ class Authenticators(ModelHandler[AuthenticatorItem]): |
110 | 113 | .icon(name='name', title=_('Name'), visible=True) |
111 | 114 | .text_column(name='type_name', title=_('Type')) |
112 | 115 | .text_column(name='comments', title=_('Comments')) |
113 | | - .numeric_column(name='priority', title=_('Priority'), width='5rem') |
| 116 | + .numeric_column(name='priority', title=_('Priority'), width='8rem') |
114 | 117 | .text_column(name='small_name', title=_('Label')) |
115 | | - .numeric_column(name='users_count', title=_('Users'), width='1rem') |
| 118 | + .numeric_column(name='users_count', title=_('Users'), width='6rem') |
116 | 119 | .text_column(name='mfa_name', title=_('MFA')) |
117 | 120 | .text_column(name='tags', title=_('tags'), visible=False) |
118 | 121 | .row_style(prefix='row-state-', field='state') |
@@ -218,6 +221,29 @@ def get_item(self, item: 'models.Model') -> AuthenticatorItem: |
218 | 221 | type_info=type(self).as_typeinfo(item.get_type()), |
219 | 222 | ) |
220 | 223 |
|
| 224 | + def apply_sort(self, qs: 'QuerySet[typing.Any]') -> 'list[typing.Any] | QuerySet[typing.Any]': |
| 225 | + if field_info := self.get_sort_field_info('users_count'): |
| 226 | + field_name, is_descending = field_info |
| 227 | + order_by_field = f"-{field_name}" if is_descending else field_name |
| 228 | + return qs.annotate(users_count=models.Count('users')).order_by(order_by_field) |
| 229 | + |
| 230 | + if field_info := self.get_sort_field_info('type_name'): |
| 231 | + _, is_descending = field_info |
| 232 | + order_by_field = f'-data_type' if is_descending else 'data_type' |
| 233 | + return qs.order_by(order_by_field) |
| 234 | + |
| 235 | + if field_info := self.get_sort_field_info('numeric_id'): |
| 236 | + _, is_descending = field_info |
| 237 | + order_by_field = f'-pk' if is_descending else 'pk' |
| 238 | + return qs.order_by(order_by_field) |
| 239 | + |
| 240 | + if field_info := self.get_sort_field_info('mfa_name'): |
| 241 | + _, is_descending = field_info |
| 242 | + order_by_field = f'-mfa__name' if is_descending else 'mfa__name' |
| 243 | + return qs.order_by(order_by_field) |
| 244 | + |
| 245 | + return super().apply_sort(qs) |
| 246 | + |
221 | 247 | def post_save(self, item: 'models.Model') -> None: |
222 | 248 | item = ensure.is_instance(item, Authenticator) |
223 | 249 | try: |
|
0 commit comments