Skip to content

Commit 2960b2e

Browse files
Add VIP status filter to enhance recipient filtering options
1 parent e717ff1 commit 2960b2e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crm/utils/admfilters.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.contrib import admin
33
from django.contrib.admin import SimpleListFilter
44
from django.contrib.admin.filters import DateFieldListFilter
5+
from django.contrib.contenttypes.models import ContentType
56
from django.core.exceptions import ObjectDoesNotExist
67
from django.db.models import CharField
78
from django.db.models import Exists
@@ -23,6 +24,7 @@
2324
from crm.models import Tag
2425
from crm.models import Output
2526
from crm.models.country import City
27+
from massmail.models import MassContact
2628

2729
# Lookup parameters must be removed from the querystring when
2830
# the corresponding filter is executed!
@@ -77,6 +79,38 @@ def queryset(self, request, queryset):
7779
return queryset.filter(city_id=int(self.value()))
7880

7981

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+
80114
class ChoicesSimpleListFilter(SimpleListFilter):
81115
"""
82116
The SimpleListFilter with overridden "choices" method

0 commit comments

Comments
 (0)