Coverage for src/backend/InvenTree/plugin/samples/integration/label_sample.py: 0%

21 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Simple sample for a plugin with the LabelPrintingMixin. 

2 

3This does not function in real usage and is more to show the required components and for unit tests. 

4""" 

5 

6from rest_framework import serializers 

7 

8from InvenTree.config import get_testfolder_dir 

9from plugin import InvenTreePlugin 

10from plugin.mixins import LabelPrintingMixin 

11 

12 

13class SampleLabelPrinter(LabelPrintingMixin, InvenTreePlugin): 

14 """Sample plugin which provides a 'fake' label printer endpoint.""" 

15 

16 NAME = 'Sample Label Printer' 

17 SLUG = 'samplelabelprinter' 

18 TITLE = 'Sample Label Printer' 

19 DESCRIPTION = 'A sample plugin which provides a (fake) label printer interface' 

20 AUTHOR = 'InvenTree contributors' 

21 VERSION = '0.3.0' 

22 

23 class PrintingOptionsSerializer(serializers.Serializer): 

24 """Serializer to return printing options.""" 

25 

26 amount = serializers.IntegerField(required=False, default=1) 

27 

28 def print_label(self, **kwargs): 

29 """Sample printing step. 

30 

31 Normally here the connection to the printer and transfer of the label would take place. 

32 """ 

33 # Test that the expected kwargs are present 

34 print(f'Printing Label: {kwargs["filename"]} (User: {kwargs["user"]})') 

35 

36 pdf_data = kwargs['pdf_data'] 

37 png_file = self.render_to_png( 

38 kwargs['label_instance'], kwargs['item_instance'], **kwargs 

39 ) 

40 

41 filename = str(get_testfolder_dir() / 'label.pdf') 

42 

43 # Dump the PDF to a local file 

44 with open(filename, 'wb') as pdf_out: 

45 pdf_out.write(pdf_data) 

46 

47 # Save the PNG to disk 

48 png_file.save(filename.replace('.pdf', '.png'))