def get_object_list(self): """ Gets objects, converts datetimes found in them and looks up counts of urgent and assigned checks. """ self.need_attention_count = self.session.get( self.get_object_list_endpoint_path(), params=dict( self.get_api_request_params(), **{ 'started_at__lt': self.need_attention_date.strftime('%Y-%m-%d %H:%M:%S'), 'offset': 0, 'limit': 1, })).json()['count'] self.my_list_count = self.session.get( self.get_object_list_endpoint_path(), params=dict( self.get_api_request_params(), **{ 'assigned_to': self.request.user.pk, 'offset': 0, 'limit': 1, })).json()['count'] object_list = convert_date_fields(super().get_object_list(), include_nested=True) for check in object_list: check['needs_attention'] = check['credit'][ 'started_at'] < self.need_attention_date return object_list
def test_converts_lists(self): """ Test that the function converts dates and datetimes correctly when the input arg is a list. """ objs = [ { 'started_at': '2019-07-01', 'received_at': '2019-07-02T10:00:00Z', # utc 'credited_at': '2019-07-03', 'refunded_at': '2019-07-04T11:01:00+01:00', # BST+1 'created': '2019-07-05', 'triggered_at': '2019-07-06T10:02:00Z', } ] converted_objects = convert_date_fields(objs) self.assertEqual( converted_objects[0], { 'started_at': datetime.date(2019, 7, 1), 'received_at': localtime(datetime.datetime(2019, 7, 2, 10, 0, tzinfo=utc)), 'credited_at': datetime.date(2019, 7, 3), 'refunded_at': make_aware(datetime.datetime(2019, 7, 4, 11, 1)), 'created': datetime.date(2019, 7, 5), 'triggered_at': localtime(datetime.datetime(2019, 7, 6, 10, 2, tzinfo=utc)), }, )
def test_handles_timezone_aware_and_naive_datetimes(self): objs = [{ # dates in BST 'started_at': '2021-05-07', 'received_at': '2021-05-07T12:00:00', # naive so assumed Europe/London (i.e. BST) 'credited_at': '2021-05-07T12:00:00Z', # 12pm UTC is 1pm in Europe/London (i.e. BST) 'refunded_at': '2021-05-07T12:00:00+01:00', # explicit time zone # dates in GMT 'created': '2021-03-01T00:00:00', # naive so assumed Europe/London (i.e. GMT) 'triggered_at': '2021-03-01T00:00:00Z', # 12am UTC is 12am in Europe/London (i.e. GMT) 'actioned_at': '2021-03-01T00:00:00+00:00', # explicit time zone }] converted_objects = convert_date_fields(objs) self.assertEqual( converted_objects[0], { 'started_at': datetime.date(2021, 5, 7), 'received_at': make_aware(datetime.datetime(2021, 5, 7, 12)), 'credited_at': make_aware(datetime.datetime(2021, 5, 7, 13)), 'refunded_at': make_aware(datetime.datetime(2021, 5, 7, 12)), 'created': make_aware(datetime.datetime(2021, 3, 1)), 'triggered_at': make_aware(datetime.datetime(2021, 3, 1)), 'actioned_at': make_aware(datetime.datetime(2021, 3, 1)), }, )
def get_object_list(self): events = convert_date_fields(super().get_object_list()) date_groups = map(summarise_date_group, group_events_by_date(events)) self.page_count = int(ceil(self.date_count / self.page_size)) self.total_count = self.date_count return date_groups
def email_export_xlsx(*, object_type, user, session, endpoint_path, filters, export_description, attachment_name): if not get_language(): language = getattr(settings, 'LANGUAGE_CODE', 'en') activate(language) if object_type == 'credits': export_message = gettext( 'Attached are the credits you exported from ‘%(service_name)s’.') elif object_type == 'disbursements': export_message = (gettext( 'Attached are the bank transfer and cheque disbursements you exported from ‘%(service_name)s’.' ) + ' ' + gettext('You can’t see cash or postal orders here.')) elif object_type == 'senders': export_message = gettext( 'Attached is a list of payment sources you exported from ‘%(service_name)s’.' ) elif object_type == 'prisoners': export_message = gettext( 'Attached is a list of prisoners you exported from ‘%(service_name)s’.' ) else: raise NotImplementedError(f'Cannot export {object_type}') api_session = get_api_session_with_session(user, session) generated_at = timezone.now() object_list = convert_date_fields( retrieve_all_pages_for_path(api_session, endpoint_path, **filters)) serialiser = ObjectListSerialiser.serialiser_for(object_type) workbook = serialiser.make_workbook(object_list) output = save_virtual_workbook(workbook) attachment_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' template_context = prepare_context({ 'export_description': export_description, 'generated_at': generated_at, 'export_message': export_message % { 'service_name': gettext('Prisoner money intelligence') } }) subject = '%s - %s' % (gettext('Prisoner money intelligence'), gettext('Exported data')) text_body = template_loader.get_template( 'security/email/export.txt').render(template_context) html_body = template_loader.get_template( 'security/email/export.html').render(template_context) email = AnymailMessage( subject=subject, body=text_body.strip('\n'), from_email=default_from_address(), to=[user.email], tags=['export'], ) email.attach_alternative(html_body, mimetype='text/html') email.attach(attachment_name, output, mimetype=attachment_type) email.send()
def get_object(self): response = super().get_object() if not response: return {} if response['count'] != 1: raise Http404('credit not found') credit = convert_date_fields(response['results'])[0] return credit
def _get_related_credits(api_session, detail_object): # Get the credits from the same sender that were actioned by FIU if detail_object['credit']['sender_profile']: sender_response = retrieve_all_pages_for_path( api_session, f'/senders/{detail_object["credit"]["sender_profile"]}/credits/', **{ 'exclude_credit__in': detail_object['credit']['id'], 'security_check__isnull': False, 'only_completed': False, 'security_check__actioned_by__isnull': False, 'include_checks': True }) else: sender_response = [] sender_credits = convert_date_fields(sender_response, include_nested=True) # Get the credits to the same prisoner that were actioned by FIU if detail_object['credit']['prisoner_profile']: prisoner_response = retrieve_all_pages_for_path( api_session, f'/prisoners/{detail_object["credit"]["prisoner_profile"]}/credits/', **{ # Exclude any credits displayed as part of sender credits, to prevent duplication where # both prisoner and sender same as the credit in question 'exclude_credit__in': ','.join([str(detail_object['credit']['id'])] + [str(c['id']) for c in sender_credits]), 'security_check__isnull': False, 'only_completed': False, 'security_check__actioned_by__isnull': False, 'include_checks': True }) else: prisoner_response = [] prisoner_credits = convert_date_fields(prisoner_response, include_nested=True) return sorted(prisoner_credits + sender_credits, key=lambda c: c['security_check']['actioned_at'], reverse=True)
def get_object(self): disbursement = super().get_object() if disbursement: disbursement = convert_date_fields([disbursement])[0] self.format_log_set(disbursement) disbursement['recipient_name'] = ( '%s %s' % (disbursement['recipient_first_name'], disbursement['recipient_last_name'])).strip() return disbursement
def format_log_set(self, disbursement): def format_staff_name(log_item): username = log_item['user']['username'] or _('Unknown user') log_item['staff_name'] = ' '.join( filter(None, (log_item['user']['first_name'], log_item['user']['last_name']))) or username return log_item disbursement['log_set'] = sorted( map(format_staff_name, convert_date_fields(disbursement.get('log_set', []))), key=lambda log_item: log_item['created'])
def test_doesnt_convert_falsy_values(self): """ Test that if the values are falsy, they are not converted. """ objs = [{ 'started_at': None, 'received_at': '', }] converted_objects = convert_date_fields(objs) self.assertEqual(converted_objects[0], { 'started_at': None, 'received_at': '', })
def get_object(self): """ Gets the check object :return: dict or None if not found """ try: obj = self.session.get(self.get_object_endpoint_path()).json() convert_dates_obj = convert_date_fields(obj, include_nested=True) convert_dates_obj['needs_attention'] = convert_dates_obj['credit']['started_at'] < self.need_attention_date return obj except RequestException as e: self._handle_request_exception(e, 'Check') return None
def get_object_list(self): """ Gets objects, converts datetimes found in them. """ object_list = convert_date_fields(super().get_object_list(), include_nested=True) self.my_list_count = self.session.get(self.get_object_list_endpoint_path(), params={ 'status': 'pending', 'credit_resolution': 'initial', 'assigned_to': self.request.user.pk, 'offset': 0, 'limit': 1 }).json()['count'] return object_list
def test_handles_invalid_strings(self): """ Test that if the values are invalid, they are not converted. """ objs = [{ 'started_at': '2019-13-01', 'received_at': 'invalid', }] with silence_logger(): converted_objects = convert_date_fields(objs) self.assertEqual(converted_objects[0], { 'started_at': '2019-13-01', 'received_at': 'invalid', })
def get_unbound_active_auto_accept_state( self, api_session, debit_card_sender_details_id: int, prisoner_profile_id: int) -> Optional[dict]: query_existing_auto_accept_rule = api_session.get( '/security/checks/auto-accept', params={ 'prisoner_profile_id': prisoner_profile_id, 'debit_card_sender_details_id': debit_card_sender_details_id }) payload = query_existing_auto_accept_rule.json().get('results') if len(payload) == 1 and self.get_latest_auto_accept_state( payload[0]).get('active'): return convert_date_fields( self.get_latest_auto_accept_state(payload[0]))
def get_object_list(self): """ Gets objects, converts datetimes found in them. """ object_list = convert_date_fields(super().get_object_list(), include_nested=True) self.my_list_count = self.session.get(self.get_check_list_endpoint_path(), params={ 'status': 'pending', 'credit_resolution': 'initial', 'assigned_to': self.request.user.pk, 'offset': 0, 'limit': 1 }).json()['count'] self.initial_index = ((self.cleaned_data.get('page', 1) - 1) * self.page_size) + 1 self.final_index = min( self.cleaned_data.get('page', 1) * self.page_size, self.total_count ) return object_list
def test_doesnt_convert_non_strings(self): """ Test that if the values are not strings, they are not converted. """ objs = [ { 'started_at': 1, 'received_at': ['date'], 'credited_at': {'key': 'value'}, 'refunded_at': datetime.date(2019, 7, 1), } ] converted_objects = convert_date_fields(objs) self.assertEqual( converted_objects[0], { 'started_at': 1, 'received_at': ['date'], 'credited_at': {'key': 'value'}, 'refunded_at': datetime.date(2019, 7, 1), } )
def test_include_nested(self): """ Test that if include_nested = True, nested values are converted as well. """ objs = [ { 'field': { 'started_at': '2019-07-01', 'received_at': '2019-07-02T10:00:00Z', # utc }, } ] converted_objects = convert_date_fields(objs, include_nested=True) self.assertEqual( converted_objects[0], { 'field': { 'started_at': datetime.date(2019, 7, 1), 'received_at': localtime(datetime.datetime(2019, 7, 2, 10, 0, tzinfo=utc)), }, }, )
def get_object_for_template(self, obj): return convert_date_fields(obj, include_nested=True)
def get_object_list(self): return convert_date_fields(super().get_object_list())
def get_complete_object_list(self): filters = self.get_api_request_params() return convert_date_fields(retrieve_all_pages_for_path( self.session, self.get_object_list_endpoint_path(), **filters) )
def get_object_list(self): object_list = super().get_object_list() convert_date_fields(object_list) return object_list