Ejemplo n.º 1
0
class ProductTestCase(unittest.TestCase):
    """Basic test for Product object."""
    def setUp(self):
        self.form_submission = PackageScanFormSubmission(VOUCHER_JSON)

    def test_voucher_qr_codes(self):
        self.assertEqual(['test'], self.form_submission.get_qr_codes())

    def test_package_qr_codes(self):
        self.form_submission = PackageScanFormSubmission(PACKAGE_JSON)
        self.assertEqual(set(['test']), self.form_submission.get_qr_codes())

    def test_gps(self):
        self.assertEqual(VOUCHER_JSON['gps'], self.form_submission.gps)

    def test_no_gps(self):
        data = VOUCHER_JSON.copy()
        data.pop('gps')
        self.form_submission = PackageScanFormSubmission(data)
        self.assertFalse(hasattr(self.form_submission, 'gps'))

    def test_submission_time(self):
        self.assertEqual(
            VOUCHER_JSON['_submission_time'],
            self.form_submission._submission_time.strftime(
                SUBMITTED_AT_FORMAT))

    def test_get_latitude(self):
        value = float(VOUCHER_JSON['gps'].split(' ')[0])
        self.assertEqual(value, self.form_submission.get_lat())

    def test_get_latitude_no_gps(self):
        data = VOUCHER_JSON.copy()
        data.pop('gps')
        self.form_submission = PackageScanFormSubmission(data)
        self.assertIsNone(self.form_submission.get_lat())

    def test_get_longitude(self):
        value = float(VOUCHER_JSON['gps'].split(' ')[1])
        self.assertEqual(value, self.form_submission.get_lng())

    def test_get_altitude(self):
        value = float(VOUCHER_JSON['gps'].split(' ')[2])
        self.assertEqual(value, self.form_submission.get_altitude())

    def test_get_accuracy(self):
        value = float(VOUCHER_JSON['gps'].split(' ')[3])
        self.assertEqual(value, self.form_submission.get_accuracy())

    def test_get_gps_data_out_of_range(self):
        self.assertIsNone(self.form_submission.get_gps_data(100))
Ejemplo n.º 2
0
class ProductTestCase(unittest.TestCase):
    """Basic test for Product object."""

    def setUp(self):
        self.form_submission = PackageScanFormSubmission(VOUCHER_JSON)

    def test_voucher_qr_codes(self):
        self.assertEqual(['test'], self.form_submission.get_qr_codes())

    def test_package_qr_codes(self):
        self.form_submission = PackageScanFormSubmission(PACKAGE_JSON)
        self.assertEqual(set(['test']), self.form_submission.get_qr_codes())

    def test_gps(self):
        self.assertEqual(VOUCHER_JSON['gps'], self.form_submission.gps)

    def test_no_gps(self):
        data = VOUCHER_JSON.copy()
        data.pop('gps')
        self.form_submission = PackageScanFormSubmission(data)
        self.assertFalse(hasattr(self.form_submission, 'gps'))

    def test_submission_time(self):
        self.assertEqual(
            VOUCHER_JSON['_submission_time'],
            self.form_submission._submission_time.strftime(SUBMITTED_AT_FORMAT))

    def test_get_latitude(self):
        value = float(VOUCHER_JSON['gps'].split(' ')[0])
        self.assertEqual(value, self.form_submission.get_lat())

    def test_get_latitude_no_gps(self):
        data = VOUCHER_JSON.copy()
        data.pop('gps')
        self.form_submission = PackageScanFormSubmission(data)
        self.assertIsNone(self.form_submission.get_lat())

    def test_get_longitude(self):
        value = float(VOUCHER_JSON['gps'].split(' ')[1])
        self.assertEqual(value, self.form_submission.get_lng())

    def test_get_altitude(self):
        value = float(VOUCHER_JSON['gps'].split(' ')[2])
        self.assertEqual(value, self.form_submission.get_altitude())

    def test_get_accuracy(self):
        value = float(VOUCHER_JSON['gps'].split(' ')[3])
        self.assertEqual(value, self.form_submission.get_accuracy())

    def test_get_gps_data_out_of_range(self):
        self.assertIsNone(self.form_submission.get_gps_data(100))
