Skip to content

Commit

Permalink
[ADD] estate: add sql & python contrainsts (ch10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mizosoft committed Mar 21, 2024
1 parent 999d7ce commit 7720d18
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
18 changes: 16 additions & 2 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from odoo import fields, models, api
from odoo.exceptions import UserError
from odoo.exceptions import UserError, ValidationError
from datetime import date
from dateutil.relativedelta import relativedelta
from odoo.tools.float_utils import float_compare


class EsateProperty(models.Model):
Expand All @@ -14,7 +15,7 @@ class EsateProperty(models.Model):
date_availability = fields.Date('Available From', copy=False,
default=lambda _: date.today() + relativedelta(months=3))
expected_price = fields.Float('Expected Price', required=True)
selling_price = fields.Float('Selling Price', readonly=True, copy=False, compute="_compute_selling_price")
selling_price = fields.Float('Selling Price', readonly=True, copy=False, compute='_compute_selling_price', store=True)
bedrooms = fields.Integer('Bedrooms', default=2)
living_area = fields.Integer('Living Area (sqm)')
facades = fields.Integer('Facades')
Expand Down Expand Up @@ -51,6 +52,12 @@ class EsateProperty(models.Model):

accepted_offer_id = fields.Many2one('estate.property.offer', string='Accepted Offer')

_sql_constraints = [
('check_expected_price',
'CHECK(expected_price > 0)',
'Expected price must be strictly positive.'),
]

@api.depends('living_area', 'garden_area')
def _compute_total_area(self):
for record in self:
Expand All @@ -75,6 +82,13 @@ def _onchange_garden(self):
self.garden_area = None
self.garden_orientation = None

@api.constrains('selling_price', 'expected_price')
def _check_selling_price(self):
for record in self:
if record.selling_price \
and float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) < 0:
raise ValidationError('Selling price must be >= 90% of the expected price.')

def action_mark_sold(self):
for record in self:
if record.status == 'cancelled':
Expand Down
6 changes: 6 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class EstatePropertyOffer(models.Model):

create_date = fields.Date('Date Created')

_sql_constraints = [
('check_price',
'CHECK(price > 0)',
'An offer\'s price must be positive')
]

@api.depends('validity')
def _compute_deadline(self):
for record in self:
Expand Down
6 changes: 6 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ class EstatePropertyTag(models.Model):
_description = 'A property tag'

name = fields.Char('Name', required=True)

_sql_constraints = [
('unique_name',
'UNIQUE(name)',
'Property tags must be unique')
]
6 changes: 6 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ class EstatePropertyType(models.Model):
_description = 'A property type'

name = fields.Char('Name', required=True)

_sql_constraints = [
('unique_name',
'UNIQUE(name)',
'Property types must be unique.')
]

0 comments on commit 7720d18

Please sign in to comment.