api books
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from . import controllers
|
||||
from . import library_api
|
||||
|
||||
@@ -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/<model("library.library"):obj>', auth='public')
|
||||
# def object(self, obj, **kw):
|
||||
# return http.request.render('library.object', {
|
||||
# 'object': obj
|
||||
# })
|
||||
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user