Skip to content

Commit

Permalink
[f] Fix import export on m2m
Browse files Browse the repository at this point in the history
  • Loading branch information
huynguyengl99 committed May 21, 2024
1 parent 793c021 commit 16b96ca
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 25 deletions.
2 changes: 1 addition & 1 deletion headless_cms/core/management/commands/export_cms_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def export_model(self, model):
through = getattr(model, field.name).through
self.export_model(through)

export_model_resource = override_modelresource_factory(model)
export_model_resource = override_modelresource_factory(model, exclude_m2m=True)
export_model = export_model_resource()

data = export_model.export()
Expand Down
4 changes: 3 additions & 1 deletion headless_cms/core/management/commands/import_cms_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def import_model(self, model):
with open(file_to_import) as fh:
imported_data = Dataset().load(fh)

import_model_resource = override_modelresource_factory(model)
import_model_resource = override_modelresource_factory(
model, exclude_m2m=True
)
import_model = import_model_resource()

import_model.import_data(imported_data, raise_errors=True)
Expand Down
14 changes: 12 additions & 2 deletions headless_cms/utils/custom_import_export.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import reversion
from django.db.models import ManyToManyField
from django.utils.translation import gettext_lazy as _
from import_export import widgets
from import_export.resources import ModelDeclarativeMetaclass, ModelResource
Expand Down Expand Up @@ -31,13 +32,22 @@ def save_instance(self, *args, **kwargs):
super().save_instance(*args, **kwargs)


def override_modelresource_factory(model, resource_class=LocalizedModelResource):
def override_modelresource_factory(
model, resource_class=LocalizedModelResource, exclude_m2m=False
):
"""
Factory for creating ``ModelResource`` class for given Django model.
"""
exclude = ["published_version"]
if exclude_m2m:
model_fields = model._meta.get_fields()
for field in model_fields:
if isinstance(field, ManyToManyField):
exclude.append(field.name)

