Skip to content

Commit

Permalink
Added option to print multiple copies, unsure of pycups api
Browse files Browse the repository at this point in the history
  • Loading branch information
understreck committed Oct 3, 2022
1 parent 8470531 commit 9b0ccd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
23 changes: 18 additions & 5 deletions koseki/plugins/print/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from flask import Blueprint, render_template, request
from flask_wtf import FlaskForm # type: ignore
from flask_wtf.file import FileField, FileRequired # type: ignore
from wtforms.fields import IntegerField
from wtforms.validators import DataRequired, NumberRange
from werkzeug.wrappers import Response

from koseki.plugin import KosekiPlugin
Expand All @@ -15,13 +17,22 @@

class PrintForm(FlaskForm):
file = FileField("Select Document", validators=[FileRequired()])
copies = IntegerField(
"Number of copies",
default = 1)

def __init__(self, maxCopies: int, *args, **kwargs):
super(PrintForm, self).__init__(*args, **kwargs)
self.copies.validators = [
DataRequired(),
NumberRange(min=1, max=maxCopies)]

class PrintPlugin(KosekiPlugin):
def config(self) -> dict:
return {
"UPLOAD_FOLDER": "./data",
"ALLOWED_EXTENSIONS": ["pdf"]
"ALLOWED_EXTENSIONS": ["pdf"],
"MAX_COPIES": 100
}

def plugin_enable(self) -> None:
Expand Down Expand Up @@ -50,7 +61,7 @@ def allowed_file(self, filename: str) -> bool:
)

def print(self) -> Union[str, Response]:
form = PrintForm()
form = PrintForm(self.app.config["MAX_COPIES"])

if form.validate_on_submit():
# check if the post request has the file part
Expand Down Expand Up @@ -81,6 +92,8 @@ def print(self) -> Union[str, Response]:
)
return render_template("print.html", form=form)

numberCopies: int = int(form.copies.data)

# save file to harddrive
filename = "".join(
[c for c in file.filename if c.isalpha() or c.isdigit() or c == ' ']).rstrip()
Expand All @@ -91,12 +104,12 @@ def print(self) -> Union[str, Response]:
file.save(filepath)

# send file to printer
self.cups_conn.printFile("printer1", filepath, "", {"media": "A4"})
self.cups_conn.printFile("printer1", filepath, "", {"media": "A4", "copies": str(numberCopies)})

# log that a document has been printed
logging.info(
"Document %s printed by %s",
form.file.name, self.util.current_user()
"%d copies of document %s by %s",
numberCopies, form.file.name, self.util.current_user()
)

# delete file after sending it to printer
Expand Down
3 changes: 2 additions & 1 deletion koseki/plugins/print/templates/print.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Print document</h1>
<div class="row">
<div class="col-12 col-md-4">
{{ render_field(form.file, autofocus=true) }}
{{ render_field(form.copies) }}
</div>
</div>
</div>
Expand All @@ -22,4 +23,4 @@ <h1>Print document</h1>
</div>
</div>
</form>
{% endblock %}
{% endblock %}

0 comments on commit 9b0ccd0

Please sign in to comment.