from models import UserProfile, Plugin, PluginPermission
from django.contrib import admin
from django import forms
from django.contrib.auth.models import User, Group

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

admin.site.register(UserProfile)
admin.site.register(Plugin, PluginAdmin)
admin.site.register(PluginPermission)

