def get_form_filter(self, request): form_fields = fields_for_model( self.model, [f for f in self.get_query_field_names() if f in self.list_filter]) if not form_fields: form_fields = { '__all__': forms.BooleanField( label='', widget=forms.HiddenInput, initial='1') } else: opts = self.model._meta for k, v in dict(form_fields).items(): if v is None: pre_field = None base_model = self.model if '__' in k: for field_lookup in k.split("__")[:-1]: if pre_field: if isinstance(pre_field, RelatedObject): base_model = pre_field.model else: base_model = pre_field.rel.to pre_field = base_model._meta.get_field_by_name( field_lookup)[0] model_field = pre_field else: field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] if isinstance(model_field, (DateField, DateTimeField)): form_fields.pop(k) field = RangeField(model_field.formfield) else: if not hasattr(model_field, 'formfield'): field = forms.ModelChoiceField( queryset=model_field.model.objects.all()) field.label = self.override_field_labels.get( k, base_label )( self, field ) if k in self.override_field_labels else field_lookup else: field = model_field.formfield() field.label = force_unicode(_(field.label)) else: if isinstance(v, (forms.BooleanField)): form_fields.pop(k) field = forms.ChoiceField() field.label = v.label field.help_text = v.help_text field.choices = ( ('', ''), (True, _('Yes')), (False, _('No')), ) setattr(field, 'as_boolean', True) elif isinstance(v, (forms.DateField, forms.DateTimeField)): field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] form_fields.pop(k) field = RangeField(model_field.formfield) else: field = v if hasattr(field, 'choices'): # self.override_field_filter_values if not hasattr(field, 'queryset'): if field.choices[0][0]: field.choices.insert(0, ('', '---------')) field.initial = '' # Provide a hook for updating the queryset if hasattr(field, 'queryset') and k in self.override_field_choices: field.queryset = self.override_field_choices.get(k)( self, field.queryset) form_fields[k] = field form_class = type('FilterFormBase', (forms.BaseForm, ), {'base_fields': form_fields}) class FilterForm(form_class): def _post_clean(self): pass def get_filter_kwargs(self): if not self.is_valid(): return {} filter_kwargs = dict(self.cleaned_data) for k, v in dict(filter_kwargs).items(): if not v: filter_kwargs.pop(k) continue if k == '__all__': filter_kwargs.pop(k) continue if isinstance(v, (list, tuple)): if isinstance(self.fields[k], (RangeField)): filter_kwargs.pop(k) start_range, end_range = v if start_range: filter_kwargs['%s__gte' % k] = start_range if end_range: filter_kwargs['%s__lte' % k] = end_range elif hasattr(self.fields[k], 'as_boolean'): if v: filter_kwargs.pop(k) filter_kwargs[k] = (unicode(v) == u'True') return filter_kwargs def get_cleaned_data(self): return getattr(self, 'cleaned_data', {}) def __init__(self, *args, **kwargs): super(FilterForm, self).__init__(*args, **kwargs) self.filter_report_is_all = '__all__' in self.fields and len( self.fields) == 1 try: data_filters = {} vals = args[0] for k in vals.keys(): if k in self.fields: data_filters[k] = vals[k] for name in self.fields: for k, v in data_filters.items(): if k == name: continue field = self.fields[name] if hasattr(field, 'queryset'): qs = field.queryset if k in qs.model._meta.get_all_field_names(): field.queryset = qs.filter(Q(**{k: v})) except: pass for field in self.fields: self.fields[field].required = False form = FilterForm(data=request.GET or None) form.is_valid() return form
def get_form_filter(self, request): form_fields = fields_for_model( self.model, [f for f in self.get_query_field_names() if f in self.list_filter]) if not form_fields: form_fields = { '__all__': forms.BooleanField(label='', widget=forms.HiddenInput, initial='1') } else: opts = self.model._meta for k, v in dict(form_fields).items(): if v is None: pre_field = None base_model = self.model if '__' in k: # for field_lookup in k.split("__")[:-1]: for field_lookup in k.split("__"): if pre_field: if isinstance(pre_field, RelatedObject): base_model = pre_field.model else: base_model = pre_field.rel.to pre_field = base_model._meta.get_field_by_name( field_lookup)[0] model_field = pre_field else: field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] if isinstance(model_field, (DateField, DateTimeField)): form_fields.pop(k) field = RangeField(model_field.formfield) else: if not hasattr(model_field, 'formfield'): field = forms.ModelChoiceField( queryset=model_field.model.objects.all()) field.label = self.override_field_labels.get( k, base_label )( self, field ) if k in self.override_field_labels else field_lookup elif isinstance(model_field, ForeignKey): field = model_field.formfield() if self.always_show_full_username and ( model_field.rel.to == User): field.label_from_instance = self.get_user_label if self.list_filter_queryset: for query_field, query in self.list_filter_queryset.iteritems( ): if query_field == k: for variable, value in query.iteritems( ): field.queryset = field.queryset.filter( **{variable: value}) else: field = model_field.formfield() if self.list_filter_widget.has_key(k): use_widget, widget, field_class = self.check_for_widget( self.list_filter_widget, k) if use_widget: field.__class__ = field_class field.widget = widget field.choices = model_field.choices field.choices.insert(0, ('', '---------')) field.initial = '' field.label = force_unicode(_(field.label)) else: if isinstance(v, (forms.BooleanField)): form_fields.pop(k) field = forms.ChoiceField() field.label = v.label field.help_text = v.help_text field.choices = ( ('', ''), (True, _('Yes')), (False, _('No')), ) setattr(field, 'as_boolean', True) elif isinstance(v, (forms.DateField, forms.DateTimeField)): field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] form_fields.pop(k) field = RangeField(model_field.formfield) else: field = v if hasattr(field, 'choices'): if not hasattr(field, 'queryset'): if field.choices[0][0]: field.choices.insert(0, ('', '---------')) field.initial = '' # Provide a hook for updating the queryset if hasattr(field, 'queryset') and k in self.override_field_choices: field.queryset = self.override_field_choices.get(k)( self, field.queryset) form_fields[k] = field FilterFormClass = type('FilterFormBase', (FilterForm, ), {'base_fields': form_fields}) form = FilterFormClass(data=request.GET or None) form.is_valid() return form
def get_form_filter(self, request): form_fields = fields_for_model(self.model, [f for f in self.get_query_field_names() if f in self.list_filter]) if not form_fields: form_fields = { '__all__': forms.BooleanField(label='', widget=forms.HiddenInput, initial='1') } else: opts = self.model._meta for k, v in dict(form_fields).items(): if v is None: pre_field = None base_model = self.model if '__' in k: # for field_lookup in k.split("__"): for field_lookup in k.split("__")[:-1]: if pre_field: if isinstance(pre_field, ForeignObjectRel): base_model = pre_field.model else: base_model = pre_field.rel.to pre_field = base_model._meta.get_field_by_name(field_lookup)[0] model_field = pre_field else: field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] if isinstance(model_field, (DateField, DateTimeField)): form_fields.pop(k) field = RangeField(model_field.formfield) else: if not hasattr(model_field, 'formfield'): field = forms.ModelChoiceField(queryset=model_field.model.objects.all()) field.label = self.override_field_labels.get(k, base_label)(self, field) if k in self.override_field_labels else field_lookup elif isinstance(model_field, ForeignKey): field = model_field.formfield() if self.always_show_full_username and (model_field.rel.to == User): field.label_from_instance = self.get_user_label if self.list_filter_queryset: for query_field, query in self.list_filter_queryset.iteritems(): if query_field == k: for variable, value in query.iteritems(): field.queryset = field.queryset.filter(**{variable: value}) else: field = model_field.formfield() if self.list_filter_widget.has_key(k): use_widget, widget, field_class = self.check_for_widget(self.list_filter_widget, k) if use_widget: field.__class__ = field_class field.widget = widget field.choices = model_field.choices field.choices.insert(0, ('', '---------')) field.initial = '' field.label = force_unicode(_(field.label)) else: if isinstance(v, (forms.BooleanField)): form_fields.pop(k) field = forms.ChoiceField() field.label = v.label field.help_text = v.help_text field.choices = ( ('', ''), (True, _('Yes')), (False, _('No')), ) setattr(field, 'as_boolean', True) elif isinstance(v, (forms.DateField, forms.DateTimeField)): field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] form_fields.pop(k) field = RangeField(model_field.formfield) else: field = v if hasattr(field, 'choices'): if not hasattr(field, 'queryset'): if field.choices[0][0]: field.choices.insert(0, ('', '---------')) field.initial = '' # Provide a hook for updating the queryset if hasattr(field, 'queryset') and k in self.override_field_choices: field.queryset = self.override_field_choices.get(k)(self, field.queryset) form_fields[k] = field FilterFormClass = type('FilterFormBase', (FilterForm,), {'base_fields': form_fields}) form = FilterFormClass(data=request.GET or None) form.is_valid() return form
def get_form_filter(self, request): form_fields = fields_for_model( self.model, [f for f in self.get_query_field_names() if f in self.list_filter]) if not form_fields: form_fields = { '__all__': forms.BooleanField(label='', widget=forms.HiddenInput, initial='1') } else: opts = self.model._meta for k, v in dict(form_fields).items(): if v is None: field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] if isinstance(model_field, (DateField, DateTimeField)): form_fields.pop(k) form_fields[k] = RangeField(model_field.formfield) else: field = model_field.formfield() field.label = force_unicode(_(field.label)) form_fields[k] = field else: if isinstance(v, (forms.BooleanField)): form_fields.pop(k) form_fields[k] = forms.ChoiceField() form_fields[k].label = v.label form_fields[k].help_text = v.help_text form_fields[k].choices = ( ('', ''), (True, _('Yes')), (False, _('No')), ) setattr(form_fields[k], 'as_boolean', True) elif isinstance(v, (forms.DateField, forms.DateTimeField)): field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] form_fields.pop(k) form_fields[k] = RangeField(model_field.formfield) form_class = type('FilterFormBase', (forms.BaseForm, ), {'base_fields': form_fields}) class FilterForm(form_class): def _post_clean(self): pass def get_filter_kwargs(self): if not self.is_valid(): return {} filter_kwargs = dict(self.cleaned_data) for k, v in dict(filter_kwargs).items(): if k == '__all__': filter_kwargs.pop(k) continue if isinstance(v, (list, tuple)): if isinstance(self.fields[k], (RangeField)): filter_kwargs.pop(k) start_range, end_range = v if start_range: filter_kwargs['%s__gte' % k] = start_range if end_range: filter_kwargs['%s__lte' % k] = end_range elif hasattr(self.fields[k], 'as_boolean'): if v: filter_kwargs.pop(k) filter_kwargs[k] = (unicode(v) == u'True') return filter_kwargs def get_cleaned_data(self): return getattr(self, 'cleaned_data', {}) def __init__(self, *args, **kwargs): super(FilterForm, self).__init__(*args, **kwargs) self.filter_report_is_all = '__all__' in self.fields and len( self.fields) == 1 try: data_filters = {} vals = args[0] for k in vals.keys(): if k in self.fields: data_filters[k] = vals[k] for name in self.fields: for k, v in data_filters.items(): if k == name: continue field = self.fields[name] if hasattr(field, 'queryset'): qs = field.queryset if k in qs.model._meta.get_all_field_names(): field.queryset = qs.filter(Q(**{k: v})) except: pass for field in self.fields: self.fields[field].required = False if hasattr(self.fields[field], 'choices'): if not hasattr(self.fields[field], 'queryset'): if self.fields[field].choices[0][0]: self.fields[field].choices.insert( 0, ('', '---------')) self.fields[field].initial = '' form = FilterForm(data=request.GET or None) form.is_valid() return form
def get_form_filter(self, request): form_fields = fields_for_model(self.model, [f for f in self.get_query_field_names() if f in self.list_filter]) if not form_fields: form_fields = {"__all__": forms.BooleanField(label="", widget=forms.HiddenInput, initial="1")} else: opts = self.model._meta for k, v in dict(form_fields).items(): if v is None: field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] if isinstance(model_field, (DateField, DateTimeField)): form_fields.pop(k) form_fields[k] = RangeField(model_field.formfield) else: field = model_field.formfield() field.label = force_unicode(_(field.label)) form_fields[k] = field else: if isinstance(v, (forms.BooleanField)): form_fields.pop(k) form_fields[k] = forms.ChoiceField() form_fields[k].label = v.label form_fields[k].help_text = v.help_text form_fields[k].choices = (("", ""), (True, _("Yes")), (False, _("No"))) setattr(form_fields[k], "as_boolean", True) elif isinstance(v, (forms.DateField, forms.DateTimeField)): field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] form_fields.pop(k) form_fields[k] = RangeField(model_field.formfield) form_class = type("FilterFormBase", (forms.BaseForm,), {"base_fields": form_fields}) class FilterForm(form_class): def _post_clean(self): pass def get_filter_kwargs(self): if not self.is_valid(): return {} filter_kwargs = dict(self.cleaned_data) for k, v in dict(filter_kwargs).items(): if k == "__all__": filter_kwargs.pop(k) continue if isinstance(v, (list, tuple)): if isinstance(self.fields[k], (RangeField)): filter_kwargs.pop(k) start_range, end_range = v if start_range: filter_kwargs["%s__gte" % k] = start_range if end_range: filter_kwargs["%s__lte" % k] = end_range elif hasattr(self.fields[k], "as_boolean"): if v: filter_kwargs.pop(k) filter_kwargs[k] = unicode(v) == u"True" return filter_kwargs def get_cleaned_data(self): return getattr(self, "cleaned_data", {}) def __init__(self, *args, **kwargs): super(FilterForm, self).__init__(*args, **kwargs) self.filter_report_is_all = "__all__" in self.fields and len(self.fields) == 1 try: data_filters = {} vals = args[0] for k in vals.keys(): if k in self.fields: data_filters[k] = vals[k] for name in self.fields: for k, v in data_filters.items(): if k == name: continue field = self.fields[name] if hasattr(field, "queryset"): qs = field.queryset if k in qs.model._meta.get_all_field_names(): field.queryset = qs.filter(Q(**{k: v})) except: pass for field in self.fields: self.fields[field].required = False if hasattr(self.fields[field], "choices"): if not hasattr(self.fields[field], "queryset"): if self.fields[field].choices[0][0]: self.fields[field].choices.insert(0, ("", "---------")) self.fields[field].initial = "" form = FilterForm(data=request.GET or None) form.is_valid() return form
def get_form_filter(self, request): form_fields = fields_for_model( self.model, [f for f in self.get_query_field_names() if f in self.list_filter]) if not form_fields: form_fields = { '__all__': forms.BooleanField(label='', widget=forms.HiddenInput, initial='1') } else: opts = self.model._meta for k, v in dict(form_fields).items(): if v is None: pre_field = None base_model = self.model if '__' in k: for field_lookup in k.split("__")[:-1]: if pre_field: if isinstance(pre_field, RelatedObject): base_model = pre_field.model else: base_model = pre_field.rel.to pre_field = base_model._meta.get_field_by_name( field_lookup)[0] model_field = pre_field else: field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] if isinstance(model_field, (DateField, DateTimeField)): form_fields.pop(k) field = RangeField(model_field.formfield) else: if not hasattr(model_field, 'formfield'): field = forms.ModelChoiceField( queryset=model_field.model.objects.all()) field.label = self.override_field_labels.get( k, base_label )( self, field ) if k in self.override_field_labels else field_lookup elif isinstance(model_field, ForeignKey): field = model_field.formfield() if self.always_show_full_username and ( model_field.rel.to == User): field.label_from_instance = self.get_user_label if self.list_filter_queryset: for query_field, query in self.list_filter_queryset.iteritems( ): if query_field == k: for variable, value in query.iteritems( ): field.queryset = field.queryset.filter( **{variable: value}) else: field = model_field.formfield() if self.list_filter_widget.has_key(k): use_widget, widget, field_class = self.check_for_widget( self.list_filter_widget, k) if use_widget: field.__class__ = field_class field.widget = widget field.choices = model_field.choices field.choices.insert(0, ('', '---------')) field.initial = '' field.label = force_unicode(_(field.label)) else: if isinstance(v, (forms.BooleanField)): form_fields.pop(k) field = forms.ChoiceField() field.label = v.label field.help_text = v.help_text field.choices = ( ('', ''), (True, _('Yes')), (False, _('No')), ) setattr(field, 'as_boolean', True) elif isinstance(v, (forms.DateField, forms.DateTimeField)): field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] form_fields.pop(k) field = RangeField(model_field.formfield) else: field = v if hasattr(field, 'choices'): # self.override_field_filter_values if not hasattr(field, 'queryset'): if field.choices[0][0]: field.choices.insert(0, ('', '---------')) field.initial = '' # Provide a hook for updating the queryset if hasattr(field, 'queryset') and k in self.override_field_choices: field.queryset = self.override_field_choices.get(k)( self, field.queryset) form_fields[k] = field form_class = type('FilterFormBase', (forms.BaseForm, ), {'base_fields': form_fields}) class FilterForm(form_class): def _post_clean(self): pass def get_filter_kwargs(self): if not self.is_valid(): return {} filter_kwargs = dict(self.cleaned_data) for k, v in dict(filter_kwargs).items(): if not v: filter_kwargs.pop(k) continue if k == '__all__': filter_kwargs.pop(k) continue if isinstance(v, (list, tuple)): if isinstance(self.fields[k], (RangeField)): filter_kwargs.pop(k) start_range, end_range = v if start_range: filter_kwargs['%s__gte' % k] = start_range if end_range: filter_kwargs['%s__lte' % k] = end_range elif hasattr(self.fields[k], 'as_boolean'): if v: filter_kwargs.pop(k) filter_kwargs[k] = (unicode(v) == u'True') return filter_kwargs def get_cleaned_data(self): return getattr(self, 'cleaned_data', {}) def __init__(self, *args, **kwargs): super(FilterForm, self).__init__(*args, **kwargs) self.filter_report_is_all = '__all__' in self.fields and len( self.fields) == 1 try: data_filters = {} vals = args[0] for k in vals.keys(): if k in self.fields: data_filters[k] = vals[k] for name in self.fields: for k, v in data_filters.items(): if k == name: continue field = self.fields[name] if hasattr(field, 'queryset'): qs = field.queryset if k in qs.model._meta.get_all_field_names(): field.queryset = qs.filter(Q(**{k: v})) except: pass for field in self.fields: self.fields[field].required = False form = FilterForm(data=request.GET or None) form.is_valid() return form
def get_form_filter(self, request): form_fields = fields_for_model(self.model, [f for f in self.get_query_field_names() if f in self.list_filter]) opts = self.model._meta for k, v in dict(form_fields).items(): if v is None: field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] if isinstance(model_field, (DateField, DateTimeField)): form_fields.pop(k) form_fields[k] = RangeField(model_field.formfield) else: field = model_field.formfield() field.label = force_unicode(_(field.label)) form_fields[k] = field else: if isinstance(v, (forms.BooleanField)): form_fields.pop(k) form_fields[k] = forms.ChoiceField() form_fields[k].label = v.label form_fields[k].help_text = v.help_text form_fields[k].choices = ( ('', ''), (True, _('Yes')), (False, _('No')), ) setattr(form_fields[k], 'as_boolean', True) elif isinstance(v, (forms.DateField, forms.DateTimeField)): field_name = k.split("__")[0] model_field = opts.get_field_by_name(field_name)[0] form_fields.pop(k) form_fields[k] = RangeField(model_field.formfield) form_class = type('FilterFormBase', (forms.BaseForm,), {'base_fields': form_fields}) class FilterForm(form_class): def _post_clean(self): pass def get_filter_kwargs(self): if not self.is_valid(): return {} filter_kwargs = dict(self.cleaned_data) for k, v in dict(filter_kwargs).items(): if isinstance(v, (list, tuple)): if isinstance(self.fields[k], (RangeField)): filter_kwargs.pop(k) start_range, end_range = v if start_range: filter_kwargs['%s__gte' % k] = start_range if end_range: filter_kwargs['%s__lte' % k] = end_range elif hasattr(self.fields[k], 'as_boolean'): if v: filter_kwargs.pop(k) filter_kwargs[k] = (unicode(v) == u'True') return filter_kwargs def get_cleaned_data(self): return getattr(self, 'cleaned_data', {}) def __init__(self, *args, **kwargs): super(FilterForm, self).__init__(*args, **kwargs) try: data_filters = {} vals = args[0] for k in vals.keys(): if k in self.fields: data_filters[k] = vals[k] for name in self.fields: for k, v in data_filters.items(): if k == name: continue field = self.fields[name] if hasattr(field, 'queryset'): qs = field.queryset if k in qs.model._meta.get_all_field_names(): field.queryset = qs.filter(Q(**{k: v})) except: pass for field in self.fields: self.fields[field].required = False if hasattr(self.fields[field], 'choices'): if not hasattr(self.fields[field], 'queryset'): if self.fields[field].choices[0][0]: self.fields[field].choices.insert(0, ('', '---------')) self.fields[field].initial = '' form = FilterForm(data=request.GET or None) form.is_valid() return form