159 lines
6.2 KiB
Python
159 lines
6.2 KiB
Python
from odoo import http, _
|
|
from odoo.http import request
|
|
from odoo.exceptions import AccessError, MissingError
|
|
from odoo.addons.portal.controllers.portal import CustomerPortal, pager as portal_pager
|
|
|
|
|
|
class LibraryPortal(CustomerPortal):
|
|
|
|
def _prepare_home_portal_values(self, counters):
|
|
values = super()._prepare_home_portal_values(counters)
|
|
|
|
user = request.env.user
|
|
member = request.env['education.library.member'].sudo().search([('user_id', '=', user.id)], limit=1)
|
|
|
|
if member:
|
|
issue_count = request.env['education.library.issue'].sudo().search_count([
|
|
('member_id', '=', member.id),
|
|
('status', 'in', ['issued', 'overdue'])
|
|
])
|
|
res_count = request.env['education.library.reservation'].sudo().search_count([
|
|
('member_id', '=', member.id),
|
|
('state', '=', 'active')
|
|
])
|
|
fine_count = request.env['education.library.fine'].sudo().search_count([
|
|
('member_id', '=', member.id),
|
|
('state', '=', 'assessed')
|
|
])
|
|
values.update({
|
|
'library_issue_count': issue_count,
|
|
'library_res_count': res_count,
|
|
'library_fine_count': fine_count,
|
|
})
|
|
return values
|
|
|
|
def _get_library_member(self):
|
|
user = request.env.user
|
|
if user._is_public():
|
|
return request.env['education.library.member']
|
|
return request.env['education.library.member'].sudo().search([('user_id', '=', user.id)], limit=1)
|
|
|
|
@http.route(['/my/library'], type='http', auth='user', website=True)
|
|
def portal_my_library(self, **kw):
|
|
member = self._get_library_member()
|
|
if not member:
|
|
return request.redirect('/my')
|
|
|
|
issues = request.env['education.library.issue'].sudo().search([
|
|
('member_id', '=', member.id),
|
|
('status', 'in', ['issued', 'overdue'])
|
|
])
|
|
fines = request.env['education.library.fine'].sudo().search([
|
|
('member_id', '=', member.id),
|
|
('state', '=', 'assessed')
|
|
])
|
|
reservations = request.env['education.library.reservation'].sudo().search([
|
|
('member_id', '=', member.id),
|
|
('state', '=', 'active')
|
|
])
|
|
|
|
values = {
|
|
'member': member,
|
|
'issues': issues,
|
|
'fines': fines,
|
|
'reservations': reservations,
|
|
'page_name': 'library_dashboard',
|
|
}
|
|
return request.render('education_library.portal_my_library', values)
|
|
|
|
@http.route(['/my/library/books', '/my/library/books/page/<int:page>'], type='http', auth='user', website=True)
|
|
def portal_library_books(self, page=1, search='', search_in='title', **kw):
|
|
domain = [('active', '=', True)]
|
|
if search:
|
|
if search_in == 'title':
|
|
domain += [('title', 'ilike', search)]
|
|
elif search_in == 'author':
|
|
domain += [('author', 'ilike', search)]
|
|
elif search_in == 'isbn':
|
|
domain += [('isbn', 'ilike', search)]
|
|
|
|
Book = request.env['education.library.book'].sudo()
|
|
total = Book.search_count(domain)
|
|
pager = portal_pager(
|
|
url="/my/library/books",
|
|
url_args={'search': search, 'search_in': search_in},
|
|
total=total,
|
|
page=page,
|
|
step=12
|
|
)
|
|
books = Book.search(domain, limit=12, offset=pager['offset'])
|
|
|
|
values = {
|
|
'books': books,
|
|
'page_name': 'library_books',
|
|
'pager': pager,
|
|
'search': search,
|
|
'search_in': search_in,
|
|
'search_in_options': [
|
|
{'key': 'title', 'label': 'Title'},
|
|
{'key': 'author', 'label': 'Author'},
|
|
{'key': 'isbn', 'label': 'ISBN'},
|
|
]
|
|
}
|
|
return request.render('education_library.portal_library_books', values)
|
|
|
|
@http.route(['/my/library/book/<int:book_id>'], type='http', auth='user', website=True)
|
|
def portal_library_book_detail(self, book_id, **kw):
|
|
book = request.env['education.library.book'].sudo().browse(book_id)
|
|
if not book.exists():
|
|
raise MissingError(_("Book not found."))
|
|
|
|
member = self._get_library_member()
|
|
|
|
# Check if already reserved
|
|
has_reservation = False
|
|
if member:
|
|
res_count = request.env['education.library.reservation'].sudo().search_count([
|
|
('member_id', '=', member.id),
|
|
('book_id', '=', book.id),
|
|
('state', '=', 'active')
|
|
])
|
|
has_reservation = res_count > 0
|
|
|
|
res_enabled = request.env['ir.config_parameter'].sudo().get_param('education_library.reservation_enabled', True)
|
|
|
|
values = {
|
|
'book': book,
|
|
'member': member,
|
|
'has_reservation': has_reservation,
|
|
'res_enabled': res_enabled,
|
|
'page_name': 'library_book_detail',
|
|
}
|
|
return request.render('education_library.portal_library_book_detail', values)
|
|
|
|
@http.route(['/my/library/book/<int:book_id>/reserve'], type='http', auth='user', methods=['POST'], website=True)
|
|
def portal_library_reserve_book(self, book_id, **kw):
|
|
member = self._get_library_member()
|
|
if not member:
|
|
return request.redirect('/my/library/books')
|
|
|
|
book = request.env['education.library.book'].sudo().browse(book_id)
|
|
if not book.exists():
|
|
raise MissingError(_("Book not found."))
|
|
|
|
res_enabled = request.env['ir.config_parameter'].sudo().get_param('education_library.reservation_enabled', True)
|
|
if not res_enabled:
|
|
return request.redirect('/my/library/book/%s' % book_id)
|
|
|
|
try:
|
|
res = request.env['education.library.reservation'].sudo().create({
|
|
'member_id': member.id,
|
|
'book_id': book.id,
|
|
'state': 'draft'
|
|
})
|
|
res.action_activate()
|
|
except Exception as e:
|
|
pass # Usually duplicate active reservation constraint
|
|
|
|
return request.redirect('/my/library/book/%s' % book_id)
|