# 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 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 """
	class Meta:
		model = Plugin
	
	def clean_user(self):
		user = self.cleaned_data['user']
		group = Group.objects.get(name="Plugin")
		if not group in user.groups.all():
			raise forms.ValidationError("The user for a plugin has to be a member of the 'Plugin' group")
		return self.cleaned_data['user']

class PluginAdmin(admin.ModelAdmin):
	form = PluginAdminForm
	list_display = ('name','version','author','descr')
	list_filter = ['author',]

class PluginPermissionAdmin(admin.ModelAdmin):
	list_display = ('user','plugin')
	list_filter = ('user','plugin')

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)

