@@ -39,30 +39,51 @@ def _build_record(cls, raw_object):
39
39
40
40
'name' : 'Rajiv Sinclair'
41
41
}
42
- ]
42
+ ],
43
+ 'Date requested by user' : raw_object .timestamp .strftime (format = '%Y-%m-%d' )
43
44
}
44
45
45
46
@classmethod
46
47
def _delay_upload (cls , raw_object ):
47
48
record = cls ._build_record (raw_object )
49
+
50
+ if raw_object .airtable_id :
51
+ airtable_id = cls ._update_airtable_row (raw_object .airtable_id , record )
52
+ else :
53
+ airtable_id = cls ._add_airtable_row (record )
54
+ time .sleep (cls .API_LIMIT )
55
+ return airtable_id
56
+
57
+ @classmethod
58
+ def _add_airtable_row (cls , record ):
48
59
try :
49
60
res = cls ._get_foia_airtable ().insert (record )
50
- time .sleep (cls .API_LIMIT )
51
61
return res ['id' ]
52
62
except HTTPError :
53
63
return ''
54
64
55
65
@classmethod
56
- def _get_uploaded_objects (cls ):
66
+ def _update_airtable_row (cls , airtable_id , record ):
67
+ try :
68
+ res = cls ._get_foia_airtable ().update (airtable_id , record )
69
+ return res ['id' ]
70
+ except HTTPError as err :
71
+ if '404' in err .args [0 ].split (' ' ):
72
+ return cls ._add_airtable_row (record )
73
+ else :
74
+ return airtable_id
75
+
76
+ @classmethod
77
+ def _get_uploaded_objects (cls , update_all_records = False ):
57
78
raise NotImplementedError
58
79
59
80
@classmethod
60
81
def _post_handle (cls , upload_results ):
61
82
raise NotImplementedError
62
83
63
84
@classmethod
64
- def upload (cls ):
65
- raw_objects = cls ._get_uploaded_objects ()
85
+ def upload (cls , update_all_records = False ):
86
+ raw_objects = cls ._get_uploaded_objects (update_all_records )
66
87
67
88
uploaded_results = [
68
89
(raw_object , cls ._delay_upload (raw_object ))
@@ -78,25 +99,26 @@ def _build_data(cls, document_request):
78
99
officer_allegations = allegation .officerallegation_set .select_related ('officer' )\
79
100
.order_by ('officer__first_name' , 'officer__last_name' )
80
101
officers_info = [
81
- "{officer_name}(ID {officer_id})" .format (
82
- officer_id = officer_allegation .officer .id ,
83
- officer_name = officer_allegation .officer .full_name
84
- )
102
+ f'{ officer_allegation .officer .full_name } (ID { officer_allegation .officer .id } )'
85
103
for officer_allegation in officer_allegations if officer_allegation .officer
86
104
]
87
- explanation = "Officers: {}" .format (', ' .join (officers_info )) if officers_info else ''
88
- requested_for = "CR {crid}" .format (crid = allegation .crid )
89
- pre_2006 = allegation .incident_date and allegation .incident_date .year < 2006
105
+ explanation = f"Officers: { ', ' .join (officers_info )} " if officers_info else ''
106
+ requested_for = f'CR { allegation .crid } '
90
107
agencies = [
91
108
settings .AIRTABLE_CPD_AGENCY_ID
92
- if pre_2006 or document_request .investigated_by_cpd ()
109
+ if document_request .investigated_by_cpd
93
110
else settings .AIRTABLE_COPA_AGENCY_ID
94
111
]
95
112
return explanation , requested_for , agencies
96
113
97
114
@classmethod
98
- def _get_uploaded_objects (cls ):
99
- return AttachmentRequest .objects .filter (airtable_id = '' )
115
+ def _get_uploaded_objects (cls , update_all_records = False ):
116
+ records = AttachmentRequest .objects .annotate_investigated_by_cpd ().select_related ('allegation' )
117
+
118
+ if not update_all_records :
119
+ records = records .filter (airtable_id = '' )
120
+
121
+ return records
100
122
101
123
@classmethod
102
124
def _post_handle (cls , uploaded_results ):
@@ -106,7 +128,7 @@ def _post_handle(cls, uploaded_results):
106
128
attachment_request .airtable_id = record_id
107
129
uploaded_attachment_requests .append (attachment_request )
108
130
109
- AttachmentRequest .objects .bulk_update (
131
+ AttachmentRequest .bulk_objects .bulk_update (
110
132
uploaded_attachment_requests ,
111
133
update_fields = ['airtable_id' ],
112
134
batch_size = 1000
@@ -117,17 +139,19 @@ class TRRRequestAirTableUploader(AirTableUploader):
117
139
@classmethod
118
140
def _build_data (cls , document_request ):
119
141
officer = document_request .trr .officer
120
- explanation = "Officer: {officer_name}(ID {officer_id})" .format (
121
- officer_id = officer .id ,
122
- officer_name = officer .full_name
123
- ) if officer else ''
142
+ explanation = f'Officer: { officer .full_name } (ID { officer .id } )' if officer else ''
124
143
125
- requested_for = " TRR {trrid}" . format ( trrid = document_request .trr_id )
144
+ requested_for = f' TRR { document_request .trr_id } '
126
145
return explanation , requested_for , []
127
146
128
147
@classmethod
129
- def _get_uploaded_objects (cls ):
130
- return TRRAttachmentRequest .objects .filter (airtable_id = '' )
148
+ def _get_uploaded_objects (cls , update_all_records = False ):
149
+ records = TRRAttachmentRequest .objects .all ()
150
+
151
+ if not update_all_records :
152
+ records = records .filter (airtable_id = '' )
153
+
154
+ return records
131
155
132
156
@classmethod
133
157
def _post_handle (cls , uploaded_results ):
0 commit comments