1
- from api .api import common_api
2
- from config import engine
3
- from flask import jsonify , current_app
4
- from sqlalchemy .sql import text
5
- import requests
6
- import time
7
- from datetime import datetime
8
-
9
- from sqlalchemy .dialects .postgresql import insert
10
1
from sqlalchemy import Table , MetaData
11
- from pipeline import flow_script
2
+ from sqlalchemy .orm import sessionmaker
3
+
12
4
from config import engine
13
- from flask import request , redirect , jsonify , current_app
14
- from api . file_uploader import validate_and_arrange_upload
15
- from sqlalchemy . orm import Session , sessionmaker
5
+
6
+ import structlog
7
+ logger = structlog . get_logger ()
16
8
17
9
18
10
def insert_animals (animal_list ):
@@ -53,11 +45,9 @@ def truncate_animals():
53
45
54
46
Session = sessionmaker (engine )
55
47
session = Session ()
56
- metadata = MetaData ()
57
- sla = Table ("shelterluv_animals" , metadata , autoload = True , autoload_with = engine )
58
48
59
49
truncate = "TRUNCATE table shelterluv_animals;"
60
- result = session .execute (truncate )
50
+ session .execute (truncate )
61
51
62
52
session .commit () # Commit all inserted rows
63
53
session .close ()
@@ -69,81 +59,74 @@ def truncate_events():
69
59
"""Truncate the shelterluv_events table"""
70
60
71
61
Session = sessionmaker (engine )
72
- session = Session ()
73
- metadata = MetaData ()
74
- sla = Table ("sl_animal_events" , metadata , autoload = True , autoload_with = engine )
75
-
76
- truncate = "TRUNCATE table sl_animal_events;"
77
- result = session .execute (truncate )
78
-
79
- session .commit () # Commit all inserted rows
80
- session .close ()
62
+ with Session () as session :
63
+ truncate = "TRUNCATE table sl_animal_events;"
64
+ session .execute (truncate )
65
+ session .commit ()
81
66
82
67
return 0
83
68
84
-
85
69
def insert_events (event_list ):
86
70
"""Insert event records into sl_animal_events table and return row count. """
87
71
88
72
# Always a clean insert
89
73
truncate_events ()
90
74
91
75
Session = sessionmaker (engine )
92
- session = Session ()
93
- metadata = MetaData ()
94
- sla = Table ("sl_animal_events" , metadata , autoload = True , autoload_with = engine )
95
-
96
- # TODO: Pull from DB - inserted in db_setup/base_users.py/populate_sl_event_types()
97
- event_map = {
98
- "Outcome.Adoption" : 1 ,
99
- "Outcome.Foster" : 2 ,
100
- "Outcome.ReturnToOwner" : 3 ,
101
- "Intake.AdoptionReturn" : 4 ,
102
- "Intake.FosterReturn" :5
103
- }
104
-
105
- # """ INSERT INTO "sl_event_types" ("id","event_name") VALUES
106
- # ( 1,'Outcome.Adoption' ),
107
- # ( 2,'Outcome.Foster' ),
108
- # ( 3,'Outcome.ReturnToOwner' ),
109
- # ( 4,'Intake.AdoptionReturn' ),
110
- # ( 5,'Intake.FosterReturn' ) """
111
-
112
-
113
-
114
-
115
- # Event record: [ AssociatedRecords[Type = Person]["Id"]',
116
- # AssociatedRecords[Type = Animal]["Id"]',
117
- # "Type",
118
- # "Time"
119
- # ]
120
- #
121
- # In db: ['id',
122
- # 'person_id',
123
- # 'animal_id',
124
- # 'event_type',
125
- # 'time']
126
-
127
- ins_list = [] # Create a list of per-row dicts
128
- for rec in event_list :
129
- ins_list .append (
130
- {
131
- "person_id" : next (
132
- filter (lambda x : x ["Type" ] == "Person" , rec ["AssociatedRecords" ])
133
- )["Id" ],
134
- "animal_id" : next (
135
- filter (lambda x : x ["Type" ] == "Animal" , rec ["AssociatedRecords" ])
136
- )["Id" ],
137
- "event_type" : event_map [rec ["Type" ]],
138
- "time" : rec ["Time" ],
139
- }
140
- )
141
-
142
- # TODO: Wrap with try/catch
143
- ret = session .execute (sla .insert (ins_list ))
144
-
145
- session .commit () # Commit all inserted rows
146
- session .close ()
76
+ with Session () as session :
77
+ metadata = MetaData ()
78
+ sla = Table ("sl_animal_events" , metadata , autoload = True , autoload_with = engine )
79
+
80
+ # TODO: Pull from DB - inserted in db_setup/base_users.py/populate_sl_event_types()
81
+ event_map = {
82
+ "Outcome.Adoption" : 1 ,
83
+ "Outcome.Foster" : 2 ,
84
+ "Outcome.ReturnToOwner" : 3 ,
85
+ "Intake.AdoptionReturn" : 4 ,
86
+ "Intake.FosterReturn" :5
87
+ }
88
+
89
+ # """ INSERT INTO "sl_event_types" ("id","event_name") VALUES
90
+ # ( 1,'Outcome.Adoption' ),
91
+ # ( 2,'Outcome.Foster' ),
92
+ # ( 3,'Outcome.ReturnToOwner' ),
93
+ # ( 4,'Intake.AdoptionReturn' ),
94
+ # ( 5,'Intake.FosterReturn' ) """
95
+
96
+
97
+
98
+
99
+ # Event record: [ AssociatedRecords[Type = Person]["Id"]',
100
+ # AssociatedRecords[Type = Animal]["Id"]',
101
+ # "Type",
102
+ # "Time"
103
+ # ]
104
+ #
105
+ # In db: ['id',
106
+ # 'person_id',
107
+ # 'animal_id',
108
+ # 'event_type',
109
+ # 'time']
110
+
111
+ ins_list = [] # Create a list of per-row dicts
112
+ for rec in event_list :
113
+ ins_list .append (
114
+ {
115
+ "person_id" : next (
116
+ filter (lambda x : x ["Type" ] == "Person" , rec ["AssociatedRecords" ])
117
+ )["Id" ],
118
+ "animal_id" : next (
119
+ filter (lambda x : x ["Type" ] == "Animal" , rec ["AssociatedRecords" ])
120
+ )["Id" ],
121
+ "event_type" : event_map [rec ["Type" ]],
122
+ "time" : rec ["Time" ],
123
+ }
124
+ )
125
+
126
+ # TODO: Wrap with try/catch
127
+ ret = session .execute (sla .insert (ins_list ))
128
+ session .commit ()
129
+ logger .debug ("finished inserting events" )
147
130
148
131
return ret .rowcount
149
132
0 commit comments