Skip to content

Commit 1255e08

Browse files
committed
Add sort field names to various models to expand the ability to sort in list views
1 parent c7a0d19 commit 1255e08

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

apps/notifier/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ def __str__(self):
115115
subject = note_type.description
116116
return "{}: {}".format(subject, self.user if self.user else "; ".join(self.emails))
117117

118+
note_type.sort_field = 'kind'
119+
to.sort_field = 'user__first_name'
120+
118121

119122
class MessageTemplateQueryset(models.QuerySet):
120123
def active(self):

apps/proposals/models.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class TYPES(models.TextChoices):
195195
comments = models.TextField(blank=True)
196196
objects = SubmissionManager()
197197

198-
@property
198+
#@property
199199
def code(self):
200200
return f'{self.track.acronym}{self.proposal.pk:0>6}'
201201

@@ -206,21 +206,27 @@ def reviewer(self):
206206

207207
def __str__(self):
208208
return '{}~{}'.format(
209-
self.code, self.proposal.spokesperson.last_name, )
209+
self.code(), self.proposal.spokesperson.last_name, )
210210

211211
def get_absolute_url(self):
212212
return reverse('submission-detail', kwargs={'pk': self.pk})
213213

214214
def title(self):
215215
return self.proposal.title
216216

217+
def spokesperson(self):
218+
return self.proposal.spokesperson
219+
217220
def siblings(self):
218221
return self.proposal.submissions.exclude(pk=self.pk)
219222

220223
def facilities(self):
221224
return Facility.objects.filter(pk__in=self.techniques.values_list('config__facility', flat=True)).distinct()
222225

226+
code.sort_field = 'id'
227+
title.sort_field = 'proposal__title'
223228
facilities.sort_field = 'techniques__config__facility__acronym'
229+
spokesperson.sort_field = 'proposal__spokesperson__first_name'
224230

225231
def close(self):
226232
comments = ""

apps/proposals/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,8 @@ def get_techniques_matrix(cycle=None, sel_techs=(), sel_fac=None):
549549
]
550550
}
551551
return matrix
552+
553+
554+
def user_format(value, obj):
555+
return str(obj.proposal.spokesperson)
556+

apps/proposals/views.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ def form_valid(self, form):
10711071
class SubmissionList(RolePermsViewMixin, ItemListView):
10721072
model = models.Submission
10731073
template_name = "item-list.html"
1074-
list_columns = ['title', 'code', 'proposal__spokesperson__last_name', 'cycle', 'kind', 'facilities', 'state']
1074+
list_columns = ['title', 'code', 'spokesperson', 'cycle', 'kind', 'facilities', 'state']
10751075
list_filters = ['created', 'state', 'track', 'kind', 'cycle']
10761076
list_search = ['proposal__title', 'proposal__id', 'proposal__spokesperson__last_name', 'proposal__keywords']
10771077
link_url = "submission-detail"
@@ -1092,13 +1092,19 @@ def get_queryset(self, *args, **kwargs):
10921092
class BeamlineSubmissionList(RolePermsViewMixin, ItemListView):
10931093
model = models.Submission
10941094
template_name = "item-list.html"
1095-
list_columns = ['code', 'title', 'proposal__spokesperson__last_name', 'cycle', 'kind', 'facilities', 'state']
1095+
list_columns = ['code', 'title', 'spokesperson', 'cycle', 'kind', 'facilities', 'state']
10961096
list_filters = ['created', 'state', 'track', 'kind', 'cycle']
10971097
list_search = ['proposal__title', 'proposal__id', 'proposal__spokesperson__last_name', 'proposal__keywords']
10981098
link_url = "submission-detail"
10991099
order_by = ['-cycle_id']
11001100
list_title = 'Proposal Submissions'
1101-
list_transforms = {'facilities': _fmt_beamlines, 'title': utils.truncated_title}
1101+
list_transforms = {
1102+
'facilities': _fmt_beamlines,
1103+
'title': utils.truncated_title,
1104+
'proposal__spokesperson': utils.user_format
1105+
1106+
}
1107+
11021108
list_styles = {'title': 'col-xs-2'}
11031109
admin_roles = USO_ADMIN_ROLES
11041110
allowed_roles = USO_ADMIN_ROLES
@@ -1113,7 +1119,9 @@ def check_allowed(self):
11131119
return allowed
11141120

11151121
def get_queryset(self, *args, **kwargs):
1116-
self.queryset = models.Submission.objects.filter(techniques__config__facility=self.kwargs['pk'])
1122+
self.queryset = models.Submission.objects.filter(
1123+
techniques__config__facility=self.kwargs['pk']
1124+
).order_by().distinct()
11171125
return super().get_queryset(*args, **kwargs)
11181126

11191127

apps/users/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.conf import settings
1010
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
1111
from django.db import models
12-
from django.db.models import Q
12+
from django.db.models import Q, Count
1313
from django.dispatch.dispatcher import receiver
1414
from django.utils import timezone
1515
from django.utils.translation import gettext as _
@@ -221,7 +221,6 @@ def get_full_name(self):
221221
last = '' if not self.last_name else f'{self.last_name}'
222222
first = '' if not first_name else f'{first_name} '
223223
return f'{first} {last}'
224-
get_full_name.short_description = "Full Name"
225224

226225
def get_name_variants(self):
227226
first_initials = name_initials(self.first_name)
@@ -248,6 +247,9 @@ def get_all_permissions(self):
248247
def get_all_roles(self):
249248
return set(self.roles)
250249

250+
get_full_name.short_description = "Full Name"
251+
get_full_name.sort_field = 'first_name'
252+
251253

252254
class Institution(DateSpanMixin, TimeStampedModel):
253255
SECTORS = Choices(
@@ -282,6 +284,7 @@ def num_users(self):
282284
return self.users.count()
283285

284286
num_users.short_description = 'Users'
287+
num_users.sort_field = 'users__id'
285288

286289
def email_users(self):
287290
if self.domains:

0 commit comments

Comments
 (0)