Example #1
0
 def resolve_action(parent, _info):
     if isinstance(parent, CreateDiff):
         return tm("created")
     elif isinstance(parent, DeleteDiff):
         return tm("deleted")
     else:
         return tm("edited")
Example #2
0
    def _diffs(self):
        prev_db_value = self.previous_version.serializable_value(
            self.field.name)
        current_db_value = self.current_version.serializable_value(
            self.field.name)

        if self.field.choices:
            # if a field is a choice field e.g. chars or ints used to represent a list of choices,
            # then its value is just that database, non-bilingual char/int value
            # fortunately model instances provide a hook to get the displayed, translated value
            choices_by_attr_value = dict(self.field.choices)
            if not choices_by_attr_value.get(None, None):
                choices_by_attr_value[None] = tm("empty")
            previous_value = choices_by_attr_value.get(prev_db_value,
                                                       prev_db_value)
            current_value = choices_by_attr_value.get(current_db_value,
                                                      current_db_value)

        else:  # just use the normal attribute
            previous_value = prev_db_value
            current_value = current_db_value

        joint, before, after = text_compare_inline(
            get_str_val(previous_value),
            get_str_val(current_value),
        )

        return (joint, before, after)
Example #3
0
def get_str_val(fetched_field_value):
    if isinstance(fetched_field_value, str):
        return escape(fetched_field_value)
    elif fetched_field_value in (None, ""):
        return escape(tm("empty"))
    if isinstance(fetched_field_value, object):
        return escape(fetched_field_value.__str__())
Example #4
0
def foreign_key_display_value(version, field_obj, dataloader_cache):
    related_id = version.serializable_value(field_obj.name)
    if related_id is None:
        return tm("empty")
    related_model = field_obj.related_model
    related_dataloader_cls = PrimaryKeyDataLoaderFactory.get_model_by_id_loader(
        related_model)
    related_dataloader = related_dataloader_cls(dataloader_cache)

    related_instance = yield related_dataloader.load(related_id)
    return get_str_val(related_instance)
Example #5
0
def get_display_value(version, field_obj, dataloader_cache):
    if isinstance(field_obj, ManyToManyField):
        return m2m_display_value(version, field_obj, dataloader_cache)

    if isinstance(field_obj, ForeignKey):
        return foreign_key_display_value(version, field_obj, dataloader_cache)

    if field_obj.choices:
        db_value = version.serializable_value(field_obj.name)
        choices_by_attr_value = dict(field_obj.choices)
        if not choices_by_attr_value.get(None, None):
            choices_by_attr_value[None] = tm("empty")
        str_val = choices_by_attr_value.get(db_value, db_value)
        return get_str_val(str_val)

    else:
        # use normal attribute
        val = getattr(version, field_obj.name)
        if not val:
            return tm("empty")
        return val
Example #6
0
def m2m_display_value(version, field_obj, dataloader_cache):
    id_list = version.get_m2m_ids(field_obj.name)
    if not id_list:
        return tm("empty")
    related_model = field_obj.related_model
    related_dataloader_cls = PrimaryKeyDataLoaderFactory.get_model_by_id_loader(
        related_model)
    related_dataloader = related_dataloader_cls(dataloader_cache)
    related_instances = yield related_dataloader.load_many(id_list)

    sorted_names = sorted([inst.name for inst in related_instances])
    return "".join([f"<p>{name}</p>" for name in sorted_names])
Example #7
0
 def _get_deleted_str(self):
     return escape(tm("deleted"))
Example #8
0
 def _get_created_str(self):
     return escape(tm("created"))