Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Bug 1030767 - Create Admin List View with filters
Browse files Browse the repository at this point in the history
Fix sorting by Status and add sorting by Time
Filter User on display name OR username OR email
  • Loading branch information
bobsilverberg committed Sep 26, 2014
1 parent c7231e8 commit b74dddd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
22 changes: 20 additions & 2 deletions oneanddone/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
import caching.base


class BaseModel(models.Model):
"""
Base class for models which adds utility methods.
"""

class Meta:
abstract = True

@classmethod
def choice_display_extra_expression(self, field):
exp = 'CASE %s ' % field
state_field = self._meta.get_field_by_name(field)[0]
for choice in state_field.choices:
exp += "WHEN %s THEN '%s' " % (choice[0], choice[1])
exp += "END"
return exp


class CachedModel(caching.base.CachingMixin, models.Model):
"""
Base class for models which adds caching via django-cache-machine.
Expand All @@ -18,9 +36,9 @@ class Meta:
abstract = True


class CreatedModifiedModel(models.Model):
class CreatedModifiedModel(BaseModel):
"""
Abstract model that tracts when an instance is created and modified.
Abstract model that tracks when an instance is created and modified.
"""
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
Expand Down
9 changes: 4 additions & 5 deletions oneanddone/tasks/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ class ActivityFilterSet(django_filters.FilterSet):
label=_lazy(u'Team'),
queryset=TaskTeam.objects.all())

user__profile__name = django_filters.CharFilter(
label=_lazy(u'User Name'),
lookup_type='icontains',
widget=forms.TextInput(attrs={'size': 50})
user = MultiFieldFilter(
['user__profile__name', 'user__profile__username', 'user__email'],
label=_lazy(u'Task User')
)

modified = MyDateRangeFilter(
Expand All @@ -70,4 +69,4 @@ class ActivityFilterSet(django_filters.FilterSet):

class Meta:
model = TaskAttempt
fields = ('task__creator', 'task__team', 'user__profile__name', 'modified')
fields = ('task__creator', 'task__team', 'user', 'modified')
10 changes: 8 additions & 2 deletions oneanddone/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ class ActivityView(LoginRequiredMixin, MyStaffUserRequiredMixin, FilterView):
list_headers = (
(_('Task'), 'task__name'),
(_('User'), 'user__profile__name'),
(_('Status'), 'state'),
(_('Status'), 'state_display'),
(_('Time'), 'elapsed_time'),
)
context_object_name = 'attempts'
template_name = 'tasks/activity.html'
Expand All @@ -189,7 +190,12 @@ def get_context_data(self, *args, **kwargs):

def get_queryset(self):
self.sort_headers = SortHeaders(self.request, self.list_headers)
return TaskAttempt.objects.order_by(self.sort_headers.get_order_by())
qs = TaskAttempt.objects.extra(
select={
'state_display': TaskAttempt.choice_display_extra_expression('state'),
'elapsed_time': 'TIMESTAMPDIFF(SECOND, tasks_taskattempt.created, tasks_taskattempt.modified)'
})
return qs.order_by(self.sort_headers.get_order_by())


class CreateTaskView(LoginRequiredMixin, MyStaffUserRequiredMixin, generic.CreateView):
Expand Down
2 changes: 2 additions & 0 deletions stackato.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ ignores:
- "*.sql"
- "mine/*"
- "stackato-*"
cron:
- "00 * * * * python manage.py taskattemptcleanup >>$HOME/cron-stdout.log 2>>$HOME/cron-stderr.log"

0 comments on commit b74dddd

Please sign in to comment.