Skip to content

Commit

Permalink
[IMP] estate: SQL and Python constrains
Browse files Browse the repository at this point in the history
-Added SQL's CHECK and UNIQUE constrains on given fields.
-CHECK: selling, offer, expected prices should be strictly positive.
-UNIQUE: property type and tag should be unique.
-odoo's @api. constrains on selling & expected price to  make sure that selling
 price does not fall more lower than 90% of expected price
  • Loading branch information
siha-odoo committed Feb 6, 2025
1 parent f22f496 commit c17bb1d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
18 changes: 16 additions & 2 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dateutil.relativedelta import relativedelta
from odoo.tools import float_compare
from odoo import api,fields, models, exceptions

#Class of EstateProperty to define fields of database table
Expand All @@ -9,7 +9,7 @@ class EstateProperty(models.Model):
name = fields.Char(string='Name',required=True)
description = fields.Text(string='Description')
postcode = fields.Char(string='Postcode')
date_availability = fields.Date(string='Available From', copy=False, default=fields.Date.add(fields.Date.today()+ relativedelta(months=3)))
date_availability = fields.Date(string='Available From', copy=False, default=fields.Date.add(fields.Date.today(), months=3))
expected_price = fields.Float(string='Expected Price', required=True)
selling_price = fields.Float(string='Selling Price ', readonly=True, copy=False)
bedrooms = fields.Integer(string='Bedrooms', default=2)
Expand Down Expand Up @@ -43,6 +43,11 @@ class EstateProperty(models.Model):
offer_ids = fields.One2many('estate.property.offer','property_id',string='Offers') #One2Many field
best_prices = fields.Float(string='Best Offer',compute='_compute_best_offer')

_sql_constraints = [
('check_expected_price','CHECK(expected_price > 0)','Expected Price must be positive'),
('check_selling_price','CHECK(selling_price > 0)','Selling Price must be positive'),
]

#Function of computing total area
@api.depends('living_area','garden_area')
def _compute_total_area(self):
Expand Down Expand Up @@ -83,3 +88,12 @@ def action_to_cancel_property(self):
record.state = 'cancelled'
return True

#constrain of selling price not fall more lower than 90% expected price
@api.constrains('selling_price','expected_price')
def _check_selling_price(self):
for record in self:
threshold = record.expected_price * 0.90
if float_compare(threshold,record.selling_price,2) == 1:
raise exceptions.ValidationError("The selling price must be at least 90% of the expected price!You must reduce the expected price if you want to accept this offer.")


11 changes: 9 additions & 2 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ class EstatePropertyOffer(models.Model):
partner_id = fields.Many2one("res.partner",string="Partner")
property_id = fields.Many2one("estate.property",string="Property")
validate = fields.Integer(string="Validity(days)",default=7)
date_deadline = fields.Date(string="Deadline",default=fields.Date.today(),compute="_compute_deadline",inverse="_update_validity")
date_deadline = fields.Date(string="Deadline",
default=fields.Date.today(),
compute="_compute_deadline",
inverse="_update_validity")

_sql_constraints = [
('check_price','CHECK(price > 0)','Offer Price must be positive'),
]

#Function of implementing comptue field and inverse function on validity days and date dadeline
@api.depends("validate","create_date")
Expand All @@ -36,7 +43,7 @@ def _update_validity(self):
#Function to perform action when offer accpeted
def action_accept(self):
for record in self:
if record.property_id.selling_price and (record.status == 'accepted' or record.status == 'refused'):
if record.status == 'accepted':
raise exceptions.UserError("This property has already accepted offer!")

record.property_id.selling_price = record.price
Expand Down
5 changes: 4 additions & 1 deletion estate/models/estate_property_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ class EstatePropertyTyags(models.Model):
_description = "Property Tags"

name = fields.Char(string="Name", required=True)


_sql_constraints = [
('name_uniq', 'unique (name)', "Tag name already exists!"),
]
5 changes: 4 additions & 1 deletion estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ class EstatePropertyType(models.Model):
_description = "Property Types"

name = fields.Char(string="Name", required=True)


_sql_constraints = [
('name_uniq', 'unique (name)', "Type name already exists!"),
]

0 comments on commit c17bb1d

Please sign in to comment.