Beispiel #1
0
    def download(self, email='', **kwargs):
        """ Download
        """
        # Fallback PDF provided
        fallback = self.context.restrictedTraverse('action-download-pdf', None)
        if fallback and fallback.absolute_url().startswith(
                self.context.absolute_url()):
            self._link = self.context.absolute_url() + '/action-download-pdf'
        else:
            fallback = None

        # PDF already generated
        storage = IStorage(self.context).of('pdf')
        filepath = storage.filepath()
        fileurl = self.link()
        url = self.context.absolute_url()
        title = self.context.title_or_id()

        portal = getSite()
        from_name = portal.getProperty('email_from_name')
        from_email = portal.getProperty('email_from_address')

        if fallback or async .file_exists(filepath):
            wrapper = async .ContextWrapper(self.context)(
                fileurl=fileurl,
                filepath=filepath,
                email=email,
                url=url,
                from_name=from_name,
                from_email=from_email,
                title=title)

            event.notify(AsyncPDFExportSuccess(wrapper))
            return self.finish(email=email)
Beispiel #2
0
    def download(self, email='', **kwargs):
        """ Download
        """
        # PDF already generated
        storage = IStorage(self.context).of('pdf')
        filepath = storage.filepath()
        fileurl = storage.absolute_url()
        url = self.context.absolute_url()
        title = self.context.title_or_id()

        portal = getSite()
        from_name = portal.getProperty('email_from_name')
        from_email = portal.getProperty('email_from_address')

        if async .file_exists(filepath):
            wrapper = async .ContextWrapper(self.context)(
                fileurl=fileurl,
                filepath=filepath,
                email=email,
                url=url,
                from_name=from_name,
                from_email=from_email,
                title=title)

            event.notify(AsyncPDFExportSuccess(wrapper))
            return self.finish(email=email)
Beispiel #3
0
    def download(self, email='', **kwargs):
        """ Download
        """
        context = self.context
        # Fallback PDF provided
        fallback = context.restrictedTraverse('action-download-pdf', None)
        if fallback and fallback.absolute_url().startswith(
                context.absolute_url()):
            self._link = context.absolute_url() + '/action-download-pdf'
        else:
            fallback = None
        wrapped_field = getattr(context, 'getWrappedField', None)
        if wrapped_field:
            static = context.getWrappedField('pdfStatic')
            if static and getattr(static, 'getFilename',
                                  lambda x: '')(context):
                self._link = context.absolute_url() + '/download.pdf.static'
                fallback = True

        # PDF already generated
        storage = IStorage(context).of('pdf')
        filepath = storage.filepath()
        fileurl = self.link()
        url = context.absolute_url()
        title = context.title_or_id()

        portal = getSite()
        from_name = portal.getProperty('email_from_name')
        from_email = portal.getProperty('email_from_address')

        if fallback or async .file_exists(filepath):
            wrapper = IContextWrapper(context)(fileurl=fileurl,
                                               filepath=filepath,
                                               email=email,
                                               url=url,
                                               from_name=from_name,
                                               from_email=from_email,
                                               title=title)

            event.notify(AsyncPDFExportSuccess(wrapper))
            return self.finish(email=email)
Beispiel #4
0
def make_async_pdf(context, converter, **kwargs):
    """ Async job
    """
    filepath = kwargs.get('filepath', '')
    filepath_lock = filepath + '.lock'
    filepath_meta = filepath + '.meta'

    url = kwargs.get('url', '')
    email = kwargs.get('email', '')

    wrapper = ContextWrapper(context)(**kwargs)

    if not filepath:
        wrapper.error = 'Invalid filepath for output PDF'
        converter.cleanup()

        event.notify(AsyncPDFExportFail(wrapper))
        raise PDFConversionError(2, 'Invalid filepath for output PDF', url)

    # Maybe another async worker is generating our PDF. If so, we update the
    # list of emails where to send a message when ready and free this worker.
    # The already running worker will do the job for us.
    if os.path.exists(filepath_lock) and os.path.exists(filepath_meta):
        update_emails(filepath_meta, email)
        converter.cleanup()
        return

    # Maybe a previous async job already generated our PDF
    if file_exists(filepath):
        converter.cleanup()
        event.notify(AsyncPDFExportSuccess(wrapper))
        return

    # Mark the begining of the convertion
    with tempfile.NamedTemporaryFile(
            prefix='eea.pdf.', suffix='.lock',
            dir=TMPDIR(), delete=False) as ofile:
        lock = ofile.name

    converter.copy(lock, filepath_lock)
    converter.toclean.add(filepath_lock)
    converter.toclean.add(lock)

    # Share some metadata with other async workers
    with tempfile.NamedTemporaryFile(
            prefix='eea.pdf.', suffix='.meta',
            dir=TMPDIR(), delete=False) as ofile:
        meta = ofile.name

    converter.copy(meta, filepath_meta)
    converter.toclean.add(filepath_meta)
    converter.toclean.add(meta)

    update_emails(filepath_meta, email)

    try:
        converter.run(safe=False)
    except Exception, err:
        wrapper.error = err
        wrapper.email = get_emails(filepath_meta, email)
        converter.cleanup()

        event.notify(AsyncPDFExportFail(wrapper))
        errno = getattr(err, 'errno', 2)
        raise PDFConversionError(errno, err, url)
Beispiel #5
0
        errno = getattr(err, 'errno', 2)
        raise PDFConversionError(errno, err, url)

    if not converter.path:
        wrapper.error = "Invalid output PDF"
        wrapper.email = get_emails(filepath_meta, email)
        converter.cleanup()

        event.notify(AsyncPDFExportFail(wrapper))
        raise PDFConversionError(2, 'Invalid output PDF', url)

    wrapper.email = get_emails(filepath_meta, email)
    converter.copy(converter.path, filepath)
    converter.cleanup()

    event.notify(AsyncPDFExportSuccess(wrapper))


def update_emails(filepath, email):
    """ Update metadata file with given email
    """
    email = email.strip()
    if not email:
        return

    try:
        db = KV(filepath, 'emails')
        db[email] = True
    except Exception, err:
        logger.exception(err)