from common.models import LinesModel


class LineFilterFormMixin:
    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        user = self.request.user
        if user.line:
            form.fields['line'].queryset = form.fields['line'].queryset.filter(pk=user.line.pk)
            form.fields['line'].initial = user.line
            form.fields['line'].widget.attrs['readonly'] = True
        else:
            form.fields['line'].queryset = LinesModel.objects.filter(company=user.company)
            form.fields['line'].widget.attrs['readonly'] = False
        return form