generated from superdesk/newsroom-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set proper section when storing products via mgmt api
CPCN-515
- Loading branch information
Showing
3 changed files
with
20 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import bson | ||
import superdesk | ||
|
||
|
||
def validate_product_refs(products): | ||
def validate_product_refs(product_refs): | ||
products_service = superdesk.get_resource_service("products") | ||
for product_spec in products: | ||
product = products_service.find_one(req=None, _id=product_spec["_id"]) | ||
assert product is not None and product["product_type"] == product_spec.get( | ||
"section" | ||
), ( | ||
f"invalid product type for product {product_spec['_id']}, should be {product['product_type']}" | ||
if product | ||
else f"unknown product {product_spec['_id']}" | ||
) | ||
product_ids = [bson.ObjectId(ref["_id"]) for ref in product_refs] | ||
products = list( | ||
products_service.get_from_mongo(req=None, lookup={"_id": {"$in": product_ids}}) | ||
) | ||
products_by_id = {str(product["_id"]): product for product in products} | ||
|
||
for ref in product_refs: | ||
product = products_by_id.get(str(ref["_id"])) | ||
assert product is not None | ||
ref["section"] = product["product_type"] |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
-r requirements.txt | ||
|
||
black>=23,<24 | ||
flake8 | ||
sphinx | ||
sphinx-autobuild | ||
|
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 |
---|---|---|
|
@@ -140,27 +140,17 @@ Feature: Management API - Users | |
] | ||
} | ||
""" | ||
Then we get error 400 | ||
""" | ||
{"code": 400, "message": "invalid product type for product #products._id#, should be agenda"} | ||
""" | ||
Then we get response code 201 | ||
|
||
When we post to "/users" | ||
When we get "/users/#users._id#" | ||
Then we get existing resource | ||
""" | ||
{ | ||
"first_name": "John", | ||
"last_name": "Cena", | ||
"email": "[email protected]", | ||
"company": "#companies._id#", | ||
"sections": { | ||
"agenda": true | ||
}, | ||
"products": [ | ||
{"section": "agenda", "_id": "#products._id#"} | ||
{"_id": "#products._id#", "section": "agenda"} | ||
] | ||
} | ||
""" | ||
Then we get response code 201 | ||
|
||
When we patch "/users/#users._id#" | ||
""" | ||
|
@@ -170,14 +160,14 @@ Feature: Management API - Users | |
] | ||
} | ||
""" | ||
Then we get error 400 | ||
Then we get response code 200 | ||
|
||
When we patch "/users/#users._id#" | ||
When we get "/users/#users._id#" | ||
Then we get existing resource | ||
""" | ||
{ | ||
"products": [ | ||
{"section": "agenda", "_id": "#products._id#"} | ||
] | ||
} | ||
""" | ||
Then we get response code 200 |