Ejemplo n.º 3
0
def record_package_location(sender, instance, **kwargs):
    # Only record package locations for package tracking forms
    try:
        logger.debug("record_package_location...")
        form_ids = [int(x) for x in settings.ONA_FORM_IDS]
        if kwargs.get('created', False) and int(instance.data['form_id']) in form_ids:
            submission = PackageScanFormSubmission(instance.data)
            logger.debug("New formsubmission. %d QR codes", len(submission.get_qr_codes()))

            for code in submission.get_qr_codes():
                logger.debug("QR code: %s" % code)
                try:
                    pkg = Package.objects.get(code=code)
                except Package.DoesNotExist:
                    logger.exception("Scanned Package with code %s not found" % code)
                else:
                    scan_status_label = submission.get_current_packagescan_label()
                    PackageScan.objects.create(
                        package=pkg,
                        longitude=submission.get_lng(),
                        latitude=submission.get_lat(),
                        altitude=submission.get_altitude(),
                        accuracy=submission.get_accuracy(),
                        when=submission._submission_time,
                        status_label=scan_status_label
                    )
                    logger.debug("created location")
                    # Update Package and Shipment Status based on selected current_location value
                    # Values should look similar to the following samples, defined by the
                    # Ona XLSFOrm:
                    # STATUS_IN_TRANSIT-Zero_Point
                    # STATUS_IN_TRANSIT-Partner_Warehouse
                    # STATUS_IN_TRANSIT-Pre-Distribution_Point
                    # STATUS_RECEIVED-Distribution Point
                    # STATUS_RECEIVED-Post-Distribution Point
                    # The prefix part (before the first -) is one of the predefined status
                    # names that are attributes of the Shipment model.
                    if submission.is_voucher():
                        status = Shipment.STATUS_RECEIVED
                    else:
                        status = submission.current_location.split('-', 1)[0]
                        logger.debug("status=%r" % status)
                        if not hasattr(Shipment, status):
                            # If no match is found, log the invalid package status as it is
                            # indicative of the app and Ona being out of sync
                            msg = "FormSubmission with form id of %s has invalid package status: %s" \
                                % (instance.form_id, status)
                            logger.error(msg)
                            continue
                        status = getattr(Shipment, status, None)
                    if status:
                        update_fields = ['last_scan_status_label']
                        pkg.last_scan_status_label = scan_status_label
                        if pkg.status != status:
                            update_fields.append('status')
                            pkg.status = status
                        if status == Shipment.STATUS_RECEIVED and not pkg.date_received:
                            update_fields.append('date_received')
                            pkg.date_received = submission._submission_time
                        elif status == Shipment.STATUS_PICKED_UP and not pkg.date_picked_up:
                            update_fields.append('date_picked_up')
                            pkg.date_picked_up = submission._submission_time
                        elif status == Shipment.STATUS_IN_TRANSIT and not pkg.date_in_transit:
                            update_fields.append('date_in_transit')
                            pkg.date_in_transit = submission._submission_time
                        pkg.save(update_fields=update_fields)
                        pkg.shipment.status = status
                        pkg.shipment.last_scan_status_label = scan_status_label
                        pkg.shipment.save(update_fields=['status', 'last_scan_status_label'])
                        logger.debug("set status to %s" % pkg.get_status_display())
        else:
            logger.debug("Ignoring this FormSubmission.  kwargs[created]=%s, form_id=%s,"
                         " form_ids=%s"
                         % (kwargs.get('created', False), int(instance.data['form_id']),
                            form_ids))
    except Exception:
        logger.exception("Something blew up in record_package_location")
Ejemplo n.º 4
0
def record_package_location(sender, instance, **kwargs):
    # Only record package locations for package tracking forms
    try:
        logger.debug("record_package_location...")
        form_id = int(settings.ONA_PACKAGE_FORM_ID)
        if kwargs.get('created', False) and int(
                instance.data['form_id']) == form_id:
            submission = PackageScanFormSubmission(instance.data)
            logger.debug("New formsubmission. %d QR codes",
                         len(submission.get_qr_codes()))
            for code in submission.get_qr_codes():
                logger.debug("QR code: %s" % code)
                try:
                    pkg = Package.objects.get(code=code)
                except Package.DoesNotExist:
                    logger.exception("Scanned Package with code %s not found" %
                                     code)
                else:
                    scan_status_label = submission.get_current_packagescan_label(
                    )
                    PackageScan.objects.create(
                        package=pkg,
                        longitude=submission.get_lng(),
                        latitude=submission.get_lat(),
                        altitude=submission.get_altitude(),
                        accuracy=submission.get_accuracy(),
                        when=submission._submission_time,
                        status_label=scan_status_label)
                    logger.debug("created location")
                    # Update Package and Shipment Status based on selected current_location value
                    # Values should look similar to the following samples, defined by the
                    # Ona XLSFOrm:
                    # STATUS_IN_TRANSIT-Zero_Point
                    # STATUS_IN_TRANSIT-Partner_Warehouse
                    # STATUS_IN_TRANSIT-Pre-Distribution_Point
                    # STATUS_RECEIVED-Distribution Point
                    # STATUS_RECEIVED-Post-Distribution Point
                    # The prefix part (before the first -) is one of the predefined status
                    # names that are attributes of the Shipment model.
                    status = submission.current_location.split('-', 1)[0]
                    logger.debug("status=%r" % status)
                    if not hasattr(Shipment, status):
                        # If no match is found, log the invalid package status as it is
                        # indicative of the app and Ona being out of sync
                        msg = "FormSubmission with form id of %s has invalid package status: %s" \
                            % (instance.form_id, status)
                        logger.error(msg)
                        continue
                    status = getattr(Shipment, status, None)
                    if status:
                        update_fields = ['last_scan_status_label']
                        pkg.last_scan_status_label = scan_status_label
                        if pkg.status != status:
                            update_fields.append('status')
                            pkg.status = status
                        if status == Shipment.STATUS_RECEIVED and not pkg.date_received:
                            update_fields.append('date_received')
                            pkg.date_received = submission._submission_time
                        elif status == Shipment.STATUS_PICKED_UP and not pkg.date_picked_up:
                            update_fields.append('date_picked_up')
                            pkg.date_picked_up = submission._submission_time
                        elif status == Shipment.STATUS_IN_TRANSIT and not pkg.date_in_transit:
                            update_fields.append('date_in_transit')
                            pkg.date_in_transit = submission._submission_time
                        pkg.save(update_fields=update_fields)
                        pkg.shipment.status = status
                        pkg.shipment.last_scan_status_label = scan_status_label
                        pkg.shipment.save(
                            update_fields=['status', 'last_scan_status_label'])
                        logger.debug("set status to %s" %
                                     pkg.get_status_display())
        else:
            logger.debug(
                "Ignoring this FormSubmission.  kwargs[created]=%s, form_id=%s,"
                " form_id=%s" % (kwargs.get(
                    'created', False), int(instance.data['form_id']), form_id))
    except Exception:
        logger.exception("Something blew up in record_package_location")