Skip to content

Commit

Permalink
fix user sections stored as list during migration (#351)
Browse files Browse the repository at this point in the history
CPCN-137
  • Loading branch information
petrjasek authored Apr 28, 2023
1 parent 0097f16 commit f7eb5d0
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions data_updates/00010_20230427-151329_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,30 @@ def forwards(self, mongodb_collection, _mongodb_database):

for user in mongodb_collection.find({}):
company = companies.get(user.get("company"))
if not company:
continue

company_section_names = get_company_section_names(company)
company_product_ids = get_company_product_ids(company)

updates = {
"sections": {
section: enabled and section in company_section_names
for section, enabled in (user.get("sections") or {}).items()
},
"products": [
product
for product in user.get("products") or []
if product.get("section") in company_section_names and product.get("_id") in company_product_ids
],
}

print(mongodb_collection.update({config.ID_FIELD: user.get(config.ID_FIELD)}, {"$set": updates}))
updates = {}

if isinstance(user.get("sections"), list):
updates["sections"] = {name: True for name in user["sections"]}

if company:
company_section_names = get_company_section_names(company)
company_product_ids = get_company_product_ids(company)
user_sections = updates.get("sections") or user.get("sections") or {}

updates = {
"sections": {
section: enabled and section in company_section_names
for section, enabled in user_sections.items()
},
"products": [
product
for product in user.get("products") or []
if product.get("section") in company_section_names and product.get("_id") in company_product_ids
],
}

if updates:
print(mongodb_collection.update({config.ID_FIELD: user.get(config.ID_FIELD)}, {"$set": updates}))

def backwards(self, mongodb_collection, mongodb_database):
pass

0 comments on commit f7eb5d0

Please sign in to comment.