def api_part_detail_print(part_id): """ API endpoint to print a selected part with `part_id`. """ number = 1 destination = 'victoria' if request.method == 'POST': form = request.get_json() n = form.get("number") destination = form.get("destination") or "victoria" if isinstance(n, str) and n.isnumeric(): number = int(n) elif isinstance(n, int): number = n in_db = Part.query.get(part_id) if in_db is None: return jsonify({"response": "error"}) in_db.printed(number) db.session.commit() msg = IpcPrintMessage(name=in_db.name, barcode=in_db.barcode, number=number, origin=IpcOrigin.HURON) if redis_send_to_print(r, destination, msg) is None: current_app.logger.warning( "No recipient in channel '%s' for the msg: '%s'" % (destination, str(msg))) return jsonify({'response': 'ok'})
def api_print(): """ API endpoint to send print job to a remote printer. """ form = request.get_json() if form is None: form = request.form barcode = form.get("barcode", "") name = form.get("name", "") number = form.get("number", "1") destination = form.get("destination") or "victoria" in_db = Part.query.filter(Part.barcode == barcode).first() if type(number) == str: if number.isdigit(): number = int(number) else: number = 1 if in_db: in_db.printed(number) db.session.commit() msg = IpcPrintMessage(name=name, barcode=barcode, number=number, origin=IpcOrigin.HURON) if redis_send_to_print(r, destination, msg) is None: current_app.logger.warning("No recipient for the msg: '%s'" % (str(msg))) return jsonify({'response': 'ok'})
def test_msg_serialize1(self): i = IpcPrintMessage( barcode="barcode", name="name", origin=IpcOrigin.TEST, destination="victoria", number=1, device=None, ) dump = json.dumps(i._asdict()) self.assertEqual( dump, '{"destination": "victoria", "origin": 1, "device": null, "msg_type": 1, "barcode": "barcode", "name": "name", "number": 1}', ) ii = IpcPrintMessage(**json.loads(dump)) self.assertEqual(ii, i)
def test_msg_asdict(self): i = IpcPrintMessage( barcode="barcode", name="name", origin=IpcOrigin.TEST, destination="victoria", number=1, device=None, ) d = i._asdict() print(d) self.assertEqual(d["barcode"], "barcode") self.assertEqual(d["name"], "name") self.assertEqual(d["origin"], 1) self.assertEqual(d["destination"], "victoria") self.assertEqual(d["number"], 1) self.assertEqual(d["msg_type"], 1) self.assertEqual(d["device"], None)
def test_printer_add_transaction(self): msg = IpcPrintMessage( barcode="hello", name="world", origin=IpcOrigin.TEST, destination="victoria", number=1, ) p = Printer( name="main", type=1, dialect=1, redis="victoria", settings='{"address": "192.168.0.1"}', ) db.session.add(p) pt = p.add_transaction(**msg._asdict()) db.session.add(pt) db.session.commit() self.assertEqual(Printer.query.count(), 1) self.assertEqual(PrinterTransaction.query.count(), 1) self.assertEqual(len(p.transactions), 1) self.assertEqual(p.transactions[0], pt)