diff --git a/server/cp/mgmt_api/companies.py b/server/cp/mgmt_api/companies.py index 3242e83..c0cfb5d 100644 --- a/server/cp/mgmt_api/companies.py +++ b/server/cp/mgmt_api/companies.py @@ -13,8 +13,23 @@ def init_app(app): CompaniesResource.internal_resource = False - superdesk.register_resource('companies', CompaniesResource, CPCompaniesService, _app=app) - superdesk.register_resource('company_products', CompanyProductsResource, CompanyProductsService, _app=app) + superdesk.register_resource( + "companies", CPCompaniesResource, CPCompaniesService, _app=app + ) + superdesk.register_resource( + "company_products", CompanyProductsResource, CompanyProductsService, _app=app + ) + + +class CPCompaniesResource(CompaniesResource): + """ + CP Companies Schema + """ + + schema = { + **CompaniesResource.schema, + "country": {"type": "string", "default": "CAN"}, + } class CPCompaniesService(CompaniesService): @@ -23,21 +38,23 @@ def on_create(self, docs): for doc in docs: errors = get_errors_company(doc) if errors: - message = ("invalid ip address") - raise SuperdeskApiError.badRequestError(message=message, payload=message) + message = "invalid ip address" + raise SuperdeskApiError.badRequestError( + message=message, payload=message + ) def on_created(self, docs): super().on_created(docs) for doc in docs: - app.cache.set(str(doc['_id']), doc) + app.cache.set(str(doc["_id"]), doc) def on_update(self, updates, original): super().on_update(updates, original) - app.cache.delete(str(original['_id'])) + app.cache.delete(str(original["_id"])) def on_delete(self, doc): super().on_delete(doc) - app.cache.delete(str(doc['_id'])) + app.cache.delete(str(doc["_id"])) class CompanyProductsResource(newsroom.Resource): @@ -45,24 +62,20 @@ class CompanyProductsResource(newsroom.Resource): url = 'companies//products' resource_methods = ["GET", "POST"] schema = { - 'product': { - 'type': 'objectid', - 'required': True, + "product": { + "type": "objectid", + "required": True, }, - 'seats': { - 'type': 'number', + "seats": { + "type": "number", }, - 'link': { - 'type': 'boolean', - 'nullable': False - } + "link": {"type": "boolean", "nullable": False}, } datasource = { "source": "products", "projection": { - name: 1 - for name in ProductsResource.schema.keys() if name != "companies" - } + name: 1 for name in ProductsResource.schema.keys() if name != "companies" + }, } @@ -91,15 +104,17 @@ def find_one(self, req, **lookup): def create(self, docs, **kwargs): ids = [] for doc in docs: - id = doc.pop('product') - link = doc.pop('link') - product = find_one('products', _id=ObjectId(id)) + id = doc.pop("product") + link = doc.pop("link") + product = find_one("products", _id=ObjectId(id)) company = get_company() assert product - company_products = [p for p in get_company_products(company) if p["_id"] != product["_id"]] + company_products = [ + p for p in get_company_products(company) if p["_id"] != product["_id"] + ] if link: company_products.append(get_product_ref(product, doc.get("seats"))) - superdesk.get_resource_service('companies').system_update( + superdesk.get_resource_service("companies").system_update( company["_id"], {"products": company_products}, company ) ids.append(id) @@ -108,7 +123,11 @@ def create(self, docs, **kwargs): def on_fetched(self, doc): for item in doc["_items"]: self._fix_link(item) - if hasattr(self, "company") and self.company and self.company.get("products"): + if ( + hasattr(self, "company") + and self.company + and self.company.get("products") + ): for product_ref in self.company["products"]: if product_ref["_id"] == item["_id"]: item["seats"] = product_ref["seats"] @@ -120,5 +139,7 @@ def on_fetched_item(self, doc): return super().on_fetched_item(doc) def _fix_link(self, item): - company_id = request.view_args['companies'] - item["_links"]["self"]["href"] = f"companies/{company_id}/products/{item['_id']}" + company_id = request.view_args["companies"] + item["_links"]["self"][ + "href" + ] = f"companies/{company_id}/products/{item['_id']}" diff --git a/server/features/mgmt_api_companies.feature b/server/features/mgmt_api_companies.feature index 0ab4d9b..c13cf8d 100644 --- a/server/features/mgmt_api_companies.feature +++ b/server/features/mgmt_api_companies.feature @@ -52,3 +52,33 @@ Feature: Management API - Companies """ {"name": "xyz company"} """ + + Scenario: Create a company with default country + Given empty "companies" + When we post to "/companies" + """ + { + "name": "zzz company", + "contact_name": "zzz company", + "contact_email": "contact@zzz.com", + "phone": "99999999" + } + """ + Then we get response code 201 + When we get "/companies" + Then we get existing resource + """ + { + "_items" : + [ + { + "name": "zzz company", + "contact_name": "zzz company", + "country": "CAN", + "contact_email": "contact@zzz.com", + "phone": "99999999" + } + ] + } + """ + \ No newline at end of file