diff --git a/easy_my_coop_api/__manifest__.py b/easy_my_coop_api/__manifest__.py index 3c051aa..f42c066 100644 --- a/easy_my_coop_api/__manifest__.py +++ b/easy_my_coop_api/__manifest__.py @@ -8,7 +8,8 @@ "depends": [ "base_rest", "easy_my_coop", - ], # auth_api_key + running_env = dev + "auth_api_key", # todo conf running_env = dev + ], "author": "Coop IT Easy SCRLfs", "category": "Cooperative management", "website": "www.coopiteasy.be", diff --git a/easy_my_coop_api/services/schemas.py b/easy_my_coop_api/services/schemas.py index 7b0f979..aed7533 100644 --- a/easy_my_coop_api/services/schemas.py +++ b/easy_my_coop_api/services/schemas.py @@ -15,32 +15,49 @@ def date_validator(field, value, error): ) -S_SUBSCRIPTION_REQUEST = { - "id": {"type": "integer"}, - "name": {"type": "string"}, - "email": {"type": "string"}, - "date": {"type": "string"}, - "ordered_parts": {"type": "integer"}, - "share_product": { - "type": "dict", - "schema": {"id": {"type": "integer"}, "name": {"type": "string"}}, - }, +# todo consistency: S_SR_GET, S_SR_RETURN_GET, S_SR_POST ... + + +S_SUBSCRIPTION_REQUEST_BASE = { + "name": {"type": "string", "required": True, "empty": False}, + "email": {"type": "string", "required": True, "empty": False}, + "ordered_parts": {"type": "integer", "required": True}, "address": { "type": "dict", "schema": { - "street": {"type": "string"}, - "zip_code": {"type": "string"}, - "city": {"type": "string"}, - "country": {"type": "string"}, + "street": {"type": "string", "required": True, "empty": False}, + "zip_code": {"type": "string", "required": True, "empty": False}, + "city": {"type": "string", "required": True, "empty": False}, + "country": {"type": "string", "required": True, "empty": False}, + }, + }, + "lang": {"type": "string", "required": True, "empty": False}, +} + +S_SUBSCRIPTION_REQUEST_GET = { + **S_SUBSCRIPTION_REQUEST_BASE, + **{ + "id": {"type": "integer", "required": True}, + "date": {"type": "string", "required": True, "empty": False}, + "share_product": { + "type": "dict", + "schema": { + "id": {"type": "integer", "required": True}, + "name": {"type": "string", "required": True, "empty": False}, + }, }, }, - "lang": {"type": "string"}, +} + +S_SUBSCRIPTION_REQUEST_CREATE = { + **S_SUBSCRIPTION_REQUEST_BASE, + **{"share_product": {"type": "integer", "required": True}}, } S_SUBSCRIPTION_REQUEST_LIST = { - "count": {"type": "integer"}, + "count": {"type": "integer", "required": True}, "rows": { "type": "list", - "schema": {"type": "dict", "schema": S_SUBSCRIPTION_REQUEST}, + "schema": {"type": "dict", "schema": S_SUBSCRIPTION_REQUEST_GET}, }, } diff --git a/easy_my_coop_api/services/subscription_request_service.py b/easy_my_coop_api/services/subscription_request_service.py index 2faa3f4..49d9ca7 100644 --- a/easy_my_coop_api/services/subscription_request_service.py +++ b/easy_my_coop_api/services/subscription_request_service.py @@ -5,7 +5,7 @@ import logging from odoo.addons.component.core import Component from odoo.addons.base_rest.http import wrapJsonException -from werkzeug.exceptions import NotFound +from werkzeug.exceptions import NotFound, BadRequest from odoo.fields import Date from odoo import _ from . import schemas @@ -43,6 +43,31 @@ class SubscriptionRequestService(Component): "lang": sr.lang, } + def _get_country(self, code): + country = self.env["res.country"].search([("code", "=", code)]) + if country: + return country + else: + raise wrapJsonException( + BadRequest(_("No country for isocode %s") % code) + ) + + def _prepare_create(self, params): + address = params["address"] + country = self._get_country(address["country"]) + + return { + "name": params["name"], + "email": params["email"], + "ordered_parts": params["ordered_parts"], + "share_product_id": params["share_product"], + "address": address["street"], + "zip_code": address["zip_code"], + "city": address["city"], + "country_id": country.id, + "lang": params["lang"], + } + def get(self, _id): # fixme remove sudo sr = self.env["subscription.request"].sudo().search([("id", "=", _id)]) @@ -73,11 +98,16 @@ class SubscriptionRequestService(Component): } return response + def create(self, **params): + params = self._prepare_create(params) + sr = self.env["subscription.request"].create(params) + return self._to_dict(sr) + def _validator_get(self): return {"_id": {"type": "integer"}} def _validator_return_get(self): - return schemas.S_SUBSCRIPTION_REQUEST + return schemas.S_SUBSCRIPTION_REQUEST_GET def _validator_search(self): return { @@ -93,3 +123,9 @@ class SubscriptionRequestService(Component): def _validator_return_search(self): return schemas.S_SUBSCRIPTION_REQUEST_LIST + + def _validator_create(self): + return schemas.S_SUBSCRIPTION_REQUEST_CREATE + + def _validator_return_create(self): + return schemas.S_SUBSCRIPTION_REQUEST_GET diff --git a/easy_my_coop_api/tests/common.py b/easy_my_coop_api/tests/common.py index c07f066..9f65a16 100644 --- a/easy_my_coop_api/tests/common.py +++ b/easy_my_coop_api/tests/common.py @@ -8,12 +8,22 @@ import json import odoo from lxml import html +from odoo.fields import Date from odoo.addons.base_rest.tests.common import BaseRestCase HOST = "127.0.0.1" PORT = odoo.tools.config["http_port"] +def _add_api_key(headers): + key_dict = {"API-KEY": "api-key"} + if headers: + headers.update(key_dict) + else: + headers = key_dict + return headers + + class BaseEMCRestCase(BaseRestCase): @classmethod def setUpClass(cls, *args, **kwargs): @@ -30,15 +40,18 @@ class BaseEMCRestCase(BaseRestCase): self.demo_request_1 = self.browse_ref( "easy_my_coop.subscription_request_1_demo" ) + self.demo_share_product = self.demo_request_1.share_product_id + + date = Date.to_string(self.demo_request_1.date) self.demo_request_1_dict = { "id": self.demo_request_1.id, "name": "Manuel Dublues", "email": "manuel@demo.net", - "date": "2020-02-23", + "date": date, "ordered_parts": 3, "share_product": { - "id": self.demo_request_1.share_product_id.id, - "name": "Part B - Worker", + "id": self.demo_share_product.id, + "name": self.demo_share_product.name, }, "address": { "street": "schaerbeekstraat", @@ -50,14 +63,10 @@ class BaseEMCRestCase(BaseRestCase): } def http_get(self, url, headers=None): - key_dict = {"API-KEY": "api-key"} - if headers: - headers.update(key_dict) - else: - headers = key_dict - + headers = _add_api_key(headers) if url.startswith("/"): url = "http://%s:%s%s" % (HOST, PORT, url) + return self.session.get(url, headers=headers) def http_get_content(self, route, headers=None): @@ -66,10 +75,12 @@ class BaseEMCRestCase(BaseRestCase): return json.loads(response.content) - def http_post(self, url, data): + def http_post(self, url, data, headers=None): + headers = _add_api_key(headers) if url.startswith("/"): url = "http://%s:%s%s" % (HOST, PORT, url) - return self.session.post(url, data=data) + + return self.session.post(url, json=data, headers=headers) @staticmethod def html_doc(response): diff --git a/easy_my_coop_api/tests/test_subscription_requests.py b/easy_my_coop_api/tests/test_subscription_requests.py index a161b98..eef6b96 100644 --- a/easy_my_coop_api/tests/test_subscription_requests.py +++ b/easy_my_coop_api/tests/test_subscription_requests.py @@ -2,8 +2,8 @@ # Robin Keunen # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). - -from datetime import date, timedelta +import json +from datetime import timedelta import odoo from odoo.fields import Date from odoo.addons.base_rest.controllers.main import _PseudoCollection @@ -95,4 +95,35 @@ class TestSRController(BaseEMCRestCase): response = self.http_get(route) self.assertEquals(response.status_code, 400) - # def test_route_create(self): + def test_route_create(self): + url = "/api/subscription_request" + data = { + "name": "Lisa des Danses", + "email": "lisa@desdanses.be", + "ordered_parts": 3, + "share_product": self.demo_share_product.id, + "address": { + "street": "schaerbeekstraat", + "zip_code": "1111", + "city": "Brussels", + "country": "BE", + }, + "lang": "en_US", + } + + response = self.http_post(url, data=data) + self.assertEquals(response.status_code, 200) + content = json.loads(response.content) + + content.pop("id") # can't know id in advance + expected = { + **data, + **{ + "date": Date.to_string(Date.today()), + "share_product": { + "id": self.demo_share_product.id, + "name": self.demo_share_product.name, + }, + }, + } + self.assertEquals(expected, content) diff --git a/oca_dependencies.txt b/oca_dependencies.txt index 4a5c828..b9e8e82 100644 --- a/oca_dependencies.txt +++ b/oca_dependencies.txt @@ -1,5 +1,7 @@ # List the OCA project dependencies, one per line # Add a repository url and branch if you need a forked version -partner-contact addons https://github.com/coopiteasy/addons +partner-contact +rest-framework +server-auth