attrs = {
"model": model,
"exclude": ["published_version"],
"exclude": exclude,
"use_natural_foreign_keys": True,
"skip_unchanged": True,
}
Expand Down
8 changes: 2 additions & 6 deletions tests/test_commands/data/corrupted_dir/test_app/Post.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
"vi": "Bảo tồn môi trường là rất quan trọng cho sự bền vững của hành tinh chúng ta."
},
"href": "{'en': 'https://example.com/en/environmental-conservation', 'ro': 'https://example.com/ro/conservarea-mediului', 'vi': 'https://example.com/vi/bao-ton-moi-truong'}",
"tags": "1",
"category": 4
},
{
"id": -1,
"title": 10,
"id": "abc",
"title": "hello",
"subtitle": {
"en": "",
"ro": "",
Expand All @@ -44,7 +42,5 @@
"vi": "Tham gia vào các môn thể thao là một cách tuyệt vời để duy trì sức khỏe thể chất và hình thể."
},
"href": "{'en': 'https://example.com/en/top-sports', 'ro': 'https://example.com/ro/sporturi-pentru-forma', 'vi': 'https://example.com/vi/mon-the-thao-giu-dang'}",
"tags": "2,10",
"category": 20
}
]
Binary file modified tests/test_commands/data/expected_data.zip
Binary file not shown.
9 changes: 3 additions & 6 deletions tests/test_commands/data/expected_dir/test_app/Article.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"en": "This is the story of the first article.",
"ro": "Aceasta este povestea primului articol.",
"vi": "\u0110\u00e2y l\u00e0 c\u00e2u chuy\u1ec7n c\u1ee7a b\u00e0i vi\u1ebft \u0111\u1ea7u ti\u00ean."
},
"images": "1,2"
}
},
{
"id": "2",
Expand All @@ -34,8 +33,7 @@
"en": "This is the story of the second article.",
"ro": "Aceasta este povestea al doilea articol.",
"vi": "\u0110\u00e2y l\u00e0 c\u00e2u chuy\u1ec7n c\u1ee7a b\u00e0i vi\u1ebft th\u1ee9 hai."
},
"images": "2,3"
}
},
{
"id": "3",
Expand All @@ -53,7 +51,6 @@
"en": "This is the story of the third article.",
"ro": "Aceasta este povestea al treilea articol.",
"vi": "\u0110\u00e2y l\u00e0 c\u00e2u chuy\u1ec7n c\u1ee7a b\u00e0i vi\u1ebft th\u1ee9 ba."
},
"images": "1,3"
}
}
]
6 changes: 0 additions & 6 deletions tests/test_commands/data/expected_dir/test_app/Blog.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"ro": "Perspective Tehnologice",
"vi": "Nh\u1eadn \u0110\u1ecbnh C\u00f4ng Ngh\u1ec7"
},
"posts": "1,2",
"articles": "1,2",
"domain": 1
},
{
Expand All @@ -37,8 +35,6 @@
"ro": "Probleme de S\u0103n\u0103tate",
"vi": "Nh\u1eefng V\u1ea5n \u0110\u1ec1 S\u1ee9c Kh\u1ecfe"
},
"posts": "2,3",
"articles": "2,3",
"domain": 2
},
{
Expand All @@ -58,8 +54,6 @@
"ro": "Alegerea Stilului de Via\u021b\u0103",
"vi": "L\u1ef1a Ch\u1ecdn Phong C\u00e1ch S\u1ed1ng"
},
"posts": "1,3",
"articles": "1,3",
"domain": 3
}
]
3 changes: 0 additions & 3 deletions tests/test_commands/data/expected_dir/test_app/Post.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"vi": "Django ORM l\u00e0 m\u1ed9t c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd \u0111\u1ec3 t\u01b0\u01a1ng t\u00e1c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u."
},
"href": "{'en': 'https://example.com/en/understanding-django-orm', 'ro': 'https://example.com/ro/in\u021belegerea-django-orm', 'vi': 'https://example.com/vi/hi\u1ec3u-v\u1ec1-django-orm'}",
"tags": "1,2",
"category": 1
},
{
Expand All @@ -48,7 +47,6 @@
"vi": "L\u00e0m theo nh\u1eefng m\u1eb9o n\u00e0y s\u1ebd gi\u00fap b\u1ea1n duy tr\u00ec s\u1ee9c kh\u1ecfe."
},
"href": "{'en': 'https://example.com/en/health-tips-2023', 'ro': 'https://example.com/ro/sfaturi-pentru-s\u0103n\u0103tate-pentru-2023', 'vi': 'https://example.com/vi/m\u1eb9o-s\u1ee9c-kh\u1ecfe-cho-n\u0103m-2023'}",
"tags": "2,3",
"category": 2
},
{
Expand All @@ -74,7 +72,6 @@
"vi": "S\u1eed d\u1ee5ng nh\u1eefng m\u1eb9o n\u00e0y \u0111\u1ec3 c\u1ea3i thi\u1ec7n th\u00f3i quen h\u00e0ng ng\u00e0y c\u1ee7a b\u1ea1n."
},
"href": "{'en': 'https://example.com/en/modern-lifestyle-hacks', 'ro': 'https://example.com/ro/trucuri-pentru-un-stil-de-via\u021b\u0103-modern', 'vi': 'https://example.com/vi/m\u1eb9o-s\u1ed1ng-hi\u1ec7n-\u0111\u1ea1i'}",
"tags": "1,3",
"category": 3
}
]
1 change: 1 addition & 0 deletions tests/test_commands/test_import_export_cms_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def setUp(self):
self.result_dir = self.data_dir / "results"

def tearDown(self):
"""Comment out this tear down to keep the results for comparison."""
if self.result_dir.exists():
shutil.rmtree(self.result_dir)

Expand Down

0 comments on commit 16b96ca

Please sign in to comment.