# This file is part of k4ever, a point-of-sale system
#   Contact............  <k4ever@lists.someserver.de>
#   Website............  http://k4ever.someserver.de/
#   Bug tracker........  http://k4ever.someserver.de/report
#
# Licensed under GNU Affero General Public License v3 or later

from django.contrib.admin.filterspecs import FilterSpec, BooleanFieldFilterSpec

class IsNegativeFilterSpec(FilterSpec):
	""" Custom filter to display only users with negative balance """
	def __init__(self, f, request, *args, **kwargs):
		super(IsNegativeFilterSpec, self).__init__(f, request, *args, **kwargs)
		self.lookup_kwarg = '%s__lt' % f.name
		self.lookup_val = request.GET.get(self.lookup_kwarg, None) # Check if current request already has a filter

	def title(self):
		return 'Kontostand'
	
	def choices(self, cl):
		yield {
				'selected': self.lookup_val == None,
				'query_string': "?", 
				'display': 'Alle'}
		yield {
				'selected': self.lookup_val == '0',
				'query_string': cl.get_query_string(
								{self.lookup_kwarg: 0},
								['balance__isnull']),
				'display': 'Nur Negative'}

FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'has_negative_filter', False), IsNegativeFilterSpec))
