from django.contrib import admin
from django import forms

from .models import Event, Activity, Profession, Attendee, AttendeeType, AttendeePayment, PaymentMethod, EventBadge

class AttendeeTypeInline(admin.TabularInline):
    model = Event.types.through

class EventBadgeInline(admin.TabularInline):
    model = EventBadge

class AttendeePaymentInline(admin.TabularInline):
    model = AttendeePayment

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "payment_method":
            kwargs["queryset"] = PaymentMethod.objects.filter(id__in=self.parent_obj.event.payment_methods.all())
        return super(AttendeePaymentInline, self).formfield_for_foreignkey(db_field, request, **kwargs)

    def get_formset(self, request, obj=None, **kwargs):
        self.parent_obj = obj
        return super(AttendeePaymentInline, self).get_formset(
            request, obj, **kwargs)

class EventAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("name",)}
    inlines = [
        AttendeeTypeInline,
        EventBadgeInline,
    ]

class ActivityAdmin(admin.ModelAdmin):
    list_filter = ('event__name',)
    prepopulated_fields = {"slug": ("name",)}

class ProfessionAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("name",)}
    #list_filter = ('event__name',)

def has_approval_permission(request, obj=None):
    if request.user.has_perm('users.can_approve_participant'):
        return True
    return False

class AttendeeAdmin(admin.ModelAdmin):
    list_display = ['id','first_name','last_name','email','profession']
    list_filter = ('event__name','country','profession','type')
    search_fields = ['id','first_name','last_name']

    inlines = [
        AttendeePaymentInline,
    ]

    fieldsets = (
    	(None, {
            'fields': ('event','type','first_name', 'last_name', 'email', 'profession',
            'phone','country','nationality','sponsored','sponsor','photo')
        }),
        ('Informacion de actividades y biografia', {
            'classes': ('collapse',),
            'fields': ('text','activities',)
    	}),
        ('Permisos de usuario', {
            'classes': ('collapse',),
            'fields': ('username', 'password', 'is_active','last_login','date_joined')
    	}),
    )

    def get_queryset(self, request):
        qs = super(AttendeeAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(event=request.user.event)

    def get_form(self, request, obj=None, **kwargs):
        if not has_approval_permission(request, obj):
            self.fieldsets = (
                (None, {
                    'fields': ('event','type','first_name', 'last_name', 'email', 'profession',
                    'phone','country','nationality','sponsored','sponsor','photo')
                }),
                ('Informacion de actividades y biografia', {
                    'classes': ('collapse',),
                    'fields': ('text','activities',)
                }),
                ('Permisos de usuario', {
                    'classes': ('collapse',),
                    'fields': ('username', 'password', 'last_login','date_joined')
                }),
            )
        form = super(AttendeeAdmin, self).get_form(request, obj, **kwargs)

        # required fields (not required in original model)
        form.base_fields['first_name'].required = True
        form.base_fields['last_name'].required = True
        form.base_fields['email'].required = True

        # if update, limit type and profession to current event
        if obj:
            form.base_fields['profession'].queryset = Profession.objects.filter(id__in=obj.event.professions.all())
            form.base_fields['type'].queryset = AttendeeType.objects.filter(id__in=obj.event.types.all())
            #form.base_fields['payment_method'].queryset = PaymentMethod.objects.filter(id__in=obj.event.payment_methods.all())

        # if not superuser, limit type and profession to user's event
        if not request.user.is_superuser:
            form.base_fields['profession'].queryset = Profession.objects.filter(id__in=request.user.event.professions.all())
            form.base_fields['type'].queryset = AttendeeType.objects.filter(id__in=request.user.event.types.all())
            form.base_fields['event'].initial = request.user.event
            form.base_fields['event'].widget = forms.HiddenInput()
            form.base_fields['event'].label = ''
            #self.readonly_fields = ('event',)
        return form

admin.site.register(Event, EventAdmin)
admin.site.register(Activity, ActivityAdmin)
admin.site.register(Profession, ProfessionAdmin)
admin.site.register(Attendee, AttendeeAdmin)
admin.site.register(AttendeeType)
admin.site.register(AttendeePayment)
admin.site.register(PaymentMethod)

@@ -512,16 +512,52 @@
 ethod%22:%0A
+            if self.parent_obj:%0A    
         
@@ -654,32 +654,193 @@
 _methods.all())%0A
+            if not request.user.is_superuser:%0A                kwargs%5B%22queryset%22%5D = PaymentMethod.objects.filter(id__in=request.user.event.payment_methods.all())%0A
         return s
@@ -4034,136 +4034,8 @@
 l())
-%0A            #form.base_fields%5B'payment_method'%5D.queryset = PaymentMethod.objects.filter(id__in=obj.event.payment_methods.all())
 %0A%0A  
@@ -4610,24 +4610,25 @@
  ('event',)%0A
+%0A
         retu

improve inline Attendee Payments