1 from django.template import Context, Template
2 from django.contrib import admin
4 from .models import LabelContent, LabelTemplate, LabelPrinter
5 from inventory.models import PrinterTemplate
6 from bcmagic.utils import print_zpl_socket
9 class LabelContentOptions(admin.ModelAdmin):
19 list_display = ('title', 'subtitle', 'text', 'barcode', 'template', 'creator')
20 list_filter = ('template', 'creator',)
23 'fields': (('title', 'subtitle', 'text', 'barcode'),
24 ('template', 'creator'))
28 actions = ['action_print_labels']
30 def action_print_labels(self, request, queryset):
32 Django action which prints labels for the selected set of labels from the
33 Django Admin interface.
37 #Iterate over selected labels to print
38 for label in queryset.all():
39 template_used = LabelTemplate.objects.get(name=label.template.name)
41 t = Template(template_used.ZPL_code)
43 # Django Template Context
44 c = Context({'label': label})
46 # Send rendered template to the printer that the template
47 # object has been attached to in the database.
48 zpl_list.append(t.render(c))
50 print_zpl_socket(zpl_list, host=template_used.printer.ip_address)
52 self.message_user(request, "%s labels printed." % (len(queryset)))
54 action_print_labels.short_description = "Print Selected Labels"
57 class LabelTemplateOptions(admin.ModelAdmin):
60 list_display = ('name', 'printer', 'ZPL_code')
63 class LabelPrinterOptions(admin.ModelAdmin):
64 list_display = ('name', 'ip_address', 'labels')
66 admin.site.register(LabelContent, LabelContentOptions)
67 admin.site.register(LabelTemplate, LabelTemplateOptions)
68 admin.site.register(LabelPrinter, LabelPrinterOptions)