예제 #1
0
 def get_query_help_message(self):
     if not self.search_fields:
         return ''
     fields = []
     for search_field in self.search_fields:
         field = get_field_from_path(self.model, search_field)
         if field is None:
             raise ImproperlyConfigured("cannot access to `%s` from %s " % (search_field, self.model))
         fields.append(field.verbose_name.title())
     return ",".join(fields)
예제 #2
0
def reportform_factory(report, bases=(ConfigurationForm, ), **kwargs):
    name = "%sFilterForm" % report.__class__.__name__
    attrs = {}
    field_display_order = []
    for fieldpath in kwargs.get('filtering', getattr(report, 'list_filter',
                                                     [])):
        if '=' not in fieldpath:
            field = get_field_from_path(report.model, fieldpath)
            if field:
                field_name = fieldpath.replace('.', '__')
                field_display_order.append(field_name)
                attrs[field_name] = get_field_from_bases(
                    bases, field_name, field.formfield(required=False))
            elif fieldpath.startswith('_extras_'):
                field_display_order.append(fieldpath)
                attrs[fieldpath] = get_field_from_bases(bases, fieldpath, None)

    choice_field_class = getattr(report, 'choice_field_class',
                                 None) or ChoiceField

    orders = kwargs.get('order_by', None)
    if orders:
        attrs['_report_order_by'] = choice_field_class(label='Order by',
                                                       choices=orders)
        field_display_order.append('_report_order_by')

    groupby = kwargs.get('groupby', None)
    if groupby:
        attrs['_report_group_by'] = choice_field_class(label='Group by',
                                                       choices=groupby)
        field_display_order.append('_report_group_by')
    else:
        groupby = get_field_from_bases(bases, '_report_group_by', None)
        if groupby:
            attrs['_report_group_by'] = groupby
            field_display_order.append('_report_group_by')

    # add report format options
    if report.formats:
        formats = [(f[0], f[0]) for f in report.formats]
        attrs['_format'] = choice_field_class(label='Format', choices=formats)
        field_display_order.append('_format')

    configure_columns = kwargs.get('configure_columns', None)
    if configure_columns or get_attr_from_bases(bases, 'configure_columns',
                                                False):
        columns_choice_field_class = getattr(report, 'columns_choice_field',
                                             ColumnsChoiceField)
        attrs['_report_list_display'] = columns_choice_field_class(report)
        field_display_order.append('_report_list_display')

    attrs['_report'] = report
    attrs['_field_display_order'] = field_display_order

    return type(name, bases, attrs)
예제 #3
0
 def get_query_help_message(self):
     if not self.search_fields:
         return ''
     fields = []
     for search_field in self.search_fields:
         field = get_field_from_path(self.model, search_field)
         if field is None:
             raise ImproperlyConfigured("cannot access to `%s` from %s " %
                                        (search_field, self.model))
         fields.append(field.verbose_name.title())
     return ",".join(fields)
예제 #4
0
def reportform_factory(report, bases=(ConfigurationForm,), **kwargs):
    name = "%sFilterForm" % report.__class__.__name__
    attrs = {}
    field_display_order = []
    for fieldpath in kwargs.get('filtering', getattr(report, 'list_filter', [])):
        if '=' not in fieldpath:
            field = get_field_from_path(report.model, fieldpath)
            if field:
                field_name = fieldpath.replace('.', '__')
                field_display_order.append(field_name)
                attrs[field_name] = get_field_from_bases(bases, field_name, field.formfield(required=False))
            elif fieldpath.startswith('_extras_'):
                field_display_order.append(fieldpath)
                attrs[fieldpath] = get_field_from_bases(bases, fieldpath, None)

    choice_field_class = getattr(report, 'choice_field_class', None) or ChoiceField

    orders = kwargs.get('order_by', None)
    if orders:
        attrs['_report_order_by'] = choice_field_class(label='Order by', choices=orders)
        field_display_order.append('_report_order_by')

    groupby = kwargs.get('groupby', None)
    if groupby:
        attrs['_report_group_by'] = choice_field_class(label='Group by', choices=groupby)
        field_display_order.append('_report_group_by')
    else:
        groupby = get_field_from_bases(bases, '_report_group_by', None)
        if groupby:
            attrs['_report_group_by'] = groupby
            field_display_order.append('_report_group_by')

    # add report format options
    if report.formats:
        formats = [(f[0], f[0]) for f in report.formats]
        attrs['_format'] = choice_field_class(
            label='Format',
            choices=formats
        )
        field_display_order.append('_format')

    configure_columns = kwargs.get('configure_columns', None)
    if configure_columns or get_attr_from_bases(bases, 'configure_columns', False):
        columns_choice_field_class = getattr(report, 'columns_choice_field', ColumnsChoiceField)
        attrs['_report_list_display'] = columns_choice_field_class(report)
        field_display_order.append('_report_list_display')

    attrs['_report'] = report
    attrs['_field_display_order'] = field_display_order

    return type(name, bases, attrs)
예제 #5
0
def get_column_for_attribute(model, fieldpath):
    """
        returns the more suitable Column class for `attr`.

    :param: can be a Django Field, attribute or callable
    :return:
    """
    attr = get_field_from_path(model, fieldpath)
    mapping = {
        models.IntegerField: IntegerColumn,
        models.DecimalField: DecimalColumn,
        models.DateField: DateColumn,
        models.CharField: CharColumn,
        models.BooleanField: BooleanColumn,
    }
    return mapping.get(type(attr), Column)
예제 #6
0
def get_column_for_attribute(model, fieldpath):
    """
        returns the more suitable Column class for `attr`.

    :param: can be a Django Field, attribute or callable
    :return:
    """
    attr = get_field_from_path(model, fieldpath)
    mapping = {
        models.IntegerField: IntegerColumn,
        models.DecimalField: DecimalColumn,
        models.DateField: DateColumn,
        models.CharField: CharColumn,
        models.BooleanField: BooleanColumn,
    }
    return mapping.get(type(attr), Column)