Example #1
0
def printer_print_api():
    printer = hardware.Printer.query.get(1)
    pixels = image_encoding.default_pipeline(
        templating.default_template(flask.request.data))
    hardware_message = messages.SetDeliveryAndPrint(
        device_address=printer.device_address,
        pixels=pixels,
    )

    # If a printer is "offline" then we won't find the printer
    # connected and success will be false.
    success, next_print_id = protocol_loop.send_message(
        printer.device_address, hardware_message)

    if success:
        flask.flash('Sent your message to the printer!')
        stats.inc('printer.print.ok')
    else:
        flask.flash(("Could not send message because the "
                     "printer {} is offline.").format(printer.name),
                    'error')
        stats.inc('printer.print.offline')

    # We know immediately if the printer wasn't online.
    if not success:
        model_message.failure_message = 'Printer offline'
        model_message.response_timestamp = datetime.datetime.utcnow()
    db.session.add(model_message)

    return 'ok'
Example #2
0
    def print_html(self, html, from_name, face='default'):
        from sirius.coding import image_encoding
        from sirius.coding import templating

        pixels = image_encoding.default_pipeline(
            templating.default_template(html, from_name=from_name))

        self.print_pixels(pixels, from_name=from_name, face=face)
Example #3
0
def preview_api():
    message = flask.request.data.decode('utf-8')
    pixels = image_encoding.default_pipeline(
        templating.default_template(message))
    png = io.BytesIO()
    pixels.save(png, "PNG")

    stats.inc('printer.preview')

    return '<img style="width: 100%;" src="data:image/png;base64,{}">'.format(base64.b64encode(png.getvalue()))
Example #4
0
    def print_html(self, html, from_name, face='default'):
        from sirius.protocol import protocol_loop
        from sirius.protocol import messages
        from sirius.coding import image_encoding
        from sirius.coding import templating
        from sirius import stats
        from sirius.models import messages as model_messages

        pixels = image_encoding.default_pipeline(
            templating.default_template(html, from_name=from_name)
        )

        hardware_message = None
        if face == "noface":
            hardware_message = messages.SetDeliveryAndPrintNoFace(
                device_address=self.device_address,
                pixels=pixels,
            )
        else:
            hardware_message = messages.SetDeliveryAndPrint(
                device_address=self.device_address,
                pixels=pixels,
            )

        # If a printer is "offline" then we won't find the printer
        # connected and success will be false.
        success, next_print_id = protocol_loop.send_message(
            self.device_address,
            hardware_message
        )

        if success:
            stats.inc('printer.print.ok')
        else:
            stats.inc('printer.print.offline')

        # Store the same message in the database.
        png = io.BytesIO()
        pixels.save(png, "PNG")
        model_message = model_messages.Message(
            print_id=next_print_id,
            pixels=bytearray(png.getvalue()),
            sender_name=from_name,
            target_printer=self,
        )

        # We know immediately if the printer wasn't online.
        if not success:
            model_message.failure_message = 'Printer offline'
            model_message.response_timestamp = datetime.datetime.utcnow()
        db.session.add(model_message)

        if not success:
            raise Printer.OfflineError()
Example #5
0
def preview(user_id, username, printer_id):
    assert user_id == login.current_user.id
    assert username == login.current_user.username

    message = flask.request.data.decode('utf-8')
    pixels = image_encoding.default_pipeline(
        templating.default_template(message, from_name=login.current_user.username))
    png = io.BytesIO()
    pixels.save(png, "PNG")

    stats.inc('printer.preview')

    return '<img style="width: 100%;" src="data:image/png;base64,{}">'.format(base64.b64encode(png.getvalue()).decode('utf-8'))
Example #6
0
def preview(user_id, username, printer_id):
    assert user_id == login.current_user.id
    assert username == login.current_user.username

    message = flask.request.data.decode('utf-8')
    pixels = image_encoding.default_pipeline(
        templating.default_template(message))
    png = io.BytesIO()
    pixels.save(png, "PNG")

    stats.inc('printer.preview')

    return '<img style="width: 100%;" src="data:image/png;base64,{}">'.format(base64.b64encode(png.getvalue()))
    def test_snapshot_template_fixtures(self):
        fixtures = {
            'hello_world': '<p>Hello, world!</p>',
        }

        fixture_username = '******'
        fixture_date = datetime.datetime(2012, 2, 3, 15, 46, 12)

        for name, snippet in fixtures.items():
            self._check_html(
                name, templating.default_template(
                    snippet,
                    fixture_username,
                    fixture_date
                )
            )
Example #8
0
def printer_print(printer_id):
    printer = hardware.Printer.query.get(printer_id)
    if printer is None:
        flask.abort(404)

    # PERMISSIONS
    # the printer must either belong to this user, or be
    # owned by a friend
    if printer.owner.id == login.current_user.id:
        # fine
        pass
    elif printer.id in [p.id for p in login.current_user.friends_printers()]:
        # fine
        pass
    else:
        flask.abort(404)

    form = PrintForm()
    # Note that the form enforces access permissions: People can't
    # submit a valid printer-id that's not owned by the user or one of
    # the user's friends.
    choices = [(x.id, x.name) for x in login.current_user.printers] + [
        (x.id, x.name) for x in login.current_user.friends_printers()
    ]
    form.target_printer.choices = choices

    # Set default printer on get
    if flask.request.method != 'POST':
        form.target_printer.data = printer.id

    if form.validate_on_submit():
        # TODO: move image encoding into a pthread.
        # TODO: use templating to avoid injection attacks
        pixels = image_encoding.default_pipeline(
            templating.default_template(form.message.data))
        hardware_message = messages.SetDeliveryAndPrint(
            device_address=printer.device_address,
            pixels=pixels,
        )

        # If a printer is "offline" then we won't find the printer
        # connected and success will be false.
        success, next_print_id = protocol_loop.send_message(
            printer.device_address, hardware_message)

        if success:
            flask.flash('Sent your message to the printer!')
            stats.inc('printer.print.ok')
        else:
            flask.flash(("Could not send message because the "
                         "printer {} is offline.").format(printer.name),
                        'error')
            stats.inc('printer.print.offline')

        # Store the same message in the database.
        png = io.BytesIO()
        pixels.save(png, "PNG")
        model_message = model_messages.Message(
            print_id=next_print_id,
            pixels=bytearray(png.getvalue()),
            sender_id=login.current_user.id,
            target_printer=printer,
        )

        # We know immediately if the printer wasn't online.
        if not success:
            model_message.failure_message = 'Printer offline'
            model_message.response_timestamp = datetime.datetime.utcnow()
        db.session.add(model_message)

        return flask.redirect(
            flask.url_for('printer_overview.printer_overview',
                          printer_id=printer.id))

    return flask.render_template(
        'printer_print.html',
        printer=printer,
        form=form,
    )
Example #9
0
def print_html(printer_id):
    printer = hardware.Printer.query.get(printer_id)
    if printer is None:
        flask.abort(404)

    # PERMISSIONS
    # the printer must either belong to this user, or be
    # owned by a friend
    if printer.owner.id == login.current_user.id:
        # fine
        pass
    elif printer.id in [p.id for p in login.current_user.friends_printers()]:
        # fine
        pass
    else:
        flask.abort(404)

    request.get_data()
    task = json.loads(request.data)
    if not task['message'] or not task['face']:
        flask.abort(500)

    pixels = image_encoding.default_pipeline(
        templating.default_template(task['message'],
                                    from_name=login.current_user.username))

    hardware_message = None
    if task['face'] == "noface":
        hardware_message = messages.SetDeliveryAndPrintNoFace(
            device_address=printer.device_address,
            pixels=pixels,
        )
    else:
        hardware_message = messages.SetDeliveryAndPrint(
            device_address=printer.device_address,
            pixels=pixels,
        )

    # If a printer is "offline" then we won't find the printer
    # connected and success will be false.
    success, next_print_id = protocol_loop.send_message(
        printer.device_address, hardware_message)

    # Store the same message in the database.
    png = io.BytesIO()
    pixels.save(png, "PNG")
    model_message = model_messages.Message(
        print_id=next_print_id,
        pixels=bytearray(png.getvalue()),
        sender_id=login.current_user.id,
        target_printer=printer,
    )

    # We know immediately if the printer wasn't online.
    if not success:
        model_message.failure_message = 'Printer offline'
        model_message.response_timestamp = datetime.datetime.utcnow()
    db.session.add(model_message)

    response = {}
    if success:
        response['status'] = 'Sent your message to the printer!'
    else:
        response['status'] = ("Could not send message because the "
                              "printer {} is offline.").format(printer.name)

    return json.dumps(response)
Example #10
0
def printer_print(printer_id):
    printer = hardware.Printer.query.get(printer_id)
    if printer is None:
        flask.abort(404)

    # PERMISSIONS
    # the printer must either belong to this user, or be
    # owned by a friend
    if printer.owner.id == login.current_user.id:
        # fine
        pass
    elif printer.id in [p.id for p in login.current_user.friends_printers()]:
        # fine
        pass
    else:
        flask.abort(404)

    form = PrintForm()
    # Note that the form enforces access permissions: People can't
    # submit a valid printer-id that's not owned by the user or one of
    # the user's friends.
    choices = [
        (x.id, x.name) for x in login.current_user.printers
    ] + [
        (x.id, x.name) for x in login.current_user.friends_printers()
    ]
    form.target_printer.choices = choices

    # Set default printer on get
    if flask.request.method != 'POST':
        form.target_printer.data = printer.id

    if form.validate_on_submit():
        # TODO: move image encoding into a pthread.
        # TODO: use templating to avoid injection attacks
        pixels = image_encoding.default_pipeline(
            templating.default_template(form.message.data))
        hardware_message = messages.SetDeliveryAndPrint(
            device_address=printer.device_address,
            pixels=pixels,
        )

        # If a printer is "offline" then we won't find the printer
        # connected and success will be false.
        success, next_print_id = protocol_loop.send_message(
            printer.device_address, hardware_message)

        if success:
            flask.flash('Sent your message to the printer!')
            stats.inc('printer.print.ok')
        else:
            flask.flash(("Could not send message because the "
                         "printer {} is offline.").format(printer.name),
                        'error')
            stats.inc('printer.print.offline')

        # Store the same message in the database.
        png = io.BytesIO()
        pixels.save(png, "PNG")
        model_message = model_messages.Message(
            print_id=next_print_id,
            pixels=bytearray(png.getvalue()),
            sender_id=login.current_user.id,
            target_printer=printer,
        )

        # We know immediately if the printer wasn't online.
        if not success:
            model_message.failure_message = 'Printer offline'
            model_message.response_timestamp = datetime.datetime.utcnow()
        db.session.add(model_message)

        return flask.redirect(flask.url_for(
            'printer_overview.printer_overview',
            printer_id=printer.id))

    return flask.render_template(
        'printer_print.html',
        printer=printer,
        form=form,
    )