1 | # This file is part of CMT, a Cluster Management Tool made at SARA. |
---|
2 | # Copyright (C) 2012 Sil Westerveld |
---|
3 | # |
---|
4 | # This program is free software; you can redistribute it and/or modify |
---|
5 | # it under the terms of the GNU General Public License as published by |
---|
6 | # the Free Software Foundation; either version 2 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | # |
---|
9 | # This program is distributed in the hope that it will be useful, |
---|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | # GNU General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU General Public License |
---|
15 | # along with this program; if not, write to the Free Software |
---|
16 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
17 | |
---|
18 | from django.db import models |
---|
19 | from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec, DateFieldFilterSpec |
---|
20 | #from django.utils.encoding import smart_unicode |
---|
21 | from django.utils.translation import ugettext as _ |
---|
22 | from datetime import date, timedelta |
---|
23 | |
---|
24 | class InSupportFilterSpec(DateFieldFilterSpec): |
---|
25 | """ |
---|
26 | Adds filtering by warranty-status values in the admin filter sidebar. |
---|
27 | Set the in_support_filter filter in the model field attribute |
---|
28 | 'in_support_filter'. |
---|
29 | |
---|
30 | my_model_field.in_support_filter = True |
---|
31 | """ |
---|
32 | |
---|
33 | def __init__(self, f, request, params, model, model_admin): |
---|
34 | super(InSupportFilterSpec, self).__init__(f, request, params, model, model_admin) |
---|
35 | |
---|
36 | today = date.today() |
---|
37 | days_thirty = today + timedelta(30) |
---|
38 | self.links = ( |
---|
39 | (_('All'), {}), |
---|
40 | (_('In Support'), {'%s__gte' % self.field.name: str(today), }), |
---|
41 | (_('Expiring in 30 days'), {'%s__gte' % self.field.name: str(today), '%s__lte' % self.field.name: str(days_thirty) }), |
---|
42 | (_('Expired'), {'%s__lte' % self.field.name: str(today), }), |
---|
43 | ) |
---|
44 | |
---|
45 | def title(self): |
---|
46 | return 'warranty status' |
---|
47 | |
---|
48 | # register the filter |
---|
49 | FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'in_support_filter', False), InSupportFilterSpec)) |
---|