Skip to content
Snippets Groups Projects
Verified Commit 782650f7 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add option to directly print newly issued cards

parent a93d2026
No related branches found
No related tags found
1 merge request!6Resolve "Direct printer select in issue form"
Pipeline #82022 failed
......@@ -9,6 +9,13 @@ from aleksis.apps.kort.models import Card, CardLayout, CardLayoutMediaFile, Card
class CardForm(forms.ModelForm):
printer = forms.ModelChoiceField(
queryset=None,
label=_("Card Printer"),
help_text=_("Select a printer to directly print the newly issued card."),
required=False,
)
class Meta:
model = Card
fields = ["person", "valid_until", "layout"]
......@@ -28,6 +35,13 @@ class CardForm(forms.ModelForm):
super().__init__(*args, **kwargs)
self.fields["layout"].required = True
layouts = CardLayout.objects.all()
if layouts.count() == 1:
self.fields["layout"].initial = layouts.first()
printers = CardPrinter.objects.all()
self.fields["printer"].queryset = printers
class CardPrinterForm(forms.ModelForm):
layout = Layout(
......
......@@ -70,6 +70,28 @@ class CardCreateView(PermissionRequiredMixin, RevisionMixin, AdvancedCreateView)
success_message = _("The card has been created successfully.")
success_url = reverse_lazy("cards")
def form_valid(self, form: CardForm) -> HttpResponse:
response = super().form_valid(form)
if form.cleaned_data.get("printer"):
printer = form.cleaned_data["printer"]
try:
job = self.object.print_card(printer)
messages.success(
self.request,
_(
"The print job #{} for the card {} on "
"the printer {} has been created successfully."
).format(job.pk, self.object.person, printer.name),
)
except ValueError as e:
messages.error(
self.request,
_(
"The print job couldn't be started because of the following error: {}"
).format(e),
)
return response
class CardDeleteView(PermissionRequiredMixin, RevisionMixin, AdvancedDeleteView):
"""View used to delete a card."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment