Example #1
0
    def __init__(
        self,
        text,
        sanitize=False,
        permitted_tags=[
            'a','b','blockquote','br/','i','li','ol','ul','p','cite',
            'code','pre','img/','h1', 'h2', 'h3', 'h4', 'h5', 'h6',
            'table', 'tr', 'td', 'div','strong', 'span'],
        allowed_attributes={
            'a': ['href', 'title', 'target'],
            'img': ['src', 'alt'],
            'blockquote': ['type'],
            'td': ['colspan']},
        ):
        """
        Args:
            text: the XML text
            sanitize: sanitize text using the permitted tags and allowed
                attributes (default False)
            permitted_tags: list of permitted tags (default: simple list of
                tags)
            allowed_attributes: dictionary of allowed attributed (default
                for A, IMG and BlockQuote).
                The key is the tag; the value is a list of allowed attributes.
        """

        if sanitize:
            from gluon.sanitizer import sanitize
            text = sanitize(text, permitted_tags, allowed_attributes)
        if isinstance(text, unicode):
            text = text.encode('utf8', 'xmlcharrefreplace')
        elif not isinstance(text, str):
            text = str(text)
        self.text = text
Example #2
0
def pyfpdf_from_html(html):
    request = current.request

    def image_map(path):
        if path.startswith('/%s/static/' % request.application):
            return os.path.join(request.folder, path.split('/', 2)[2])
        return 'http%s://%s%s' % (request.is_https and 's'
                                  or '', request.env.http_host, path)

    class MyFPDF(FPDF, HTMLMixin):
        pass

    pdf = MyFPDF()
    pdf.add_page()
    # pyfpdf needs some attributes to render the table correctly:
    html = sanitize(html,
                    allowed_attributes={
                        'a': ['href', 'title'],
                        'img': ['src', 'alt'],
                        'blockquote': ['type'],
                        'td':
                        ['align', 'bgcolor', 'colspan', 'height', 'width'],
                        'tr': ['bgcolor', 'height', 'width'],
                        'table': ['border', 'bgcolor', 'height', 'width'],
                    },
                    escape=False)
    pdf.write_html(html, image_map=image_map)
    return XML(pdf.output(dest='S'))
Example #3
0
def pyfpdf_from_html(html):
    request = current.request
    def image_map(path):
        if path.startswith('/%s/static/' % request.application):
            return os.path.join(request.folder,path.split('/',2)[2])
        return 'http%s://%s%s' % (request.is_https and 's' or '',request.env.http_host, path)
    class MyFPDF(FPDF, HTMLMixin): pass
    pdf=MyFPDF()
    pdf.add_page()
    html = sanitize(html, escape=False)  #### should have better list of allowed tags
    pdf.write_html(html,image_map=image_map)
    return XML(pdf.output(dest='S'))
Example #4
0
def pyfpdf_from_html(html):
    request = current.request
    def image_map(path):
        if path.startswith('/%s/static/' % request.application):
            return os.path.join(request.folder,path.split('/',2)[2])
        return 'http%s://%s%s' % (request.is_https and 's' or '',request.env.http_host, path)
    class MyFPDF(FPDF, HTMLMixin): pass
    pdf=MyFPDF()
    pdf.add_page()
    html = sanitize(html, escape=False)  #### should have better list of allowed tags
    pdf.write_html(html,image_map=image_map)
    return XML(pdf.output(dest='S'))
Example #5
0
def contactus():
    """
    Contact view
    """
    form = SQLFORM(db.contact)
    if form.process().accepted:
        message = 'User <b>%s</b> contacted us. ' \
                  '<br><br><b>Original message:</b><br><br>%s<br><br>' \
                  '<em>Gene4Breed mailer robot</em>' % (form.vars.contact_email, sanitize(form.vars.question))
        mail = Mail()
        mail.settings.server = conf.mail_host_noauth
        mail.settings.sender = conf.mail_from
        mail.send(to=conf.mail_to, subject=conf.mail_subject, message=('Alternative plain text', message))
        response.flash = 'Thank you for contacting us, your question has been stored'
        response.flash_level = 'flash-success'
    elif form.errors:
        response.flash = 'Can not submit your question'
        response.flash_level = 'flash-error'
    return dict(form=form)
Example #6
0
def contactus():
    """
    Contact view
    """
    form = SQLFORM(db.contact)
    if form.process().accepted:
        message = 'User <b>%s</b> contacted us. ' \
                  '<br><br><b>Original message:</b><br><br>%s<br><br>' \
                  '<em>Gene4Breed mailer robot</em>' % (form.vars.contact_email, sanitize(form.vars.question))
        mail = Mail()
        mail.settings.server = conf.mail_host_noauth
        mail.settings.sender = conf.mail_from
        mail.send(to=conf.mail_to,
                  subject=conf.mail_subject,
                  message=('Alternative plain text', message))
        response.flash = 'Thank you for contacting us, your question has been stored'
        response.flash_level = 'flash-success'
    elif form.errors:
        response.flash = 'Can not submit your question'
        response.flash_level = 'flash-error'
    return dict(form=form)
Example #7
0
def pyfpdf_from_html(html):
    request = current.request

    def image_map(path):
        if path.startswith('/%s/static/' % request.application):
            return os.path.join(request.folder, path.split('/', 2)[2])
        return 'http%s://%s%s' % (request.is_https and 's' or '', request.env.http_host, path)

    class MyFPDF(FPDF, HTMLMixin):
        pass
    pdf = MyFPDF()
    pdf.add_page()
    # pyfpdf needs some attributes to render the table correctly:
    html = sanitize(
        html, allowed_attributes={
            'a': ['href', 'title'],
            'img': ['src', 'alt'],
            'blockquote': ['type'],
            'td': ['align', 'bgcolor', 'colspan', 'height', 'width'],
            'tr': ['bgcolor', 'height', 'width'],
            'table': ['border', 'bgcolor', 'height', 'width'],
        }, escape=False)
    pdf.write_html(html, image_map=image_map)
    return XML(pdf.output(dest='S'))
Example #8
0
    def __init__(
        self,
        text,
        sanitize=False,
        permitted_tags=[
            'a', 'b', 'blockquote', 'br/', 'i', 'li', 'ol', 'ul', 'p', 'cite',
            'code', 'pre', 'img/', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'table',
            'tr', 'td', 'div', 'strong', 'span'
        ],
        allowed_attributes={
            'a': ['href', 'title', 'target'],
            'img': ['src', 'alt'],
            'blockquote': ['type'],
            'td': ['colspan']
        },
    ):
        """
        Args:
            text: the XML text
            sanitize: sanitize text using the permitted tags and allowed
                attributes (default False)
            permitted_tags: list of permitted tags (default: simple list of
                tags)
            allowed_attributes: dictionary of allowed attributed (default
                for A, IMG and BlockQuote).
                The key is the tag; the value is a list of allowed attributes.
        """

        if sanitize:
            from gluon.sanitizer import sanitize
            text = sanitize(text, permitted_tags, allowed_attributes)
        if isinstance(text, unicode):
            text = text.encode('utf8', 'xmlcharrefreplace')
        elif not isinstance(text, str):
            text = str(text)
        self.text = text