Example #1
0
 def test_related_name(self):
     """
     Regression test for #13963
     """
     self.assertEqual(
         label_for_field('location', Event, return_attr=True),
         ('location', None),
     )
     self.assertEqual(
         label_for_field('event', Location, return_attr=True),
         ('awesome event', None),
     )
     self.assertEqual(
         label_for_field('guest', Event, return_attr=True),
         ('awesome guest', None),
     )
Example #2
0
 def fields(self):
     fk = getattr(self.formset, "fk", None)
     for i, field in enumerate(flatten_fieldsets(self.fieldsets)):
         if fk and fk.name == field:
             continue
         if field in self.readonly_fields:
             yield {
                 'label': label_for_field(field, self.opts.model, self.opts),
                 'widget': {
                     'is_hidden': False
                 },
                 'required': False
             }
         else:
             yield self.formset.form.base_fields[field]
Example #3
0
 def __init__(self, form, field, is_first, model_admin=None):
     label = label_for_field(field, form._meta.model, model_admin)
     # Make self.field look a little bit like a field. This means that
     # {{ field.name }} must be a useful class name to identify the field.
     # For convenience, store other field-related data here too.
     if callable(field):
         class_name = field.__name__ != '<lambda>' and field.__name__ or ''
     else:
         class_name = field
     self.field = {
         'name': class_name,
         'label': label,
         'field': field,
         'help_text': help_text_for_field(class_name, form._meta.model)
     }
     self.form = form
     self.model_admin = model_admin
     self.is_first = is_first
     self.is_checkbox = False
     self.is_readonly = True
Example #4
0
    def test_label_for_field(self):
        """
        Tests for label_for_field
        """
        self.assertEqual(
            label_for_field("title", Article),
            "title"
        )
        self.assertEqual(
            label_for_field("title2", Article),
            "another name"
        )
        self.assertEqual(
            label_for_field("title2", Article, return_attr=True),
            ("another name", None)
        )

        self.assertEqual(
            label_for_field("__unicode__", Article),
            "article"
        )
        self.assertEqual(
            label_for_field("__str__", Article),
            str("article")
        )

        self.assertRaises(
            AttributeError,
            lambda: label_for_field("unknown", Article)
        )

        def test_callable(obj):
            return "nothing"
        self.assertEqual(
            label_for_field(test_callable, Article),
            "Test callable"
        )
        self.assertEqual(
            label_for_field(test_callable, Article, return_attr=True),
            ("Test callable", test_callable)
        )

        self.assertEqual(
            label_for_field("test_from_model", Article),
            "Test from model"
        )
        self.assertEqual(
            label_for_field("test_from_model", Article, return_attr=True),
            ("Test from model", Article.test_from_model)
        )
        self.assertEqual(
            label_for_field("test_from_model_with_override", Article),
            "not What you Expect"
        )

        self.assertEqual(
            label_for_field(lambda x: "nothing", Article),
            "--"
        )

        class MockModelAdmin(object):
            def test_from_model(self, obj):
                return "nothing"
            test_from_model.short_description = "not Really the Model"

        self.assertEqual(
            label_for_field("test_from_model", Article, model_admin=MockModelAdmin),
            "not Really the Model"
        )
        self.assertEqual(
            label_for_field("test_from_model", Article,
                model_admin = MockModelAdmin,
                return_attr = True
            ),
            ("not Really the Model", MockModelAdmin.test_from_model)
        )
Example #5
0
def result_headers(cl):
    """
    Generates the list column headers.
    """
    ordering_field_columns = cl.get_ordering_field_columns()
    for i, field_name in enumerate(cl.list_display):
        text, attr = label_for_field(field_name, cl.model, model_admin=cl.model_admin, return_attr=True)
        if attr:
            # Potentially not sortable

            # if the field is the action checkbox: no sorting and special class
            if field_name == "action_checkbox":
                yield {"text": text, "class_attrib": mark_safe(' class="action-checkbox-column"'), "sortable": False}
                continue

            admin_order_field = getattr(attr, "admin_order_field", None)
            if not admin_order_field:
                # Not sortable
                yield {"text": text, "sortable": False}
                continue

        # OK, it is sortable if we got this far
        th_classes = ["sortable"]
        order_type = ""
        new_order_type = "asc"
        sort_priority = 0
        sorted = False
        # Is it currently being sorted on?
        if i in ordering_field_columns:
            sorted = True
            order_type = ordering_field_columns.get(i).lower()
            sort_priority = list(ordering_field_columns).index(i) + 1
            th_classes.append("sorted %sending" % order_type)
            new_order_type = {"asc": "desc", "desc": "asc"}[order_type]

        # build new ordering param
        o_list_primary = []  # URL for making this field the primary sort
        o_list_remove = []  # URL for removing this field from sort
        o_list_toggle = []  # URL for toggling order type for this field
        make_qs_param = lambda t, n: ("-" if t == "desc" else "") + str(n)

        for j, ot in ordering_field_columns.items():
            if j == i:  # Same column
                param = make_qs_param(new_order_type, j)
                # We want clicking on this header to bring the ordering to the
                # front
                o_list_primary.insert(0, param)
                o_list_toggle.append(param)
                # o_list_remove - omit
            else:
                param = make_qs_param(ot, j)
                o_list_primary.append(param)
                o_list_toggle.append(param)
                o_list_remove.append(param)

        if i not in ordering_field_columns:
            o_list_primary.insert(0, make_qs_param(new_order_type, i))

        yield {
            "text": text,
            "sortable": True,
            "sorted": sorted,
            "ascending": order_type == "asc",
            "sort_priority": sort_priority,
            "url_primary": cl.get_query_string({ORDER_VAR: ".".join(o_list_primary)}),
            "url_remove": cl.get_query_string({ORDER_VAR: ".".join(o_list_remove)}),
            "url_toggle": cl.get_query_string({ORDER_VAR: ".".join(o_list_toggle)}),
            "class_attrib": format_html(' class="{0}"', " ".join(th_classes)) if th_classes else "",
        }