__package__ = 'archivebox.abid_utils'
from django.contrib import admin
from datetime import datetime
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from abid_utils.abid import abid_part_from_ts, abid_part_from_uri, abid_part_from_rand, abid_part_from_subtype
from api.auth import get_or_create_api_token
from ..util import parse_date
def highlight_diff(display_val, compare_val):
"""highlight each character in red that differs with the char at the same index in compare_val"""
display_val = str(display_val)
compare_val = str(compare_val)
return mark_safe(''.join(
format_html('{}', display_val[i])
if display_val[i] != compare_val[i] else
format_html('{}', display_val[i])
for i in range(len(display_val))
))
def get_abid_info(self, obj, request=None):
try:
abid_diff = f' != obj.ABID: {highlight_diff(obj.ABID, obj.abid)} ❌' if str(obj.ABID) != str(obj.abid) else ' == .ABID ✅'
fresh_abid = obj.ABID
fresh_abid_diff = f' != .fresh_abid: {highlight_diff(fresh_abid, obj.ABID)} ❌' if str(fresh_abid) != str(obj.ABID) else '✅'
fresh_uuid_diff = f' != .fresh_uuid: {highlight_diff(fresh_abid.uuid, obj.ABID.uuid)} ❌' if str(fresh_abid.uuid) != str(obj.ABID.uuid) else '✅'
id_fresh_abid_diff = f' != .fresh_abid ❌' if str(fresh_abid.uuid) != str(obj.id) else ' == .fresh_abid ✅'
id_abid_diff = f' != .abid.uuid: {highlight_diff(obj.ABID.uuid, obj.id)} ❌' if str(obj.id) != str(obj.ABID.uuid) else ' == .abid ✅'
id_pk_diff = f' != .pk: {highlight_diff(obj.pk, obj.id)} ❌' if str(obj.pk) != str(obj.id) else ' == .pk ✅'
fresh_ts = parse_date(obj.ABID_FRESH_VALUES['ts']) or None
derived_ts = abid_part_from_ts(fresh_ts) if fresh_ts else None
ts_diff = f'!= {highlight_diff(derived_ts, obj.ABID.ts)} ❌' if derived_ts != obj.ABID.ts else '✅'
derived_uri = abid_part_from_uri(obj.ABID_FRESH_VALUES['uri'])
uri_diff = f'!= {highlight_diff(derived_uri, obj.ABID.uri)} ❌' if derived_uri != obj.ABID.uri else '✅'
derived_subtype = abid_part_from_subtype(obj.ABID_FRESH_VALUES['subtype'])
subtype_diff = f'!= {highlight_diff(derived_subtype, obj.ABID.subtype)} ❌' if derived_subtype != obj.ABID.subtype else '✅'
derived_rand = abid_part_from_rand(obj.ABID_FRESH_VALUES['rand'])
rand_diff = f'!= {highlight_diff(derived_rand, obj.ABID.rand)} ❌' if derived_rand != obj.ABID.rand else '✅'
# any_abid_discrepancies = any(
# '❌' in diff or '!=' in diff
# for diff in (abid_diff, fresh_abid_diff, id_abid_diff, id_pk_diff, ts_diff, uri_diff, subtype_diff, rand_diff)
# )
# total_diff = f' != .generate_abid() -> {fresh_abid} ❌' if any_abid_discrepancies else '✅'
return format_html(
# URL Hash: {}
'''
{} 📖 API DOCS
{}
{}{}
{}{}
{}{} {}
{}
{}: {}
{} {}
{}
{} {}
{}
{}: {}
{} {}
{}
{}: {}
{}