New routeNes

parent d0d28ab3
......@@ -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]:
"""
Generate barcode image as PNG bytes
Args:
data: Data to encode in barcode
standard: Barcode standard (code128, code39, ean13, etc.)
width: Image width in pixels
height: Image height in pixels
Returns:
PNG image bytes or None if generation failed
"""
if standard == 'none':
return None
try:
# Special case for UPC-E which is not supported by python-barcode library
if standard == 'upce':
......@@ -146,7 +146,7 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i
except Exception as e:
logger.error(f"Failed to get barcode class for {barcode_type}: {e}")
return None
# Create image writer with custom options
writer = ImageWriter()
writer.set_options({
......@@ -159,21 +159,24 @@ def generate_barcode_image(data: str, standard: str, width: int = 300, height: i
'write_text': True,
'text': data
})
# Generate barcode
# Create barcode
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()
barcode_instance.write(buffer)
pil_image.save(buffer, format='PNG')
buffer.seek(0)
image_bytes = buffer.getvalue()
buffer.close()
logger.debug(f"Generated {standard} barcode for data: {data}")
return image_bytes
except Exception as e:
logger.error(f"Failed to generate barcode: {e}")
return None
......@@ -216,16 +219,12 @@ def calculate_ean13_check_digit(data: str) -> str:
raise ValueError("EAN-13 check digit calculation requires exactly 12 digits")
# EAN-13 check digit calculation
# Step 1: Sum digits in odd positions (1st, 3rd, 5th, etc.) multiplied by 3
odd_sum = sum(int(data[i]) for i in range(0, 12, 2)) * 3
# Step 2: Sum digits in even positions (2nd, 4th, 6th, etc.)
even_sum = sum(int(data[i]) for i in range(1, 12, 2))
# Step 3: Total sum
total = odd_sum + even_sum
# Weights alternate starting with 1 for leftmost digit, 3 for next, etc.
# This ensures the rightmost data digit gets weight 3
weights = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
total = sum(int(data[i]) * weights[i] for i in range(12))
# 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
return data + str(check_digit)
......
......@@ -5484,16 +5484,11 @@ def generate_bet_barcode(bet_id):
logger.debug(f"Using generated barcode data for bet {bet_uuid}: {barcode_data}")
# Generate barcode image
barcode_image = 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)
barcode_image_bytes = generate_barcode_image(barcode_data, standard, width, height)
if barcode_image_bytes:
return Response(
img_buffer.getvalue(),
barcode_image_bytes,
mimetype='image/png',
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