Python django.conf.settings.FILE_CHARSET Examples

The following are 24 code examples for showing how to use django.conf.settings.FILE_CHARSET(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.conf.settings , or try the search function .

Example 1
Project: python   Author: Yeah-Kun   File: dummy.py    License: Apache License 2.0 6 votes vote down vote up
def get_template(self, template_name):
        tried = []
        for template_file in self.iter_template_filenames(template_name):
            try:
                with io.open(template_file, encoding=settings.FILE_CHARSET) as fp:
                    template_code = fp.read()
            except IOError as e:
                if e.errno == errno.ENOENT:
                    tried.append((
                        Origin(template_file, template_name, self),
                        'Source does not exist',
                    ))
                    continue
                raise

            return Template(template_code)

        else:
            raise TemplateDoesNotExist(template_name, tried=tried, backend=self) 
Example 2
Project: luscan-devel   Author: blackye   File: eggs.py    License: GNU General Public License v2.0 6 votes vote down vote up
def load_template_source(self, template_name, template_dirs=None):
        """
        Loads templates from Python eggs via pkg_resource.resource_string.

        For every installed app, it tries to get the resource (app, template_name).
        """
        if resource_string is not None:
            pkg_name = 'templates/' + template_name
            for app in settings.INSTALLED_APPS:
                try:
                    resource = resource_string(app, pkg_name)
                except Exception:
                    continue
                if not six.PY3:
                    resource = resource.decode(settings.FILE_CHARSET)
                return (resource, 'egg:%s:%s' % (app, pkg_name))
        raise TemplateDoesNotExist(template_name) 
Example 3
Project: luscan-devel   Author: blackye   File: sql.py    License: GNU General Public License v2.0 6 votes vote down vote up
def custom_sql_for_model(model, style, connection):
    opts = model._meta
    app_dir = os.path.normpath(os.path.join(os.path.dirname(upath(models.get_app(model._meta.app_label).__file__)), 'sql'))
    output = []

    # Post-creation SQL should come before any initial SQL data is loaded.
    # However, this should not be done for models that are unmanaged or
    # for fields that are part of a parent model (via model inheritance).
    if opts.managed:
        post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')]
        for f in post_sql_fields:
            output.extend(f.post_create_sql(style, model._meta.db_table))

    # Find custom SQL, if it's available.
    backend_name = connection.settings_dict['ENGINE'].split('.')[-1]
    sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), backend_name)),
                 os.path.join(app_dir, "%s.sql" % opts.object_name.lower())]
    for sql_file in sql_files:
        if os.path.exists(sql_file):
            with codecs.open(sql_file, 'U', encoding=settings.FILE_CHARSET) as fp:
                # Some backends can't execute more than one SQL statement at a time,
                # so split into separate statements.
                output.extend(_split_statements(fp.read()))
    return output 
Example 4
Project: openhgsenti   Author: drexly   File: dummy.py    License: Apache License 2.0 6 votes vote down vote up
def get_template(self, template_name):
        tried = []
        for template_file in self.iter_template_filenames(template_name):
            try:
                with io.open(template_file, encoding=settings.FILE_CHARSET) as fp:
                    template_code = fp.read()
            except IOError as e:
                if e.errno == errno.ENOENT:
                    tried.append((
                        Origin(template_file, template_name, self),
                        'Source does not exist',
                    ))
                    continue
                raise

            return Template(template_code)

        else:
            raise TemplateDoesNotExist(template_name, tried=tried, backend=self) 
Example 5
Project: django_coverage_plugin   Author: nedbat   File: plugin.py    License: Apache License 2.0 6 votes vote down vote up
def read_template_source(filename):
    """Read the source of a Django template, returning the Unicode text."""
    # Import this late to be sure we don't trigger settings machinery too
    # early.
    from django.conf import settings

    if not settings.configured:
        settings.configure()

    with open(filename, "rb") as f:
        # The FILE_CHARSET setting will be removed in 3.1:
        # https://docs.djangoproject.com/en/3.0/ref/settings/#file-charset
        if django.VERSION >= (3, 1):
            charset = 'utf-8'
        else:
            charset = settings.FILE_CHARSET
        text = f.read().decode(charset)

    return text 
Example 6
Project: python2017   Author: bpgc-cte   File: dummy.py    License: MIT License 6 votes vote down vote up
def get_template(self, template_name):
        tried = []
        for template_file in self.iter_template_filenames(template_name):
            try:
                with io.open(template_file, encoding=settings.FILE_CHARSET) as fp:
                    template_code = fp.read()
            except IOError as e:
                if e.errno == errno.ENOENT:
                    tried.append((
                        Origin(template_file, template_name, self),
                        'Source does not exist',
                    ))
                    continue
                raise

            return Template(template_code)

        else:
            raise TemplateDoesNotExist(template_name, tried=tried, backend=self) 
