1 from __future__ import unicode_literals
3 from django.template import Context, Template
4 from django.contrib import admin
6 from .models import LabelContent, LabelTemplate, LabelPrinter
7 from inventory.models import PrinterTemplate
8 from bcmagic.utils import print_zpl_socket
11 class LabelContentOptions(admin.ModelAdmin):
21 list_display = ('title', 'subtitle', 'text', 'barcode', 'template', 'creator')
22 list_filter = ('template', 'creator',)
25 'fields': (('title', 'subtitle', 'text', 'barcode'),
26 ('template', 'creator'))
30 actions = ['action_print_labels']
32 def action_print_labels(self, request, queryset):
34 Django action which prints labels for the selected set of labels from the
35 Django Admin interface.
39 #Iterate over selected labels to print
40 for label in queryset.all():
41 template_used = LabelTemplate.objects.get(name=label.template.name)
43 t = Template(template_used.ZPL_code)
45 # Django Template Context
46 c = Context({'label': label})
48 # Send rendered template to the printer that the template
49 # object has been attached to in the database.
50 zpl_list.append(t.render(c))
52 print_zpl_socket(zpl_list, host=template_used.printer.ip_address)
54 self.message_user(request, "%s labels printed." % (len(queryset)))
56 action_print_labels.short_description = "Print Selected Labels"
59 class LabelTemplateOptions(admin.ModelAdmin):
62 list_display = ('name', 'printer', 'ZPL_code')
65 class LabelPrinterOptions(admin.ModelAdmin):
66 list_display = ('name', 'ip_address', 'labels')
68 admin.site.register(LabelContent, LabelContentOptions)
69 admin.site.register(LabelTemplate, LabelTemplateOptions)
70 admin.site.register(LabelPrinter, LabelPrinterOptions)