(frontend) set up Vite-based frontend project

Chose Vite for static output efficiency, aligning with project needs.

All API interactions are currently unauthenticated. SSO support
planned soon, using ProConnect.

UX is minimalistic, and showcases the core idea.

Components introduced:
* AppProvider
* Select and TextArea Rhf inputs

API hooks introduced:
* useGeneratePDF, generates a PDF, and downloads it in the client.
* useTemplates, fetches available templates to populate Select options.
This commit is contained in:
Lebaud Antoine
2024-01-10 15:07:05 +01:00
parent 62df0524ac
commit 312a680b66
41 changed files with 6926 additions and 3 deletions

View File

@@ -236,7 +236,7 @@ class Template(BaseModel):
Generate and return a PDF document for this template around the
markdown body passed as argument.
"""
body_html = markdown.markdown(textwrap.dedent(body)) if body else ""
body_html = markdown.markdown(textwrap.dedent(body)) if body else ""
document_html = HTML(string=DjangoTemplate(self.code).render(Context({"body": body_html})))
css = CSS(
string=self.css,

View File

@@ -1,8 +1,10 @@
"""URL configuration for the core app."""
from django.urls import path
from core.views import generate_document
from core.views import generate_document, TemplatesApiView, GenerateDocumentAPIView
urlpatterns = [
path('generate-document/', generate_document, name='generate_document'),
path('api/generate-document/', GenerateDocumentAPIView.as_view(), name='generate-document'),
path('api/templates', TemplatesApiView.as_view()),
]

View File

@@ -1,6 +1,14 @@
from django.shortcuts import render, HttpResponse
from .forms import DocumentGenerationForm
from .models import Template
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers
from django.http import FileResponse
from io import BytesIO
def generate_document(request):
@@ -25,3 +33,47 @@ def generate_document(request):
return render(request, 'core/generate_document.html', {'form': form})
class DocumentGenerationSerializer(serializers.Serializer):
body = serializers.CharField(label="Markdown Body")
template_id = serializers.UUIDField(format='hex_verbose')
class GenerateDocumentAPIView(APIView):
def post(self, request):
serializer = DocumentGenerationSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
template_id = serializer.validated_data['template_id']
body = serializer.validated_data['body']
try:
template = Template.objects.get(pk=template_id)
except Template.DoesNotExist:
return Response("Template not found", status=status.HTTP_404_NOT_FOUND)
pdf_content = template.generate_document(body)
response = FileResponse(BytesIO(pdf_content), content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename={template.title}.pdf'
return response
class TemplateSerializer(serializers.ModelSerializer):
class Meta:
model = Template
fields = ['id', 'title']
class TemplatesApiView(APIView):
"""Wip."""
def get(self, request, *args, **kwargs):
"""Wip."""
templates = Template.objects.all()
serializer = TemplateSerializer(templates, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

View File

@@ -270,7 +270,7 @@ class Base(Configuration):
# CORS
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_ALL_ORIGINS = values.BooleanValue(False)
CORS_ALLOW_ALL_ORIGINS = values.BooleanValue(True)
CORS_ALLOWED_ORIGINS = values.ListValue([])
CORS_ALLOWED_ORIGIN_REGEXES = values.ListValue([])