Skip to content

Commit 73e0311

Browse files
hstievatalanzhu0
authored andcommitted
fix(printing): make sure printer is responding
1 parent 95a86e6 commit 73e0311

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

intranet/apps/printing/views.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import subprocess
77
import tempfile
88
from io import BytesIO
9-
from typing import Dict, Optional, Tuple
9+
from typing import Dict, List, Optional, Tuple
1010

1111
import magic
1212
from django.conf import settings
@@ -88,14 +88,14 @@ def parse_alerts(alerts: str) -> Tuple[str, str]:
8888
return alerts_text, printer_class
8989

9090

91-
def get_printers() -> Dict[str, str]:
91+
def get_printers() -> Dict[str, List[str]]:
9292
"""Returns a dictionary mapping name:description for available printers.
9393
9494
This requires that a CUPS client be configured on the server.
9595
Otherwise, this returns an empty dictionary.
9696
9797
Returns:
98-
A dictionary mapping name:description for available printers.
98+
A dictionary mapping name:[description,alerts] for available printers.
9999
"""
100100

101101
key = "printing:printers"
@@ -130,17 +130,20 @@ def get_printers() -> Dict[str, str]:
130130
# extended description we know which printer it's referring to.
131131
last_name = name
132132
elif last_name is not None:
133+
if line.strip() == "The printer is not responding.":
134+
printers[last_name].append("not-responding")
133135
description_match = DESCRIPTION_LINE_RE.match(line)
134136
if description_match is not None:
135137
# Pull out the description
136138
description = description_match.group(1)
137139
# And make sure we don't set an empty description
138140
if description:
139-
printers[last_name] = [description]
141+
printers[last_name][0] = description
140142
alerts_match = ALERTS_LINE_RE.match(line)
141143
if alerts_match is not None:
142144
alerts = alerts_match.group(1)
143-
printers[last_name].append(alerts)
145+
if len(printers[last_name]) == 1: # If already marked as not responding, ignore alerts
146+
printers[last_name].append(alerts)
144147
last_name = None
145148

146149
cache.set(key, printers, timeout=settings.CACHE_AGE["printers_list"])

0 commit comments

Comments
 (0)