Skip to content

Commit 4f49572

Browse files
committed
ch.10 - all exercises
ch.10 - added constraints on expected_price, selling_price, offer_price, tag_name and type_name ch.10 - Modified constraint on selling price: must be at least 90% of expected price to move to next stage
1 parent 4c367c5 commit 4f49572

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

realestatinator/models/estate_property.py

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ class EstatePropery(models.Model):
44
_name = 'estate_property'
55
_description = 'real estate property'
66
_order = 'sequence'
7+
_sql_constraints = [
8+
('check_expected_price_positive', 'CHECK (0 < expected_price)', 'Check that the expected price is strictly positive'),
9+
10+
]
11+
712

813
sequence = fields.Integer('Sequence', default=0)
914
name = fields.Char('Title', required=True)
@@ -86,3 +91,12 @@ def mark_sold(self):
8691
raise exceptions.UserError('This property cannot be sold because it has already been cancelled.')
8792

8893
record.state = 'sold'
94+
95+
@api.constrains('selling_price')
96+
def _check_selling_price(self):
97+
print('\n\nconstraint\n\n')
98+
for record in self:
99+
if record.state not in ['offer_accepted', 'sold']:
100+
return
101+
if record.selling_price < 0.9 * record.expected_price:
102+
raise exceptions.ValidationError('Selling price must be at least 90% of expected price.')

realestatinator/models/estate_property_offer.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
class EstatePropertyOffer(models.Model):
44
_name = 'estate.property.offer'
55
_description = 'estate property offer'
6-
6+
_sql_constraints = [
7+
('check_offer_price_positive', 'CHECK (0 < price)', 'Check that the offer price is strictly positive.'),
8+
]
79
creation_date = fields.Date('Creation Date', default=fields.Date.today())
810
price = fields.Float('Price')
911
status = fields.Selection(string='Status', selection=[
@@ -28,14 +30,16 @@ def _inverse_deadline(self):
2830
def refuse_offer(self):
2931
for record in self:
3032
if record.status == 'accepted':
31-
record.property_id.selling_price = 0
33+
record.property_id.state = 'offer_received'
34+
record.property_id.selling_price = 0
3235
record.property_id.buyer = None
3336
record.status = 'refused'
3437

3538
def accept_offer(self):
3639
for record in self:
3740
if record.property_id.selling_price != 0:
3841
raise exceptions.UserError('An offer as already been accepted for this property.')
42+
record.property_id.state = 'offer_accepted'
3943
record.status = 'accepted'
4044
record.property_id.selling_price = record.price
4145
record.property_id.buyer = record.partner_id

realestatinator/models/estate_property_tags.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
class EstatePropertyTags(models.Model):
44
_name = 'estate.property.tags'
55
_description = 'estate property tag'
6-
6+
_sql_constraints = [
7+
('name_unique', 'UNIQUE (name)', 'make sure tag name is unique.')
8+
]
79
name = fields.Char('Name', required=True)

realestatinator/models/estate_property_type.py

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
class EstatePropertyType(models.Model):
44
_name = 'estate.property.type'
55
_description = 'real estate property type'
6+
_sql_constraints = [
7+
('name_unique', 'UNIQUE (name)', 'make sure type name is unique.')
8+
]
69
# _order = 'sequence'
710

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

0 commit comments

Comments
 (0)