Переглянути джерело

added label generation for the SMD book

stefan 2 місяців тому
батько
коміт
fd0fae1864

+ 77 - 29
shimatta_kenkyusho_print_label.py

@@ -11,6 +11,28 @@ import json
 import re
 
 MICE_TOILET_TEMPLATE = 'https://parts.shimatta.net/api/v1/parts/storages/075acbaf-ed60-47b9-9bd5-98bfbc3a79ea/'
+SMD_STRAP_PAGE_TEMPLATE = 'https://parts.shimatta.net/api/v1/parts/storages/4df58899-7338-48f5-9d93-cf5a59a6a400/'
+
+def get_child_storages(storage_query_url, token, uuid, expected_amount):
+	# get children
+	query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid, 'expand_stocks': True})
+	if query_res.status_code != 200:
+		print('Request for children unsuccessful')
+		sys.exit(-3)
+
+	storage_json = query_res.json()
+	if storage_json['count'] != expected_amount:
+		print('Storage json does not contain correct amount of storage entries')
+		sys.exit(-1)
+
+	# pagination
+	storage_list = storage_json['results']
+	while storage_json['next']:
+		query_res = requests.get(storage_json['next'], headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid})
+		storage_json = query_res.json()
+		storage_list.extend(storage_json['results'])
+
+	return {storage['name']: storage for storage in storage_list}
 
 def get_component_print_name(comp):
 	'''
@@ -89,25 +111,7 @@ def handle_storage_qr_code(base_url, token, uuid):
 			return None
 		row = int(row)
 
-		# get children
-		query_res = requests.get(storage_query_url, headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid, 'expand_stocks': True})
-		if query_res.status_code != 200:
-			print('Request for children unsuccessful')
-			sys.exit(-3)
-
-		storage_json = query_res.json()
-		if storage_json['count'] != 24:
-			print('Storage json does not contain correct amount of storage entries')
-			sys.exit(-1)
-
-		# pagination
-		storage_list = storage_json['results']
-		while storage_json['next']:
-			query_res = requests.get(storage_json['next'], headers={'Authorization': f'Token {token}'}, params={'parent_storage':uuid})
-			storage_json = query_res.json()
-			storage_list.extend(storage_json['results'])
-
-		storage_dict = {storage['name']: storage for storage in storage_list}
+		storage_dict = get_child_storages(storage_query_url, token, uuid, 24)
 
 		if side == 'b':
 
@@ -138,6 +142,47 @@ def handle_storage_qr_code(base_url, token, uuid):
 								comp['ro_manufacturer_name'])
 
 				labels.append(label)
+	elif storage['template'] == SMD_STRAP_PAGE_TEMPLATE:
+
+		type = input('Select type to be printed (components, storage) [c,s]:')
+		if type not in ['c', 's']:
+			print('Error - invalid type')
+			return None
+
+		if type == 's':
+			label = shimatta_label.storage_label.StorageLabelSmdStrap()
+			label.put_content(f'[stor_uuid]{uuid}', storage['full_path'], storage['verbose_name'])
+			labels.append(label)
+			return [label]
+
+		storage_dict = get_child_storages(storage_query_url, token, uuid, 12)
+
+		for row in range(1,13):
+			label = shimatta_label.component_label.StorageComponentLabelSmdBook()
+
+			if not storage_dict[f'{row}']['ro_stocks']:
+				print("Only printing start of column!!!")
+				break
+
+			storage_uuid = storage_dict[f'{row}']['id']
+			stock_uuild = storage_dict[f'{row}']['ro_stocks'][0]['id']
+			comp = storage_dict[f'{row}']['ro_stocks'][0]['ro_component']
+			param_dict = {param['ro_parameter_type']: param for param in comp['ro_parameters']}
+
+			comp_name = get_component_print_name(comp)
+			if not comp_name:
+				return[]
+
+			label.put_content(f'[stor_uuid]{storage_uuid}',
+					 		f'[stck_uuid]{stock_uuild}',
+							comp_name,
+							f"{param_dict['Tolerance']['value']}% {comp['package_data']['name']}",
+							comp['ro_manufacturer_name'])
+
+			labels.append(label)
+		pass
+
+
 	else:
 		label = shimatta_label.storage_label.StorageLabel()
 		label.put_content(f'[stor_uuid]{uuid}', storage['full_path'], storage['verbose_name'])
@@ -205,14 +250,17 @@ login_token = json.loads(ans.content)['token']
 while True:
 	qr = input('Please scan Storage or Component QR: ')
 
-	matches = qr_re.match(qr)
-	if matches.group('prefix') == 'stor_uuid' :
-		labels = handle_storage_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
-	elif matches.group('prefix') == 'comp_uuid':
-		labels = handle_component_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
-	else:
-		print('Invalid QR code!')
-		continue
+	try:
+		matches = qr_re.match(qr)
+		if matches.group('prefix') == 'stor_uuid' :
+			labels = handle_storage_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
+		elif matches.group('prefix') == 'comp_uuid':
+			labels = handle_component_qr_code(kenkyusho_url, login_token, matches.group('uuid'))
+		else:
+			print('Invalid QR code!')
+			continue
 
-	if labels:
-		ql_wrapper.print_labels(labels)
+		if labels:
+			ql_wrapper.print_labels(labels)
+	except Exception as ex:
+		print("Something went wrong:", ex)

+ 29 - 5
shimatta_label/component_label.py

@@ -68,10 +68,7 @@ class ComponentLabelSmall(ComponentLabel):
     dpi_600 = True
     red_label = False
     text_pos_x = 235
-
-
-    def __init__(self):
-        super().__init__()
+    qrcode_size = 7.2
 
     def put_content(self, qr_data, component_name='', manufacturer='', package=''):
         self.draw_text(component_name, self.text_pos_x, self.pixels_y/2-65, size=42, font_file='bold', scale_to_fit=True, centered_x=False, centered_y=True)
@@ -80,7 +77,7 @@ class ComponentLabelSmall(ComponentLabel):
         qr = qrcode.QRCode(
             version=1,
             error_correction=qrcode.constants.ERROR_CORRECT_L,
-            box_size=7.2,
+            box_size=self.qrcode_size,
             border=0,
         )
         qr.add_data(qr_data)
@@ -89,6 +86,33 @@ class ComponentLabelSmall(ComponentLabel):
         qr_y_size = qr_image.size[1]
         self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))
 
+class StorageComponentLabelSmdBook(ComponentLabelSmall):
+    pixels_x = 826
+    pixels_y = 191
+
+    ql_format = '38'
+    ql_rotation = 0
+    dpi_600 = True
+    red_label = False
+    text_pos_x = 235
+    qrcode_size = 6
+
+    def put_content(self, storage_qr_data, stock_qr_data, component_name='', manufacturer='', package=''):
+        super().put_content(stock_qr_data, component_name, manufacturer, package)
+
+        qr = qrcode.QRCode(
+            version=1,
+            error_correction=qrcode.constants.ERROR_CORRECT_L,
+            box_size=self.qrcode_size,
+            border=0,
+        )
+        qr.add_data(storage_qr_data)
+        qr.make(fit=True)
+        qr_image = qr.make_image(fill_color="black", back_color="white")
+        qr_y_size = qr_image.size[1]
+        self.img.paste(qr_image, box=(550, int(self.pixels_y/2 - qr_y_size/2)))
+
+
 class ComponentLabelSmdMiceToiletPocket(Label):
     pixels_x = 307
     pixels_y = 212

+ 14 - 0
shimatta_label/storage_label.py

@@ -55,6 +55,20 @@ class StorageLabel(Label):
         qr_y_size = qr_image.size[1]
         self.img.paste(qr_image, box=(0, int(self.pixels_y/2 - qr_y_size/2)))
 
+class StorageLabelSmdStrap(StorageLabel):
+    pixels_x = 700
+    pixels_y = 212
+
+    qr_size = 7
+    text_size_verbose_name = 64
+    text_size_name = 64
+    text_pos_x = 250
+
+    ql_format = '12'
+    ql_rotation = 270
+    dpi_600 = True
+    red_label = False
+
 class StorageLabelSmdMiceToilet(StorageLabel):
     pixels_x = 1108
     pixels_y = 200