コード例 #1
0
def _field_actions_for_field(field, actions):
    label = capitalize_first(getattr(field, "verbose_name", field.name))

    for field_action_type, items in actions.items():
        if field.many_to_many or field.many_to_one or field.one_to_one:
            # convert item values from primary keys to string-representation for relation-based fields
            related_objects = field.related_model.objects.filter(pk__in=items)
            missing = len(items) - related_objects.count()
            items = [str(obj) for obj in related_objects] + [_("<deleted object>")] * missing
        elif hasattr(field, "choices") and field.choices:
            # convert values from choice-based fields to their display equivalent
            items = [_choice_to_display(field, item) for item in items]
        elif isinstance(field, models.BooleanField):
            # convert boolean to yes/no
            items = list(map(yesno, items))
        yield FieldAction(label, field_action_type, items)
コード例 #2
0
    def message(self):
        if self.action_type == InstanceActionType.CHANGE:
            if self.content_object:
                message = _("The {cls} {obj} was changed.")
            else:  # content_object might be deleted
                message = _("A {cls} was changed.")
        elif self.action_type == InstanceActionType.CREATE:
            if self.content_object:
                message = _("The {cls} {obj} was created.")
            else:
                message = _("A {cls} was created.")
        elif self.action_type == InstanceActionType.DELETE:
            message = _("A {cls} was deleted.")

        return message.format(
            cls=capitalize_first(self.content_type.model_class()._meta.verbose_name),
            obj=f'"{str(self.content_object)}"' if self.content_object else "",
        )