1
1
from odoo import api , fields , models
2
2
from odoo .exceptions import Warning
3
+ from odoo .exceptions import ValidationError
3
4
4
5
5
6
class Book (models .Model ):
6
7
_name = 'library.book'
7
8
_description = 'Book'
8
9
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 )
10
17
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
+
12
33
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 )
13
39
image = fields .Binary ('Cover' )
40
+
14
41
publisher_id = fields .Many2one ('res.partner' , string = 'Publisher' )
15
42
author_ids = fields .Many2many ('res.partner' , string = 'Authors' )
16
43
@@ -32,6 +59,50 @@ def button_check_isbn(self):
32
59
if not book .isbn :
33
60
raise Warning ('Please provide an ISBN13 for %s' % book .name )
34
61
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 )
36
63
37
64
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 )
0 commit comments