Exemplo n.º 1
0
Arquivo: pdf.py Projeto: zfb103/pretix
    def post(self, request, *args, **kwargs):
        if "background" in request.FILES:
            error, fileobj = self.process_upload()
            if error:
                return JsonResponse({
                    "status": "error",
                    "error": error
                })
            c = CachedFile()
            c.expires = now() + timedelta(days=7)
            c.date = now()
            c.filename = 'background_preview.pdf'
            c.type = 'application/pdf'
            c.file = fileobj
            c.save()
            c.refresh_from_db()
            return JsonResponse({
                "status": "ok",
                "id": c.id,
                "url": reverse('control:pdf.background', kwargs={
                    'event': request.event.slug,
                    'organizer': request.organizer.slug,
                    'filename': str(c.id)
                })
            })

        cf = None
        if request.POST.get("background", "").strip():
            try:
                cf = CachedFile.objects.get(id=request.POST.get("background"))
            except CachedFile.DoesNotExist:
                pass

        if "preview" in request.POST:
            with rolledback_transaction(), language(request.event.settings.locale):
                p = self._get_preview_position()
                fname, mimet, data = self.generate(
                    p,
                    override_layout=(json.loads(self.request.POST.get("data"))
                                     if self.request.POST.get("data") else None),
                    override_background=cf.file if cf else None
                )

            resp = HttpResponse(data, content_type=mimet)
            ftype = fname.split(".")[-1]
            resp['Content-Disposition'] = 'attachment; filename="ticket-preview.{}"'.format(ftype)
            return resp
        elif "data" in request.POST:
            if cf:
                self.save_background(cf)
            self.save_layout()
            return JsonResponse({'status': 'ok'})
        return HttpResponseBadRequest()
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        if "background" in request.FILES:
            error, fileobj = self.process_upload()
            if error:
                return JsonResponse({"status": "error", "error": error})
            c = CachedFile()
            c.expires = now() + timedelta(days=7)
            c.date = now()
            c.filename = 'background_preview.pdf'
            c.type = 'application/pdf'
            c.file = fileobj
            c.save()
            c.refresh_from_db()
            return JsonResponse({
                "status":
                "ok",
                "id":
                c.id,
                "url":
                reverse('plugins:ticketoutputpdf:pdf',
                        kwargs={
                            'event': request.event.slug,
                            'organizer': request.organizer.slug,
                            'filename': str(c.id)
                        })
            })

        cf = None
        if request.POST.get("background", "").strip():
            try:
                cf = CachedFile.objects.get(id=request.POST.get("background"))
            except CachedFile.DoesNotExist:
                pass

        if "preview" in request.POST:
            with rolledback_transaction(), language(
                    request.event.settings.locale):
                p = self._get_preview_position()

                prov = self.get_output(
                    override_layout=(json.loads(request.POST.get("data"))
                                     if request.POST.get("data") else None),
                    override_background=cf.file if cf else None)
                fname, mimet, data = prov.generate(p)

            resp = HttpResponse(data, content_type=mimet)
            ftype = fname.split(".")[-1]
            resp[
                'Content-Disposition'] = 'attachment; filename="ticket-preview.{}"'.format(
                    ftype)
            return resp
        elif "data" in request.POST:
            if cf:
                fexisting = request.event.settings.get(
                    'ticketoutput_{}_layout'.format(self.identifier),
                    as_type=File)
                if fexisting:
                    try:
                        default_storage.delete(fexisting.name)
                    except OSError:  # pragma: no cover
                        logger.error('Deleting file %s failed.' %
                                     fexisting.name)

                # Create new file
                nonce = get_random_string(length=8)
                fname = '%s-%s/%s/%s.%s.%s' % (
                    'event', 'settings', self.request.event.pk,
                    'ticketoutput_{}_layout'.format(
                        self.identifier), nonce, 'pdf')
                newname = default_storage.save(fname, cf.file)
                request.event.settings.set(
                    'ticketoutput_{}_background'.format(self.identifier),
                    'file://' + newname)

            request.event.settings.set(
                'ticketoutput_{}_layout'.format(self.identifier),
                request.POST.get("data"))

            CachedTicket.objects.filter(
                order_position__order__event=self.request.event,
                provider=self.identifier).delete()
            CachedCombinedTicket.objects.filter(
                order__event=self.request.event,
                provider=self.identifier).delete()

            return JsonResponse({'status': 'ok'})
        return HttpResponseBadRequest()
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        if "background" in request.FILES:
            error, fileobj = self.process_upload()
            if error:
                return JsonResponse({
                    "status": "error",
                    "error": error
                })
            c = CachedFile()
            c.expires = now() + timedelta(days=7)
            c.date = now()
            c.filename = 'background_preview.pdf'
            c.type = 'application/pdf'
            c.file = fileobj
            c.save()
            c.refresh_from_db()
            return JsonResponse({
                "status": "ok",
                "id": c.id,
                "url": reverse('plugins:ticketoutputpdf:pdf', kwargs={
                    'event': request.event.slug,
                    'organizer': request.organizer.slug,
                    'filename': str(c.id)
                })
            })

        cf = None
        if request.POST.get("background", "").strip():
            try:
                cf = CachedFile.objects.get(id=request.POST.get("background"))
            except CachedFile.DoesNotExist:
                pass

        if "preview" in request.POST:
            with rolledback_transaction(), language(request.event.settings.locale):
                item = request.event.items.create(name=_("Sample product"), default_price=42.23)
                item2 = request.event.items.create(name=_("Sample workshop"), default_price=23.40)

                from pretix.base.models import Order
                order = request.event.orders.create(status=Order.STATUS_PENDING, datetime=now(),
                                                    email='*****@*****.**',
                                                    expires=now(), code="PREVIEW1234", total=119)

                p = order.positions.create(item=item, attendee_name=_("John Doe"), price=item.default_price)
                order.positions.create(item=item2, attendee_name=_("John Doe"), price=item.default_price, addon_to=p)
                order.positions.create(item=item2, attendee_name=_("John Doe"), price=item.default_price, addon_to=p)

                prov = PdfTicketOutput(request.event,
                                       override_layout=(json.loads(request.POST.get("data"))
                                                        if request.POST.get("data") else None),
                                       override_background=cf.file if cf else None)
                fname, mimet, data = prov.generate(p)

            resp = HttpResponse(data, content_type=mimet)
            ftype = fname.split(".")[-1]
            resp['Content-Disposition'] = 'attachment; filename="ticket-preview.{}"'.format(ftype)
            return resp
        elif "data" in request.POST:
            if cf:
                fexisting = request.event.settings.get('ticketoutput_pdf_layout', as_type=File)
                if fexisting:
                    try:
                        default_storage.delete(fexisting.name)
                    except OSError:  # pragma: no cover
                        logger.error('Deleting file %s failed.' % fexisting.name)

                # Create new file
                nonce = get_random_string(length=8)
                fname = '%s-%s/%s/%s.%s.%s' % (
                    'event', 'settings', self.request.event.pk, 'ticketoutput_pdf_layout', nonce, 'pdf'
                )
                newname = default_storage.save(fname, cf.file)
                request.event.settings.set('ticketoutput_pdf_background', 'file://' + newname)

            request.event.settings.set('ticketoutput_pdf_layout', request.POST.get("data"))
            return JsonResponse({'status': 'ok'})
        return HttpResponseBadRequest()
