from django import forms
from django.contrib.auth.models import Group

from tickets.models import TicketList


class TicketListForm(forms.ModelForm):
    def __init__(self, user, *args, **kwargs):
        super(TicketListForm, self).__init__(*args, **kwargs)

        groups = Group.objects.all()
        if not user.is_superuser:
            self.fields['group'].queryset = groups.filter(user=user)
        self.fields['group'].empty_label = "Select a group"

    class Meta:
        model = TicketList
        fields = "__all__"
        
        labels = {
            'name': 'List Name',
            'group': 'Group',
        }

        widgets = {
            'name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'The full display name for this list',
                'autofocus': True
            }),
            'group': forms.Select(attrs={
                'class': 'form-control',
            }),
        }