|
2 | 2 | from django.contrib import admin |
3 | 3 | from django.contrib.admin import SimpleListFilter |
4 | 4 | from django.contrib.admin.filters import DateFieldListFilter |
| 5 | +from django.contrib.contenttypes.models import ContentType |
5 | 6 | from django.core.exceptions import ObjectDoesNotExist |
6 | 7 | from django.db.models import CharField |
7 | 8 | from django.db.models import Exists |
|
23 | 24 | from crm.models import Tag |
24 | 25 | from crm.models import Output |
25 | 26 | from crm.models.country import City |
| 27 | +from massmail.models import MassContact |
26 | 28 |
|
27 | 29 | # Lookup parameters must be removed from the querystring when |
28 | 30 | # the corresponding filter is executed! |
@@ -77,6 +79,38 @@ def queryset(self, request, queryset): |
77 | 79 | return queryset.filter(city_id=int(self.value())) |
78 | 80 |
|
79 | 81 |
|
| 82 | +class ByVIPStatus(SimpleListFilter): |
| 83 | + """ Filter by recipients' VIP status. """ |
| 84 | + title = _('VIP Status') |
| 85 | + parameter_name = 'vip_status' |
| 86 | + |
| 87 | + def lookups(self, request, model_admin): |
| 88 | + return ( |
| 89 | + ('yes', _('VIP')), |
| 90 | + ('no', _('Non-VIP')), |
| 91 | + ) |
| 92 | + |
| 93 | + def queryset(self, request, queryset): |
| 94 | + if self.value(): |
| 95 | + content_type = ContentType.objects.get_for_model(queryset.model) |
| 96 | + ids = queryset.values_list('id', flat=True) |
| 97 | + mcs = MassContact.objects.filter( |
| 98 | + object_id__in=ids, |
| 99 | + content_type=content_type, |
| 100 | + ) |
| 101 | + if self.value() == 'yes': |
| 102 | + vip_ids = mcs.filter( |
| 103 | + email_account__main=True |
| 104 | + ).values_list('object_id', flat=True) |
| 105 | + return queryset.filter(id__in=list(vip_ids)) |
| 106 | + if self.value() == 'no': |
| 107 | + non_vip_ids = mcs.filter( |
| 108 | + email_account__main=False |
| 109 | + ).values_list('object_id', flat=True) |
| 110 | + return queryset.filter(id__in=list(non_vip_ids)) |
| 111 | + return queryset |
| 112 | + |
| 113 | + |
80 | 114 | class ChoicesSimpleListFilter(SimpleListFilter): |
81 | 115 | """ |
82 | 116 | The SimpleListFilter with overridden "choices" method |
|
0 commit comments