Admin: Added filter for UserProfile: balance < 0

This commit is contained in:
Theresa Enghardt 2011-12-23 02:00:12 +01:00
parent 9e2b0574ee
commit 35195d69f0
3 changed files with 42 additions and 2 deletions

View File

@ -2,6 +2,7 @@ from models import UserProfile, Plugin, PluginPermission
from django.contrib import admin from django.contrib import admin
from django import forms from django import forms
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from filters import *
class PluginAdminForm(forms.ModelForm): class PluginAdminForm(forms.ModelForm):
""" Form made to require that the user of a plugin is in the plugin group """ """ Form made to require that the user of a plugin is in the plugin group """
@ -24,7 +25,17 @@ class PluginPermissionAdmin(admin.ModelAdmin):
list_display = ('user','plugin') list_display = ('user','plugin')
list_filter = ('user','plugin') list_filter = ('user','plugin')
admin.site.register(UserProfile) class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user','balance')
search_fields = ['user_username','user__first_name','user__last_name']
list_filter = ('balance',)
# def queryset(self, request):
# return UserProfile.
# list_filter = ['balance__gt=0',]
# admin_order_field = 'balance'
admin.site.register(UserProfile, UserProfileAdmin)
admin.site.register(Plugin, PluginAdmin) admin.site.register(Plugin, PluginAdmin)
admin.site.register(PluginPermission, PluginPermissionAdmin) admin.site.register(PluginPermission, PluginPermissionAdmin)

25
k4ever/main/filters.py Normal file
View File

@ -0,0 +1,25 @@
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))

View File

@ -7,10 +7,14 @@ class UserProfile(models.Model):
""" Contains data for a user, especially the account balance. """ """ Contains data for a user, especially the account balance. """
user = models.ForeignKey(User, unique=True) user = models.ForeignKey(User, unique=True)
balance = models.DecimalField(max_digits=9, decimal_places=2, default=Decimal(0)) balance = models.DecimalField(max_digits=9, decimal_places=2, default=Decimal(0))
balance.has_negative_filter = True
def balance_negative(self):
return (self.balance < 0)
def __unicode__(self): def __unicode__(self):
return "%s (Kontostand: %s)" % (self.user ,self.balance) return "%s (Kontostand: %s)" % (self.user ,self.balance)
def createUserProfile(sender, instance, created, **kwargs): def createUserProfile(sender, instance, created, **kwargs):
""" Hook to create a new :class:`UserProfile` when the user is created. """ """ Hook to create a new :class:`UserProfile` when the user is created. """
if created: if created: