Remove the NoNaturalKey error

It's too awkward, and it makes more sense to return None instead.
This commit is contained in:
Jeff Bradberry 2020-04-02 10:49:59 -04:00
parent e053a58223
commit ab15349c8c
3 changed files with 15 additions and 13 deletions

View File

@ -319,14 +319,16 @@ class Page(object):
def get_natural_key(self):
if not getattr(self, 'NATURAL_KEY', None):
raise exc.NoNaturalKey(
"Page does not have a natural key: {}".format(getattr(self, 'endpoint', repr(self.__class__)))
)
return None
natural_key = {}
for key in self.NATURAL_KEY:
if key in self.related:
# FIXME: use caching by url
natural_key[key] = self.related[key].get().get_natural_key()
try:
# FIXME: use caching by url
natural_key[key] = self.related[key].get().get_natural_key()
except exc.Forbidden:
return None
elif key in self:
natural_key[key] = self[key]
if not natural_key:
@ -395,7 +397,7 @@ class PageList(object):
return self.__item_class__(self.connection).create(*a, **kw)
def get_natural_key(self):
raise exc.NoNaturalKey
return None
class TentativePage(str):

View File

@ -1,4 +1,6 @@
from awxkit.api.resources import resources
import awxkit.exceptions as exc
from . import base
from . import page
@ -14,8 +16,11 @@ class Role(base.Base):
if name not in ('users', 'teams')
]
if related_objs:
# FIXME: use caching by url
natural_key['content_object'] = related_objs[0].get().get_natural_key()
try:
# FIXME: use caching by url
natural_key['content_object'] = related_objs[0].get().get_natural_key()
except exc.Forbidden:
return None
return natural_key

View File

@ -106,8 +106,3 @@ class IsMigrating(Common):
class ImportExportError(Exception):
pass
class NoNaturalKey(ImportExportError):
pass