Exemplo n.º 4
0
    def post(self, request, *args, **kwargs):
        if "emptybackground" in request.POST:
            p = PdfFileWriter()
            try:
                p.addBlankPage(
                    width=float(request.POST.get('width')) * mm,
                    height=float(request.POST.get('height')) * mm,
                )
            except ValueError:
                return JsonResponse({
                    "status": "error",
                    "error": "Invalid height/width given."
                })
            buffer = BytesIO()
            p.write(buffer)
            buffer.seek(0)
            c = CachedFile(web_download=True)
            c.expires = now() + timedelta(days=7)
            c.date = now()
            c.filename = 'background_preview.pdf'
            c.type = 'application/pdf'
            c.save()
            c.file.save('empty.pdf', ContentFile(buffer.read()))
            c.refresh_from_db()
            return JsonResponse({
                "status":
                "ok",
                "id":
                c.id,
                "url":
                reverse('control:pdf.background',
                        kwargs={
                            'event': request.event.slug,
                            'organizer': request.organizer.slug,
                            'filename': str(c.id)
                        })
            })

        if "background" in request.FILES:
            error, fileobj = self.process_upload()
            if error:
                return JsonResponse({"status": "error", "error": error})
            c = CachedFile(web_download=True)
            c.expires = now() + timedelta(days=7)
            c.date = now()
            c.filename = 'background_preview.pdf'
            c.type = 'application/pdf'
            c.file = fileobj
            c.save()
            c.refresh_from_db()
            return JsonResponse({
                "status":
                "ok",
                "id":
                c.id,
                "url":
                reverse('control:pdf.background',
                        kwargs={
                            'event': request.event.slug,
                            'organizer': request.organizer.slug,
                            'filename': str(c.id)
                        })
            })

        cf = None
        if request.POST.get("background", "").strip():
            try:
                cf = CachedFile.objects.get(id=request.POST.get("background"))
            except CachedFile.DoesNotExist:
                pass

        if "preview" in request.POST:
            with rolledback_transaction(), language(
                    request.event.settings.locale,
                    request.event.settings.region):
                p = self._get_preview_position()
                fname, mimet, data = self.generate(
                    p,
                    override_layout=(json.loads(self.request.POST.get("data"))
                                     if self.request.POST.get("data") else
                                     None),
                    override_background=cf.file if cf else None)

            resp = HttpResponse(data, content_type=mimet)
            ftype = fname.split(".")[-1]
            if settings.DEBUG:
                # attachment is more secure as we're dealing with user-generated stuff here, but inline is much more convenient during debugging
                resp[
                    'Content-Disposition'] = 'inline; filename="ticket-preview.{}"'.format(
                        ftype)
                resp._csp_ignore = True
            else:
                resp[
                    'Content-Disposition'] = 'attachment; filename="ticket-preview.{}"'.format(
                        ftype)
            return resp
        elif "data" in request.POST:
            if cf:
                self.save_background(cf)
            self.save_layout()
            return JsonResponse({'status': 'ok'})
        return HttpResponseBadRequest()