Skip to content
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

Check uid type in Field Locator and handle if str #135

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
WAGTAILTRANSFER_UPDATE_RELATED_MODELS = ['wagtailimages.Image', 'tests.advert']

WAGTAILTRANSFER_LOOKUP_FIELDS = {
'tests.category': ['name']
'tests.category': ['name'],
'wagtailcore.page': ['slug', 'locale_id'],
}

# The default name for the Page -> Comment relation from Wagtail 2.15 onward. Setting this ensures that
Expand Down
100 changes: 100 additions & 0 deletions tests/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@ def test_import_model(self):
cats = Category.objects.all()
self.assertEquals(cats.count(), 2)

def test_import_page_via_lookup(self):

data = """{
"ids_for_import": [
["wagtailcore.page", 12]
],
"mappings": [
["wagtailcore.page", 12, "/home/"]
],
"objects": [
{
"model": "tests.simplepage",
"pk": 12,
"parent_id": 1,
"fields": {
"title": "New home",
"show_in_menus": false,
"live": true,
"slug": "home",
"intro": "This is the updated homepage",
"wagtail_admin_comments": []
}
}
]
}"""

importer = ImportPlanner(model="tests.category")
importer.add_json(data)
importer.run()
# add assertions



def test_import_pages(self):
# make a draft edit to the homepage
Expand Down Expand Up @@ -134,6 +166,73 @@ def test_import_pages(self):
created_page_revision = created_page.get_latest_revision_as_page()
self.assertEqual(created_page_revision.intro, "This page is imported from the source site")

def test_import_pages_via_lookup(self):
# make a draft edit to the homepage
home = SimplePage.objects.get(slug='home')
home.title = "Draft home"
home.save_revision()

data = """{
"ids_for_import": [
["wagtailcore.page", 12],
["wagtailcore.page", 15]
],
"mappings": [
["wagtailcore.page", 12, "/home/"],
["wagtailcore.page", 15, "/home/imported-child-page"]
],
"objects": [
{
"model": "tests.simplepage",
"pk": 15,
"parent_id": 12,
"fields": {
"title": "Imported child page",
"show_in_menus": false,
"live": true,
"slug": "imported-child-page",
"intro": "This page is imported from the source site",
"wagtail_admin_comments": []
}
},
{
"model": "tests.simplepage",
"pk": 12,
"parent_id": 1,
"fields": {
"title": "New home",
"show_in_menus": false,
"live": true,
"slug": "home",
"intro": "This is the updated homepage",
"wagtail_admin_comments": []
}
}
]
}"""

importer = ImportPlanner(root_page_source_pk=12, destination_parent_id=None)
importer.add_json(data)
importer.run()

updated_page = SimplePage.objects.get(url_path='/home/')
self.assertEqual(updated_page.intro, "This is the updated homepage")
self.assertEqual(updated_page.title, "New home")
self.assertEqual(updated_page.draft_title, "New home")

# get_latest_revision (as used in the edit-page view) should also reflect the imported content
updated_page_revision = updated_page.get_latest_revision_as_page()
self.assertEqual(updated_page_revision.intro, "This is the updated homepage")
self.assertEqual(updated_page_revision.title, "New home")

created_page = SimplePage.objects.get(url_path='/home/imported-child-page/')
self.assertEqual(created_page.intro, "This page is imported from the source site")
# An initial page revision should also be created
self.assertTrue(created_page.get_latest_revision())
created_page_revision = created_page.get_latest_revision_as_page()
self.assertEqual(created_page_revision.intro, "This page is imported from the source site")


def test_import_pages_with_fk(self):
data = """{
"ids_for_import": [
Expand Down Expand Up @@ -423,6 +522,7 @@ def test_import_page_with_child_models(self):

importer = ImportPlanner(root_page_source_pk=100, destination_parent_id=2)
importer.add_json(data)
#
importer.run()

new_page = SectionedPage.objects.get(id=page_id)
Expand Down
24 changes: 17 additions & 7 deletions tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from datetime import date, datetime, timezone
from unittest import mock

from django.conf import settings
from django.contrib.auth.models import AnonymousUser, Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import redirect
from django.test import TestCase
from django.test import TestCase, override_settings, modify_settings
from django.urls import reverse

from tests.models import SponsoredPage
Expand All @@ -24,6 +25,17 @@ def test_get(self):
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'data-wagtail-component="content-import-form"')

class TestCheckPageExistence(TestCase):
fixtures = ['test.json']

def setUp(self):
self.client.login(username='admin', password='password')

def test_get_page_existence(self):
# in the case of `'wagtailcore.page': ['slug', 'locale_id']` TODO: override this value instead
response = self.client.get('/admin/wagtail-transfer/api/check_uid/?uid=home,1')
# 200 means existing page was found
self.assertEqual(response.status_code, 200)

@mock.patch('requests.post')
@mock.patch('requests.get')
Expand All @@ -35,16 +47,14 @@ def setUp(self):

def test_run(self, get, post):
get.return_value.status_code = 200
# TODO these values should be adjusted only in cases where we are not using UIDs for pages
get.return_value.content = b"""{
"ids_for_import": [
["wagtailcore.page", 12],
["wagtailcore.page", 15],
["wagtailcore.page", 16]
],
"mappings": [
["wagtailcore.page", 12, "22222222-2222-2222-2222-222222222222"],
["wagtailcore.page", 15, "00017017-5555-5555-5555-555555555555"],
["wagtailcore.page", 16, "00e99e99-6666-6666-6666-666666666666"],
["wagtailcore.page", 12, "/home/"],
["wagtailcore.page", 15, "/home/oil-is-great/"],
["wagtailcore.page", 16, "/home/eggs-are-great-too/"],
["tests.advert", 11, "adadadad-1111-1111-1111-111111111111"],
["tests.advert", 8, "adadadad-8888-8888-8888-888888888888"]
],
Expand Down
6 changes: 6 additions & 0 deletions wagtail_transfer/locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def uid_from_json(self, 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
if type(uid) == tuple:
filters = dict(zip(self.fields, uid))
elif type(uid) == str:
# 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(",")))
filters = dict(zip(self.fields, uid))

try:
Expand Down