Example 7
Project: GTDWeb   Author: lanbing510   File: django.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        options.setdefault('debug', settings.DEBUG)
        options.setdefault('file_charset', settings.FILE_CHARSET)
        super(DjangoTemplates, self).__init__(params)
        self.engine = Engine(self.dirs, self.app_dirs, **options) 
Example 8
Project: GTDWeb   Author: lanbing510   File: dummy.py    License: GNU General Public License v2.0 5 votes vote down vote up
def get_template(self, template_name):
        for template_file in self.iter_template_filenames(template_name):
            try:
                with io.open(template_file, encoding=settings.FILE_CHARSET) as fp:
                    template_code = fp.read()
            except IOError:
                continue

            return Template(template_code)

        else:
            raise TemplateDoesNotExist(template_name) 
Example 9
Project: GTDWeb   Author: lanbing510   File: sql.py    License: GNU General Public License v2.0 5 votes vote down vote up
def custom_sql_for_model(model, style, connection):
    opts = model._meta
    app_dirs = []
    app_dir = apps.get_app_config(model._meta.app_label).path
    app_dirs.append(os.path.normpath(os.path.join(app_dir, 'sql')))

    # Deprecated location -- remove in Django 1.9
    old_app_dir = os.path.normpath(os.path.join(app_dir, 'models/sql'))
    if os.path.exists(old_app_dir):
        warnings.warn("Custom SQL location '<app_label>/models/sql' is "
                      "deprecated, use '<app_label>/sql' instead.",
                      RemovedInDjango19Warning)
        app_dirs.append(old_app_dir)

    output = []

    # Post-creation SQL should come before any initial SQL data is loaded.
    # However, this should not be done for models that are unmanaged or
    # for fields that are part of a parent model (via model inheritance).
    if opts.managed:
        post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')]
        for f in post_sql_fields:
            output.extend(f.post_create_sql(style, model._meta.db_table))

    # Find custom SQL, if it's available.
    backend_name = connection.settings_dict['ENGINE'].split('.')[-1]
    sql_files = []
    for app_dir in app_dirs:
        sql_files.append(os.path.join(app_dir, "%s.%s.sql" % (opts.model_name, backend_name)))
        sql_files.append(os.path.join(app_dir, "%s.sql" % opts.model_name))
    for sql_file in sql_files:
        if os.path.exists(sql_file):
            with io.open(sql_file, encoding=settings.FILE_CHARSET) as fp:
                output.extend(connection.ops.prepare_sql_script(fp.read(), _allow_fallback=True))
    return output 
Example 10
Project: bioforum   Author: reBiocoder   File: django.py    License: MIT License 5 votes vote down vote up
def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        options.setdefault('autoescape', True)
        options.setdefault('debug', settings.DEBUG)
        options.setdefault('file_charset', settings.FILE_CHARSET)
        libraries = options.get('libraries', {})
        options['libraries'] = self.get_templatetag_libraries(libraries)
        super().__init__(params)
        self.engine = Engine(self.dirs, self.app_dirs, **options) 
Example 11
Project: bioforum   Author: reBiocoder   File: dummy.py    License: MIT License 5 votes vote down vote up
def get_template(self, template_name):
        tried = []
        for template_file in self.iter_template_filenames(template_name):
            try:
                with open(template_file, encoding=settings.FILE_CHARSET) as fp:
                    template_code = fp.read()
            except FileNotFoundError:
                tried.append((
                    Origin(template_file, template_name, self),
                    'Source does not exist',
                ))
            else:
                return Template(template_code)
        raise TemplateDoesNotExist(template_name, tried=tried, backend=self) 
Example 12
Project: django-blog-it   Author: MicroPyramid   File: loaders.py    License: MIT License 5 votes vote down vote up
def load_template_source(self, template_name, template_dirs=None):

        themes = Theme.objects.filter(enabled=True)
        for theme in themes:
            filepath = os.path.join(os.path.dirname(__file__), 'themes', theme.name, 'templates', template_name)
            try:
                file = open(filepath)
                try:
                    return (file.read().decode(settings.FILE_CHARSET), filepath)
                finally:
                    file.close()
            except IOError:
                pass

        raise TemplateDoesNotExist("Could not find template '%s'." % template_name) 
Example 13
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: django.py    License: MIT License 5 votes vote down vote up
def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        options.setdefault('autoescape', True)
        options.setdefault('debug', settings.DEBUG)
        options.setdefault('file_charset', settings.FILE_CHARSET)
        libraries = options.get('libraries', {})
        options['libraries'] = self.get_templatetag_libraries(libraries)
        super().__init__(params)
        self.engine = Engine(self.dirs, self.app_dirs, **options) 
