-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_past_sales_info.py
97 lines (91 loc) · 2.77 KB
/
write_past_sales_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from connect_db import get_db_connection
import logging
def past_sales_check_in_db(pid, sid, contract_date):
cnxn = get_db_connection()
cursor = cnxn.cursor()
try:
row = cursor.execute("""
select *
from (
select id,
row_number() OVER (order by id desc) row_num
from past_sales_info
where property_id = ?
and source_id = ?
and contract_date = ?) t
where row_num = 1
""", pid, sid, contract_date)
row = cursor.fetchone()
cursor.close()
cnxn.close()
if row:
return row[0]
else:
return None
except Exception as e:
logging.warning('Could not query db: {}, {}, {}: {}'.format(pid,
sid,
contract_date,
e))
cursor.close()
cnxn.close()
def past_sales_write_to_db(pid, sid, data):
cnxn = get_db_connection()
cursor = cnxn.cursor()
price = 0
listed_date = None
contract_date = None
transfer_date = None
sale_type = None
try:
price = data['price']
except KeyError:
price = None
try:
listed_date = data['listed_date']
except KeyError:
listed_date = None
try:
contract_date = data['contract_date']
except KeyError:
print('Data must have contract date')
return None
try:
transfer_date = data['transfer_date']
except KeyError:
transfer_date = None
try:
sale_type = data['sale_type']
except KeyError:
sale_type = None
try:
cursor.execute("""
INSERT INTO past_sales_info (
property_id,
source_id,
price,
list_date,
contract_date,
transfer_date,
sale_type)
VALUES (
?, ?, ?, ?, ?, ?, ?)""",
pid,
sid,
price,
listed_date,
contract_date,
transfer_date,
sale_type,
)
cursor.commit()
cursor.close()
cnxn.close()
except Exception as e:
logging.warning('Could not query db: {}, {}, {}: {}'.format(pid,
sid,
data,
e))
cursor.close()
cnxn.close()
pass