Browse Source

[ADD] emc_api: get subscription request by id

+ get all requests
pull/71/head
robin.keunen 5 years ago
parent
commit
83aca966f3
  1. 1
      easy_my_coop_api/services/__init__.py
  2. 46
      easy_my_coop_api/services/schemas.py
  3. 75
      easy_my_coop_api/services/subscription_request_service.py
  4. 1
      easy_my_coop_api/tests/__init__.py
  5. 28
      easy_my_coop_api/tests/common.py
  6. 64
      easy_my_coop_api/tests/test_subscription_requests.py

1
easy_my_coop_api/services/__init__.py

@ -1 +1,2 @@
from . import ping_service
from . import subscription_request_service

46
easy_my_coop_api/services/schemas.py

@ -0,0 +1,46 @@
# Copyright 2020 Coop IT Easy SCRL fs
# Robin Keunen <robin@coopiteasy.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import _
from odoo.fields import Date
def date_validator(field, value, error):
try:
Date.from_string(value)
except ValueError as e:
return error(
field, _("{} does not match format '%Y-%m-%d'".format(value))
)
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"}},
},
"address": {
"type": "dict",
"schema": {
"street": {"type": "string"},
"zip_code": {"type": "string"},
"city": {"type": "string"},
"country": {"type": "string"},
},
},
"lang": {"type": "string"},
}
S_SUBSCRIPTION_REQUEST_LIST = {
"count": {"type": "integer"},
"rows": {
"type": "list",
"schema": {"type": "dict", "schema": S_SUBSCRIPTION_REQUEST},
},
}

75
easy_my_coop_api/services/subscription_request_service.py

@ -0,0 +1,75 @@
# Copyright 2019 Coop IT Easy SCRL fs
# Robin Keunen <robin@coopiteasy.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.addons.component.core import Component
from odoo.fields import Date
from . import schemas
class SubscriptionRequestService(Component):
_inherit = "base.rest.service"
_name = "subscription.request.services"
_usage = "subscription_request" # service_name
_collection = "emc.services"
_description = """
Subscription requests
"""
def _to_dict(self, sr):
sr.ensure_one()
return {
"id": sr.id,
"name": sr.name,
"email": sr.email,
"date": Date.to_string(sr.date),
"ordered_parts": sr.ordered_parts,
"share_product": {
"id": sr.share_product_id.id,
"name": sr.share_product_id.name,
},
"address": {
"street": sr.address,
"zip_code": sr.zip_code,
"city": sr.city,
"country": sr.country_id.code,
},
"lang": sr.lang,
}
def get(self, _id):
# fixme remove sudo
sr = self.env["subscription.request"].sudo().search([("id", "=", _id)])
if sr:
return self._to_dict(sr)
else:
raise wrapJsonException(
NotFound(_("No subscription request for id %s") % _id)
)
return {"response": "Get called with message " + message}
def search(self, params=None):
# fixme remove sudo
if params is None:
requests = self.env["subscription.request"].sudo().search([])
else:
requests = self.env["subscription.request"].sudo().search([])
response = {
"count": len(requests),
"rows": [self._to_dict(sr) for sr in requests],
}
return response
def _validator_get(self):
return {"_id": {"type": "integer"}}
def _validator_return_get(self):
return schemas.S_SUBSCRIPTION_REQUEST
def _validator_search(self):
return {}
def _validator_return_search(self):
return schemas.S_SUBSCRIPTION_REQUEST_LIST

1
easy_my_coop_api/tests/__init__.py

@ -1,2 +1,3 @@
from . import test_ping
from . import test_registry
from . import test_subscription_requests

28
easy_my_coop_api/tests/common.py

@ -4,6 +4,7 @@
import requests
import json
import odoo
from odoo.addons.base_rest.tests.common import BaseRestCase
@ -16,12 +17,39 @@ class BaseEMCRestCase(BaseRestCase):
def setUp(self):
super().setUp()
self.session = requests.Session()
self.demo_request_1 = self.browse_ref(
"easy_my_coop.subscription_request_1_demo"
)
self.demo_request_1_dict = {
"id": self.demo_request_1.id,
"name": "Manuel Dublues",
"email": "manuel@demo.net",
"date": "2020-02-23",
"ordered_parts": 3,
"share_product": {
"id": self.demo_request_1.share_product_id.id,
"name": "Part B - Worker",
},
"address": {
"street": "schaerbeekstraat",
"zip_code": "1111",
"city": "Brussels",
"country": "BE",
},
"lang": "en_US",
}
def http_get(self, url):
if url.startswith("/"):
url = "http://%s:%s%s" % (HOST, PORT, url)
return self.session.get(url)
def http_get_content(self, route):
response = self.http_get(route)
self.assertEquals(response.status_code, 200)
return json.loads(response.content)
def http_post(self, url, data):
if url.startswith("/"):
url = "http://%s:%s%s" % (HOST, PORT, url)

64
easy_my_coop_api/tests/test_subscription_requests.py

@ -0,0 +1,64 @@
# Copyright 2020 Coop IT Easy SCRL fs
# Robin Keunen <robin@coopiteasy.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import json
from datetime import date, timedelta
from odoo.fields import Date
from odoo.addons.base_rest.controllers.main import _PseudoCollection
from odoo.addons.component.core import WorkContext
from .common import BaseEMCRestCase
class TestSRController(BaseEMCRestCase):
def test_service(self):
# kept as example
# useful if you need to change data in database and check db type
collection = _PseudoCollection("emc.services", self.env)
emc_services_env = WorkContext(
model_name="rest.service.registration", collection=collection
)
service = emc_services_env.component(usage="subscription_request")
result = service.get(self.demo_request_1.id)
self.assertEquals(self.demo_request_1_dict, result)
def test_get_route(self):
id_ = self.demo_request_1.id
route = "/api/subscription_request/%s" % id_
content = self.http_get_content(route)
self.assertEquals(self.demo_request_1_dict, content)
@odoo.tools.mute_logger("odoo.addons.base_rest.http")
def test_route_get_returns_not_found(self):
route = "/api/subscription_request/%s" % "99999"
response = self.http_get(route)
self.assertEquals(response.status_code, 404)
def test_route_get_string_returns_method_not_allowed(self):
route = "/api/subscription_request/%s" % "abc"
response = self.http_get(route)
self.assertEquals(response.status_code, 405)
def test_route_search_all(self):
route = "/api/subscription_request"
response = self.http_get(route)
self.assertEquals(response.status_code, 200)
content = json.loads(response.content)
self.assertIn(
self.demo_request_1_dict, content["rows"]
)
# def test_search_route_from_date(self):
# from_ = Date.to_string(date.today() - timedelta(days=12))
# response = self.http_get("/api/subscription_request?from=%s" % from_)
# self.assertEquals(response.status_code, 200)
#
# content = json.loads(response.content)
# self.assertTrue("message" in content)
Loading…
Cancel
Save