Skip to content

Commit 6948881

Browse files
committed
Book category.
1 parent 6f771f3 commit 6948881

9 files changed

+161
-5
lines changed

__manifest__.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'views/library_menu.xml',
1111
'views/book_view.xml',
1212
'views/book_list_template.xml',
13+
'views/book_category_view.xml',
1314
],
1415
'application': True,
1516
'installable': True

models/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
from . import library_book
2+
from . import library_book_category
3+
from . import res_partner
4+

models/library_book.py

+74-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
from odoo import api, fields, models
22
from odoo.exceptions import Warning
3+
from odoo.exceptions import ValidationError
34

45

56
class Book(models.Model):
67
_name = 'library.book'
78
_description = 'Book'
89

9-
name = fields.Char('Title', required=True)
10+
name = fields.Char('Title',
11+
default=None,
12+
index=True,
13+
help='Book cover title',
14+
readonly=False,
15+
required=True,
16+
translate=False)
1017
isbn = fields.Char('ISBN')
11-
active = fields.Boolean('Active?', default=True)
18+
book_type = fields.Selection(
19+
[('paper', 'Paperback'),
20+
('hard', 'Hardcover'),
21+
('electronic', 'Electronic'),
22+
('other', 'Other')],
23+
'Type'
24+
)
25+
notes = fields.Text('Internal Notes')
26+
descr = fields.Html('Description')
27+
28+
copies = fields.Integer(default=1)
29+
avg_rating = fields.Float('Average Rating', (3, 2))
30+
currency_id = fields.Many2one('res.currency')
31+
price = fields.Monetary('Price', 'currency_id')
32+
1233
date_published = fields.Date()
34+
last_borrow_date = fields.Datetime(
35+
'Last Borrowed On',
36+
default=lambda self: fields.Datetime.now())
37+
38+
active = fields.Boolean('Active?', default=True)
1339
image = fields.Binary('Cover')
40+
1441
publisher_id = fields.Many2one('res.partner', string='Publisher')
1542
author_ids = fields.Many2many('res.partner', string='Authors')
1643

@@ -32,6 +59,50 @@ def button_check_isbn(self):
3259
if not book.isbn:
3360
raise Warning('Please provide an ISBN13 for %s' % book.name)
3461
if book.isbn and not book._check_isbn():
35-
raise Warning('%s is an invalid ISBD' % book.isbn)
62+
raise Warning('%s is an invalid ISBN' % book.isbn)
3663

3764
return True
65+
66+
category_id = fields.Many2one('library.book.category', string='Category')
67+
68+
publisher_country_id = fields.Many2one(
69+
'res.country',
70+
string='Publisher Country',
71+
compute = '_compute_publisher_country',
72+
inverser = '_inverse_publisher_country',
73+
search = '_search_publisher_country',
74+
)
75+
76+
@api.depends('publisher_id.country_id')
77+
def _compute_publisher_country(self):
78+
for book in self:
79+
book.publisher_country_id = book.publisher_id.country_id
80+
81+
def _inverse_publisher_country(self):
82+
for book in self:
83+
book.publisher_id.country_id = book.publisher_country_id
84+
85+
@staticmethod
86+
def _search_publisher_country(operator, value):
87+
return [('publisher_id.country_id', operator, value)]
88+
89+
publisher_country_related = fields.Many2one(
90+
'res.country',
91+
string='Publisher Country (related)',
92+
related='publisher_id.country_id'
93+
)
94+
95+
_sql_constraints = [
96+
('library_book_name_date_uq',
97+
'UNIQUE(name, date_published)',
98+
'Book title and publication date must be unique.'),
99+
('library_book_check_date',
100+
'CHECK (date_published <= current_date)',
101+
'Publication date must not be in the future.'),
102+
]
103+
104+
@api.constrains('isbn')
105+
def _constrain_isbn_valid(self):
106+
for book in self:
107+
if book.isbn and not book._check_isbn():
108+
raise ValidationError('%s is an invalid ISBN' % book.isbn)

models/library_book_category.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from odoo import fields, models
2+
3+
4+
class BookCategory(models.Model):
5+
_name = 'library.book.category'
6+
_description = 'Book Category'
7+
_parent_store = True
8+
9+
name = fields.Char(translate=True, required=True)
10+
parent_id = fields.Many2one(
11+
'library.book.category',
12+
string='Parent Category',
13+
ondelete='restrict',)
14+
parent_path = fields.Char(index=True)
15+
16+
child_ids = fields.One2many(
17+
'library.book.category',
18+
'parent_id',
19+
string='Subcategories',)
20+
21+
highlighted_id = fields.Reference(
22+
[('library.book', 'Book'),
23+
('res.partner', 'Author')],
24+
'Category Highlight',)

models/res_partner.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from odoo import fields, models
2+
3+
4+
class Partner(models.Model):
5+
_inherit = 'res.partner'
6+
_parent_store = True
7+
8+
published_book_ids = fields.One2many(
9+
'library.book',
10+
'publisher_id',
11+
string='Published Books',)

security/ir.model.access.csv

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
22
access_book_user,Book User Access,model_library_book,library_group_user,1,0,0,0
3-
access_book_manager,Book Manager Access,model_library_book,library_group_manager,1,1,1,1
3+
access_book_manager,Book Manager Access,model_library_book,library_group_manager,1,1,1,1
4+
access_categ_user,Category User Access,model_library_book_category,library_group_user,1,0,0,0
5+
access_categ_manager,Category Manager Access,model_library_book_category,library_group_manager,1,1,1,1

views/book_category_view.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="view_form_categ" model="ir.ui.view">
4+
<field name="name">Book Category Form</field>
5+
<field name="model">library.book.category</field>
6+
<field name="arch" type="xml">
7+
<form>
8+
<group>
9+
<field name="name" />
10+
<field name="parent_id" />
11+
<field name="child_ids" widget="many2many_tags" />
12+
<field name="highlighted_id" />
13+
</group>
14+
</form>
15+
</field>
16+
</record>
17+
</odoo>

views/book_view.xml

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,27 @@
1212
<sheet>
1313
<group>
1414
<field name="name" />
15+
<field name="category_id" />
1516
<field name="author_ids" widget="many2many_tags" />
1617
<field name="publisher_id" />
17-
<field name="date_published" />
18+
<field name="publisher_country_id" />
19+
<field name="publisher_country_related" />
20+
<separator string="Text fields" />
1821
<field name="isbn" />
22+
<field name="book_type" />
23+
<field name="notes" />
24+
<field name="descr" />
25+
</group>
26+
<group>
27+
<separator string="Numeric fields" />
28+
<field name="copies" />
29+
<field name="avg_rating" />
30+
<field name="price" />
31+
<field name="currency_id" />
32+
<separator string="Date and time fields" />
33+
<field name="date_published" />
34+
<field name="last_borrow_date" />
35+
<separator string="Other fields" />
1936
<field name="active" />
2037
<field name="image" widget="image" />
2138
</group>

views/library_menu.xml

+10
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@
1111
name="Book"
1212
action="action_library_book"
1313
parent="library_menu" />
14+
15+
<act_window id="action_library_categ"
16+
name="Categories"
17+
res_model="library.book.category"
18+
view_mode="tree,form" />
19+
20+
<menuitem id="menu_library_categ"
21+
name="Categories"
22+
action="action_library_categ"
23+
parent="library_menu" />
1424
</odoo>

0 commit comments

Comments
 (0)