예제 #1
0
 def post(self, request, *args, **kwargs):
     order = get_object_or_404(self.request.event.orders, code=request.GET.get("code"))
     cf = CachedFile(web_download=True, session_key=self.request.session.session_key)
     cf.date = now()
     cf.type = 'application/pdf'
     cf.expires = now() + timedelta(days=3)
     if 'position' in request.GET:
         qs = order.positions.filter(pk=request.GET.get('position'))
         positions = [p.pk for p in qs]
         if len(positions) < 5:
             cf.filename = f'badges_{self.request.event.slug}_{order.code}_{"_".join(str(p.positionid) for p in qs)}.pdf'
     else:
         positions = [p.pk for p in order.positions.all()]
         cf.filename = f'badges_{self.request.event.slug}_{order.code}.pdf'
     cf.save()
     return self.do(
         self.request.event.pk,
         str(cf.id),
         positions,
     )
예제 #2
0
 def test_file_handling(self):
     cf = CachedFile()
     val = SimpleUploadedFile("testfile.txt", b"file_content")
     cf.file.save("testfile.txt", val)
     cf.type = "text/plain"
     cf.filename = "testfile.txt"
     cf.save()
     assert default_storage.exists(cf.file.name)
     with default_storage.open(cf.file.name, 'r') as f:
         assert f.read().strip() == "file_content"
     cf.delete()
     assert not default_storage.exists(cf.file.name)
예제 #3
0
 def test_file_handling(self):
     cf = CachedFile()
     val = SimpleUploadedFile("testfile.txt", b"file_content")
     cf.file.save("testfile.txt", val)
     cf.type = "text/plain"
     cf.filename = "testfile.txt"
     cf.save()
     assert default_storage.exists(cf.file.name)
     with default_storage.open(cf.file.name, 'r') as f:
         assert f.read().strip() == "file_content"
     cf.delete()
     assert not default_storage.exists(cf.file.name)
예제 #4
0
파일: pdf.py 프로젝트: 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()
예제 #5
0
def export(event: Event, shredders: List[str], session_key=None) -> None:
    known_shredders = event.get_data_shredders()

    with NamedTemporaryFile() as rawfile:
        with ZipFile(rawfile, 'w') as zipfile:
            ccode = get_random_string(6)
            zipfile.writestr(
                'CONFIRM_CODE.txt',
                ccode,
            )
            zipfile.writestr(
                'index.json',
                json.dumps(
                    {
                        'instance': settings.SITE_URL,
                        'organizer': event.organizer.slug,
                        'event': event.slug,
                        'time': now().isoformat(),
                        'shredders': shredders,
                        'confirm_code': ccode
                    },
                    indent=4))
            for s in shredders:
                shredder = known_shredders.get(s)
                if not shredder:
                    continue

                it = shredder.generate_files()
                if not it:
                    continue
                for fname, ftype, content in it:
                    zipfile.writestr(fname, content)

        rawfile.seek(0)

        cf = CachedFile()
        cf.date = now()
        cf.filename = event.slug + '.zip'
        cf.type = 'application/zip'
        cf.session_key = session_key
        cf.web_download = True
        cf.expires = now() + timedelta(hours=1)
        cf.save()
        cf.file.save(cachedfile_name(cf, cf.filename), rawfile)

    return cf.pk
예제 #6
0
def export(event: str, shredders: List[str]) -> None:
    event = Event.objects.get(id=event)
    known_shredders = event.get_data_shredders()

    with NamedTemporaryFile() as rawfile:
        with ZipFile(rawfile, 'w') as zipfile:
            ccode = get_random_string(6)
            zipfile.writestr(
                'CONFIRM_CODE.txt',
                ccode,
            )
            zipfile.writestr(
                'index.json',
                json.dumps({
                    'instance': settings.SITE_URL,
                    'organizer': event.organizer.slug,
                    'event': event.slug,
                    'time': now().isoformat(),
                    'shredders': shredders,
                    'confirm_code': ccode
                }, indent=4)
            )
            for s in shredders:
                shredder = known_shredders.get(s)
                if not shredder:
                    continue

                it = shredder.generate_files()
                if not it:
                    continue
                for fname, ftype, content in it:
                    zipfile.writestr(fname, content)

        rawfile.seek(0)

        cf = CachedFile()
        cf.date = now()
        cf.filename = event.slug + '.zip'
        cf.type = 'application/pdf'
        cf.expires = now() + timedelta(hours=1)
        cf.save()
        cf.file.save(cachedfile_name(cf, cf.filename), rawfile)

    return cf.pk
예제 #7
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()
예제 #8
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()
예제 #9
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()