From d62d9ee714a27a2732966e2984c4b2b229370f06 Mon Sep 17 00:00:00 2001 From: Marcoapu Date: Tue, 22 Oct 2024 15:47:00 +0200 Subject: [PATCH] [ADD] added tag get,post,put rest api --- inrim_anagrafiche/models/ca_tag.py | 27 +++++++++++++++- .../controllers/tag.py | 32 +++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/inrim_anagrafiche/models/ca_tag.py b/inrim_anagrafiche/models/ca_tag.py index d83572d..1f925a6 100644 --- a/inrim_anagrafiche/models/ca_tag.py +++ b/inrim_anagrafiche/models/ca_tag.py @@ -70,4 +70,29 @@ def _compute_revoked(self): record.revoked = False if record.ca_proprieta_tag_ids: if self.env.ref('inrim_anagrafiche.proprieta_tag_revocato') in record.ca_proprieta_tag_ids: - record.revoked = True \ No newline at end of file + record.revoked = True + + def rest_boby_hint(self): + return { + "name": "Temporaneo", + "tag_code": "E0010150AD255C11", + } + + def rest_get_record(self): + vals = { + 'id': self.id, + 'name': self.name, + 'tag_code': self.tag_code, + 'ca_proprieta_tag_ids': self.f_m2m(self.ca_proprieta_tag_ids), + 'in_use': self.in_use, + 'temp': self.temp, + 'revoked': self.revoked, + } + return vals + + def rest_eval_body(self, body): + body, msg = super().rest_eval_body( + body, [ + 'name', 'tag_code' + ]) + return body, msg \ No newline at end of file diff --git a/inrim_controllo_accessi_api/controllers/tag.py b/inrim_controllo_accessi_api/controllers/tag.py index 4ff824e..6ee6c9c 100644 --- a/inrim_controllo_accessi_api/controllers/tag.py +++ b/inrim_controllo_accessi_api/controllers/tag.py @@ -2,12 +2,40 @@ from .api_controller_inrim import InrimApiController, BadRequest +class InrimApiTag(InrimApiController): + + @http.route('/api/tag', auth="none", type='http', methods=['GET'], + csrf=False) + def api_get_ca_tag(self, **params): + self.check_token('ca.tag', 'read') + return self.handle_response( + *self.model.rest_get(params), is_list=True) + + @http.route('/api/tag', auth="none", type='http', methods=['POST'], + csrf=False) + def api_post_ca_tag(self): + self.check_token('ca.tag', 'create') + data = self.check_and_decode_body() + try: + return self.handle_response(*self.model.rest_post(data)) + except Exception as e: + raise BadRequest(str(e)) + + @http.route('/api/tag', auth="none", type='http', methods=['PUT'], + csrf=False) + def api_put_ca_tag(self): + self.check_token('ca.tag', 'write') + data = self.check_and_decode_body() + try: + return self.handle_response(*self.model.rest_put(data)) + except Exception as e: + raise BadRequest(str(e)) + class InrimApiProprietaTag(InrimApiController): @http.route('/api/proprieta_tag', auth="none", type='http', methods=['GET'], csrf=False) def api_get_ca_proprieta_tag(self, **params): - model = 'ca.proprieta_tag' - self.check_token(model, 'read') + self.check_token('ca.proprieta_tag', 'read') return self.handle_response( *self.model.rest_get(params), is_list=True) \ No newline at end of file