Advertisement
OlegKl

Untitled

Feb 13th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. class EmailDuplicatesFilter(SimpleListFilter):
  2.     """
  3.    Return user duplicates by email
  4.    """
  5.     title = _("Duplicates")
  6.     parameter_name = 'duplicate_field'
  7.  
  8.     def lookups(self, request, model_admin):
  9.         return (
  10.             ('email', _('Email')),
  11.         )
  12.  
  13.     def queryset(self, request, queryset):
  14.         if self.value() == 'email':
  15.             dups = (
  16.                 queryset
  17.                 .annotate(lower_email=Lower('email'))
  18.                 .values('lower_email')
  19.                 .annotate(count=Count('lower_email'))
  20.                 .values('lower_email', 'count')
  21.                 .filter(count__gt=1)
  22.                 .order_by('lower_email', 'count')
  23.             )
  24.  
  25.             return (
  26.                 queryset
  27.                 .annotate(lower_email=Lower('email'))
  28.                 .filter(lower_email__in=[item['lower_email'] for item in dups])
  29.                 .order_by('lower_email')
  30.             )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement