2022-03-10 10:33:06 +00:00
|
|
|
from django.views import generic
|
2022-03-09 17:13:09 +00:00
|
|
|
from .models import Container, ContainerType
|
2022-04-01 08:19:41 +00:00
|
|
|
import logging, json
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2022-03-30 08:04:38 +00:00
|
|
|
|
2022-03-17 09:27:31 +00:00
|
|
|
|
|
|
|
class ContainerListView(generic.ListView):
|
|
|
|
model = Container
|
|
|
|
template_name = 'container/container_list.html'
|
2022-03-10 10:33:06 +00:00
|
|
|
context_object_name = 'container_list'
|
2022-03-17 09:27:31 +00:00
|
|
|
paginate_by = 20
|
2022-03-09 17:13:09 +00:00
|
|
|
|
2022-03-17 09:27:31 +00:00
|
|
|
'''
|
2022-03-10 10:33:06 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
# Return the last five created containers
|
|
|
|
return Container.objects.order_by('-created_ts')[:5]
|
2022-03-17 09:27:31 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerCreateView(generic.CreateView):
|
|
|
|
model = Container
|
|
|
|
# template_name = 'container/detail.html'
|
|
|
|
fields = ['named_id', 'description', 'color', 'container_type']
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.changed_by = self.request.user
|
|
|
|
form.instance.created_by = self.request.user
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerUpdateView(generic.UpdateView):
|
|
|
|
model = Container
|
|
|
|
# template_name = 'container/detail.html'
|
|
|
|
fields = ['named_id', 'description', 'color', 'container_type']
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.changed_by = self.request.user
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
2022-03-10 10:33:06 +00:00
|
|
|
|
2022-03-17 09:27:31 +00:00
|
|
|
class ContainerDetailView(generic.DetailView):
|
|
|
|
model = Container
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerDeleteView(generic.DetailView):
|
|
|
|
model = Container
|
|
|
|
|
|
|
|
|
2022-03-24 14:54:04 +00:00
|
|
|
class ContainerPrintLabelView(generic.DetailView):
|
|
|
|
model = Container
|
|
|
|
template_name = 'container/container_print_label.html'
|
|
|
|
|
2022-03-30 08:04:38 +00:00
|
|
|
def get(self, request, **kwargs):
|
|
|
|
context = super().get(request, **kwargs)
|
|
|
|
'''
|
2022-03-24 14:54:04 +00:00
|
|
|
label_writer = LabelWriter(
|
2022-03-30 08:04:38 +00:00
|
|
|
"container/templates/label/container_label.html",
|
|
|
|
default_stylesheets=("container/templates/label/label_style.css",)
|
2022-03-24 14:54:04 +00:00
|
|
|
)
|
2022-03-30 08:04:38 +00:00
|
|
|
label_writer.write_labels([dict(named_id=kwargs['pk'], id=kwargs['pk'])], target="container_label.pdf")
|
|
|
|
'''
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2022-04-01 08:19:41 +00:00
|
|
|
num_copies = int(self.request.GET['num_copies']) if 'num_copies' in self.request.GET else 0
|
|
|
|
context['num_copies'] = num_copies
|
|
|
|
context['logs'] = ['creating label for container ' + self.object.named_id]
|
|
|
|
from labelprinter.labels import container_label, pil_to_html_imgdata
|
2022-03-30 08:04:38 +00:00
|
|
|
container_description = self.object.container_type.named_id
|
|
|
|
if len(self.object.container_type.description) > 0:
|
|
|
|
container_description += ': ' + self.object.container_type.description
|
|
|
|
if len(self.object.description) > 0:
|
2022-04-01 08:19:41 +00:00
|
|
|
container_description_line2 = self.object.description
|
|
|
|
label_image = container_label(
|
|
|
|
self.object.named_id,
|
|
|
|
fmt='image',
|
|
|
|
description=container_description,
|
|
|
|
description_line2=container_description_line2,
|
|
|
|
writer_options={'font_size': 14,
|
|
|
|
'module_height': 10.0,
|
|
|
|
'quiet_zone': 4.5,
|
|
|
|
'text': 'This is the text',
|
|
|
|
'text_distance': 1.0
|
|
|
|
})
|
|
|
|
context['barcode_img'] = pil_to_html_imgdata(label_image, fmt='PNG')
|
|
|
|
context['logs'].append('Image created')
|
|
|
|
if num_copies > 0:
|
|
|
|
#from brother_ql.devicedependent import models, label_type_specs, label_sizes
|
|
|
|
#from brother_ql.devicedependent import ENDLESS_LABEL, DIE_CUT_LABEL, ROUND_DIE_CUT_LABEL
|
|
|
|
from brother_ql import BrotherQLRaster, create_label
|
|
|
|
from brother_ql.backends import backend_factory, guess_backend
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open('config.json', encoding='utf-8') as fh:
|
|
|
|
config = json.load(fh)
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
with open('config.example.json', encoding='utf-8') as fh:
|
|
|
|
config = json.load(fh)
|
|
|
|
|
|
|
|
# LABEL_SIZES = [(name, label_type_specs[name]['name']) for name in label_sizes]
|
|
|
|
|
|
|
|
'''
|
|
|
|
if context['kind'] == ENDLESS_LABEL:
|
|
|
|
rotate = 0 if context['orientation'] == 'standard' else 90
|
|
|
|
elif context['kind'] in (ROUND_DIE_CUT_LABEL, DIE_CUT_LABEL):
|
|
|
|
rotate = 'auto'
|
|
|
|
red = False
|
|
|
|
if 'red' in context['label_size']:
|
|
|
|
red = True
|
|
|
|
'''
|
|
|
|
|
|
|
|
qlr = BrotherQLRaster(config['PRINTER']['MODEL'])
|
|
|
|
context['logs'].append('Connected to printer')
|
|
|
|
create_label(qlr, image=label_image, label_size='62', red=False, threshold=1,
|
|
|
|
cut=True, rotate='auto')
|
|
|
|
context['logs'].append('Created raster information')
|
|
|
|
|
|
|
|
try:
|
|
|
|
selected_backend = guess_backend(config['PRINTER']['PRINTER'])
|
|
|
|
except ValueError:
|
|
|
|
context['messages'].append(
|
|
|
|
"Could't guess the backend to use from the printer string descriptor (config PRINTER.PRINTER)")
|
|
|
|
context['logs'].append('Backend found: ' + selected_backend)
|
|
|
|
try:
|
|
|
|
be_class = backend_factory(selected_backend)['backend_class']
|
|
|
|
be = be_class(config['PRINTER']['PRINTER'])
|
|
|
|
context['logs'].append('Got backend driver')
|
|
|
|
for i in range(1,num_copies):
|
|
|
|
be.write(qlr.data)
|
|
|
|
|
|
|
|
context['logs'].append('Data sent to printer')
|
|
|
|
be.dispose()
|
|
|
|
del be
|
|
|
|
except Exception as e:
|
|
|
|
context['logs'].append('Exception: ' + str(e))
|
|
|
|
logger.warning('Exception happened: %s', e)
|
|
|
|
|
|
|
|
context['logs'].append('Finished printing label')
|
|
|
|
|
2022-03-30 08:04:38 +00:00
|
|
|
return context
|
2022-03-24 14:54:04 +00:00
|
|
|
|
|
|
|
|
2022-03-17 09:27:31 +00:00
|
|
|
class ContainerTypeListView(generic.ListView):
|
|
|
|
template_name = 'container/container_type_list.html'
|
2022-03-10 10:33:06 +00:00
|
|
|
context_object_name = 'container_type_list'
|
2022-03-17 09:27:31 +00:00
|
|
|
paginate_by = 20
|
|
|
|
model = ContainerType
|
2022-03-10 10:33:06 +00:00
|
|
|
|
2022-03-17 09:27:31 +00:00
|
|
|
'''
|
2022-03-10 10:33:06 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
# Return the last five created container types
|
|
|
|
return ContainerType.objects.order_by('-created_ts')[:5]
|
2022-03-17 09:27:31 +00:00
|
|
|
'''
|
2022-03-10 10:33:06 +00:00
|
|
|
|
|
|
|
|
2022-03-17 09:27:31 +00:00
|
|
|
class ContainerTypeDetailView(generic.DetailView):
|
2022-03-10 10:33:06 +00:00
|
|
|
model = ContainerType
|
|
|
|
context_object_name = 'container_type'
|
|
|
|
template_name = 'container/container_type_detail.html'
|
|
|
|
|
|
|
|
|
2022-03-17 09:27:31 +00:00
|
|
|
class ContainerTypeCreateView(generic.CreateView):
|
|
|
|
model = ContainerType
|
|
|
|
# template_name = 'container/detail.html'
|
|
|
|
fields = ['named_id', 'description', 'width', 'length', 'height', 'inner_width', 'inner_length', 'inner_height',
|
|
|
|
'has_cover', 'contains_container']
|
2022-03-09 10:33:05 +00:00
|
|
|
|
2022-03-17 09:27:31 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.changed_by = self.request.user
|
|
|
|
form.instance.created_by = self.request.user
|
|
|
|
return super().form_valid(form)
|
2022-03-17 09:42:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContainerTypeUpdateView(generic.UpdateView):
|
|
|
|
model = ContainerType
|
|
|
|
# template_name = 'container/detail.html'
|
|
|
|
fields = ['named_id', 'description', 'width', 'length', 'height', 'inner_width', 'inner_length', 'inner_height',
|
|
|
|
'has_cover', 'contains_container']
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.changed_by = self.request.user
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerTypeDeleteView(generic.DetailView):
|
|
|
|
model = ContainerType
|