Beispiel #1
0
    def export(self, file_type="CSV", as_attachment=False, REQUEST=None):
        """ """
        perm_manage = self.checkPermissionManageTalkBackConsultation()
        if not (perm_manage or self.own_comments):
            raise Unauthorized

        html2text = self.getSite().html2text

        def plain(s, trim=None):
            return html2text(s, trim)

        fields = [
            ('Section', lambda i: i['comment'].get_section().title_or_id()),
            ('Paragraph', lambda i:
             (i['comment'].get_paragraph().plaintext_summary())),
            ('Message Id', lambda i: i['comment'].getId()),
            ('Replies', lambda i: str(len(i['children']))),
            ('In reply to', lambda i: i['comment'].reply_to or ''),
            ('Message', lambda i: plain(i['comment'].message)),
            ('Contributor', lambda i: i['comment'].get_contributor_name()),
            ('Date', lambda i:
             (i['comment'].comment_date.strftime('%Y/%m/%d %H:%M'))),
            ('Paragraph url', lambda i:
             (i['comment'].get_paragraph().absolute_url())),
        ]

        comments = self.all_comments()

        if file_type == 'CSV':
            ret = self.generate_csv_output(fields, comments)
            content_type = 'text/csv; charset=utf-8'
            filename = 'comments.csv'

        elif file_type == 'Excel':
            ret = self.generate_excel_output(fields, comments)
            content_type = 'application/vnd.ms-excel'
            filename = 'comments.xls'

        elif file_type == 'CustomExcel':
            fields = [
                ('Section', lambda c: c.get_section().title_or_id()),
                ('Message', lambda c: plain(c.message)),
                ('Contributor', lambda c: c.get_contributor_name()),
                ('Date', lambda c: c.comment_date.strftime('%Y/%m/%d %H:%M')),
            ]
            ret = self.generate_custom_excel_output(fields, comments)
            content_type = 'application/vnd.ms-excel'
            filename = 'comments.xls'

        else:
            raise ValueError('unknown file format %r' % file_type)

        if as_attachment and REQUEST is not None:
            filesize = len(ret)
            set_response_attachment(REQUEST.RESPONSE, filename, content_type,
                                    filesize)
        return ret
    def export(self, file_type="CSV", as_attachment=False, REQUEST=None):
        """ """
        perm_manage = self.checkPermissionManageTalkBackConsultation()
        if not (perm_manage or self.own_comments):
            raise Unauthorized

        html2text = self.getSite().html2text
        def plain(s, trim=None):
            return html2text(s, trim)

        fields = [
            ('Section', lambda i: i['comment'].get_section().title_or_id()),
            ('Paragraph', lambda i: (i['comment'].get_paragraph()
                                     .plaintext_summary())),
            ('Message Id', lambda i: i['comment'].getId()),
            ('Replies', lambda i: str(len(i['children']))),
            ('In reply to', lambda i: i['comment'].reply_to or ''),
            ('Message', lambda i: plain(i['comment'].message)),
            ('Contributor', lambda i: i['comment'].get_contributor_name()),
            ('Date', lambda i: (i['comment'].comment_date
                                .strftime('%Y/%m/%d %H:%M'))),
            ('Paragraph url', lambda i: (i['comment'].get_paragraph()
                                         .absolute_url())),
        ]

        comments = self.all_comments()

        if file_type == 'CSV':
            ret = self.generate_csv_output(fields, comments)
            content_type = 'text/csv; charset=utf-8'
            filename = 'comments.csv'

        elif file_type == 'Excel':
            ret = self.generate_excel_output(fields, comments)
            content_type = 'application/vnd.ms-excel'
            filename = 'comments.xls'

        elif file_type == 'CustomExcel':
            fields = [
                ('Section', lambda c: c.get_section().title_or_id()),
                ('Message', lambda c: plain(c.message)),
                ('Contributor', lambda c: c.get_contributor_name()),
                ('Date', lambda c: c.comment_date.strftime('%Y/%m/%d %H:%M')),
            ]
            ret = self.generate_custom_excel_output(fields, comments)
            content_type = 'application/vnd.ms-excel'
            filename = 'comments.xls'

        else: raise ValueError('unknown file format %r' % file_type)

        if as_attachment and REQUEST is not None:
            filesize = len(ret)
            set_response_attachment(REQUEST.RESPONSE, filename,
                content_type, filesize)
        return ret
def do_export(context, REQUEST):
    """ Export the document as a html file """
    sm = get_site_manager(context)
    target = sm.queryAdapter(context, IZipExportObject)
    if target.skip:
        context.setSessionErrorsTrans("You don't have permission "
            "to download this document")
        return REQUEST.RESPONSE.redirect(REQUEST.HTTP_REFERER)
    data = target.data
    if isinstance(data, unicode):
        data = data.encode('utf-8')
    output = StringIO()
    output.write(data)
    set_response_attachment(REQUEST.RESPONSE, target.filename,
            'text/html; charset=utf-8', output.len)
    return output.getvalue()
Beispiel #4
0
def admin_download_log_file(site, REQUEST=None, RESPONSE=None):
    """
    Download logging files for actions made on content types

    """
    from Products.NaayaCore.managers.import_export import set_response_attachment
    from StringIO import StringIO
    abs_path = get_log_dir(site)
    log_filepath = os.path.join(abs_path, LOG_FILENAME)
    log_file = open(log_filepath, 'r+')
    data = log_file.read()
    log_file.close()
    output = StringIO()
    output.write(data)
    set_response_attachment(REQUEST.RESPONSE, '%s.log' % get_site_slug(site),
                            'text/html; charset=utf-8', output.len)
    return output.getvalue()
Beispiel #5
0
def admin_download_log_file(site, REQUEST=None, RESPONSE=None):
    """
    Download logging files for actions made on content types

    """
    from Products.NaayaCore.managers.import_export import set_response_attachment
    from StringIO import StringIO
    abs_path = get_log_dir(site)
    log_filepath = os.path.join(abs_path, LOG_FILENAME)
    log_file = open(log_filepath, 'r+')
    data = log_file.read()
    log_file.close()
    output = StringIO()
    output.write(data)
    set_response_attachment(REQUEST.RESPONSE, '%s.log' % get_site_slug(site),
                            'text/html; charset=utf-8', output.len)
    return output.getvalue()