Browse Source

Admin: Added filter for UserProfile: balance < 0

master
Theresa Enghardt 11 years ago
parent
commit
35195d69f0
  1. 13
      k4ever/main/admin.py
  2. 25
      k4ever/main/filters.py
  3. 6
      k4ever/main/models.py

13
k4ever/main/admin.py

@ -2,6 +2,7 @@ from models import UserProfile, Plugin, PluginPermission @@ -2,6 +2,7 @@ from models import UserProfile, Plugin, PluginPermission
from django.contrib import admin
from django import forms
from django.contrib.auth.models import User, Group
from filters import *
class PluginAdminForm(forms.ModelForm):
""" Form made to require that the user of a plugin is in the plugin group """
@ -24,7 +25,17 @@ class PluginPermissionAdmin(admin.ModelAdmin): @@ -24,7 +25,17 @@ class PluginPermissionAdmin(admin.ModelAdmin):
list_display = ('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(PluginPermission, PluginPermissionAdmin)

25
k4ever/main/filters.py

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

6
k4ever/main/models.py

@ -7,10 +7,14 @@ class UserProfile(models.Model): @@ -7,10 +7,14 @@ class UserProfile(models.Model):
""" Contains data for a user, especially the account balance. """
user = models.ForeignKey(User, unique=True)
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):
return "%s (Kontostand: %s)" % (self.user ,self.balance)
def createUserProfile(sender, instance, created, **kwargs):
""" Hook to create a new :class:`UserProfile` when the user is created. """
if created:

Loading…
Cancel
Save