Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ COPY . .

EXPOSE 8000

CMD ["sh", "-c", "until mysqladmin ping -hdb -ucheatsheet_user -pcheatsheet_pass --silent; do sleep 2; done && python manage.py runserver 0.0.0.0:8000"]
CMD ["sh", "-c", "until mysqladmin ping -hdb -ucheatsheet_user -pcheatsheet_pass --ssl=0 --silent; do sleep 2; done && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"]
103 changes: 103 additions & 0 deletions backend/api/latex_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import subprocess
import tempfile
import os

LATEX_HEADER = r"""\documentclass[fleqn]{article}
\usepackage[margin=0.15in]{geometry}
\usepackage{amsmath, amssymb}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{titlesec}

\setlength{\mathindent}{0pt}
\setlist[itemize]{noitemsep, topsep=0pt, leftmargin=*}
\pagestyle{empty}

\titlespacing*{\subsection}{0pt}{2pt}{1pt}
\titlespacing*{\section}{0pt}{4pt}{2pt}

\begin{document}
\scriptsize
"""

LATEX_FOOTER = r"""
\end{document}
"""

def build_latex_for_formulas(selected_formulas):
"""
Given a list of selected formulas (each with class_name, category, name, latex),
build a complete LaTeX document.
"""
body_lines = []

# Group formulas by class
by_class = {}
for formula in selected_formulas:
class_name = formula.get("class_name") or formula.get("class")
if class_name not in by_class:
by_class[class_name] = {}

category = formula.get("category")
if category not in by_class[class_name]:
by_class[class_name][category] = []

by_class[class_name][category].append(formula)

# Build LaTeX for each class
for class_name, categories in by_class.items():
body_lines.append("\\section*{" + class_name + "}")
body_lines.append("")

for category_name, formulas in categories.items():
body_lines.append("\\subsection*{" + category_name + "}")
body_lines.append("")
body_lines.append(r"\begin{flushleft}")

for formula in formulas:
name = formula.get("name", "")
latex = formula.get("latex", "")
body_lines.append("\\textbf{" + name + "}")
body_lines.append("\\[ " + latex + " \\]")
body_lines.append("\\\\[4pt]")

body_lines.append(r"\end{flushleft}")
body_lines.append("")

body = "\n".join(body_lines)
return LATEX_HEADER + body + LATEX_FOOTER

def compile_latex_to_pdf(content):
"""
Compiles LaTeX content to a PDF using Tectonic.
Returns the generated PDF as bytes or raises an Exception.
"""
# Ensure document has proper structure
if r"\begin{document}" not in content:
content = LATEX_HEADER + content + LATEX_FOOTER

# Use a context manager so the temporary directory is always cleaned up
with tempfile.TemporaryDirectory() as tempdir:
tex_file_path = os.path.join(tempdir, "document.tex")
with open(tex_file_path, "w", encoding="utf-8") as f:
f.write(content)

try:
subprocess.run(
["tectonic", tex_file_path],
cwd=tempdir,
capture_output=True,
text=True,
check=True,
)
except subprocess.CalledProcessError:
# Propagate the error; the temporary directory will still be cleaned up
raise

pdf_file_path = os.path.join(tempdir, "document.pdf")
if not os.path.exists(pdf_file_path):
raise FileNotFoundError("PDF not generated")

# Read and return the PDF bytes before the temporary directory is removed
with open(pdf_file_path, "rb") as pdf_file:
return pdf_file.read()
19 changes: 10 additions & 9 deletions backend/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from django.urls import path
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from . import views

# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'templates', views.TemplateViewSet, basename='template')
router.register(r'cheatsheets', views.CheatSheetViewSet, basename='cheatsheet')
router.register(r'problems', views.PracticeProblemViewSet, basename='problem')

urlpatterns = [
path("health/", views.health_check, name="health-check"),
path("classes/", views.get_classes, name="get-classes"),
path("generate-sheet/", views.generate_sheet, name="generate-sheet"),
path("compile/", views.compile_latex, name="compile-latex"),

# CRUD endpoints
path("templates/", views.template_list, name="template-list"),
path("templates/<int:pk>/", views.template_detail, name="template-detail"),
path("cheatsheets/", views.cheatsheet_list, name="cheatsheet-list"),
path("cheatsheets/from-template/", views.cheatsheet_from_template, name="cheatsheet-from-template"),
path("cheatsheets/<int:pk>/", views.cheatsheet_detail, name="cheatsheet-detail"),
path("problems/", views.problem_list, name="problem-list"),
path("problems/<int:pk>/", views.problem_detail, name="problem-detail"),
# Include the router URLs for CRUD operations
path('', include(router.urls)),
]
Loading
Loading