Ejemplo n.º 1
0
    def _get_header_order_by(self, model, full_field_name):
        if "__" in full_field_name:
            prefix, field_name = full_field_name.rsplit("__", 1)
        else:
            prefix, field_name = "", full_field_name

        try:
            field = model._meta.get_field(field_name)
            default_order_by = full_field_name
        except FieldDoesNotExist:
            field = get_class_method(model, field_name)
            default_order_by = False

        order_by = getattr(field, "order_by", None)
        if order_by:
            return "__".join(filter(None, (prefix, order_by)))

        if order_by is None:
            return default_order_by

        return order_by
Ejemplo n.º 2
0
def get_model_field_or_method_filter(full_field_term, model, value=None, filter_term=None):
    if not filter_term:
        filter_term = full_field_term

    next_filter_term = None
    current_filter_term = filter_term

    if '__' in filter_term:
        current_filter_term, next_filter_term = filter_term.split('__', 1)

    field_or_method = None
    try:
        field_or_method = model._meta.get_field(current_filter_term)

        if (next_filter_term and next_filter_term not in field_or_method.filter.get_suffixes()
            and isinstance(field_or_method, RelatedField)):
            return get_model_field_or_method_filter(filter_term, model._meta.get_field(current_filter_term).rel.to,
                                                    value, next_filter_term)

    except FieldDoesNotExist:
        try:
            field_or_method = get_class_method(model, current_filter_term)
            if hasattr(field_or_method, 'filter_by'):
                full_field_term = full_field_term[:-len(current_filter_term)] + field_or_method.filter_by
                current_filter_term = field_or_method.filter_by
                filter_term = field_or_method.filter_by
                field_or_method = model._meta.get_field(field_or_method.filter_by)
        except (AttributeError, FieldDoesNotExist):
            raise FilterException(_('Not valid filter: %s') % full_field_term)

    if (hasattr(field_or_method, 'filter') and
        (not next_filter_term or next_filter_term in field_or_method.filter.get_suffixes()) and
        field_or_method.filter):
        return field_or_method.filter(filter_term, full_field_term, field_or_method, value)
    else:
        raise FilterException(_('Not valid filter: %s') % full_field_term)
Ejemplo n.º 3
0
 def _get_header_label(self, model, field_name):
     try:
         return model._meta.get_field(field_name).verbose_name
     except FieldDoesNotExist:
         method = get_class_method(model, field_name)
         return getattr(method, "short_description", pretty_name(field_name))