Browse Source

[ADD] emc_api: create subscription request route

pull/71/head
robin.keunen 5 years ago
parent
commit
37b2433273
  1. 3
      easy_my_coop_api/__manifest__.py
  2. 49
      easy_my_coop_api/services/schemas.py
  3. 40
      easy_my_coop_api/services/subscription_request_service.py
  4. 33
      easy_my_coop_api/tests/common.py
  5. 37
      easy_my_coop_api/tests/test_subscription_requests.py
  6. 4
      oca_dependencies.txt

3
easy_my_coop_api/__manifest__.py

@ -8,7 +8,8 @@
"depends": [ "depends": [
"base_rest", "base_rest",
"easy_my_coop", "easy_my_coop",
], # auth_api_key + running_env = dev
"auth_api_key", # todo conf running_env = dev
],
"author": "Coop IT Easy SCRLfs", "author": "Coop IT Easy SCRLfs",
"category": "Cooperative management", "category": "Cooperative management",
"website": "www.coopiteasy.be", "website": "www.coopiteasy.be",

49
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": {
# 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", "type": "dict",
"schema": {"id": {"type": "integer"}, "name": {"type": "string"}},
"schema": {
"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},
}, },
"address": {
},
"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", "type": "dict",
"schema": { "schema": {
"street": {"type": "string"},
"zip_code": {"type": "string"},
"city": {"type": "string"},
"country": {"type": "string"},
"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 = { S_SUBSCRIPTION_REQUEST_LIST = {
"count": {"type": "integer"},
"count": {"type": "integer", "required": True},
"rows": { "rows": {
"type": "list", "type": "list",
"schema": {"type": "dict", "schema": S_SUBSCRIPTION_REQUEST},
"schema": {"type": "dict", "schema": S_SUBSCRIPTION_REQUEST_GET},
}, },
} }

40
easy_my_coop_api/services/subscription_request_service.py

@ -5,7 +5,7 @@
import logging import logging
from odoo.addons.component.core import Component from odoo.addons.component.core import Component
from odoo.addons.base_rest.http import wrapJsonException 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.fields import Date
from odoo import _ from odoo import _
from . import schemas from . import schemas
@ -43,6 +43,31 @@ class SubscriptionRequestService(Component):
"lang": sr.lang, "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): def get(self, _id):
# fixme remove sudo # fixme remove sudo
sr = self.env["subscription.request"].sudo().search([("id", "=", _id)]) sr = self.env["subscription.request"].sudo().search([("id", "=", _id)])
@ -73,11 +98,16 @@ class SubscriptionRequestService(Component):
} }
return response 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): def _validator_get(self):
return {"_id": {"type": "integer"}} return {"_id": {"type": "integer"}}
def _validator_return_get(self): def _validator_return_get(self):
return schemas.S_SUBSCRIPTION_REQUEST
return schemas.S_SUBSCRIPTION_REQUEST_GET
def _validator_search(self): def _validator_search(self):
return { return {
@ -93,3 +123,9 @@ class SubscriptionRequestService(Component):
def _validator_return_search(self): def _validator_return_search(self):
return schemas.S_SUBSCRIPTION_REQUEST_LIST 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

33
easy_my_coop_api/tests/common.py

@ -8,12 +8,22 @@ import json
import odoo import odoo
from lxml import html from lxml import html
from odoo.fields import Date
from odoo.addons.base_rest.tests.common import BaseRestCase from odoo.addons.base_rest.tests.common import BaseRestCase
HOST = "127.0.0.1" HOST = "127.0.0.1"
PORT = odoo.tools.config["http_port"] 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): class BaseEMCRestCase(BaseRestCase):
@classmethod @classmethod
def setUpClass(cls, *args, **kwargs): def setUpClass(cls, *args, **kwargs):
@ -30,15 +40,18 @@ class BaseEMCRestCase(BaseRestCase):
self.demo_request_1 = self.browse_ref( self.demo_request_1 = self.browse_ref(
"easy_my_coop.subscription_request_1_demo" "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 = { self.demo_request_1_dict = {
"id": self.demo_request_1.id, "id": self.demo_request_1.id,
"name": "Manuel Dublues", "name": "Manuel Dublues",
"email": "manuel@demo.net", "email": "manuel@demo.net",
"date": "2020-02-23",
"date": date,
"ordered_parts": 3, "ordered_parts": 3,
"share_product": { "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": { "address": {
"street": "schaerbeekstraat", "street": "schaerbeekstraat",
@ -50,14 +63,10 @@ class BaseEMCRestCase(BaseRestCase):
} }
def http_get(self, url, headers=None): 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("/"): if url.startswith("/"):
url = "http://%s:%s%s" % (HOST, PORT, url) url = "http://%s:%s%s" % (HOST, PORT, url)
return self.session.get(url, headers=headers) return self.session.get(url, headers=headers)
def http_get_content(self, route, headers=None): def http_get_content(self, route, headers=None):
@ -66,10 +75,12 @@ class BaseEMCRestCase(BaseRestCase):
return json.loads(response.content) 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("/"): if url.startswith("/"):
url = "http://%s:%s%s" % (HOST, PORT, url) 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 @staticmethod
def html_doc(response): def html_doc(response):

37
easy_my_coop_api/tests/test_subscription_requests.py

@ -2,8 +2,8 @@
# Robin Keunen <robin@coopiteasy.be> # Robin Keunen <robin@coopiteasy.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # 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 import odoo
from odoo.fields import Date from odoo.fields import Date
from odoo.addons.base_rest.controllers.main import _PseudoCollection from odoo.addons.base_rest.controllers.main import _PseudoCollection
@ -95,4 +95,35 @@ class TestSRController(BaseEMCRestCase):
response = self.http_get(route) response = self.http_get(route)
self.assertEquals(response.status_code, 400) 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)

4
oca_dependencies.txt

@ -1,5 +1,7 @@
# List the OCA project dependencies, one per line # List the OCA project dependencies, one per line
# Add a repository url and branch if you need a forked version # Add a repository url and branch if you need a forked version
partner-contact
addons https://github.com/coopiteasy/addons addons https://github.com/coopiteasy/addons
partner-contact
rest-framework
server-auth
Loading…
Cancel
Save