New routeNes

parent d0d28ab3
...@@ -92,19 +92,19 @@ def validate_barcode_data(data: str, standard: str) -> bool: ...@@ -92,19 +92,19 @@ def validate_barcode_data(data: str, standard: str) -> bool:
def generate_barcode_image(data: str, standard: str, width: int = 300, height: int = 100) -> Optional[bytes]: def generate_barcode_image(data: str, standard: str, width: int = 300, height: int = 100) -> Optional[bytes]:
""" """
Generate barcode image as PNG bytes Generate barcode image as PNG bytes
Args: Args:
data: Data to encode in barcode data: Data to encode in barcode
standard: Barcode standard (code128, code39, ean13, etc.) standard: Barcode standard (code128, code39, ean13, etc.)
width: Image width in pixels width: Image width in pixels
height: Image height in pixels height: Image height in pixels
Returns: Returns:
PNG image bytes or None if generation failed PNG image bytes or None if generation failed
""" """
if standard == 'none': if standard == 'none':
return None return None
try: try:
# Special case for UPC-E which is not supported by python-barcode library # Special case for UPC-E which is not supported by python-barcode library
if standard == 'upce': if standard == 'upce':
...@@ -146,7 +146,7 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i ...@@ -146,7 +146,7 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i
except Exception as e: except Exception as e:
logger.error(f"Failed to get barcode class for {barcode_type}: {e}") logger.error(f"Failed to get barcode class for {barcode_type}: {e}")
return None return None
# Create image writer with custom options # Create image writer with custom options
writer = ImageWriter() writer = ImageWriter()
writer.set_options({ writer.set_options({
...@@ -159,21 +159,24 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i ...@@ -159,21 +159,24 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i
'write_text': True, 'write_text': True,
'text': data 'text': data
}) })
# Generate barcode # Create barcode
barcode_instance = barcode_class(data, writer=writer) barcode_instance = barcode_class(data, writer=writer)
# Render to bytes # Get PIL Image using render() method
pil_image = barcode_instance.render()
# Save to bytes
buffer = BytesIO() buffer = BytesIO()
barcode_instance.write(buffer) pil_image.save(buffer, format='PNG')
buffer.seek(0) buffer.seek(0)
image_bytes = buffer.getvalue() image_bytes = buffer.getvalue()
buffer.close() buffer.close()
logger.debug(f"Generated {standard} barcode for data: {data}") logger.debug(f"Generated {standard} barcode for data: {data}")
return image_bytes return image_bytes
except Exception as e: except Exception as e:
logger.error(f"Failed to generate barcode: {e}") logger.error(f"Failed to generate barcode: {e}")
return None return None
...@@ -216,16 +219,12 @@ def calculate_ean13_check_digit(data: str) -> str: ...@@ -216,16 +219,12 @@ def calculate_ean13_check_digit(data: str) -> str:
raise ValueError("EAN-13 check digit calculation requires exactly 12 digits") raise ValueError("EAN-13 check digit calculation requires exactly 12 digits")
# EAN-13 check digit calculation # EAN-13 check digit calculation
# Step 1: Sum digits in odd positions (1st, 3rd, 5th, etc.) multiplied by 3 # Weights alternate starting with 1 for leftmost digit, 3 for next, etc.
odd_sum = sum(int(data[i]) for i in range(0, 12, 2)) * 3 # This ensures the rightmost data digit gets weight 3
weights = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
# Step 2: Sum digits in even positions (2nd, 4th, 6th, etc.) total = sum(int(data[i]) * weights[i] for i in range(12))
even_sum = sum(int(data[i]) for i in range(1, 12, 2))
# Step 3: Total sum
total = odd_sum + even_sum
# Step 4: Find the smallest number that makes total divisible by 10 # Find the smallest number that makes total divisible by 10
check_digit = (10 - (total % 10)) % 10 check_digit = (10 - (total % 10)) % 10
return data + str(check_digit) return data + str(check_digit)
......
...@@ -5484,16 +5484,11 @@ def generate_bet_barcode(bet_id): ...@@ -5484,16 +5484,11 @@ def generate_bet_barcode(bet_id):
logger.debug(f"Using generated barcode data for bet {bet_uuid}: {barcode_data}") logger.debug(f"Using generated barcode data for bet {bet_uuid}: {barcode_data}")
# Generate barcode image # Generate barcode image
barcode_image = generate_barcode_image(barcode_data, standard, width, height) barcode_image_bytes = generate_barcode_image(barcode_data, standard, width, height)
if barcode_image:
# Convert PIL image to bytes
img_buffer = io.BytesIO()
barcode_image.save(img_buffer, format='PNG')
img_buffer.seek(0)
if barcode_image_bytes:
return Response( return Response(
img_buffer.getvalue(), barcode_image_bytes,
mimetype='image/png', mimetype='image/png',
headers={'Cache-Control': 'public, max-age=3600'} # Cache for 1 hour headers={'Cache-Control': 'public, max-age=3600'} # Cache for 1 hour
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment