68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
from django.http import Http404, FileResponse
|
|
from django.views.generic.base import TemplateView, View
|
|
from cv.models import Profile
|
|
from cv.services.dowload.docx import DocxRenderer
|
|
from cv.services.dowload.pdf import PdfRenderer
|
|
|
|
class ProfileView(TemplateView):
|
|
template_name = 'index.html'
|
|
extra_context = {}
|
|
|
|
def get_context_data(self, **kwargs):
|
|
kwargs.setdefault("view", self)
|
|
extra_context = self._get_extra_context()
|
|
self.extra_context.update(extra_context)
|
|
if self.extra_context is not None:
|
|
kwargs.update(self.extra_context)
|
|
return kwargs
|
|
|
|
def _get_extra_context(self):
|
|
profile = Profile.objects.prefetch_related('experience', 'skills_map').first()
|
|
if not profile:
|
|
raise Http404("Профиль не найден")
|
|
return {
|
|
'profile': profile,
|
|
}
|
|
|
|
|
|
class DownloadDocxView(View):
|
|
_docx = DocxRenderer()
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
profile = Profile.objects.prefetch_related("experience", "skills_map").first()
|
|
if not profile:
|
|
raise Http404("Профиль не найден")
|
|
|
|
buffer = self._docx.render(profile)
|
|
|
|
safe_utf8 = f"resume_{profile.full_name.replace(' ', '_')}.docx"
|
|
buffer.seek(0)
|
|
resp = FileResponse(
|
|
buffer,
|
|
as_attachment=True,
|
|
filename=safe_utf8,
|
|
content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
)
|
|
resp["Cache-Control"] = "no-store"
|
|
return resp
|
|
|
|
|
|
class DownloadPdfView(View):
|
|
_pdf = PdfRenderer()
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
profile = Profile.objects.prefetch_related("experience", "skills_map").first()
|
|
if not profile:
|
|
raise Http404("Профиль не найден")
|
|
|
|
stream = self._pdf.render(profile)
|
|
|
|
safe_utf8 = f"resume_{profile.full_name.replace(' ', '_')}.pdf"
|
|
resp = FileResponse(
|
|
stream,
|
|
as_attachment=True,
|
|
filename=safe_utf8,
|
|
content_type="application/pdf",
|
|
)
|
|
resp["Cache-Control"] = "no-store"
|
|
return resp |