示例#1
0
 def render(self):
     with NamedTemporaryFile(suffix=".xml") as buffer:
         buffer.write('<?xml version="1.0" encoding="UTF-8"?>\n')
         buffer.write('<sitemapindex ')
         buffer.write(
             'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
         )
         for page in xrange(1, self.page_count + 1):
             loc = '<sitemap><loc>%s</loc></sitemap>\n'
             method = '%s.sitemap' % self.model._name
             buffer.write(loc % url_for(method, page=page, _external=True))
         buffer.write('</sitemapindex>')
         return send_file(buffer.name, cache_timeout=self.cache_timeout)
示例#2
0
 def render(self):
     """This method writes the sitemap directly into the response as a stream
     using the ResponseStream
     """
     with NamedTemporaryFile(suffix=".xml") as buffer:
         buffer.write('<?xml version="1.0" encoding="UTF-8"?>\n')
         buffer.write(
             '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
             )
         for line in self:
             buffer.write(etree.tostring(line) + u'\n')
         buffer.write('</urlset>')
         return send_file(buffer.name, cache_timeout=self.cache_timeout)
示例#3
0
    def send_static_file(cls, folder, name):
        """
        Invokes the send_file method in nereid.helpers to send a file as the
        response to the request. The file is sent in a way which is as
        efficient as possible. For example nereid will use the X-Send_file
        header to make nginx send the file if possible.

        :param folder: name of the folder
        :param name: name of the file
        """
        # TODO: Separate this search and find into separate cached method

        files = cls.search([("folder.name", "=", folder), ("name", "=", name)])
        if not files:
            abort(404)
        return send_file(files[0].file_path)
示例#4
0
    def send_static_file(cls, folder, name):
        """
        Invokes the send_file method in nereid.helpers to send a file as the
        response to the request. The file is sent in a way which is as
        efficient as possible. For example nereid will use the X-Send_file
        header to make nginx send the file if possible.

        :param folder: folder_name of the folder
        :param name: name of the file
        """
        #TODO: Separate this search and find into separate cached method

        files = cls.search([('folder.folder_name', '=', folder),
                            ('name', '=', name)])
        if not files:
            abort(404)
        return send_file(files[0].file_path)
示例#5
0
    def download_invoice(self):
        """
        Allow user to download invoice.
        """
        Report = Pool().get('account.invoice', type='report')

        if self.party != current_user.party:
            abort(403)

        vals = Report.execute([self.id], {})
        with tempfile.NamedTemporaryFile(delete=False) as file:
            file.write(vals[1])
        return send_file(
            file.name,
            as_attachment=True,
            attachment_filename=vals[3],
        )
示例#6
0
    def send_static_file(self, folder, name):
        """
        Invokes the send_file method in nereid.helpers to send a file as the
        response to the reuqest. The file is sent in a way which is as 
        efficient as possible. For example nereid will use the X-Send_file
        header to make nginx send the file if possible.

        :param folder: folder_name of the folder
        :param name: name of the file
        """
        #TODO: Separate this search and find into separate cached method

        ids = self.search([
            ('folder.folder_name', '=', folder),
            ('name', '=', name)
            ])
        if not ids:
            abort(404)
        file_ = self.browse(ids[0])
        return send_file(file_.file_path)
    def transform_static_file(self, commands, extension):
        """
        Transform the static file and send the transformed file

        :param commands: A list of commands separated by /
        :param extension: The image format to use
        """
        tmp_folder = os.path.join(
            '/tmp/nereid/', Transaction().cursor.dbname, str(self.id)
        )
        if not os.path.exists(tmp_folder):
            os.makedirs(tmp_folder)

        filename = os.path.join(
            tmp_folder,
            '%s.%s' % (secure_filename(commands), extension)
        )

        if not os.path.exists(filename):
            self._transform_static_file(commands, extension, filename)

        rv = send_file(filename)
        rv.headers['Cache-Control'] = 'public, max-age=%d' % 86400
        return rv
示例#8
0
class NereidStaticFile:
    __name__ = "nereid.static.file"

    allowed_operations = ['resize', 'thumbnail']

    @staticmethod
    def thumbnail(image, w=128, h=128, m='n'):
        """
        :param image: Image instance
        :param w: width
        :param h: height
        :param m: mode for the resize operation
                      * n - NEAREST
                      * l - BILINEAR
                      * c - BICUBIC
                      * a - ANTIALIAS (best quality)
        """
        image.thumbnail((int(w), int(h)), FILTER_MAP[m])
        return image

    @staticmethod
    def resize(image, w=128, h=128, m='n'):
        """
        :param image: Image instance
        :param w: width
        :param h: height
        :param m: mode for the resize operation
                      * n - NEAREST
                      * l - BILINEAR
                      * c - BICUBIC
                      * a - ANTIALIAS (best quality)
        """
        return image.resize((int(w), int(h)), FILTER_MAP[m])

    def _transform_static_file(self, commands, extension, filename):
        """
        Transform the static file and send the transformed file

        :param commands: A list of commands separated by /
        :param extension: The image format to use
        :param filename: The file to which the transformed image
                         needs to be written
        """
        # Ugly hack '[:]' to fix issue in python 2.7.3
        image_file = Image.open(BytesIO(self.file_binary[:]))

        parse_command = TransformationCommand.parse_command

        for command in commands.split('/'):
            operation, params = parse_command(command)
            assert operation in self.allowed_operations
            image_file = getattr(self, operation)(image_file, **params)

        image_file.save(filename)

    def transform_static_file(self, commands, extension):
        """
        Transform the static file and send the transformed file

        :param commands: A list of commands separated by /
        :param extension: The image format to use
        """
        tmp_folder = os.path.join('/tmp/nereid/',
                                  Transaction().cursor.dbname, str(self.id))

        try:
            os.makedirs(tmp_folder)
        except OSError, err:
            if err.errno == 17:
                # directory exists
                pass
            else:
                raise

        filename = os.path.join(
            tmp_folder, '%s.%s' % (secure_filename(commands), extension))
        file_date = os.path.exists(filename) and datetime.fromtimestamp(
            os.path.getmtime(filename), pytz.timezone('UTC'))

        if not file_date or (self.write_date and
                             file_date < pytz.UTC.localize(self.write_date)):
            self._transform_static_file(commands, extension, filename)

        rv = send_file(filename)
        rv.headers['Cache-Control'] = 'public, max-age=%d' % 86400
        return rv