11from dateutil .relativedelta import relativedelta
22from odoo import fields , models , api , exceptions
33from odoo .tools .float_utils import float_compare , float_is_zero
4- from odoo .exceptions import ValidationError
4+ from odoo .exceptions import ValidationError , UserError
55
66
77class EstateProperty (models .Model ):
@@ -12,24 +12,36 @@ class EstateProperty(models.Model):
1212 _positif_expected_price = models .Constraint ("CHECK (expected_price > 0)" , "A price can't be negatif" )
1313 _positif_selling_price = models .Constraint ("CHECK (selling_price > 0)" , "A price can't be negatif" )
1414
15- state = fields .Selection (selection = [("New" , "New" ), ("Offer_Received" , "Offer Received" ), ("Offer_Accepted" , "Offer Accepted" ), ("Sold" , "Sold" ), ("Cancelled" , "Cancelled" )])
15+ state = fields .Selection (selection = [
16+ ("new" , "New" ),
17+ ("offer_received" , "Offer Received" ),
18+ ("offer_accepted" , "Offer Accepted" ),
19+ ("sold" , "Sold" ),
20+ ("cancelled" , "Cancelled" )
21+ ], default = 'new' )
22+
1623 active = fields .Boolean ('Active' , default = True )
1724 name = fields .Char (required = True , default = "Unkown" )
1825 description = fields .Text ()
1926 postcode = fields .Char ()
20- date_availability = fields .Date ("Available From" , copy = False , default = fields .Datetime .today () + relativedelta (months = 3 ))
21- last_seen = fields .Date ("Last Seen" , default = fields .Datetime .now )
27+ date_availability = fields .Date ("Available From" , copy = False , default = lambda self : fields .Datetime .today () + relativedelta (months = 3 ))
28+ last_seen = fields .Date ("Last Seen" , default = lambda self : fields .Datetime .now )
2229 expected_price = fields .Float (required = True )
2330 selling_price = fields .Float (readonly = True , copy = False )
24- best_price = fields .Float (string = "Best Price" , compute = "_get_best_price " )
31+ best_price = fields .Float (string = "Best Price" , compute = "_compute_best_price " )
2532 bedrooms = fields .Integer (default = 2 )
2633 living_area = fields .Integer ()
2734 facades = fields .Integer ()
2835 garage = fields .Boolean ()
2936 garden = fields .Boolean ()
3037 garden_area = fields .Integer ()
31- garden_orientation = fields .Selection (selection = [('North' , 'North' ), ('South' , 'South' ), ('East' , 'East' ), ('West' , 'West' )])
32- total_area = fields .Float (compute = "_get_total_area" , string = "Total Area" )
38+ garden_orientation = fields .Selection (selection = [
39+ ('north' , 'North' ),
40+ ('south' , 'South' ),
41+ ('east' , 'East' ),
42+ ('west' , 'West' )])
43+
44+ total_area = fields .Float (compute = "_compute_total_area" , string = "Total Area" )
3345 property_type_id = fields .Many2one ("estate.property.type" , string = "Type" )
3446 buyer_id = fields .Many2one ("res.partner" , string = "Buyer" )
3547 seller_id = fields .Many2one ("res.users" , default = lambda self : self .env .user , string = "Seller" )
@@ -38,43 +50,40 @@ class EstateProperty(models.Model):
3850
3951 def sell_property (self ):
4052 for record in self :
41- if (record .state == "Cancelled " ):
42- raise exceptions . UserError ("Can't sell cancelled property." )
43- record .state = "Sold "
53+ if (record .state == "cancelled " ):
54+ raise UserError ("Can't sell cancelled property." )
55+ record .state = "sold "
4456 return True
4557
4658 def cancel_property (self ):
4759 for record in self :
48- if (record .state == "Sold " ):
49- raise exceptions . UserError ("Can't cancel sold property." )
50- record .state = "Cancelled "
60+ if (record .state == "sold " ):
61+ raise UserError ("Can't cancel sold property." )
62+ record .state = "cancelled "
5163 return True
5264
5365 @api .depends ('living_area' , 'garden_area' )
54- def _get_total_area (self ):
66+ def _compute_total_area (self ):
5567 for record in self :
5668 record .total_area = record .garden_area + record .living_area
5769
5870 @api .depends ('offer_ids' )
59- def _get_best_price (self ):
71+ def _compute_best_price (self ):
6072 for record in self :
6173 max : int = 0
62- if (len ( record .offer_ids ) == 0 ):
74+ if (not record .offer_ids ):
6375 record .best_price = 0
6476 continue
6577
66- for offer in record .offer_ids :
67- if (offer .price >= max ):
68- max = offer .price
69- record .best_price = max
78+ record .best_price = max (record .offer_ids .mapped ('price' ))
7079
7180 @api .onchange ("garden" )
72- def _garden_pre_fill (self ):
81+ def _on_change_garden (self ):
7382 self .garden_area = 10 if self .garden else 0
7483 self .garden_orientation = 'North' if self .garden else ''
7584
7685 @api .constrains ('selling_price' , 'expected_price' )
77- def _check_prices (self ):
86+ def _constrain_prices (self ):
7887 for record in self :
7988 if float_is_zero (record .selling_price , 2 ):
8089 continue
0 commit comments