-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix non uid lookup for pages #138
base: main
Are you sure you want to change the base?
Changes from all commits
58ce04f
6cccf71
9ac5966
e0c1a29
89fdf70
8974f04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,17 @@ def test_get(self): | |
self.assertEqual(response.status_code, 200) | ||
self.assertContains(response, 'data-wagtail-component="content-import-form"') | ||
|
||
class TestCheckUIDView(TestCase): | ||
fixtures = ['test.json'] | ||
|
||
def setUp(self): | ||
self.client.login(username='admin', password='password') | ||
|
||
def test_get(self): | ||
with self.settings(WAGTAILTRANSFER_LOOKUP_FIELDS = {'wagtailcore.page': ['slug', 'locale_id'],}): | ||
# the view should parse comma-seperated params correctly | ||
response = self.client.get('/admin/wagtail-transfer/api/check_uid/?uid=home,1') | ||
self.assertEqual(response.status_code, 200) | ||
|
||
@mock.patch('requests.post') | ||
@mock.patch('requests.get') | ||
|
@@ -383,6 +394,7 @@ def _test_view(self, method, url, data=None, success_url=None): | |
with self.subTest(user=user): | ||
self.client.login(username=user.username, password="password") | ||
request = getattr(self.client, method) | ||
breakpoint() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like an accidental addition? |
||
response = request(url, data) | ||
if success_url: | ||
self.assertRedirects(response, success_url) | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -23,9 +23,12 @@ | |||
'taggit.tag': ['slug'], # sensible default for taggit; can still be overridden | ||||
'wagtailcore.locale': ["language_code"] | ||||
} | ||||
for model_label, fields in getattr(settings, 'WAGTAILTRANSFER_LOOKUP_FIELDS', {}).items(): | ||||
LOOKUP_FIELDS[model_label.lower()] = fields | ||||
|
||||
def get_lookup_fields(): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth caching this as well? We'll work it out once per locator we look up, which feels a little redundant |
||||
""" get all fields for lookup, including those declared in settings """ | ||||
lookup_fields = LOOKUP_FIELDS.copy() | ||||
for model_label, fields in getattr(settings, 'WAGTAILTRANSFER_LOOKUP_FIELDS', {}).items(): | ||||
lookup_fields[model_label.lower()] = fields | ||||
return lookup_fields | ||||
|
||||
class IDMappingLocator: | ||||
def __init__(self, model): | ||||
|
@@ -122,12 +125,19 @@ def uid_from_json(self, json_uid): | |||
# A UID coming from JSON data will arrive as a list (because JSON has no tuple type), | ||||
# but we need a tuple because the importer logic expects a hashable type that we can use | ||||
# in sets and dict keys | ||||
if type(json_uid) is str: | ||||
json_uid = [json_uid] | ||||
return tuple(json_uid) | ||||
|
||||
def find(self, uid): | ||||
# pair up field names with their respective items in the UID tuple, to form a filter dict | ||||
# that we can use for an ORM lookup | ||||
filters = dict(zip(self.fields, uid)) | ||||
if type(uid) == tuple: | ||||
filters = dict(zip(self.fields, uid)) | ||||
elif type(uid) == str: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the last PR, I'm not sure whether this is the right approach - it feels like this method should accept a single type, and the caller (or another method, like we use when we get it from JSON with wagtail-transfer/wagtail_transfer/static_src/components/ContentImportForm/index.js Line 150 in 8974f04
request.GET.getlist() suggestion failed - we're not using the typical param=a¶m=b GET syntax for passing a list. We could fix that and use getlist in the view, or else we could do the conversion to tuple in the view/in another method like uid_from_json . But just like the JSON case isn't in the main function, I don't think the list case should be either.
|
||||
# if lookup fields are configured for wagtailcore.page, then in the admin view those | ||||
# fields get passed along as a string | ||||
filters = dict(zip(self.fields, uid.split(","))) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the lookup field value contains a ","? |
||||
|
||||
try: | ||||
return self.model.objects.get(**filters) | ||||
|
@@ -138,9 +148,10 @@ def find(self, uid): | |||
@lru_cache(maxsize=None) | ||||
def get_locator_for_model(model): | ||||
base_model = get_base_model(model) | ||||
lookup_fields = get_lookup_fields() | ||||
try: | ||||
# Use FieldLocator if an entry exists in LOOKUP_FIELDS | ||||
fields = LOOKUP_FIELDS[base_model._meta.label_lower] | ||||
fields = lookup_fields[base_model._meta.label_lower] | ||||
return FieldLocator(base_model, fields) | ||||
except KeyError: | ||||
# Fall back on IDMappingLocator | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: "interferes" and "effectively" typos