Пример #1
0
    def test_one_prisoner_many_credits(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [credit_cls(1000, 'Mrs. Halls')] * 11, [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[9], [2]])
    def test_one_prisoner_many_credits(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [credit_cls(1000, 'Mrs. Halls')] * 11, [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[9], [2]])
Пример #3
0
    def test_two_prisoners(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [credit_cls(1000, 'Mrs. Halls')], []),
                     ('RICKIE RIPPIN', 'A1617FY', sample_location, [credit_cls(2500, 'JOHNSON & ASSOCIATES')], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1], [1]])
    def test_one_prisoner_one_credit(self, canvas_save, canvas_show_page, canvas_draw_string, canvas_draw_image):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [credit_cls(1000, 'Mrs. Halls')], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1]])
        self.assertEqual(canvas_draw_image.call_count, self.image_per_template)
Пример #5
0
    def test_one_prisoner_one_credit(self, canvas_save, canvas_show_page, canvas_draw_string, canvas_draw_image):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [credit_cls(1000, 'Mrs. Halls')], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1]])
        self.assertEqual(canvas_draw_image.call_count, self.image_per_template)
    def test_two_prisoners(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [credit_cls(1000, 'Mrs. Halls')], []),
                     ('RICKIE RIPPIN', 'A1617FY', sample_location, [credit_cls(2500, 'JOHNSON & ASSOCIATES')], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1], [1]])
    def test_long_text(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('NKFUVMY PMNDINERGGPGL-UMR-X-YFMESG', 'A1234AA', sample_location, [
            credit_cls(3035011, 'X' * 100)
        ], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1]])
Пример #8
0
    def test_one_prisoner_two_different_updates(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location,
                      [credit_cls(1000, 'Mrs. Halls')],
                      [disbursement_cls(2000, 'cheque', 'Mary', 'Johnson')])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1, 1]])
    def test_one_prisoner_two_different_updates(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location,
                      [credit_cls(1000, 'Mrs. Halls')],
                      [disbursement_cls(2000, 'cheque', 'Mary', 'Johnson')])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1, 1]])
Пример #10
0
    def test_long_text(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('NKFUVMY PMNDINERGGPGL-UMR-X-YFMESG', 'A1234AA', sample_location, [
            credit_cls(3035011, 'X' * 100)
        ], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1]])
Пример #11
0
    def test_one_prisoner_two_disbursements(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [], [
            disbursement_cls(2000, 'cheque', 'Rose', 'Johnson'),
            disbursement_cls(3000, 'bank_transfer', 'Janet', 'Johnson')
        ])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[2]])
Пример #12
0
    def test_location_complete(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [credit_cls(1000, 'Mrs. Halls')], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1]])
        expected_string = 'Wing: A    Landing: 2    Cell: 002'
        drawn_strings = [call[0][2] for call in canvas_draw_string.call_args_list]
        self.assertTrue(any(expected_string in drawn_string for drawn_string in drawn_strings))
    def test_location_complete(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [credit_cls(1000, 'Mrs. Halls')], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1]])
        expected_string = 'Wing: A    Landing: 2    Cell: 002'
        drawn_strings = [call[0][2] for call in canvas_draw_string.call_args_list]
        self.assertTrue(any(expected_string in drawn_string for drawn_string in drawn_strings))
    def test_one_prisoner_two_disbursements(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        prisoners = [('JAMES HALLS', 'A1409AE', sample_location, [], [
            disbursement_cls(2000, 'cheque', 'Rose', 'Johnson'),
            disbursement_cls(3000, 'bank_transfer', 'Janet', 'Johnson')
        ])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[2]])
    def test_location_malformed(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        malformed_location = {
            'description': 'LEIA2',
        }
        prisoners = [('JAMES HALLS', 'A1409AE', malformed_location, [credit_cls(1000, 'Mrs. Halls')], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1]])
        expected_string = 'Location: LEIA2'
        drawn_strings = [call[0][2] for call in canvas_draw_string.call_args_list]
        self.assertTrue(any(expected_string in drawn_string for drawn_string in drawn_strings))
Пример #16
0
    def test_location_malformed(self, canvas_save, canvas_show_page, canvas_draw_string):
        canvas_save.return_value = None

        malformed_location = {
            'description': 'LEIA2',
        }
        prisoners = [('JAMES HALLS', 'A1409AE', malformed_location, [credit_cls(1000, 'Mrs. Halls')], [])]
        bundle = PrisonerCreditNoticeBundle('INB', prisoners, datetime.date(2017, 6, 16))
        bundle.render(None)

        self.assertPageUpdates(canvas_show_page, canvas_draw_string, [[1]])
        expected_string = 'Location: LEIA2'
        drawn_strings = [call[0][2] for call in canvas_draw_string.call_args_list]
        self.assertTrue(any(expected_string in drawn_string for drawn_string in drawn_strings))
    def handle(self, path, prison, date=None, **options):
        verbosity = options.get('verbosity', 1)

        path = pathlib.Path(path).absolute()
        if path.exists():
            raise CommandError('Path exists %s' % path)
        try:
            prison = Prison.objects.get(pk=prison)
        except Prison.DoesNotExist:
            raise CommandError('Prison %s does not exist' % prison)
        if date:
            date = parse_date(date)
            if not date:
                raise CommandError('Date %s cannot be parsed, use YYYY-MM-DD format' % date)
        else:
            date = now().date() - datetime.timedelta(days=1)
        date_range = (make_aware(datetime.datetime.combine(date, datetime.time.min)),
                      make_aware(datetime.datetime.combine(date, datetime.time.max)))

        credit_logs = CreditLog.objects.filter(
            action=CREDIT_ACTIONS.CREDITED,
            created__range=date_range,
            credit__prison=prison.pk,
        )
        credit_count = credit_logs.count()
        disbursement_logs = DisbursementLog.objects.filter(
            action=DISBURSEMENT_ACTIONS.SENT,
            created__range=date_range,
            disbursement__prison=prison.pk,
        )
        disbursement_count = disbursement_logs.count()
        if credit_count + disbursement_count == 0:
            if verbosity:
                self.stdout.write('Nothing credited or disbursed at %s on %s' % (prison, date))
            return
        if verbosity > 1:
            self.stdout.write('%d credits received, %d disbursements sent at %s on %s' % (
                credit_count, disbursement_count, prison, date,
            ))

        prisoner_updates = collections.defaultdict(lambda: {'credits': [], 'disbursements': []})
        for log in credit_logs:
            credit = log.credit
            prisoner_updates[credit.prisoner_number]['credits'].append(credit)
        for log in disbursement_logs:
            disbursement = log.disbursement
            prisoner_updates[disbursement.prisoner_number]['disbursements'].append(disbursement)

        prisoners = []
        for prisoner_number in sorted(prisoner_updates.keys()):
            credits_list = prisoner_updates[prisoner_number]['credits']
            credits_list = sorted(credits_list, key=lambda credit: credit.received_at)
            disbursements_list = prisoner_updates[prisoner_number]['disbursements']
            disbursements_list = sorted(disbursements_list, key=lambda disbursement: disbursement.modified)
            prisoner_name = (credits_list or disbursements_list)[0].prisoner_name
            location = self.get_housing(prisoner_number)
            prisoners.append((
                prisoner_name,
                prisoner_number,
                location,
                credits_list,
                disbursements_list,
            ))

        if verbosity:
            self.stdout.write('Generating notices bundle for %s at %s' % (prison.name, path))
        bundle = PrisonerCreditNoticeBundle(prison.name, prisoners, date)
        bundle.render(str(path))