Skip to content

Commit

Permalink
add view to add jobs based on stl
Browse files Browse the repository at this point in the history
  • Loading branch information
luto committed Nov 2, 2024
1 parent 7d91bd2 commit 326c3a8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block content %}
<div class="grid-x grid-margin-x">
<h1 class="cell">Add Print Job</h1>
<form method="POST" class="cell small-6 medium-4">
<form method="POST" class="cell small-6 medium-4" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button class="button success large" type="submit">add</a>
Expand Down
9 changes: 8 additions & 1 deletion src/crowdprinter/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
path("", views.PrintJobListView.as_view()),
path("myprints", views.MyPrintAttempts.as_view(), name="my_printattempts"),
path(
"create/text", views.PrintJobCreateView.as_view(), name="printjob_create_text"
"create/text",
views.PrintJobTextCreateView.as_view(),
name="printjob_create_text",
),
path(
"create/stl",
views.PrintJobStlCreateView.as_view(),
name="printjob_create_stl",
),
path(
"printjob/<slug>/",
Expand Down
70 changes: 53 additions & 17 deletions src/crowdprinter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,21 @@ def get_queryset(self):
return super().get_queryset().filter(can_attempt=True).order_by("?")


class PrintJobForm(forms.ModelForm):
def make_gcode_files(job):
for printer in [
# TODO: support multiple printers w/ presets
models.Printer.objects.first()
]:
with (tempfile.NamedTemporaryFile(suffix=".gcode", delete=False) as f_gcode,):
stl_generator.stl_to_gcode(job.file_stl.path, f_gcode)
models.PrintJobFile.objects.create(
job=job,
printer=printer,
file_gcode=ContentFile(f_gcode.read(), name=f"{job.slug}.gcode"),
)


class PrintJobTextForm(forms.ModelForm):
text = forms.CharField(
required=True,
widget=forms.Textarea(attrs={"rows": 4, "cols": 40}),
Expand All @@ -83,7 +97,6 @@ def save(self, commit=True):
with (
tempfile.NamedTemporaryFile(suffix=".stl", delete=False) as f_stl,
tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f_png,
tempfile.NamedTemporaryFile(suffix=".gcode", delete=False) as f_gcode,
):
stl_generator.text_to_stl(self.cleaned_data.get("text"), f_stl)
f_stl.seek(0)
Expand All @@ -94,30 +107,53 @@ def save(self, commit=True):
job.file_render = ContentFile(f_png.read(), name=f"{slug}.png")

job.save()

for printer in [
models.Printer.objects.first()
]: # TODO: support multiple printers w/ presets
stl_generator.stl_to_gcode(f_stl.name, f_gcode)
f_gcode.seek(0)
models.PrintJobFile.objects.create(
job=job,
printer=printer,
file_gcode=ContentFile(f_gcode.read(), name=f"{slug}.gcode"),
)
make_gcode_files(job)

return job


class PrintJobCreateView(SuperUserRequiredMixin, SuccessMessageMixin, CreateView):
template_name = "crowdprinter/printjob_create_text.html"
class PrintJobTextCreateView(SuperUserRequiredMixin, SuccessMessageMixin, CreateView):
template_name = "crowdprinter/printjob_create_form.html"
model = PrintJob
form_class = PrintJobForm
template_name = "crowdprinter/printjob_create_text.html"
form_class = PrintJobTextForm
success_url = reverse_lazy("printjob_create_text")
success_message = "Job %(slug)s was created successfully"


class PrintJobStlForm(forms.ModelForm):
class Meta:
model = PrintJob
fields = [
"slug",
"count_needed",
"file_stl",
]

@transaction.atomic
def save(self, commit=True):
job = super().save(commit=False)
slug = self.cleaned_data.get("slug")

with (tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f_png,):
stl_generator.stl_to_png(job.file_stl.path, f_png)
f_png.seek(0)
job.file_render = ContentFile(f_png.read(), name=f"{slug}.png")

job.save()

make_gcode_files(job)

return job


class PrintJobStlCreateView(SuperUserRequiredMixin, SuccessMessageMixin, CreateView):
template_name = "crowdprinter/printjob_create_form.html"
model = PrintJob
form_class = PrintJobStlForm
success_url = reverse_lazy("printjob_create_stl")
success_message = "Job %(slug)s was created successfully"


class MyPrintAttempts(ListView):
model = models.PrintAttempt
template_name = "crowdprinter/myprintattempts.html"
Expand Down

0 comments on commit 326c3a8

Please sign in to comment.