11from dateutil .relativedelta import relativedelta
2- from odoo import api , models , fields
2+
3+ from odoo import api , fields , models
34from odoo .exceptions import UserError
45from odoo .tools .float_utils import float_compare , float_is_zero
56
@@ -12,7 +13,9 @@ class EstateProperty(models.Model):
1213 name = fields .Char (required = True )
1314 description = fields .Text ()
1415 postcode = fields .Char ()
15- date_availability = fields .Date (copy = False , default = fields .Date .today () + relativedelta (months = + 3 ))
16+ date_availability = fields .Date (
17+ copy = False , default = fields .Date .today () + relativedelta (months = + 3 )
18+ )
1619 expected_price = fields .Float (required = True , default = 1 )
1720 selling_price = fields .Float (readonly = True , copy = False )
1821 bedrooms = fields .Integer (default = 2 )
@@ -22,78 +25,91 @@ class EstateProperty(models.Model):
2225 garden = fields .Boolean ()
2326 garden_area = fields .Integer ()
2427 garden_orientation = fields .Selection (
25- selection = [('north' , 'North' ), ('south' , 'South' ), ('east' , 'East' ), ('west' , 'West' )]
28+ selection = [
29+ ("north" , "North" ),
30+ ("south" , "South" ),
31+ ("east" , "East" ),
32+ ("west" , "West" ),
33+ ]
2634 )
2735 active = fields .Boolean (default = True )
2836 state = fields .Selection (
2937 selection = [
30- (' new' , ' New' ),
31- (' offer_received' , ' Offer Received' ),
32- (' offer_accepted' , ' Offer Accepted' ),
33- (' sold' , ' Sold' ),
34- (' cancelled' , ' Cancelled' )
38+ (" new" , " New" ),
39+ (" offer_received" , " Offer Received" ),
40+ (" offer_accepted" , " Offer Accepted" ),
41+ (" sold" , " Sold" ),
42+ (" cancelled" , " Cancelled" ),
3543 ],
3644 required = True ,
3745 copy = False ,
38- default = ' new'
46+ default = " new" ,
3947 )
4048 property_type_id = fields .Many2one ("estate.property.type" , string = "Property Type" )
4149 buyer_id = fields .Many2one ("res.partner" , copy = False )
4250 salesperson_id = fields .Many2one ("res.users" , default = lambda self : self .env .user )
4351 property_tag_ids = fields .Many2many ("estate.property.tag" , string = "Propert Tags" )
44- offer_ids = fields .One2many ('estate.property.offer' , 'property_id' , string = "Offers" )
45-
52+ offer_ids = fields .One2many ("estate.property.offer" , "property_id" , string = "Offers" )
4653 total_area = fields .Float (compute = "_compute_total_area" )
4754 best_price = fields .Float (compute = "_compute_best_price" )
48-
49- _positive_expected_price = models .Constraint (
50- 'CHECK(expected_price > 0)' ,
51- 'The Expected Price of a Property must be strictly positive'
55+ _check_positive_expected_price = models .Constraint (
56+ "CHECK(expected_price > 0)" ,
57+ "The Expected Price of a Property must be strictly positive" ,
5258 )
53-
54- _positive_selling_price = models .Constraint (
55- 'CHECK(selling_price > 0)' ,
56- 'The Selling Price of a Property must be strictly positive'
59+ _check_positive_selling_price = models .Constraint (
60+ "CHECK(selling_price > 0)" ,
61+ "The Selling Price of a Property must be strictly positive" ,
5762 )
5863
59- @api .constrains (' expected_price' , ' selling_price' )
64+ @api .constrains (" expected_price" , " selling_price" )
6065 def _check_selling_price (self ):
61- for prop in self :
62- percentage = prop .selling_price / prop .expected_price
63- if not float_is_zero (prop .selling_price , precision_digits = 2 ) \
64- and float_compare (percentage , 0.9 , precision_digits = 2 ) == - 1 :
65- raise UserError ("selling price cannot be lower than 90% of the expected price" )
66+ for property in self :
67+ percentage = property .selling_price / property .expected_price
68+ if (
69+ not float_is_zero (property .selling_price , precision_digits = 2 )
70+ and float_compare (percentage , 0.9 , precision_digits = 2 ) == - 1
71+ ):
72+ raise UserError (
73+ "selling price cannot be lower than 90% of the expected price"
74+ )
6675
67- @api .depends (' living_area' , ' garden_area' )
76+ @api .depends (" living_area" , " garden_area" )
6877 def _compute_total_area (self ):
69- for prop in self :
70- prop .total_area = prop .living_area + prop .garden_area
78+ self .ensure_one ()
79+ for property in self :
80+ property .total_area = property .living_area + property .garden_area
7181
72- @api .depends (' offer_ids' )
82+ @api .depends (" offer_ids" )
7383 def _compute_best_price (self ):
74- for prop in self :
75- prop .best_price = max (prop .offer_ids .mapped ('price' ) or [0 ])
84+ self .ensure_one ()
85+ for property in self :
86+ property .best_price = max (property .offer_ids .mapped ("price" ) or [0 ])
7687
77- @api .onchange (' garden' )
88+ @api .onchange (" garden" )
7889 def _onchange_garden (self ):
7990 self .garden_area = 10 if self .garden else 0
80- self .garden_orientation = ' north' if self .garden else ''
91+ self .garden_orientation = " north" if self .garden else ""
8192
8293 @api .ondelete (at_uninstall = False )
8394 def _unlink_if_new_or_cancelled_state (self ):
84- if any (not (prop .state == 'new' or prop .state == 'cancelled' ) for prop in self ):
95+ if any (
96+ not (property .state == "new" or property .state == "cancelled" )
97+ for property in self
98+ ):
8599 raise UserError ("You can only delete new or cancelled properties" )
86100
87101 def action_sold (self ):
88- for prop in self :
89- if prop .state == "cancelled" :
102+ self .ensure_one ()
103+ for property in self :
104+ if property .state == "cancelled" :
90105 raise UserError ("You cannot sell a cancelled property" )
91- prop .state = "sold"
106+ property .state = "sold"
92107 return True
93108
94109 def action_cancel (self ):
95- for prop in self :
96- if prop .state == "sold" :
110+ self .ensure_one ()
111+ for property in self :
112+ if property .state == "sold" :
97113 raise UserError ("You cannot cancel a sold property" )
98- prop .state = "cancelled"
114+ property .state = "cancelled"
99115 return True
0 commit comments