Esempio n. 1
0
    def create(self):
        """
        Create a new translation supplied as a string.

        Returns:
            A dict with information for the request.

        Raises:
            BadRequestError: There was a problem with the request.
            NoContentError: There was no content string in the request.
        """
        if 'content' not in self.data:
            raise NoContentError("No content found.")
        parser = parser_for(
            mimetype=get_mimetype_from_method(self.resource.i18n_type)
        )
        if parser is None:
            raise BadRequestError("Mimetype not supported")

        file_ = tempfile.NamedTemporaryFile(
            mode='wb',
            suffix=get_file_extension_for_method(self.resource.i18n_type),
            delete=False,
        )
        try:
            file_.write(self.data['content'].encode('UTF-8'))
            file_.close()
            try:
                parser.contents_check(file_.name)
            except (FileCheckError, ParseError), e:
                raise BadRequestError(e.message)
            except Exception, e:
                logger.error(e.message, exc_info=True)
                raise BadRequestError("A strange error has happened.")
Esempio n. 2
0
 def mimetype(cls, r):
     """
     Return the mimetype in a GET request instead of the i18n_type.
     """
     return get_mimetype_from_method(r.i18n_type)