robin.keunen
5 years ago
6 changed files with 215 additions and 0 deletions
-
1easy_my_coop_api/services/__init__.py
-
46easy_my_coop_api/services/schemas.py
-
75easy_my_coop_api/services/subscription_request_service.py
-
1easy_my_coop_api/tests/__init__.py
-
28easy_my_coop_api/tests/common.py
-
64easy_my_coop_api/tests/test_subscription_requests.py
@ -1 +1,2 @@ |
|||||
from . import ping_service |
from . import ping_service |
||||
|
from . import subscription_request_service |
@ -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}, |
||||
|
}, |
||||
|
} |
@ -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,2 +1,3 @@ |
|||||
from . import test_ping |
from . import test_ping |
||||
from . import test_registry |
from . import test_registry |
||||
|
from . import test_subscription_requests |
@ -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) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue