|
6 | 6 |
|
7 | 7 |
|
8 | 8 | class EstateProperty(models.Model): |
9 | | - _name = "estate.property" |
| 9 | + _name = 'estate.property' |
10 | 10 | _description = "Real Estate Property" |
11 | | - _check_expected_price = models.Constraint( |
12 | | - "Check(expected_price > 0)", "The expected price must be strictly positive." |
13 | | - ) |
14 | | - _check_selling_price = models.Constraint( |
15 | | - "Check(selling_price >= 0)", "The selling price must be positive." |
16 | | - ) |
17 | | - |
18 | | - |
| 11 | + _order = 'id desc' |
| 12 | + _check_expected_price = models.Constraint('Check(expected_price > 0)', "The expected price must be strictly positive.") |
| 13 | + _check_selling_price = models.Constraint('Check(selling_price >= 0)', "The selling price must be positive.") |
19 | 14 |
|
20 | | - name = fields.Char(required=True) |
21 | | - description = fields.Text() |
22 | | - postcode = fields.Char() |
23 | | - date_availability = fields.Date() |
24 | | - expected_price = fields.Float(required=True) |
25 | | - selling_price = fields.Float() |
26 | | - bedrooms = fields.Integer() |
27 | | - living_area = fields.Integer() |
28 | | - facades = fields.Integer() |
29 | | - garage = fields.Boolean() |
30 | | - garden = fields.Boolean() |
31 | | - garden_area = fields.Integer() |
| 15 | + name = fields.Char(required=True, string="Name") |
| 16 | + description = fields.Text(string="Description") |
| 17 | + postcode = fields.Char(string="Postcode") |
| 18 | + date_availability = fields.Date(string="Available From") |
| 19 | + expected_price = fields.Float(required=True, string="Expected Price") |
| 20 | + selling_price = fields.Float(string="Selling Price") |
| 21 | + bedrooms = fields.Integer(string="Bedrooms") |
| 22 | + living_area = fields.Integer(string="Living Area (sqm)") |
| 23 | + facades = fields.Integer(string="Facades") |
| 24 | + garage = fields.Boolean(string="Garage") |
| 25 | + garden = fields.Boolean(string="Garden") |
| 26 | + garden_area = fields.Integer(string="Garden Area (sqm)") |
32 | 27 | garden_orientation = fields.Selection( |
33 | 28 | selection=[ |
34 | | - ("north", "North"), |
35 | | - ("south", "South"), |
36 | | - ("east", "East"), |
37 | | - ("west", "West"), |
38 | | - ] |
| 29 | + ('north', "North"), |
| 30 | + ('south', "South"), |
| 31 | + ('east', "East"), |
| 32 | + ('west', "West"), |
| 33 | + ], |
| 34 | + string="Garden Orientation", |
39 | 35 | ) |
40 | 36 | state = fields.Selection( |
41 | 37 | selection=[ |
42 | | - ("new", "New"), |
43 | | - ("offer_received", "Offer Received"), |
44 | | - ("offer_accepted", "Offer Accepted"), |
45 | | - ("sold", "Sold"), |
46 | | - ("cancelled", "Cancelled"), |
| 38 | + ('new', "New"), |
| 39 | + ('offer_received', "Offer Received"), |
| 40 | + ('offer_accepted', "Offer Accepted"), |
| 41 | + ('sold', "Sold"), |
| 42 | + ('cancelled', "Cancelled"), |
47 | 43 | ], |
48 | | - default="new", |
| 44 | + default='new', |
49 | 45 | required=True, |
| 46 | + string="Status", |
| 47 | + ) |
| 48 | + buyer_id = fields.Many2one(comodel_name='res.partner', string="Buyer") |
| 49 | + property_type_id = fields.Many2one(comodel_name='estate.property.type', string="Property Type") |
| 50 | + tag_ids = fields.Many2many(comodel_name='estate.property.tag', string="Tags") |
| 51 | + offer_ids = fields.One2many( |
| 52 | + comodel_name='estate.property.offer', |
| 53 | + inverse_name='property_id', |
| 54 | + string="Offers", |
50 | 55 | ) |
51 | | - buyer_id = fields.Many2one("res.partner", string="Buyer") |
52 | | - property_type_id = fields.Many2one("estate.property.type", string="Property Type") |
53 | | - tag_ids = fields.Many2many("estate.property.tag", string="Tags") |
54 | | - offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers") |
55 | | - total_area = fields.Integer(compute="_compute_total_area") |
56 | | - best_price = fields.Float(compute="_compute_best_price", string="Best Offer Price") |
| 56 | + total_area = fields.Integer(compute='_compute_total_area', string="Total Area (sqm)") |
| 57 | + best_price = fields.Float(compute='_compute_best_price', string="Best Offer Price") |
57 | 58 |
|
58 | | - @api.depends("living_area", "garden_area") |
| 59 | + @api.depends('living_area', 'garden_area') |
59 | 60 | def _compute_total_area(self): |
60 | | - for record in self: |
61 | | - record.total_area = record.living_area + record.garden_area |
| 61 | + for property in self: |
| 62 | + property.total_area = property.living_area + property.garden_area |
62 | 63 |
|
63 | | - @api.depends("offer_ids.price") |
| 64 | + @api.depends('offer_ids.price') |
64 | 65 | def _compute_best_price(self): |
65 | | - for record in self: |
66 | | - if record.offer_ids: |
67 | | - record.best_price = max(record.offer_ids.mapped("price")) |
| 66 | + for property in self: |
| 67 | + if property.offer_ids: |
| 68 | + property.best_price = max(property.offer_ids.mapped('price')) |
68 | 69 | else: |
69 | | - record.best_price = 0.0 |
| 70 | + property.best_price = 0.0 |
70 | 71 |
|
71 | | - @api.onchange("garden") |
| 72 | + @api.onchange('garden') |
72 | 73 | def _onchange_garden(self): |
73 | 74 | if self.garden: |
74 | 75 | self.garden_area = 10 |
75 | | - self.garden_orientation = "north" |
| 76 | + self.garden_orientation = 'north' |
76 | 77 | else: |
77 | 78 | self.garden_area = 0 |
78 | 79 | self.garden_orientation = False |
79 | 80 |
|
80 | | - @api.constrains("selling_price", "expected_price") |
| 81 | + @api.constrains('selling_price', 'expected_price') |
81 | 82 | def _check_selling_price(self): |
82 | | - for record in self: |
83 | | - if not float_is_zero(record.selling_price, precision_digits=2): |
84 | | - if ( |
85 | | - float_compare( |
86 | | - record.selling_price, |
87 | | - record.expected_price * 0.9, |
88 | | - precision_digits=2, |
89 | | - ) |
90 | | - < 0 |
91 | | - ): |
92 | | - raise ValidationError( |
93 | | - "The selling price cannot be lower than 90% of the expected price." |
94 | | - ) |
| 83 | + for property in self: |
| 84 | + if not float_is_zero(property.selling_price, precision_digits=2): |
| 85 | + if float_compare(property.selling_price, property.expected_price * 0.9, precision_digits=2) < 0: |
| 86 | + raise ValidationError(self.env._("The selling price cannot be lower than 90% of the expected price.")) |
95 | 87 |
|
96 | 88 | def action_sold(self): |
97 | | - for record in self: |
98 | | - if record.state == "cancelled": |
99 | | - raise UserError("Cancelled property cannot be sold.") |
100 | | - record.state = "sold" |
| 89 | + for property in self: |
| 90 | + if property.state == 'cancelled': |
| 91 | + raise UserError(self.env._("Cancelled property cannot be sold.")) |
| 92 | + property.state = 'sold' |
101 | 93 | return True |
102 | 94 |
|
103 | 95 | def action_cancel(self): |
104 | | - for record in self: |
105 | | - if record.state == "sold": |
106 | | - raise UserError("Sold property cannot be cancelled.") |
107 | | - record.state = "cancelled" |
| 96 | + for property in self: |
| 97 | + if property.state == 'sold': |
| 98 | + raise UserError(self.env._("Sold property cannot be cancelled.")) |
| 99 | + property.state = 'cancelled' |
108 | 100 | return True |
0 commit comments