def create_document_row(resource, reg_no, reg_date, body, app_type, cancelled=False): info('INTENT: Create document row for %s %d %s', app_type, reg_no, reg_date) doc_row = { 'class': class_to_numeric(body['class_of_charge']), 'reg_no': str(reg_no), 'date': reg_date, 'orig_class': class_to_numeric(body['class_of_charge']), 'orig_number': body['registration']['number'], 'orig_date': body['registration']['date'], 'canc_ind': '', 'type': app_type } if cancelled: doc_row['canc_ind'] = 'Y' url = CONFIG['LEGACY_DB_URI'] + '/doc_info' + resource info(" > Row data: " + json.dumps(doc_row)) put = requests.put(url, data=json.dumps(doc_row), headers=get_headers({'Content-Type': 'application/json'})) logging.debug('PUT %s - %s - %s', url, str(put.status_code), put.text) if put.status_code != 200: raise SynchroniserError('PUT /doc_info - ' + str(put.status_code)) info(" > Success") return put
def receive_new_regs(body): global step error_messages = '' for application in body['data']: number = application['number'] date = application['date'] info("Process registration %d/%s", number, date) step = '5.1' response = requests.get(CONFIG['REGISTER_URI'] + '/registrations/' + date + '/' + str(number), headers=get_headers()) if response.status_code != 200: msg = "Unexpected response {} on GET /registrations/{}/{}: .".format(response.status_code, date, number, response.text) error(msg) error_messages += msg else: logging.debug('Registration retrieved') body = response.json() step = '5.2' try: move_images(number, date, body['class_of_charge']) except SynchroniserError as e: error_messages += str(e) error(str(e)) continue step = '5.3' converted = create_legacy_data(body) try: create_lc_row(converted) except SynchroniserError as e: msg = str(e) error(" > FAILED: " + msg) error_messages += msg step = '5.4' coc = class_to_numeric(body['class_of_charge']) try: create_document_row("/{}/{}/{}".format(number, date, coc), number, date, body, 'NR') except SynchroniserError as e: error_messages += str(e) error(" > FAILED: " + str(e)) if error_messages != '': raise SynchroniserError(error_messages)
def receive_amendment(body, sync_date): global step # Get history from the first registration number if len(body['data']) == 0: raise SynchroniserError("Received empty amendment data list") sync_date = datetime.datetime.strptime(sync_date, '%Y-%m-%d').date() number = body['data'][0]['number'] date = body['data'][0]['date'] step = '6.1' history = get_history(number, date) if len(history) <= 1: # raise SynchroniserError("Received insufficient historical data for amendment") # current_record = history[0] # The first item is the amended (current) record # amendment_type = current_record['application'] # Get application_type from history[0]['application'] # previous_record = history[1] # The second item is the predecessor error_messages = '' prev = {'number': '', 'date': ''} step = '6.2' for hist_index, item in enumerate(history): if len(item['registrations']) < len(history[-1]['registrations']): raise SynchroniserError("Unable to handle cancellation implied by len[current] < len[previous]") for index, reg_summary in enumerate(item['registrations']): # if reg_summary['number'] == prev['number'] and reg_summary['date'] == prev['date']: # info('SKIPPING') # continue # Just don't repeat the same thing prev['number'] = reg_summary['number'] prev['date'] = reg_summary['date'] step = '6.3' reg = get_registration(reg_summary['number'], reg_summary['date']) reg_date = datetime.datetime.strptime(reg_summary['date'], '%Y-%m-%d').date() step = '6.4' if reg_date == sync_date: # 'today' as far as sync is concerned step = '6.9' move_images(reg_summary['number'], reg_summary['date'], reg['class_of_charge']) step = '6.10' if 'amends_registration' in reg and 'PAB' in reg['amends_registration']: pab_amend_case(reg) # Something new; if revealed, make LC row step = '6.12' if not has_expired(reg['expired_date']): step = '6.13' info("%s %d %s is current", reg['class_of_charge'], reg_summary['number'], reg_summary['date']) try: create_lc_row(create_legacy_data(reg)) except SynchroniserError as e: error_messages += str(e) error(str(e)) # Create document row regardless, unless a correction # DEFECT fix: this may be the new reg, so won't have amends_registration field step = '6.14' if 'amends_registration' not in reg or reg['amends_registration']['type'] != 'Correction': original = history[-1] if index < len(original['registrations']): predecessor = original['registrations'][index] else: predecessor = original['registrations'][0] coc = class_to_numeric(reg['class_of_charge']) step = '6.15' res = "/{}/{}/{}".format(reg_summary['number'], reg_summary['date'], coc) amtype = get_amendment_type(reg) try: create_document_row(res, reg_summary['number'], reg_summary['date'], { 'class_of_charge': original['class_of_charge'], 'registration': {'number': predecessor['number'], 'date': predecessor['date']} }, amtype) except SynchroniserError as e: error_messages += str(e) error(str(e)) elif reg_date < sync_date: step = '6.5' # Something old; if not revealed, remove LC row if it exists step = '6.6' if has_expired(reg['expired_date']): step = '6.7' info("%s %d %s has expired", reg['class_of_charge'], reg_summary['number'], reg_summary['date']) try: delete_lc_row(reg_summary['number'], reg_summary['date'], reg['class_of_charge']) except SynchroniserError as e: error_messages += str(e) error(str(e)) # If revealed, replace LC row as appropriate (some fields like addl info may change) else: step = '6.8' try: create_lc_row(create_legacy_data(reg)) except SynchroniserError as e: error_messages += str(e) error(str(e)) # Documents do not change else: raise SynchroniserError('Error: synchronising records from the future') # Or being run for a past day if item['application'] == 'Correction': # No need to proceed break if error_messages != '': raise SynchroniserError(error_messages)
def receive_cancellation(body): global step # Everything in body pertains to *one* detail record, but multiple legacy records # However, still assume an image per item error_messages = '' if len(body['data']) > 0: # Iterate through the whole history... number = body['data'][0]['number'] date = body['data'][0]['date'] step = '7.1' history = get_history(number, date) original_record = history[-1] # We'll need this later logging.debug(original_record) original_registrations = [] step = '7.2' for reg in original_record['registrations']: oreg = get_registration(reg['number'], reg['date']) original_registrations.append(oreg) step = '7.3' for item in history[1:]: # Don't need to to 'remove' the cancellation itself - it's not there for index, reg in enumerate(item['registrations']): step = '7.4' try: delete_lc_row(reg['number'], reg['date'], item['class_of_charge']) except SynchroniserError as e: error(str(e)) error_messages += str(e) step = '7.5' try: cancel_document(reg['number'], reg['date'], item['class_of_charge']) except SynchroniserError as e: error(str(e)) error_messages += str(e) else: raise SynchroniserError("Unexpected lack of data for id {}".format(body['id'])) body['data'] = sorted(body['data'], key=get_regn_key) original_registrations = sorted(original_registrations, key=get_full_regn_key) if len(original_registrations) != len(body['data']): msg = "Unable to process unmatched cancellation lengths. " error(msg) error_messages += msg #raise SynchroniserError("Unable to process unmatched cancellation lengths") step = '7.6' for index, ref in enumerate(body['data']): number = ref['number'] date = ref['date'] registration = get_registration(number, date) move_images(number, date, registration['class_of_charge']) resource = "/{}/{}/{}".format(registration['registration']['number'], registration['registration']['date'], class_to_numeric(registration['class_of_charge'])) original_registration = original_registrations[index] try: create_document_row(resource, number, date, { "class_of_charge": original_registration['class_of_charge'], "registration": { "number": original_registration['registration']['number'], "date": original_registration['registration']['date'] } }, "CN", True) except SynchroniserError as e: error(str(e)) error_messages += str(e) if error_messages != '': raise SynchroniserError(error_messages)