Repository: saleor/saleor

Issue:
Generate populatedb_data.json from modified database
### What I'm trying to achieve
If I run populatedb and try to overwrite the `populatedb_data.json` file with the version that results from `python manage.py dumpdata product` I firstly, get different files. Second, if there have been no modifications to the sample data that is generated, the generated json file is still unable to be processed by the populate db script.

### Steps to reproduce the problem
```bash
python manage.py populatedb --createsuperuser
python manage.py flush
python manage.py dumpdata product > saleor/static/populatedb_data.json
```

### What I expected to happen
```
python manage.py populatedb --createsuperuser  # Should result in no errors
```

### What happens on my end
Random fields are missing, fields have different outputs (i.e. weight is "1.0:kg" instead of "1.0 kg") etc...




Discussion:
`populatedb_data.json` wasn't expected to work out of the box with `dumpdata` command; it was generated with some additional flags if I remember correctly. I agree it would be helpful to have some easy way to update the file. But this file is really meant for our "demo" data and it's not intended to be used in any other way. Why do you need to use this file and not just dump data to some other json file and load it from there?
@maarcingebala We are using the populatedb command on our end to be able to easily test on demo data as well. We totally can just dump the whole database and load it back in but saw using this as the more correct way to programmatically seed the database for testing. (similar to `rake db:seed` in rails) If you could pass along the flags for generating that file, that would be much appreciated!
My mistake - I asked @IKarbowiak who made some changes in that file recently and it seems we were using the same command as you wrote. There is an issue with weights, but you also said some random fields are missing - what fields? Maybe we've made some changes in models that were not reflected in populatedb_data.json?
@maarcingebala @IKarbowiak I'm not sure why the files are not invariant. I've added the output of the command and linked it to a gist below. There is around a ~1k line difference between what is generated and what exists in master right now. I started to go through and try to edit the random_data file to properly parse my output but paused to check-in to see if maybe I was generating the file incorrectly. (the file was generated using the command listed above)
https://gist.github.com/adithyabsk/caf28403039596a15e74a0b688d9e314
We've investigated the issue and the main problem is that after adding warehouses models (#4986) we didn't change the data file, but only the `populatedb` to keep working with an outdated file. I would consider it a bug - the exported data should include warehouses and stock info.
@maarcingebala Got it, so in the meantime if I'm looking to repeatably load sample data for our system, what approach would you suggest. Should I simply just dump the database and load that back in? We're just looking for a an approach that can be extended as we modify our sample data.
@adithyabsk Yes, in this case I would recommend using `dumpdata` and `loaddata`.
Okay, I can go that route as a stopgap. @maarcingebala any idea how high this lands on your priority list? (so I can figure out an eta from our end)

Resolving patch:
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -36,7 +36,6 @@
 from ...discount import DiscountValueType, VoucherType
 from ...discount.models import Sale, Voucher
 from ...discount.utils import fetch_discounts
-from ...plugins.manager import get_plugins_manager
 from ...giftcard.models import GiftCard
 from ...menu.models import Menu
 from ...menu.utils import update_menu
@@ -45,6 +44,7 @@
 from ...page.models import Page
 from ...payment import gateway
 from ...payment.utils import create_payment
+from ...plugins.manager import get_plugins_manager
 from ...product.models import (
     AssignedProductAttribute,
     AssignedVariantAttribute,
@@ -137,7 +137,7 @@
 def get_weight(weight):
     if not weight:
         return zero_weight()
-    value, unit = weight.split()
+    value, unit = weight.split(":")
     return Weight(**{unit: value})
 
 


Test patch: