Created
February 23, 2021 18:01
-
-
Save bennett39/38f9a877ec51fc2d02bb073e9fca9a48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def analyze_audit_history(self): | |
""" Parse audit history response and separate out deletes vs modifications/creations """ | |
last_modified_by_type, modified_ids, deleted_ids = {}, set(), set() | |
last_modified_by_type['glbatch'] = arrow.now() # Always consider glbatches modified | |
for audit_trail in self._get_audit_trail(): | |
for event in audit_trail.response.data.find_all('audithistory'): | |
object_type = event.objecttype.get_text() | |
# Consolidate journal and glbatch to be the same, since they hit the same endpoint | |
object_type = 'glbatch' if object_type == 'journal' else object_type | |
if object_type in self.methods_by_object_type: | |
access_mode = event.accessmode.get_text() | |
if access_mode in ['Deleted', 'Delete Event', 'D']: | |
deleted_ids.add(event.objectkey.get_text()) | |
elif access_mode in ['Modify Event', 'Modified', 'M', 'Add Event', 'Created', 'C']: | |
modified_ids.add(event.objectkey.get_text()) | |
event_time = arrow.get(event.accesstime.get_text(), self.INTACCT_DATE_FORMAT) | |
existing_time = last_modified_by_type.get(object_type) | |
max_time = max(event_time, existing_time) if existing_time else event_time | |
last_modified_by_type[object_type] = max_time | |
return last_modified_by_type, modified_ids, deleted_ids | |
def _get_audit_trail(self): | |
""" A generator that returns pages of audit trail history via the Intacct API """ | |
last_synced = self.erp_connection.metadata.get('last_transaction_sync') | |
last_synced = arrow.get(last_synced).format(self.INTACCT_DATE_FORMAT) if last_synced else None | |
num_remaining, offset = 1, 0 | |
while num_remaining: | |
query = f''' | |
<object>AUDITHISTORY</object> | |
<select> | |
<field>ACCESSTIME</field> | |
<field>ACCESSMODE</field> | |
<field>OBJECTTYPE</field> | |
<field>OBJECTKEY</field> | |
<field>SOURCE</field> | |
</select> | |
<orderby> | |
<order> | |
<field>ACCESSTIME</field> | |
<descending/> | |
</order> | |
</orderby> | |
<pagesize>2000</pagesize> | |
<offset>{offset}</offset> | |
''' | |
if last_synced: | |
query += f''' | |
<filter> | |
<greaterthan> | |
<field>ACCESSTIME</field> | |
<value>{last_synced}</value> | |
</greaterthan> | |
</filter> | |
''' | |
query = '<query>' + query + '</query>' | |
root = IntacctClient._make_xml_request(self.erp_connection, query) | |
num_remaining = int(root.response.data['numremaining']) | |
offset += int(root.response.data['count']) | |
yield root |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment