Flatten project hierarchy, moving djano applications out of htsworkflow.frontend...
[htsworkflow.git] / labels / admin.py
1 from django.template import Context, Template
2 from django.contrib import admin
3
4 from .models import LabelContent, LabelTemplate, LabelPrinter
5 from inventory.models import PrinterTemplate
6 from bcmagic.utils import print_zpl_socket
7
8 class LabelContentOptions(admin.ModelAdmin):
9     save_as = True
10     save_on_top = True
11     search_fields = (
12         'title',
13         'subtitle',
14         'text',
15         'barcode',
16         'creator',
17     )
18     list_display = ('title','subtitle','text','barcode','template','creator')
19     list_filter = ('template','creator',)
20     fieldsets = (
21       (None, {
22           'fields': (('title','subtitle','text','barcode'),
23                      ('template','creator'))
24
25       }),
26     )
27     actions = ['action_print_labels']
28     
29     def action_print_labels(self, request, queryset):
30         """
31         Django action which prints labels for the selected set of labels from the
32         Django Admin interface.
33         """
34        
35         zpl_list = []
36         #Iterate over selected labels to print
37         for label in queryset.all():
38
39           template_used = LabelTemplate.objects.get(name=label.template.name)
40           # ZPL Template
41           t = Template(template_used.ZPL_code)
42
43           # Django Template Context
44           c = Context({'label': label})
45             
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))
49         
50         print_zpl_socket(zpl_list, host=template_used.printer.ip_address)
51     
52         self.message_user(request, "%s labels printed." % (len(queryset)))
53                           
54     action_print_labels.short_description = "Print Selected Labels"
55
56 class LabelTemplateOptions(admin.ModelAdmin):
57     save_as = True
58     save_on_top = True
59     list_display = ('name', 'printer', 'ZPL_code')
60
61 class LabelPrinterOptions(admin.ModelAdmin):
62     list_display = ('name', 'ip_address', 'labels')
63
64 admin.site.register(LabelContent, LabelContentOptions)
65 admin.site.register(LabelTemplate, LabelTemplateOptions)
66 admin.site.register(LabelPrinter, LabelPrinterOptions)
67