5acc6ddefd6045473ad498c000ec60ae48f8b49c
[htsworkflow.git] / htsworkflow / frontend / bcmagic / utils.py
1 from django.conf import settings
2
3 import ftplib
4 import socket
5 import StringIO
6
7
8 def print_zpl(zpl_text, host=settings.BCPRINTER_PRINTER1_HOST):
9     """
10     Sends zpl_text to printer
11     """
12     ftp = ftplib.FTP(host=host, user='blank', passwd='')
13     ftp.login()
14     ftp.storlines("STOR printme.txt", StringIO.StringIO(zpl_text))
15     ftp.quit()
16
17
18 def print_zpl_socket(zpl_text,
19                      host=settings.BCPRINTER_PRINTER1_HOST,
20                      port=settings.BCPRINTER_PRINTER1_PORT):
21     """
22     Sends zpl_text to printer via a socket
23
24     if zpl_text is a list of zpl_texts, it will print each one
25     in that list.
26     """
27
28     # Process anyway if zpl_text is a list.
29     if type(zpl_text) is list:
30         zpl_text = '\n'.join(zpl_text)
31
32     s = socket.socket()
33     # PORT 9100 is default for Zebra tabletop/desktop printers
34     # PORT 6101 is default for Zebra mobile printers
35     s.connect((host, port))
36     s.sendall(zpl_text)
37     s.close()
38
39
40 def report_error(message):
41     """
42     Return a dictionary with a command to display 'message'
43     """
44     return {'mode': 'Error', 'status': message}
45
46
47 def redirect_to_url(url):
48     """
49     Return a bcm dictionary with a command to redirect to 'url'
50     """
51     return {'mode': 'redirect', 'url': url}
52
53
54 def autofill(field, value):
55     """
56     Return a bcm dictionary with a command to automatically fill the
57     corresponding "field" with "value"
58     """
59     return {'mode': 'autofill', 'field': field, 'value': value}