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
8 class LabelContentOptions(admin.ModelAdmin):
18 list_display = ('title','subtitle','text','barcode','template','creator')
19 list_filter = ('template','creator',)
22 'fields': (('title','subtitle','text','barcode'),
23 ('template','creator'))
27 actions = ['action_print_labels']
29 def action_print_labels(self, request, queryset):
31 Django action which prints labels for the selected set of labels from the
32 Django Admin interface.
36 #Iterate over selected labels to print
37 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"
56 class LabelTemplateOptions(admin.ModelAdmin):
59 list_display = ('name', 'printer', 'ZPL_code')
61 class LabelPrinterOptions(admin.ModelAdmin):
62 list_display = ('name', 'ip_address', 'labels')
64 admin.site.register(LabelContent, LabelContentOptions)
65 admin.site.register(LabelTemplate, LabelTemplateOptions)
66 admin.site.register(LabelPrinter, LabelPrinterOptions)