Example 14
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: dummy.py    License: MIT License 5 votes vote down vote up
def get_template(self, template_name):
        tried = []
        for template_file in self.iter_template_filenames(template_name):
            try:
                with open(template_file, encoding=settings.FILE_CHARSET) as fp:
                    template_code = fp.read()
            except FileNotFoundError:
                tried.append((
                    Origin(template_file, template_name, self),
                    'Source does not exist',
                ))
            else:
                return Template(template_code)
        raise TemplateDoesNotExist(template_name, tried=tried, backend=self) 
Example 15
Project: python   Author: Yeah-Kun   File: django.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        options.setdefault('autoescape', True)
        options.setdefault('debug', settings.DEBUG)
        options.setdefault('file_charset', settings.FILE_CHARSET)
        libraries = options.get('libraries', {})
        options['libraries'] = self.get_templatetag_libraries(libraries)
        super(DjangoTemplates, self).__init__(params)
        self.engine = Engine(self.dirs, self.app_dirs, **options) 
Example 16
Project: luscan-devel   Author: blackye   File: app_directories.py    License: GNU General Public License v2.0 5 votes vote down vote up
def load_template_source(self, template_name, template_dirs=None):
        for filepath in self.get_template_sources(template_name, template_dirs):
            try:
                with open(filepath, 'rb') as fp:
                    return (fp.read().decode(settings.FILE_CHARSET), filepath)
            except IOError:
                pass
        raise TemplateDoesNotExist(template_name) 
Example 17
Project: luscan-devel   Author: blackye   File: filesystem.py    License: GNU General Public License v2.0 5 votes vote down vote up
def load_template_source(self, template_name, template_dirs=None):
        tried = []
        for filepath in self.get_template_sources(template_name, template_dirs):
            try:
                with open(filepath, 'rb') as fp:
                    return (fp.read().decode(settings.FILE_CHARSET), filepath)
            except IOError:
                tried.append(filepath)
        if tried:
            error_msg = "Tried %s" % tried
        else:
            error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
        raise TemplateDoesNotExist(error_msg) 
Example 18
Project: openhgsenti   Author: drexly   File: django.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        options.setdefault('debug', settings.DEBUG)
        options.setdefault('file_charset', settings.FILE_CHARSET)
        libraries = options.get('libraries', {})
        options['libraries'] = self.get_templatetag_libraries(libraries)
        super(DjangoTemplates, self).__init__(params)
        self.engine = Engine(self.dirs, self.app_dirs, **options) 
Example 19
Project: python2017   Author: bpgc-cte   File: django.py    License: MIT License 5 votes vote down vote up
def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        options.setdefault('autoescape', True)
        options.setdefault('debug', settings.DEBUG)
        options.setdefault('file_charset', settings.FILE_CHARSET)
        libraries = options.get('libraries', {})
        options['libraries'] = self.get_templatetag_libraries(libraries)
        super(DjangoTemplates, self).__init__(params)
        self.engine = Engine(self.dirs, self.app_dirs, **options) 
Example 20
Project: djongo   Author: nesdis   File: test_file_charset.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_override_settings_warning(self):
        with self.assertRaisesMessage(RemovedInDjango31Warning, self.msg):
            with self.settings(FILE_CHARSET='latin1'):
                pass 
Example 21
Project: djongo   Author: nesdis   File: test_file_charset.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_settings_init_warning(self):
        settings_module = ModuleType('fake_settings_module')
        settings_module.FILE_CHARSET = 'latin1'
        settings_module.SECRET_KEY = 'ABC'
        sys.modules['fake_settings_module'] = settings_module
        try:
            with self.assertRaisesMessage(RemovedInDjango31Warning, self.msg):
                Settings('fake_settings_module')
        finally:
            del sys.modules['fake_settings_module'] 
Example 22
Project: djongo   Author: nesdis   File: test_file_charset.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_access_warning(self):
        with self.assertRaisesMessage(RemovedInDjango31Warning, self.msg):
            settings.FILE_CHARSET
        # Works a second time.
        with self.assertRaisesMessage(RemovedInDjango31Warning, self.msg):
            settings.FILE_CHARSET 
Example 23
Project: djongo   Author: nesdis   File: test_file_charset.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_access(self):
        with self.settings(FILE_CHARSET='latin1'):
            self.assertEqual(settings.FILE_CHARSET, 'latin1')
            # Works a second time.
            self.assertEqual(settings.FILE_CHARSET, 'latin1') 
Example 24
Project: avos   Author: CiscoSystems   File: loaders.py    License: Apache License 2.0 5 votes vote down vote up
def load_template_source(self, template_name, template_dirs=None):
        for path in self.get_template_sources(template_name):
            try:
                with open(path) as file:
                    return (file.read().decode(settings.FILE_CHARSET), path)
            except IOError:
                pass
        raise TemplateDoesNotExist(template_name)