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

Fix for populatedb and currency issue #3512

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions saleor/core/utils/random_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ def create_products(products_data, placeholder_dir, create_images):
defaults['weight'] = get_weight(defaults['weight'])
defaults['category_id'] = defaults.pop('category')
defaults['product_type_id'] = defaults.pop('product_type')
defaults['price'] = get_in_default_currency(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do this workaround instead of fixing how defaults are generated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if the defaults were generated with the right currency - they seem to be coming from a static json file. Right now I've only got time and sufficient python to offer this workaround.

Yea - it's static json - no generation involved. https://github.com/mirumee/saleor/blob/11f30e13551069299a97e73b65f086a920cf63ec/saleor/core/utils/random_data.py#L210

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, this data is coming from db.json which has USD currency hardcoded. I think this is an acceptable solution for now. If we have a better idea I'd like to have it described in this PR or a new issue.

defaults, 'price', settings.DEFAULT_CURRENCY)
defaults['attributes'] = json.loads(defaults['attributes'])
product, _ = Product.objects.update_or_create(pk=pk, defaults=defaults)

Expand All @@ -203,9 +205,19 @@ def create_product_variants(variants_data):
continue
defaults['product_id'] = product_id
defaults['attributes'] = json.loads(defaults['attributes'])
defaults['price_override'] = get_in_default_currency(
defaults, 'price_override', settings.DEFAULT_CURRENCY)
defaults['cost_price'] = get_in_default_currency(
defaults, 'cost_price', settings.DEFAULT_CURRENCY)
ProductVariant.objects.update_or_create(pk=pk, defaults=defaults)


def get_in_default_currency(defaults, field, currency):
if field in defaults and defaults[field] is not None:
return Money(defaults[field].amount, currency)
return None


def create_products_by_schema(placeholder_dir, create_images):
path = os.path.join(settings.PROJECT_ROOT, 'saleor', 'static', 'db.json')
with open(path) as f:
Expand Down