Exemplo n.º 1
0
    def _binary_set_headers(self,
                            status,
                            content,
                            filename,
                            mimetype,
                            unique,
                            filehash=None,
                            download=False):
        headers = [('Content-Type', mimetype),
                   ('X-Content-Type-Options', 'nosniff')]
        # cache
        etag = bool(request) and request.httprequest.headers.get(
            'If-None-Match')
        status = status or 200
        if filehash:
            headers.append(('ETag', filehash))
            if etag == filehash and status == 200:
                status = 304
        headers.append(
            ('Cache-Control',
             'max-age=%s' % (http.STATIC_CACHE_LONG if unique else 0)))
        # content-disposition default name
        if download:
            headers.append(
                ('Content-Disposition', content_disposition(filename)))

        return (status, headers, content)
Exemplo n.º 2
0
    def survey_get_certification(self, survey_id, **kwargs):
        """ The certification document can be downloaded as long as the user has succeeded the certification """
        survey = request.env['survey.survey'].sudo().search([
            ('id', '=', survey_id), ('certificate', '=', True)
        ])

        if not survey:
            # no certification found
            return werkzeug.utils.redirect("/")

        succeeded_attempt = request.env['survey.user_input'].sudo().search(
            [('partner_id', '=', request.env.user.partner_id.id),
             ('survey_id', '=', survey_id), ('quizz_passed', '=', True)],
            limit=1)

        if not succeeded_attempt:
            raise UserError(_("The user has not succeeded the certification"))

        report_sudo = request.env.ref('survey.certification_report').sudo()

        report = report_sudo.render_qweb_pdf([succeeded_attempt.id],
                                             data={'report_type': 'pdf'})[0]
        reporthttpheaders = [
            ('Content-Type', 'application/pdf'),
            ('Content-Length', len(report)),
        ]
        reporthttpheaders.append(
            ('Content-Disposition', content_disposition('Certification.pdf')))
        return request.make_response(report, headers=reporthttpheaders)
Exemplo n.º 3
0
    def _show_report(self, model, report_type, report_ref, download=False):
        if report_type not in ('html', 'pdf', 'text'):
            raise UserError(_("Invalid report type: %s") % report_type)

        report_sudo = request.env.ref(report_ref).sudo()

        if not isinstance(report_sudo, type(request.env['ir.actions.report'])):
            raise UserError(
                _("%s is not the reference of a report") % report_ref)

        method_name = 'render_qweb_%s' % (report_type)
        report = getattr(report_sudo, method_name)([model.id],
                                                   data={
                                                       'report_type':
                                                       report_type
                                                   })[0]
        reporthttpheaders = [
            ('Content-Type',
             'application/pdf' if report_type == 'pdf' else 'text/html'),
            ('Content-Length', len(report)),
        ]
        if report_type == 'pdf' and download:
            filename = "%s.pdf" % (re.sub('\W+', '-',
                                          model._get_report_base_filename()))
            reporthttpheaders.append(
                ('Content-Disposition', content_disposition(filename)))
        return request.make_response(report, headers=reporthttpheaders)
Exemplo n.º 4
0
 def base(self, data, token):
     params = json.loads(data)
     header, dashboard_data = operator.itemgetter('header', 'dashboard_data')(params)
     return request.make_response(self.from_data(dashboard_data),
                                  headers=[('Content-Disposition',
                                            content_disposition(self.filename(header))),
                                           ('Content-Type', self.content_type)],
                                  cookies={'fileToken': token})
Exemplo n.º 5
0
 def make_event_ics_file(self, event, **kwargs):
     if not event or not event.registration_ids:
         return request.not_found()
     files = event._get_ics_file()
     content = files[event.id]
     return request.make_response(content, [
         ('Content-Type', 'application/octet-stream'),
         ('Content-Length', len(content)),
         ('Content-Disposition', content_disposition('%s.ics' % event.name))
     ])
    def base(self, data, token):
        params = json.loads(data)
        header, chart_data = operator.itemgetter('header',
                                                 'chart_data')(params)
        chart_data = json.loads(chart_data)
        chart_data['labels'].insert(0, 'Measure')
        columns_headers = chart_data['labels']
        import_data = []

        for dataset in chart_data['datasets']:
            dataset['data'].insert(0, dataset['label'])
            import_data.append(dataset['data'])

        return request.make_response(
            self.from_data(columns_headers, import_data),
            headers=[('Content-Disposition',
                      content_disposition(self.filename(header))),
                     ('Content-Type', self.content_type)],
            cookies={'fileToken': token})
Exemplo n.º 7
0
 def report_routes(self, reportname, docids=None, converter=None, **data):
     if converter == 'xlsx':
         report = request.env['ir.actions.report']._get_report_from_name(
             reportname)
         context = dict(request.env.context)
         if docids:
             docids = [int(i) for i in docids.split(',')]
         if data.get('options'):
             data.update(json.loads(data.pop('options')))
         if data.get('context'):
             # Ignore 'lang' here, because the context in data is the one
             # from the webclient *but* if the user explicitely wants to
             # change the lang, this mechanism overwrites it.
             data['context'] = json.loads(data['context'])
             if data['context'].get('lang'):
                 del data['context']['lang']
             context.update(data['context'])
         xlsx = report.with_context(context).render_xlsx(
             docids, data=data
         )[0]
         report_name = report.report_file
         if report.print_report_name and not len(docids) > 1:
             obj = request.env[report.model].browse(docids[0])
             report_name = safe_eval(report.print_report_name,
                                     {'object': obj, 'time': time})
         xlsxhttpheaders = [
             ('Content-Type', 'application/vnd.openxmlformats-'
                              'officedocument.spreadsheetml.sheet'),
             ('Content-Length', len(xlsx)),
             (
                 'Content-Disposition',
                 content_disposition(report_name + '.xlsx')
             )
         ]
         return request.make_response(xlsx, headers=xlsxhttpheaders)
     return super(ReportController, self).report_routes(
         reportname, docids, converter, **data
     )
Exemplo n.º 8
0
 def content_disposition(cls, filename):
     return content_disposition(filename)