def test_png_url(self): is_first = True for url_options in product([True, False, None], [True, False, None]): cache_enabled = url_options[0] url_signature_enabled = url_options[1] url_options_kwargs = dict() if cache_enabled is not None: url_options_kwargs['cache_enabled'] = cache_enabled if url_signature_enabled is not None: url_options_kwargs[ 'url_signature_enabled'] = url_signature_enabled url1 = make_qr_code_url(TEST_TEXT, QRCodeOptions(image_format='png', size=1), **url_options_kwargs) url2 = qr_url_from_text(TEST_TEXT, image_format='png', size=1, **url_options_kwargs) url3 = qr_url_from_text(TEST_TEXT, image_format='PNG', size=1, **url_options_kwargs) url4 = qr_url_from_text(TEST_TEXT, options=QRCodeOptions(image_format='PNG', size=1), **url_options_kwargs) url = url1 if url_signature_enabled is not False: urls = get_urls_without_token_for_comparison( url1, url2, url3, url4) else: urls = [url1, url2, url3, url4] self.assertEqual(urls[0], urls[1]) self.assertEqual(urls[0], urls[2]) self.assertEqual(urls[0], urls[3]) response = self.client.get(url) print("\t - cache_enabled=%s, url_signature_enabled=%s" % (cache_enabled, url_signature_enabled)) expected_status_code = 200 if url_signature_enabled is False and not allows_external_request_from_user( None): expected_status_code = 403 self.assertEqual(response.status_code, expected_status_code) if expected_status_code == 200: if is_first and REFRESH_REFERENCE_IMAGES: write_png_content_to_file( TestQRUrlFromTextResult.default_ref_base_file_name, response.content) is_first = False self.assertEqual( response.content, TestQRUrlFromTextResult. _get_reference_result_for_default_png())
def test_svg_error_correction(self): base_file_name = 'qrfromtext_error_correction' for correction_level in ERROR_CORRECTION_DICT: print('Testing SVG URL with error correction: %s' % correction_level) url1 = make_qr_code_url( COMPLEX_TEST_TEXT, QRCodeOptions(error_correction=correction_level), cache_enabled=False) url2 = qr_url_from_text(COMPLEX_TEST_TEXT, error_correction=correction_level, cache_enabled=False) url3 = qr_url_from_text(COMPLEX_TEST_TEXT, error_correction=correction_level, image_format='svg', cache_enabled=False) url4 = qr_url_from_text(COMPLEX_TEST_TEXT, error_correction=correction_level, image_format='SVG', cache_enabled=False) url5 = qr_url_from_text(COMPLEX_TEST_TEXT, options=QRCodeOptions( error_correction=correction_level, image_format='SVG'), cache_enabled=False) # Using an invalid image format should fallback to SVG. url6 = qr_url_from_text(COMPLEX_TEST_TEXT, error_correction=correction_level, image_format='invalid-format-name', cache_enabled=False) url = url1 urls = get_urls_without_token_for_comparison( url1, url2, url3, url4, url5, url6) self.assertEqual(urls[0], urls[1]) self.assertEqual(urls[0], urls[2]) self.assertEqual(urls[0], urls[3]) self.assertEqual(urls[0], urls[4]) self.assertEqual(urls[0], urls[5]) response = self.client.get(url) self.assertEqual(response.status_code, 200) source_image_data = response.content.decode('utf-8') # Skip header and adjust tag format. source_image_data = source_image_data[source_image_data. index('\n') + 1:] source_image_data = _make_closing_path_tag(source_image_data) ref_file_name = '%s_%s' % (base_file_name, correction_level.lower()) if REFRESH_REFERENCE_IMAGES: write_svg_content_to_file(ref_file_name, source_image_data) ref_image_data = get_svg_content_from_file_name(ref_file_name) self.assertEqual(source_image_data, ref_image_data)
def get_context_data(self, **kwargs): context = super(VentaDetailView, self).get_context_data(**kwargs) productos_generales = VentaDetalle.objects.values( 'equipo__producto__producto_general', 'equipo__producto__producto_general__codigo', 'precio_unitario').annotate( precio_total=Sum('precio_total'), base_imponible=Sum('base_imponible'), cantidad=Sum('cantidad')).filter(venta=self.get_object()) for pr in productos_generales: equipos = Equipo.objects.filter( ventadetalle__venta=self.get_object(), producto__producto_general__id=pr[ 'equipo__producto__producto_general']) pr['equipos'] = equipos qr_url = qr_url_from_text(self.object.qr, version=5, image_format="png", error_correction="L") context['opts'] = self.model._meta context['qr_url'] = qr_url context['productos_generales'] = productos_generales context['empresa'] = Empresa.objects.get(estado=True) context['title'] = "Venta" return context
def test_png_error_correction(self): base_file_name = 'qrfromtext_error_correction' for correction_level in ERROR_CORRECTION_DICT: print('Testing PNG URL with error correction: %s' % correction_level) url1 = make_qr_code_url(COMPLEX_TEST_TEXT, QRCodeOptions( error_correction=correction_level, image_format='png'), cache_enabled=False) url2 = make_qr_code_url(COMPLEX_TEST_TEXT, QRCodeOptions( error_correction=correction_level, image_format='PNG'), cache_enabled=False) url3 = qr_url_from_text(COMPLEX_TEST_TEXT, error_correction=correction_level, image_format='png', cache_enabled=False) url4 = qr_url_from_text(COMPLEX_TEST_TEXT, error_correction=correction_level, image_format='PNG', cache_enabled=False) url5 = qr_url_from_text(COMPLEX_TEST_TEXT, options=QRCodeOptions( error_correction=correction_level, image_format='PNG'), cache_enabled=False) url = url1 urls = get_urls_without_token_for_comparison( url1, url2, url3, url4, url5) self.assertEqual(urls[0], urls[1]) self.assertEqual(urls[0], urls[2]) self.assertEqual(urls[0], urls[3]) self.assertEqual(urls[0], urls[4]) response = self.client.get(url) self.assertEqual(response.status_code, 200) source_image_data = response.content ref_file_name = '%s_%s' % (base_file_name, correction_level.lower()) if REFRESH_REFERENCE_IMAGES: write_png_content_to_file(ref_file_name, source_image_data) ref_image_data = get_png_content_from_file_name(ref_file_name) self.assertEqual(source_image_data, ref_image_data)
def get(self, request, *args, **kwargs): venta = Venta.objects.get(id=self.request.GET.get("venta_id")) items = [] productos_generales = VentaDetalle.objects.values( 'equipo__producto__producto_general__codigo', 'precio_unitario').annotate(precio_total=Sum('precio_total'), base_imponible=Sum('base_imponible'), cantidad=Sum('cantidad'), igv=Sum('igv')).filter(venta=venta) for p in productos_generales: equipos = [(e.imei) for e in Equipo.objects.filter( ventadetalle__venta=venta, producto__producto_general__codigo=p[ 'equipo__producto__producto_general__codigo'])] str1 = ', '.join(str(e) for e in equipos) desc = "%s %s" % (p['equipo__producto__producto_general__codigo'], str1) items.append({ 'codigo': p['equipo__producto__producto_general__codigo'], 'descripcion': desc, 'precio_unitario': "%.2f" % p['precio_unitario'], 'precio_total': "%.2f" % p['precio_total'], 'base_imponible': "%.2f" % p['base_imponible'], 'cantidad': p['cantidad'], 'igv': "%.2f" % p['igv'], }) empresa = Empresa.objects.get(estado=True) qr_img = qr_url_from_text(venta.qr, version=5, image_format="png", error_correction="L") domain = request.META['HTTP_HOST'] if settings.DEBUG == True: http = "http://" else: http = "https://" data = { 'empresa': empresa.nombre, 'nro_ruc': empresa.nro_ruc, "qr_img": http + domain + qr_img, "serie_numero": venta.serie_numero(), "fecha": venta.fecha_local().strftime("%d-%m-%Y"), "cliente": { "nombre_completo": venta.cliente.nombre_completo(), "nro_documento": venta.cliente.nro_documento, "tipo_documento": venta.cliente.tipo_documento.descripcion, "direccion": venta.cliente.direccion, }, 'venta': model_to_dict(venta, fields=[ 'nro_correlativo', 'igv', 'base_imponible', 'total', 'monto_texto', 'qr' ]), "items": items, } dump = json.dumps(data) return HttpResponse(dump, status=200, content_type="application/json")
def test_svg_url(self): is_first = True users = [None, AnonymousUser(), User(username='******')] for url_options in product([True, False, None], [True, False, None], users): cache_enabled = url_options[0] url_signature_enabled = url_options[1] user = url_options[2] print("\t - cache_enabled=%s, url_signature_enabled=%s, user=%s" % (cache_enabled, url_signature_enabled, user)) url_options_kwargs = dict() url0 = make_qr_code_url( TEST_TEXT, QRCodeOptions(size=1), **dict(**url_options_kwargs, cache_enabled=cache_enabled, url_signature_enabled=url_signature_enabled)) if cache_enabled is not None: url_options_kwargs['cache_enabled'] = cache_enabled url1 = make_qr_code_url( TEST_TEXT, QRCodeOptions(size=1), **dict(**url_options_kwargs, url_signature_enabled=url_signature_enabled)) if url_signature_enabled is not None: url_options_kwargs[ 'url_signature_enabled'] = url_signature_enabled url2 = qr_url_from_text(TEST_TEXT, size=1, **url_options_kwargs) url3 = qr_url_from_text(TEST_TEXT, image_format='svg', size=1, **url_options_kwargs) url4 = qr_url_from_text(TEST_TEXT, image_format='SVG', size=1, **url_options_kwargs) url5 = qr_url_from_text(TEST_TEXT, options=QRCodeOptions(image_format='SVG', size=1), **url_options_kwargs) # Using an invalid image format should fallback to SVG. url6 = qr_url_from_text(TEST_TEXT, image_format='invalid-format-name', size=1, **url_options_kwargs) url = url1 if url_signature_enabled is not False: urls = get_urls_without_token_for_comparison( url0, url1, url2, url3, url4, url5, url6) else: urls = [url0, url1, url2, url3, url4, url5, url6] self.assertEqual(urls[0], urls[1]) self.assertEqual(urls[0], urls[2]) self.assertEqual(urls[0], urls[3]) self.assertEqual(urls[0], urls[4]) self.assertEqual(urls[0], urls[5]) response = self.client.get(url) expected_status_code = 200 if url_signature_enabled is False and not allows_external_request_from_user( user): expected_status_code = 403 self.assertEqual(response.status_code, expected_status_code) image_data = response.content.decode('utf-8') if expected_status_code == 200: if is_first and REFRESH_REFERENCE_IMAGES: write_svg_content_to_file( TestQRUrlFromTextResult.default_ref_base_file_name, image_data) is_first = False self.assertEqual( image_data, TestQRUrlFromTextResult. _get_reference_result_for_default_svg())