From a9f51b56473ea50ae5d91c62fd1e58bcba5cf3c0 Mon Sep 17 00:00:00 2001 From: XuanHuyen Date: Sat, 8 Mar 2025 10:00:27 +0700 Subject: [PATCH] api books --- library/__manifest__.py | 2 ++ library/controllers/__init__.py | 2 +- library/controllers/controllers.py | 22 -------------------- library/controllers/library_api.py | 31 ++++++++++++++++++++++++++++ library/data/library_cron.xml | 15 ++++++++++++++ library/models/library_book.py | 28 +++++++++++++++++-------- library/views/library_book_views.xml | 10 ++++----- 7 files changed, 73 insertions(+), 37 deletions(-) delete mode 100644 library/controllers/controllers.py create mode 100644 library/controllers/library_api.py create mode 100644 library/data/library_cron.xml diff --git a/library/__manifest__.py b/library/__manifest__.py index 8498819..5c3da15 100644 --- a/library/__manifest__.py +++ b/library/__manifest__.py @@ -25,8 +25,10 @@ 'security/ir.model.access.csv', 'views/library_book_views.xml', 'views/library_member_views.xml', + 'data/library_cron.xml', 'views/library_menus.xml' ], + 'controllers': ['controllers/library_api.py'], # only loaded in demonstration mode 'demo': [ 'demo/demo.xml', diff --git a/library/controllers/__init__.py b/library/controllers/__init__.py index b0f26a9..3eb2e0c 100644 --- a/library/controllers/__init__.py +++ b/library/controllers/__init__.py @@ -1,3 +1,3 @@ # -*- coding: utf-8 -*- -from . import controllers +from . import library_api diff --git a/library/controllers/controllers.py b/library/controllers/controllers.py deleted file mode 100644 index 86e40ad..0000000 --- a/library/controllers/controllers.py +++ /dev/null @@ -1,22 +0,0 @@ -# -*- coding: utf-8 -*- -# from odoo import http - - -# class Library(http.Controller): -# @http.route('/library/library', auth='public') -# def index(self, **kw): -# return "Hello, world" - -# @http.route('/library/library/objects', auth='public') -# def list(self, **kw): -# return http.request.render('library.listing', { -# 'root': '/library/library', -# 'objects': http.request.env['library.library'].search([]), -# }) - -# @http.route('/library/library/objects/', auth='public') -# def object(self, obj, **kw): -# return http.request.render('library.object', { -# 'object': obj -# }) - diff --git a/library/controllers/library_api.py b/library/controllers/library_api.py new file mode 100644 index 0000000..8f9b8bd --- /dev/null +++ b/library/controllers/library_api.py @@ -0,0 +1,31 @@ +from odoo import http +from odoo.http import request +import json + +class LibraryAPI(http.Controller): + @http.route('/library/books', type='http', auth='none', methods=['GET']) + def get_books(self, **kwargs): + token = kwargs.get('token') + valid_token = 'token_123' + + if not token or token != valid_token: + return { + 'error': 'Unauthorized', + "message": "Invalid API token" + }, 401 + + books = request.env['library.book'].sudo().search([]) + book_list = [] + + for book in books: + book_list.append({ + 'id': book.id, + 'name': book.name, + 'author': book.author, + 'published_date': str(book.published_date) if book.published_date else None, + 'isbn': book.isbn, + 'price': book.price, + 'state': book.state, + }) + + return json.dumps({'books': book_list}, ensure_ascii=False) \ No newline at end of file diff --git a/library/data/library_cron.xml b/library/data/library_cron.xml new file mode 100644 index 0000000..1f7663c --- /dev/null +++ b/library/data/library_cron.xml @@ -0,0 +1,15 @@ + + + + + Update Overdue Book Status + + 1 + days + + code + model.update_book_status() + + + + \ No newline at end of file diff --git a/library/models/library_book.py b/library/models/library_book.py index 4f084f7..e44c5b6 100644 --- a/library/models/library_book.py +++ b/library/models/library_book.py @@ -1,5 +1,6 @@ from odoo import fields, models from odoo.exceptions import UserError +from datetime import date class LibraryBook(models.Model): _name = "library.book" @@ -12,14 +13,23 @@ class LibraryBook(models.Model): price = fields.Float(string="Giá sách") borrowed_by = fields.Many2one("library.member", string="Được mượn bởi") + return_date = fields.Date(string="Ngày trả") + state = fields.Selection([ + ('available', 'Có sẵn'), + ('borrowed', 'Đã mượn'), + ('overdue', 'Quá hạn') + ], string='State', default='available') def action_mark_as_borrowed(self): - if self.borrowed_by: - raise UserError("Sách này đã được mượn rồi!") - return { - 'name': 'Select Member', - 'type': 'ir.actions.act_window', - 'res_model': 'library.member', - 'view_mode': 'form', - 'target': 'new' - } \ No newline at end of file + for book in self: + if book.borrowed_by: + raise UserError("Sách này đã được mượn!") + else: + book.borrowed_by = self.env.user.partner_id.id + + def update_book_status(self): + books = self.search([ + ('return_date', '<', date.today()), + ('state', '=', 'borrowed') + ]) + books.write({'state': 'overdue'}) \ No newline at end of file diff --git a/library/views/library_book_views.xml b/library/views/library_book_views.xml index bcecab0..12c4d15 100644 --- a/library/views/library_book_views.xml +++ b/library/views/library_book_views.xml @@ -32,12 +32,12 @@ - + + + -
-
+