|
- # -*- coding: utf-8 -*-
-
- from odoo import api, fields, models
- from odoo.exceptions import Warning
- from odoo.exceptions import ValidationError
-
- # name和active为特殊字段名
-
- class Book(models.Model):
- _name = 'library.book'
- _description = "Book"
-
- # String fields
- name = fields.Char('Title',
- default=None,
- index=True,
- help='Book cover title',
- readonly=False,
- required=True,
- translate=False,
- )
- isbn = fields.Char('ISBN')
- # active = fields.Boolean('Active?', default=True)
- # date_published = fields.Date()
- # image = fields.Binary('Cover')
- publisher_id = fields.Many2one('res.partner', string='publisher') # 对partner记录的一个引用
- author_ids = fields.Many2many('res.partner', string='Authors') # 一本书可以有多个作者,一个作者可以有多本书
- _order = 'name, date_published desc'
- book_type = fields.Selection(
- [('paper', 'Paperback'),
- ('hard', 'Hardcover'),
- ('electronic', 'Electronic'),
- ('other', 'Other')],
- 'Type')
- notes = fields.Text('Internal Notes')
- descr = fields.Html('Description')
-
- # Numeric fields
- copies = fields.Integer(default=1)
- avg_rating = fields.Float('Average Rating', (3, 2))
- price = fields.Monetary('Price', 'currency_id')
- currency_id = fields.Many2one('res.currency') # price helper
-
- # Date and time fields
- date_published = fields.Date()
-
- # last_borrow_date = fields.Datetime(
- # 'Last Borrowed On',
- # default=lambda self: fields.Datetime.now()) # 计算当前时间
-
- # 默认值也可以是一个函数引用,或待定义函数名字符串
- def _default_last_borrow_date(self):
- return fields.Datetime.now()
-
- last_borrow_date = fields.Datetime('Last Borrowed On', default=_default_last_borrow_date)
-
-
- # Other fields
- active = fields.Boolean('Active?', default=True)
- image = fields.Binary('Cover')
-
- def _check_isbn(self):
- self.ensure_one()
- isbn = self.isbn.replace('-', '') # 为保持兼容性 Alan 自行添加
- digits = [int(x) for x in isbn if x.isdigit()]
- if len(digits) == 13:
- ponderations = [1, 3] * 6
- terms = [a * b for a, b in zip(digits[:13], ponderations)]
- remain = sum(terms) % 10
- check = 10 - remain if remain != 0 else 0
- return digits[-1] == check
-
- def button_check_isbn(self):
- for book in self:
- if not book.isbn:
- raise Warning('Please provide an ISBN for %s' % book.name)
- if book.isbn and not book._check_isbn():
- raise Warning('%s is an invalid ISBN' % book.isbn)
- return True
-
- # publisher_country_id = fields.Many2one(
- # 'res.country', string='Publisher Country',
- # compute='_compute_publisher_country'
- # )
- publisher_country_id = fields.Many2one(
- 'res.country', string='Publisher Country',
- compute='_compute_publisher_country',
- inverse = '_inverse_publisher_country',
- search = '_search_publisher_country',
- )
- # store = False, # 默认不在数据库中存储
- #反向(inverse)逻辑
-
- @api.depends('publisher_id.country_id')
- def _compute_publisher_country(self):
- for book in self:
- book.publisher_country_id = book.publisher_id.country_id
-
- # related属性也能达到关联效果
- # publisher_country_id = fields.Many2one(
- # 'res.country', string='Publisher Country',
- # related='publisher_id.country_id',
- # )
-
- def _inverse_publisher_country(self):
- for book in self:
- book.publisher_id.country_id = book.publisher_country_id
-
- # 要为计算字段开启搜索操作,需要实现search方法
- def _search_publisher_country(self, operator, value):
- return [('publisher_id.country_id', operator, value)]
-
- # 通过标题和出版日期是否相同来确保没有重复的图书;另一条是检查出版日期是否为未出版
- _sql_constraints = [
- ('library_book_name_date_uq', # 约束唯一标识符
- 'UNIQUE (name, date_published)', # 约束 SQL 语法
- 'Book title and publication date must be unique'), # 消息
- ('library_book_check_date',
- 'CHECK (date_published <= current_date)',
- 'Publication date must not be in the future.'),
- ]
-
- # 防止插入不正确的ISBN号
- @api.constrains('isbn')
- def _constrain_isbn_valid(self):
- for book in self:
- if book.isbn and not book._check_isbn():
- raise ValidationError('%s is an invalid ISBN' % book.isbn)
|