Skip to content

Commit

Permalink
add view to create new jobs based on text
Browse files Browse the repository at this point in the history
  • Loading branch information
luto committed Nov 2, 2024
1 parent 3330b4b commit 5ae189d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/crowdprinter/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{% include "crowdprinter/head.html" %}
<div class="content">
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% block content %}{% endblock %}
</div>
<div class="stldecoration top">
Expand Down
12 changes: 12 additions & 0 deletions src/crowdprinter/templates/crowdprinter/printjob_create_text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'base.html' %}

{% 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">
{% csrf_token %}
{{ form.as_p }}
<button class="button success large" type="submit">add</a>
</form>
</div>
{% endblock %}
3 changes: 3 additions & 0 deletions src/crowdprinter/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
path("accounts/", include("allauth.urls")),
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"
),
path(
"printjob/<slug>/",
include(
Expand Down
74 changes: 74 additions & 0 deletions src/crowdprinter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@
import math
import os.path
import random
import tempfile

from django import forms
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.core.files.base import ContentFile
from django.db import transaction
from django.http import FileResponse
from django.http import Http404
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_control
from django.views.generic import CreateView
from django.views.generic import DetailView
from django.views.generic import ListView
from django.views.generic.base import View

import crowdprinter.models as models
import stl_generator

from .models import PrintJob


class SuperUserRequiredMixin(LoginRequiredMixin, UserPassesTestMixin):
def test_func(self):
return self.request.user.is_superuser


class PrintJobListView(ListView):
Expand Down Expand Up @@ -44,6 +61,63 @@ def get_queryset(self):
return super().get_queryset().filter(can_attempt=True).order_by("?")


class PrintJobForm(forms.ModelForm):
text = forms.CharField(
required=True,
widget=forms.Textarea(attrs={"rows": 4, "cols": 40}),
)

class Meta:
model = PrintJob
fields = [
"slug",
"count_needed",
"text",
]

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

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)
job.file_stl = ContentFile(f_stl.read(), name=f"{slug}.stl")

stl_generator.stl_to_png(f_stl.name, f_png)
f_png.seek(0)
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"),
)

return job


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


class MyPrintAttempts(ListView):
model = models.PrintAttempt
template_name = "crowdprinter/myprintattempts.html"
Expand Down
18 changes: 4 additions & 14 deletions pipeline/process.py → src/stl_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import subprocess
import tempfile

BASE_DIR = pathlib.Path(__file__).parent


def text_to_stl(text, f_stl):
path_lib = pathlib.Path("generate_braille_lib.scad").absolute()
path_lib = BASE_DIR / "generate_braille_lib.scad"
with tempfile.NamedTemporaryFile("w", suffix=".scad", delete=False) as f_scad:
f_scad.write(f'BrailleText1="{text.lower()}";')
f_scad.write(f'ProfilText1="{text}";')
Expand Down Expand Up @@ -42,7 +44,7 @@ def stl_to_png(path_stl, f_png):


def stl_to_gcode(path_stl, f_gcode):
path_pp_script = pathlib.Path("insert_m600.py").absolute()
path_pp_script = BASE_DIR / "insert_m600.py"
subprocess.check_call(
[
"prusa-slicer",
Expand All @@ -54,15 +56,3 @@ def stl_to_gcode(path_stl, f_gcode):
f_gcode.name,
]
)


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,
):
text_to_stl("Hello World", f_stl)
stl_to_png(f_stl.name, f_png)
stl_to_gcode(f_stl.name, f_gcode)
print(f_png.name)
print(f_gcode.name)
File renamed without changes.
File renamed without changes.

0 comments on commit 5ae189d

Please sign in to comment.