Move use of settings default into the function instead of function definition.
[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=None):
9     """
10     Sends zpl_text to printer
11     """
12     if not host:
13         host = settings.BCPRINTER_PRINTER1_HOST
14     ftp = ftplib.FTP(host=host, user='blank', passwd='')
15     ftp.login()
16     ftp.storlines("STOR printme.txt", StringIO.StringIO(zpl_text))
17     ftp.quit()
18
19
20 def print_zpl_socket(zpl_text, host=None, port=None):
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     if not host:
29         host=settings.BCPRINTER_PRINTER1_HOST
30     if not port:
31         port=settings.BCPRINTER_PRINTER1_PORT
32
33     # Process anyway if zpl_text is a list.
34     if type(zpl_text) is list:
35         zpl_text = '\n'.join(zpl_text)
36
37     s = socket.socket()
38     # PORT 9100 is default for Zebra tabletop/desktop printers
39     # PORT 6101 is default for Zebra mobile printers
40     s.connect((host, port))
41     s.sendall(zpl_text)
42     s.close()
43
44
45 def report_error(message):
46     """
47     Return a dictionary with a command to display 'message'
48     """
49     return {'mode': 'Error', 'status': message}
50
51
52 def redirect_to_url(url):
53     """
54     Return a bcm dictionary with a command to redirect to 'url'
55     """
56     return {'mode': 'redirect', 'url': url}
57
58
59 def autofill(field, value):
60     """
61     Return a bcm dictionary with a command to automatically fill the
62     corresponding "field" with "value"
63     """
64     return {'mode': 'autofill', 'field': field, 'value': value}