-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from django-oscar/resources-to-db
[FEAT] Import resources to database
- Loading branch information
Showing
2 changed files
with
48 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from oscar_odin.mappings.context import ModelMapperContext | ||
from oscar_odin.utils import validate_resources | ||
|
||
|
||
def resources_to_db( | ||
resources, | ||
fields_to_update, | ||
identifier_mapping, | ||
model_mapper, | ||
delete_related=False, | ||
clean_instances=True, | ||
skip_invalid_resources=False, | ||
error_identifiers=None, | ||
): | ||
"""Map mulitple resources to a model and store them in the database. | ||
The method will first bulk update or create the foreign keys | ||
After that all the resources will be bulk saved. | ||
At last all related models can will be saved and set on the record. | ||
""" | ||
error_identifiers = error_identifiers or identifier_mapping.get(model_mapper.to_obj) | ||
valid_resources, resource_errors = validate_resources(resources, error_identifiers) | ||
if not skip_invalid_resources and resource_errors: | ||
return [], resource_errors | ||
|
||
context = ModelMapperContext( | ||
model_mapper.to_obj, | ||
delete_related=delete_related, | ||
error_identifiers=error_identifiers, | ||
) | ||
result = model_mapper.apply(valid_resources, context=context) | ||
|
||
try: | ||
instances = list(result) | ||
except TypeError: # it is not a list | ||
instances = [result] | ||
|
||
saved_resources, errors = context.bulk_save( | ||
instances, | ||
fields_to_update, | ||
identifier_mapping, | ||
clean_instances, | ||
) | ||
return saved_resources, resource_errors + errors |