fix abid calculation

This commit is contained in:
Nick Sweeting 2024-05-17 20:11:00 -07:00
parent fdf6f465db
commit a1afd0211f
No known key found for this signature in database
5 changed files with 14 additions and 10 deletions

View file

@ -108,9 +108,12 @@ def uri_hash(uri: Union[str, bytes]) -> str:
# only hash the domain part of URLs # only hash the domain part of URLs
if '://' in uri_str: if '://' in uri_str:
domain = urlparse(uri_str).host try:
if domain: domain = urlparse(uri_str).netloc
url_str = domain if domain:
uri_str = domain
except AttributeError:
pass
uri_bytes = uri_str.encode('utf-8') uri_bytes = uri_str.encode('utf-8')

View file

@ -82,11 +82,11 @@ class ABIDModel(models.Model):
def save(self, *args: Any, **kwargs: Any) -> None: def save(self, *args: Any, **kwargs: Any) -> None:
if hasattr(self, 'abid'): if hasattr(self, 'abid'):
# self.abid = ABID.parse(self.abid) if self.abid else self.calculate_abid() # self.abid = ABID.parse(self.abid) if self.abid else self.get_abid()
self.abid = self.calculate_abid() self.abid = self.get_abid()
else: else:
print(f'[!] WARNING: {self.__class__.__name__}.abid is not a DB field so ABID will not be persisted!') print(f'[!] WARNING: {self.__class__.__name__}.abid is not a DB field so ABID will not be persisted!')
self.abid = self.calculate_abid() self.abid = self.get_abid()
super().save(*args, **kwargs) super().save(*args, **kwargs)
@ -141,7 +141,7 @@ class ABIDModel(models.Model):
""" """
ULIDParts(timestamp='01HX9FPYTR', url='E4A5CCD9', subtype='00', randomness='ZYEBQE') ULIDParts(timestamp='01HX9FPYTR', url='E4A5CCD9', subtype='00', randomness='ZYEBQE')
""" """
return ABID.parse(self.abid) if getattr(self, 'abid', None) else self.calculate_abid() return ABID.parse(self.abid) if getattr(self, 'abid', None) else self.get_abid()
@property @property
def ULID(self) -> ULID: def ULID(self) -> ULID:
@ -274,7 +274,7 @@ def find_obj_from_abid_rand(rand: Union[ABID, str], model=None) -> List[ABIDMode
) )
for obj in qs: for obj in qs:
if obj.calculate_abid() == abid: if obj.get_abid() == abid:
# found exact match, no need to keep iterating # found exact match, no need to keep iterating
return [obj] return [obj]
partial_matches.append(obj) partial_matches.append(obj)

View file

@ -56,7 +56,7 @@ class APIToken(ABIDModel):
return { return {
"TYPE": "APIToken", "TYPE": "APIToken",
"uuid": str(self.id), "uuid": str(self.id),
"abid": str(self.calculate_abid()), "abid": str(self.get_abid()),
"user_id": str(self.user.id), "user_id": str(self.user.id),
"user_username": self.user.username, "user_username": self.user.username,
"token": self.token, "token": self.token,

View file

@ -37,7 +37,7 @@ from sqlite3 import dbapi2 as sqlite3
from hashlib import md5 from hashlib import md5
from pathlib import Path from pathlib import Path
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Optional, Type, Tuple, Dict, Union, List from typing import Optional, Type, Tuple, Dict, Union, List, Any
from subprocess import run, PIPE, DEVNULL from subprocess import run, PIPE, DEVNULL
from configparser import ConfigParser from configparser import ConfigParser
from collections import defaultdict from collections import defaultdict

View file

@ -10,6 +10,7 @@ from pathlib import Path
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from ..config import ( from ..config import (
CONFIG,
DEBUG, DEBUG,
SECRET_KEY, SECRET_KEY,
ALLOWED_HOSTS, ALLOWED_HOSTS,