1
-
2
1
from odoo import fields , models , api
3
2
from dateutil .relativedelta import relativedelta
4
3
from datetime import date
8
7
9
8
10
9
class EstateProperty (models .Model ):
11
-
12
10
_name = "estate.property"
13
11
_description = "Estate Model"
14
12
15
13
name = fields .Char ()
16
- description = fields .Text ()
14
+ description = fields .Text ()
17
15
postcode = fields .Text ()
18
- date_availability = fields .Date (copy = False , default = lambda self :date .today () + relativedelta (months = 3 ))
16
+ date_availability = fields .Date (
17
+ copy = False , default = lambda self : date .today () + relativedelta (months = 3 )
18
+ )
19
19
expected_price = fields .Float ()
20
20
selling_price = fields .Integer (readonly = True , copy = False )
21
21
bedrooms = fields .Integer (default = 2 )
22
22
living_area = fields .Integer ()
23
23
facades = fields .Integer ()
24
24
garage = fields .Boolean ()
25
- garden = fields .Boolean ()
26
- garden_area = fields .Integer ()
25
+ garden = fields .Boolean ()
26
+ garden_area = fields .Integer ()
27
27
garden_orientation = fields .Selection (
28
- string = 'Type' ,
29
- selection = [('north' , 'North' ), ('south' , 'South' ),('east' , 'East' ),('west' , 'West' )],
30
- )
28
+ string = "Type" ,
29
+ selection = [
30
+ ("north" , "North" ),
31
+ ("south" , "South" ),
32
+ ("east" , "East" ),
33
+ ("west" , "West" ),
34
+ ],
35
+ )
31
36
32
37
active = fields .Boolean (default = True )
33
38
34
39
state = fields .Selection (
35
40
selection = [
36
- ('new' , 'New' ), ('offer_received' , 'Offer Received ' ),
37
- ('offer_accepted' , 'Offer Accepted' ), ('sold' ,'Sold' ),
38
- ('cancelled' ,'Cancelled' )],
39
- string = 'Status' ,
40
- default = 'new' ,
41
- copy = False
41
+ ("new" , "New" ),
42
+ ("offer_received" , "Offer Received " ),
43
+ ("offer_accepted" , "Offer Accepted" ),
44
+ ("sold" , "Sold" ),
45
+ ("cancelled" , "Cancelled" ),
46
+ ],
47
+ string = "Status" ,
48
+ default = "new" ,
49
+ copy = False ,
42
50
)
43
51
44
52
# property will have Many to one relation with property type since many properties can belong to one property type
45
53
46
- property_type_id = fields .Many2one ("estate.property.type" , "Property Type" )
47
-
48
- user_id = fields .Many2one ('res.users' , string = 'Salesperson' , copy = False , default = lambda self : self .env .user )
49
-
50
- partner_id = fields .Many2one ('res.partner' , string = 'Buyer' , copy = False , )
51
-
52
-
53
- tag_ids = fields .Many2many ("estate.property.tag" , string = "Tags" )
54
+ property_type_id = fields .Many2one ("estate.property.type" , "Property Type" )
54
55
56
+ user_id = fields .Many2one (
57
+ "res.users" ,
58
+ string = "Salesperson" ,
59
+ copy = False ,
60
+ default = lambda self : self .env .user ,
61
+ )
55
62
56
- offer_ids = fields .One2many ('estate.property.offer' , 'property_id' , string = "Offers" )
63
+ partner_id = fields .Many2one (
64
+ "res.partner" ,
65
+ string = "Buyer" ,
66
+ copy = False ,
67
+ )
57
68
69
+ tag_ids = fields .Many2many ("estate.property.tag" , string = "Tags" )
58
70
59
- total_area = fields .Integer ( compute = "_compute_total_property_area " )
71
+ offer_ids = fields .One2many ( "estate.property.offer" , "property_id" , string = "Offers " )
60
72
73
+ total_area = fields .Integer (compute = "_compute_total_property_area" )
61
74
62
- best_price = fields .Integer (compute = "_best_property_offer" )
75
+ best_price = fields .Integer (compute = "_best_property_offer" )
63
76
64
- status = fields .Char (default = ' new' )
77
+ status = fields .Char (default = " new" )
65
78
66
79
_order = "id desc"
67
80
68
-
69
- _sql_constraints = [('check_expected_price' , 'CHECK(expected_price > 0)' , 'Expected price must be strictly positive' ),
70
- ('check_selling_price' , 'CHECK(selling_price >= 0)' , 'Selling price should be positive' )
81
+ _sql_constraints = [
82
+ (
83
+ "check_expected_price" ,
84
+ "CHECK(expected_price > 0)" ,
85
+ "Expected price must be strictly positive" ,
86
+ ),
87
+ (
88
+ "check_selling_price" ,
89
+ "CHECK(selling_price >= 0)" ,
90
+ "Selling price should be positive" ,
91
+ ),
71
92
]
72
-
73
93
74
94
@api .depends ("garden_area" , "living_area" )
75
95
def _compute_total_property_area (self ):
76
96
for area in self :
77
97
self .total_area = self .garden_area + self .living_area
78
98
79
-
80
- @api .depends ('offer_ids.price' )
99
+ @api .depends ("offer_ids.price" )
81
100
def _best_property_offer (self ):
82
101
for record in self :
83
102
offers_list = record .mapped ("offer_ids.price" )
@@ -86,88 +105,70 @@ def _best_property_offer(self):
86
105
return
87
106
self .best_price = 0
88
107
89
-
90
-
91
-
92
108
# on change of garden status , update gardern area and its orientation
93
109
94
- @api .onchange (' garden' )
110
+ @api .onchange (" garden" )
95
111
def _onchange_garden_status (self ):
96
112
if self .garden :
97
113
self .garden_area = 10
98
- self .garden_orientation = ' north'
114
+ self .garden_orientation = " north"
99
115
return
100
116
self .garden_area = 0
101
- self .garden_orientation = ''
102
-
117
+ self .garden_orientation = ""
103
118
104
- # acts when property is sold
119
+ # acts when property is sold
105
120
# In case property is cancelled it cannot be sold
106
121
def action_sell_property (self ):
107
-
108
- # dictionary for the property status
109
- property_sell_status_dict = {
110
- 'new' : True ,
111
- 'sold' : True ,
112
- 'cancelled' : False
113
- }
122
+ # dictionary for the property status
123
+ property_sell_status_dict = {"new" : True , "sold" : True , "cancelled" : False }
114
124
115
125
for record in self :
116
126
print ("the object on sell action" , record .read ())
117
127
if property_sell_status_dict [record .status ]:
118
- record .status = ' sold'
119
- record .state = ' sold'
128
+ record .status = " sold"
129
+ record .state = " sold"
120
130
return
121
- raise UserError ('Cancelled property cannot be sold.' )
122
-
131
+ raise UserError ("Cancelled property cannot be sold." )
123
132
124
133
# action in case of cancel property button
125
- # If property is sold than Cannot be cancelled
134
+ # If property is sold than Cannot be cancelled
126
135
127
136
def action_cancel_property_selling (self ):
128
137
property_cancel_status_dict = {
129
- ' new' : True ,
130
- ' cancelled' : True ,
131
- ' sold' : False ,
138
+ " new" : True ,
139
+ " cancelled" : True ,
140
+ " sold" : False ,
132
141
}
133
142
for record in self :
134
143
if property_cancel_status_dict [record .status ]:
135
- record .status = 'cancelled'
136
- return
137
- raise UserError ('Sold property cannot be cancelled.' )
138
-
139
-
140
-
141
- # constrains for the selling price
144
+ record .status = "cancelled"
145
+ return
146
+ raise UserError ("Sold property cannot be cancelled." )
142
147
143
- @ api . constrains ( 'selling_price' , 'expected_price' )
148
+ # constrains for the selling price
144
149
150
+ @api .constrains ("selling_price" , "expected_price" )
145
151
def _check_selling_price (self ):
146
-
147
152
for data in self :
148
153
# if call will come after selling price change than it will allow updated price to work
149
- if data .selling_price <= 0 :
154
+ if data .selling_price <= 0 :
150
155
return
151
156
152
- price_float_ratio = ( data .selling_price / self .expected_price )
153
- ratio_diffrence = float_compare (price_float_ratio ,0.9 , precision_digits = 2 )
154
- if ratio_diffrence == - 1 :
157
+ price_float_ratio = data .selling_price / self .expected_price
158
+ ratio_diffrence = float_compare (price_float_ratio , 0.9 , precision_digits = 2 )
159
+ if ratio_diffrence == - 1 :
155
160
data .selling_price = 0
156
- raise ValidationError ('The selling price cannot be lower than 90% of the expected price' )
161
+ raise ValidationError (
162
+ "The selling price cannot be lower than 90% of the expected price"
163
+ )
157
164
return
158
-
159
-
160
165
161
166
# delete opration for the process
162
167
163
-
164
168
@api .ondelete (at_uninstall = False )
165
169
def _unlink_if_state_new_or_cancelled (self ):
166
170
for data in self :
167
- if not bool (self .state == 'new' or self .state == 'cancelled' ) :
168
- raise UserError ("Can't delete property which is not in new or cancelled state!" )
169
-
170
-
171
-
172
-
173
-
171
+ if not bool (self .state == "new" or self .state == "cancelled" ):
172
+ raise UserError (
173
+ "Can't delete property which is not in new or cancelled state!"
174
+ )
0 commit comments