lib_path = None if lib_path: lib_names = None elif os.name == 'nt': # Windows NT shared libraries lib_names = ['gdal301', 'gdal300', 'gdal204', 'gdal203', 'gdal202', 'gdal201', 'gdal20'] elif os.name == 'posix': # *NIX library names. lib_names = [ 'gdal', 'GDAL', 'gdal3.1.0', 'gdal3.0.0', 'gdal2.4.0', 'gdal2.3.0', 'gdal2.2.0', 'gdal2.1.0', 'gdal2.0.0','gdal202' ] else: raise ImproperlyConfigured('GDAL is unsupported on OS "%s".' % os.name) # Using the ctypes `find_library` utility to find the # path to the GDAL library from the list of library names. if lib_names: for lib_name in lib_names: lib_path = find_library(lib_name) if lib_path is not None: break if lib_path is None: raise ImproperlyConfigured( 'Could not find the GDAL library (tried "%s"). Is GDAL installed? ' 'If it is, try setting GDAL_LIBRARY_PATH in your settings.' % '", "'.join(lib_names) )
if not included_file or included_file == settings_file: # The import doesn't always give permission denied, so try to open the # settings file directly. try: e = None open(settings_file) except IOError: pass if e and e.errno == errno.EACCES: SECRET_KEY = 'permission-denied' LOGGING = {} else: msg = 'No AWX configuration found at %s.' % settings_file msg += '\nDefine the AWX_SETTINGS_FILE environment variable to ' msg += 'specify an alternate path.' raise ImproperlyConfigured(msg) else: raise # The below runs AFTER all of the custom settings are imported. CELERYBEAT_SCHEDULE.update({ # noqa 'isolated_heartbeat': { 'task': 'awx.main.tasks.awx_isolated_heartbeat', 'schedule': timedelta(seconds=AWX_ISOLATED_PERIODIC_CHECK), # noqa 'options': { 'expires': AWX_ISOLATED_PERIODIC_CHECK * 2 }, # noqa } })
else: CACHE_DIR = None local_settings_file_path = os.path.join( BASE_DIR, 'config.json' ) # load config.json try: with open(local_settings_file_path, 'r') as f: local_settings = json.load(f) except IOError: local_settings = {} except ValueError as e: error_msg = "Invalid config '{}': {}".format(local_settings_file_path, e) raise ImproperlyConfigured(error_msg) def get_setting(name, default=None, settings=local_settings): """Get the local settings variable or return explicit exception""" if default is None: raise ImproperlyConfigured( "Missing default value for '{0}'".format(name) ) # Try looking up setting in `config.json` first try: return settings[name] except KeyError: pass
def __init__(self): super().__init__() if not self.slug: raise ImproperlyConfigured('Every %s must have a slug.' % self.__class__)
from django.core.exceptions import ImproperlyConfigured #third-party apps imports from rfdocs.mixins.tools import which #app imports logger = logging.getLogger(name=__name__) phantomjs_bin = 'phantomjs' try: phantomjs_bin = settings.PHANTOMJS_BIN except AttributeError: returncode = which(phantomjs_bin, silent=True) if returncode: raise ImproperlyConfigured("Set the PHANTOMJS_BIN variable in your settings") class PhantomJSHelper(object): def __init__(self, url=None, error_callback=None, script_js=None): self.jsfetcher = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'fetcher.js' or script_js) self.url = url self.error_callback = error_callback logger.info('Fetching resource from: %s' % self.url) def _execute_script(self): if self.jsfetcher is None or not os.path.exists(self.jsfetcher): return self.error_callback('error', 'IOError. No such file or directory: %s' % self.jsfetcher) command = "%s --ignore-ssl-errors=true %s %s" % (phantomjs_bin, self.jsfetcher, self.url) logger.debug('Using command: %s' % command) phantom_output = ''
def get_env_variable(var_name): try: return os.environ[var_name] except KeyError: error_msg = "Set the %s environment variable" % var_name raise ImproperlyConfigured(error_msg)
os.environ.update(environ) _setup_environment([ # Oracle takes client-side character set encoding from the environment. ('NLS_LANG', '.UTF8'), # This prevents unicode from getting mangled by getting encoded into the # potentially non-unicode database character set. ('ORA_NCHAR_LITERAL_REPLACE', 'TRUE'), ]) try: import cx_Oracle as Database except ImportError as e: from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e) try: import pytz except ImportError: pytz = None from django.db import utils from django.db.backends import * from django.db.backends.oracle.client import DatabaseClient from django.db.backends.oracle.creation import DatabaseCreation from django.db.backends.oracle.introspection import DatabaseIntrospection from django.utils.encoding import force_bytes, force_text DatabaseError = Database.DatabaseError
def handle(self, *args, **options): # verbose = options["verbosity"] > 1 if not settings.DATABASES.get("kinto"): raise ImproperlyConfigured( "See configuration documentation about setting up " "second the 'kinto' connection.") current_count = Build.objects.all().count() print( f"There are currently {current_count:,} in our existing database.") with connections["kinto"].cursor() as cursor: cursor.execute( """ SELECT COUNT(*) FROM records WHERE parent_id = %s AND collection_id = %s """, [options["parent_id"], options["collection_id"]], ) total_records, = cursor.fetchone() print( f"There are currently {total_records:,} in the Kinto database." ) pages = 0 done = 0 skip_validation = options["skip_validation"] skip_invalid = options["skip_invalid"] skipped = 0 inserted_total = 0 total_t0 = time.time() for batch in self.iterator(options): # builds = [x[0] for x in batch if not skip_invalid or "build" in x[0]] builds = [] for build in batch: if not skip_invalid or "build" in build[0]: if build[0].get("schema"): # The one common thing in the old Kinto database is that each # build has a key 'schema' which is just a timestamp (integer). # Just pop it out so as to not get validation errors that # not actually critical. build[0].pop("schema") builds.append(build[0]) count = len(builds) print(f"Page {pages + 1} ({count:,} records)") t0 = time.time() inserted, batch_skipped = Build.bulk_insert( builds, skip_validation=skip_validation, skip_invalid=skip_invalid, metadata={"kinto-migration": True}, ) t1 = time.time() done += count skipped += batch_skipped inserted_total += inserted print("Inserted {} new out of {} in " "{:.2f} seconds. {} of {} ({:.1f}%)".format( format(inserted, ","), format(count, ","), t1 - t0, format(done, ","), format(total_records, ","), 100 * done / total_records, )) if batch_skipped: print(f"Skipped {batch_skipped:,} invalid records.") pages += 1 total_t1 = time.time() print(f"In total, skipped {skipped:,} invalid records.") print(f"In total, processed {done:,} valid records.") print(f"In total, inserted {inserted_total:,} valid records.") print("The whole migration took {:.1f} minutes.".format( (total_t1 - total_t0) / 60))
def captcha_image(request, key, scale=1): try: store = CaptchaStore.objects.get(hashkey=key) except CaptchaStore.DoesNotExist: # HTTP 410 Gone status so that crawlers don't index these expired urls. return HttpResponse(status=410) text = store.challenge if isinstance(settings.CAPTCHA_FONT_PATH, six.string_types): fontpath = settings.CAPTCHA_FONT_PATH elif isinstance(settings.CAPTCHA_FONT_PATH, (list, tuple)): fontpath = random.choice(settings.CAPTCHA_FONT_PATH) else: raise ImproperlyConfigured( 'settings.CAPTCHA_FONT_PATH needs to be a path to a font or list of paths to fonts' ) if fontpath.lower().strip().endswith('ttf'): font = ImageFont.truetype(fontpath, settings.CAPTCHA_FONT_SIZE * scale) else: font = ImageFont.load(fontpath) if settings.CAPTCHA_IMAGE_SIZE: size = settings.CAPTCHA_IMAGE_SIZE else: size = getsize(font, text) size = (size[0] * 2, int(size[1] * 1.4)) image = makeimg(size) try: PIL_VERSION = int(NON_DIGITS_RX.sub('', Image.VERSION)) except: PIL_VERSION = 116 xpos = 2 charlist = [] for char in text: if char in settings.CAPTCHA_PUNCTUATION and len(charlist) >= 1: charlist[-1] += char else: charlist.append(char) for char in charlist: fgimage = Image.new('RGB', size, settings.CAPTCHA_FOREGROUND_COLOR) charimage = Image.new('L', getsize(font, ' %s ' % char), '#000000') chardraw = ImageDraw.Draw(charimage) chardraw.text((0, 0), ' %s ' % char, font=font, fill='#ffffff') if settings.CAPTCHA_LETTER_ROTATION: if PIL_VERSION >= 116: charimage = charimage.rotate( random.randrange(*settings.CAPTCHA_LETTER_ROTATION), expand=0, resample=Image.BICUBIC) else: charimage = charimage.rotate( random.randrange(*settings.CAPTCHA_LETTER_ROTATION), resample=Image.BICUBIC) charimage = charimage.crop(charimage.getbbox()) maskimage = Image.new('L', size) maskimage.paste(charimage, (xpos, from_top, xpos + charimage.size[0], from_top + charimage.size[1])) size = maskimage.size image = Image.composite(fgimage, image, maskimage) xpos = xpos + 2 + charimage.size[0] if settings.CAPTCHA_IMAGE_SIZE: # centering captcha on the image tmpimg = makeimg(size) tmpimg.paste(image, (int((size[0] - xpos) / 2), int((size[1] - charimage.size[1]) / 2 - from_top))) image = tmpimg.crop((0, 0, size[0], size[1])) else: image = image.crop((0, 0, xpos + 1, size[1])) draw = ImageDraw.Draw(image) for f in settings.noise_functions(): draw = f(draw, image) for f in settings.filter_functions(): image = f(image) out = StringIO() image.save(out, "PNG") out.seek(0) response = HttpResponse(content_type='image/png') response.write(out.read()) response['Content-length'] = out.tell() return response
def create(cls, entry): """ Factory that creates an app config from an entry in INSTALLED_APPS. """ # create() eventually returns app_config_class(app_name, app_module). app_config_class = None app_config_name = None app_name = None app_module = None # If import_module succeeds, entry points to the app module. try: app_module = import_module(entry) except Exception: pass else: # If app_module has an apps submodule that defines a single # AppConfig subclass, use it automatically. # To prevent this, an AppConfig subclass can declare a class # variable default = False. # If the apps module defines more than one AppConfig subclass, # the default one can declare default = True. if module_has_submodule(app_module, APPS_MODULE_NAME): mod_path = "%s.%s" % (entry, APPS_MODULE_NAME) mod = import_module(mod_path) # Check if there's exactly one AppConfig candidate, # excluding those that explicitly define default = False. app_configs = [ (name, candidate) for name, candidate in inspect.getmembers(mod, inspect.isclass) if ( issubclass(candidate, cls) and candidate is not cls and getattr(candidate, "default", True) ) ] if len(app_configs) == 1: app_config_class = app_configs[0][1] app_config_name = "%s.%s" % (mod_path, app_configs[0][0]) else: # Check if there's exactly one AppConfig subclass, # among those that explicitly define default = True. app_configs = [ (name, candidate) for name, candidate in app_configs if getattr(candidate, "default", False) ] if len(app_configs) > 1: candidates = [repr(name) for name, _ in app_configs] raise RuntimeError( "%r declares more than one default AppConfig: " "%s." % (mod_path, ", ".join(candidates)) ) elif len(app_configs) == 1: app_config_class = app_configs[0][1] app_config_name = "%s.%s" % (mod_path, app_configs[0][0]) # If app_module specifies a default_app_config, follow the link. # default_app_config is deprecated, but still takes over the # automatic detection for backwards compatibility during the # deprecation period. try: new_entry = app_module.default_app_config except AttributeError: # Use the default app config class if we didn't find anything. if app_config_class is None: app_config_class = cls app_name = entry else: message = "%r defines default_app_config = %r. " % (entry, new_entry) if new_entry == app_config_name: message += ( "Django now detects this configuration automatically. " "You can remove default_app_config." ) else: message += ( "However, Django's automatic detection %s. You should " "move the default config class to the apps submodule " "of your application and, if this module defines " "several config classes, mark the default one with " "default = True." % ( "picked another configuration, %r" % app_config_name if app_config_name else "did not find this configuration" ) ) warnings.warn(message, RemovedInDjango41Warning, stacklevel=2) entry = new_entry app_config_class = None # If import_string succeeds, entry is an app config class. if app_config_class is None: try: app_config_class = import_string(entry) except Exception: pass # If both import_module and import_string failed, it means that entry # doesn't have a valid value. if app_module is None and app_config_class is None: # If the last component of entry starts with an uppercase letter, # then it was likely intended to be an app config class; if not, # an app module. Provide a nice error message in both cases. mod_path, _, cls_name = entry.rpartition(".") if mod_path and cls_name[0].isupper(): # We could simply re-trigger the string import exception, but # we're going the extra mile and providing a better error # message for typos in INSTALLED_APPS. # This may raise ImportError, which is the best exception # possible if the module at mod_path cannot be imported. mod = import_module(mod_path) candidates = [ repr(name) for name, candidate in inspect.getmembers(mod, inspect.isclass) if issubclass(candidate, cls) and candidate is not cls ] msg = "Module '%s' does not contain a '%s' class." % ( mod_path, cls_name, ) if candidates: msg += " Choices are: %s." % ", ".join(candidates) raise ImportError(msg) else: # Re-trigger the module import exception. import_module(entry) # Check for obvious errors. (This check prevents duck typing, but # it could be removed if it became a problem in practice.) if not issubclass(app_config_class, AppConfig): raise ImproperlyConfigured("'%s' isn't a subclass of AppConfig." % entry) # Obtain app name here rather than in AppClass.__init__ to keep # all error checking for entries in INSTALLED_APPS in one place. if app_name is None: try: app_name = app_config_class.name except AttributeError: raise ImproperlyConfigured("'%s' must supply a name attribute." % entry) # Ensure app_name points to a valid module. try: app_module = import_module(app_name) except ImportError: raise ImproperlyConfigured( "Cannot import '%s'. Check that '%s.%s.name' is correct." % ( app_name, app_config_class.__module__, app_config_class.__qualname__, ) ) # Entry is a path to an app config class. return app_config_class(app_name, app_module)
def _get_render_function(dotted_path, kwargs): module, func = dotted_path.rsplit('.', 1) module, func = smart_str(module), smart_str(func) func = getattr(__import__(module, {}, {}, [func]), func) return curry(func, **kwargs) try: markup_lang = machina_settings.MACHINA_MARKUP_LANGUAGE render_func = _get_render_function(markup_lang[0], markup_lang[1]) if markup_lang \ else lambda text: text except ImportError as e: raise ImproperlyConfigured(_('Could not import MACHINA_MARKUP_LANGUAGE {}: {}').format( machina_settings.MACHINA_MARKUP_LANGUAGE, e)) except AttributeError as e: raise ImproperlyConfigured(_('MACHINA_MARKUP_LANGUAGE setting is required')) @python_2_unicode_compatible class MarkupText(SafeData): def __init__(self, instance, field_name, rendered_field_name): # Stores a reference to the instance along with field names # to make assignment possible. self.instance = instance self.field_name = field_name self.rendered_field_name = rendered_field_name # raw is read/write
import logging import os import socket import sys import warnings from django.contrib.messages import constants as messages from django.core.exceptions import ImproperlyConfigured try: from netbox import configuration except ImportError: raise ImproperlyConfigured( "Configuration file is not present. Please define netbox/netbox/configuration.py per the documentation." ) # Raise a deprecation warning for Python 2.x if sys.version_info[0] < 3: warnings.warn( "Support for Python 2 will be removed in NetBox v2.5. Please consider migration to Python 3 at your earliest " "opportunity. Guidance is available in the documentation at http://netbox.readthedocs.io/.", DeprecationWarning) VERSION = '2.3.7' BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Import required configuration parameters ALLOWED_HOSTS = DATABASE = SECRET_KEY = None for setting in ['ALLOWED_HOSTS', 'DATABASE', 'SECRET_KEY']: try:
def reinit_pad(self): raise ImproperlyConfigured('This method does not work for Ethercalcs')
from django.core.exceptions import ImproperlyConfigured from django.db import DatabaseError as WrappedDatabaseError, connections from django.db.backends.base.base import BaseDatabaseWrapper from django.db.backends.utils import ( CursorDebugWrapper as BaseCursorDebugWrapper, ) from django.utils.asyncio import async_unsafe from django.utils.functional import cached_property from django.utils.safestring import SafeString from django.utils.version import get_version_tuple try: import psycopg2 as Database import psycopg2.extensions import psycopg2.extras except ImportError as e: raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e) def psycopg2_version(): version = psycopg2.__version__.split(' ', 1)[0] return get_version_tuple(version) PSYCOPG2_VERSION = psycopg2_version() if PSYCOPG2_VERSION < (2, 5, 4): raise ImproperlyConfigured( "psycopg2_version 2.5.4 or newer is required; you have %s" % psycopg2.__version__) # Some of these import psycopg2, so import them after checking if it's installed.
def dummy(contacts): raise ImproperlyConfigured("CONTACTS_IMPORT_CALLBACK is set to a dummy " "callback. You should define your own.")
# Validate Python version if platform.python_version_tuple() < ('3', '6'): raise RuntimeError( "NetBox requires Python 3.6 or higher (current: Python {})".format( platform.python_version())) # # Configuration import # # Import configuration parameters try: from netbox import configuration except ImportError: raise ImproperlyConfigured( "Configuration file is not present. Please define netbox/netbox/configuration.py per the documentation." ) # Enforce required configuration parameters for parameter in ['ALLOWED_HOSTS', 'DATABASE', 'SECRET_KEY', 'REDIS']: if not hasattr(configuration, parameter): raise ImproperlyConfigured( "Required parameter {} is missing from configuration.py.".format( parameter)) # Set required parameters ALLOWED_HOSTS = getattr(configuration, 'ALLOWED_HOSTS') DATABASE = getattr(configuration, 'DATABASE') REDIS = getattr(configuration, 'REDIS') SECRET_KEY = getattr(configuration, 'SECRET_KEY')
def __new__(cls, name, bases, attrs): super_new = super(WorkflowMetaclass, cls).__new__ parents = [ b for b in bases if isinstance(b, WorkflowMetaclass) and not (b.__name__ == 'NewBase' and b.__mro__ == (b, object)) ] if not parents: return super_new(cls, name, bases, attrs) # Create the class. new_cls = super_new(cls, name, bases, attrs) # validation if not hasattr(new_cls, 'States'): raise ImproperlyConfigured('No states defined in %s' % new_cls) if not hasattr(new_cls, 'Transitions'): raise ImproperlyConfigured('No transitions defined in %s' % new_cls) # set and check states new_cls._states = OrderedDict() for key, value in sorted(inspect.getmembers( new_cls.States, lambda o: isinstance(o, State)), key=lambda i: i[1]._order): new_cls._states[key] = value if value.default: if hasattr(new_cls, '_default_state'): raise ImproperlyConfigured( 'Two states are defined with an default value in %s' % new_cls) else: new_cls._default_state = value new_cls._default_state_key = key if len(new_cls._states) == 0: raise ImproperlyConfigured('No states defined in %s' % new_cls) if not hasattr(new_cls, '_default_state'): raise ImproperlyConfigured( 'You must define a default state in %s' % new_cls) # set and check transitions new_cls._transitions = OrderedDict() for key, value in sorted(inspect.getmembers( new_cls.Transitions, lambda o: isinstance(o, Transition)), key=lambda i: i[1]._order): if key == "user": raise ImproperlyConfigured( 'The name "user" is reserved, please rename your transition defined in %s' % new_cls) if key == "instance": raise ImproperlyConfigured( 'The name "instance" is reserved, please rename your transition defined in %s' % new_cls) if key[0] == "_": raise ImproperlyConfigured( 'The keys can not start with an underscore in %s' % new_cls) for state in value.affected_states(): if state not in new_cls._states: raise ImproperlyConfigured( 'The state %s is not defined in %s' % (state, new_cls)) new_cls._transitions[key] = value # autoset transition functions for key, value in new_cls._transitions.items(): if hasattr(new_cls, key): continue # function template def f(self): """ I'm a template-function. Please overwrite me and let me return either None or a (redirect) URL """ pass setattr(new_cls, key, f) # return class return new_cls
TIME_ZONE = 'UTC' # https://docs.djangoproject.com/en/dev/ref/settings/#language-code LANGUAGE_CODE = 'en-us' # https://docs.djangoproject.com/en/dev/ref/settings/#site-id SITE_ID = 1 # https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n USE_I18N = True # https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n USE_L10N = True # https://docs.djangoproject.com/en/dev/ref/settings/#use-tz USE_TZ = True # Secret key must be kept secret DEFAULT_SECRET_KEY = '3l$35+@a%g!(^y^98oi%ei+%+yvtl3y0k^_7-fmx2oj09-ac5@' SECRET_KEY = env.str('AMY_SECRET_KEY', default=DEFAULT_SECRET_KEY) if not DEBUG and SECRET_KEY == DEFAULT_SECRET_KEY: raise ImproperlyConfigured('You must specify non-default value for ' 'SECRET_KEY when running with Debug=FALSE.') SITE_ID = env.int('AMY_SITE_ID', default=1) ALLOWED_HOSTS = env.list('AMY_ALLOWED_HOSTS', default=['amy.software-carpentry.org']) if DEBUG: ALLOWED_HOSTS.append('127.0.0.1') # DATABASES # ----------------------------------------------------------------------------- # https://docs.djangoproject.com/en/dev/ref/settings/#databases DATABASES = { 'default': env.db('DATABASE_URL', default='sqlite:///db.sqlite3'), } DATABASES['default']['ATOMIC_REQUESTS'] = True
def __init__(self, *args, **kwargs): """ Initialize the EncryptedFieldMixin with the following optional settings: * keyname: The name of the keyczar key * crypter_klass: A custom class that is extended from Keyczar. * prefix: A static string prepended to all encrypted data * decrypt_only: Boolean whether to only attempt to decrypt data coming from the database and not attempt to encrypt the data being written to the database. """ # Allow for custom class extensions of Keyczar. self._crypter_klass = kwargs.pop('crypter_klass', KeyczarWrapper) self.keyname = kwargs.pop('keyname', None) # If settings.DEFAULT_KEY_DIRECTORY, then the key # is located in DEFAULT_KEY_DIRECTORY/keyname if self.keyname: if hasattr(settings, 'DEFAULT_KEY_DIRECTORY'): self.keydir = os.path.join( settings.DEFAULT_KEY_DIRECTORY, self.keyname ) else: raise ImproperlyConfigured( 'You must set settings.DEFAULT_KEY_DIRECTORY' 'when using the keyname kwarg' ) # If the keyname is not defined on a per-field # basis, then check for the global data encryption key. if not self.keyname and hasattr(settings, 'ENCRYPTED_FIELDS_KEYDIR'): self.keydir = settings.ENCRYPTED_FIELDS_KEYDIR # If we still do not have a keydir, then raise an exception if not self.keydir: raise ImproperlyConfigured( 'You must set settings.ENCRYPTED_FIELDS_KEYDIR ' 'or name a key with kwarg `keyname`' ) # The name of the keyczar key without path for logging purposes. self.keyname = os.path.dirname(self.keydir) # Prefix encrypted data with a static string to allow filtering # of encrypted data vs. non-encrypted data using vanilla MySQL queries. self.prefix = kwargs.pop('prefix', '') # Allow for model decryption-only, bypassing encryption of data. # Useful for models that have a sparse amount of data that is required # to be encrypted. self.decrypt_only = kwargs.pop('decrypt_only', False) self._crypter = self._crypter_klass(self.keydir) # Ensure the encrypted data does not exceed the max_length # of the database. Data truncation is a possibility otherwise. self.enforce_max_length = getattr(settings, 'ENFORCE_MAX_LENGTH', False) if not self.enforce_max_length: self.enforce_max_length = kwargs.pop('enforce_max_length', False) super(EncryptedFieldMixin, self).__init__(*args, **kwargs)
# TODO: Don't use wildcards from .article import * from .pluginbase import * from .urlpath import * # TODO: Should the below stuff be executed a more logical place? # Follow Django's default_settings.py / settings.py pattern and put these # in d_s.py? That might be confusing, though. ###################### # Configuration stuff ###################### if not 'mptt' in django_settings.INSTALLED_APPS: raise ImproperlyConfigured('django-wiki: needs mptt in INSTALLED_APPS') if not 'sekizai' in django_settings.INSTALLED_APPS: raise ImproperlyConfigured('django-wiki: needs sekizai in INSTALLED_APPS') # if not 'django_nyt' in django_settings.INSTALLED_APPS: # raise ImproperlyConfigured('django-wiki: needs django_nyt in INSTALLED_APPS') if not 'django.contrib.humanize' in django_settings.INSTALLED_APPS: raise ImproperlyConfigured( 'django-wiki: needs django.contrib.humanize in INSTALLED_APPS') if not 'django.contrib.contenttypes' in django_settings.INSTALLED_APPS: raise ImproperlyConfigured( 'django-wiki: needs django.contrib.contenttypes in INSTALLED_APPS')
from storages.utils import ( check_location, clean_name, get_available_overwrite_name, safe_join, setting, ) try: from google.cloud.storage import Blob, Client from google.cloud.storage.blob import _quote from google.cloud.exceptions import Conflict, NotFound except ImportError: raise ImproperlyConfigured( "Could not load Google Cloud Storage bindings.\n" "See https://github.com/GoogleCloudPlatform/gcloud-python") class GoogleCloudFile(File): def __init__(self, name, mode, storage): self.name = name self.mime_type = mimetypes.guess_type(name)[0] self._mode = mode self._storage = storage self.blob = storage.bucket.get_blob(name) if not self.blob and 'w' in mode: self.blob = Blob(self.name, storage.bucket, chunk_size=setting('GS_BLOB_CHUNK_SIZE')) self._file = None
_PERIODS = { 's': 1, 'm': 60, 'h': 60 * 60, 'd': 24 * 60 * 60, } ALL = (None, ) # Sentinel value for all HTTP methods. UNSAFE = ['DELETE', 'PATCH', 'POST', 'PUT'] # Extend the expiration time by a few seconds to avoid misses. EXPIRATION_FUDGE = 5 ratelimit_cache = caches[getattr(settings, 'RATELIMIT_USE_CACHE', 'default')] if isinstance(ratelimit_cache, LocMemCache): raise ImproperlyConfigured( 'Ratelimit cache backend must not be LocMemCache') def ip_mask(ip): if ':' in ip: # IPv6 mask = getattr(settings, 'RATELIMIT_IPV6_MASK', 64) else: # IPv4 mask = getattr(settings, 'RATELIMIT_IPV4_MASK', 32) network = ipaddress.ip_network('{}/{}'.format(ip, mask), strict=False) return str(network.network_address)
def __init__(self): self._registry = {} if not getattr(self, '_registerable_class', None): raise ImproperlyConfigured('Subclasses of Registry must set a ' '"_registerable_class" property.')
def get_usage(request, group=None, fn=None, key=None, rate=None, method=ALL, increment=False, reset=False): if group is None and fn is None: raise ImproperlyConfigured('get_usage must be called with either ' '`group` or `fn` arguments') if not getattr(settings, 'RATELIMIT_ENABLE', True): return None if not _method_match(request, method): return None if group is None: parts = [] if isinstance(fn, functools.partial): fn = fn.func # Django <2.1 doesn't use a partial. This is ugly and inelegant, but # throwing __qualname__ into the list below helps. if fn.__name__ == 'bound_func': fn = fn.__closure__[0].cell_contents if hasattr(fn, '__module__'): parts.append(fn.__module__) if hasattr(fn, '__self__'): parts.append(fn.__self__.__class__.__name__) parts.append(fn.__qualname__) group = '.'.join(parts) if callable(rate): rate = rate(group, request) if rate is None: return None limit, period = _split_rate(rate) if not key: raise ImproperlyConfigured('Ratelimit key must be specified') if callable(key): value = key(group, request) elif key in _SIMPLE_KEYS: value = _SIMPLE_KEYS[key](request) elif ':' in key: accessor, k = key.split(':', 1) if accessor not in _ACCESSOR_KEYS: raise ImproperlyConfigured('Unknown ratelimit key: %s' % key) value = _ACCESSOR_KEYS[accessor](request, k) elif '.' in key: keyfn = import_string(key) value = keyfn(group, request) else: raise ImproperlyConfigured('Could not understand ratelimit key: %s' % key) window = _get_window(value, period) initial_value = 1 if increment else 0 cache_key = _make_cache_key(group, window, rate, value, method) count = None if reset: ratelimit_cache.delete(cache_key) return added = ratelimit_cache.add(cache_key, initial_value, period + EXPIRATION_FUDGE) if added: count = initial_value else: if increment: try: # python3-memcached will throw a ValueError if the server is # unavailable or (somehow) the key doesn't exist. redis, on the # other hand, simply returns None. count = ratelimit_cache.incr(cache_key) except ValueError: pass else: count = ratelimit_cache.get(cache_key, initial_value) # Getting or setting the count from the cache failed if count is None: if getattr(settings, 'RATELIMIT_FAIL_OPEN', False): return None return { 'count': 0, 'limit': 0, 'should_limit': True, 'time_left': -1, } time_left = window - int(time.time()) return { 'count': count, 'limit': limit, 'should_limit': count > limit, 'time_left': time_left, }
""" Django migrations for email_log app This package does not contain South migrations. South migrations can be found in the ``south_migrations`` package. """ SOUTH_ERROR_MESSAGE = """\n For South support, customize the SOUTH_MIGRATION_MODULES setting like so: SOUTH_MIGRATION_MODULES = { 'email_log': 'email_log.south_migrations', } """ # Ensure the user is not using Django 1.6 or below with South try: from django.db import migrations # noqa except ImportError: from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured(SOUTH_ERROR_MESSAGE)
def __init__(self, get_response): self.get_response = get_response if not settings.HIDE_ADMIN_ALLOWED_IPS: raise ImproperlyConfigured( "Setting HIDE_ADMIN_ALLOWED_IPS is not specified") self.allowed_ips = settings.HIDE_ADMIN_ALLOWED_IPS
def __init__(self, *args, **kwargs): super(ShortUUIDField, self).__init__(*args, **kwargs) if not HAS_SHORT_UUID: raise ImproperlyConfigured("'shortuuid' module is required for ShortUUIDField. (Do you have Python 2.5 or higher installed ?)") kwargs.setdefault('max_length', self.DEFAULT_MAX_LENGTH)
def __init__(self, *args, **kwargs): if not apps.is_installed("django.contrib.sites"): raise ImproperlyConfigured(self.no_site_message) super().__init__(*args, **kwargs)
} elif os.environ.get("POSTHOG_DB_NAME"): DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql_psycopg2", "NAME": get_env("POSTHOG_DB_NAME"), "USER": os.environ.get("POSTHOG_DB_USER", "postgres"), "PASSWORD": os.environ.get("POSTHOG_DB_PASSWORD", ""), "HOST": os.environ.get("POSTHOG_POSTGRES_HOST", "localhost"), "PORT": os.environ.get("POSTHOG_POSTGRES_PORT", "5432"), "CONN_MAX_AGE": 0, } } else: raise ImproperlyConfigured( f'The environment vars "DATABASE_URL" or "POSTHOG_DB_NAME" are absolutely required to run this software' ) # Broker # The last case happens when someone upgrades Heroku but doesn't have Redis installed yet. Collectstatic gets called before we can provision Redis. if TEST or DEBUG or (len(sys.argv) > 1 and sys.argv[1] == "collectstatic"): REDIS_URL = os.environ.get("REDIS_URL", "redis://localhost/") else: REDIS_URL = os.environ.get("REDIS_URL", "") if not REDIS_URL and os.environ.get("POSTHOG_REDIS_HOST", ""): REDIS_URL = "redis://:{}@{}:{}/".format( os.environ.get("POSTHOG_REDIS_PASSWORD", ""), os.environ.get("POSTHOG_REDIS_HOST", ""), os.environ.get("POSTHOG_REDIS_PORT", "6379"),
from django.core import signals from django.core.cache.backends.base import ( InvalidCacheBackendError, CacheKeyWarning, BaseCache) from django.core.exceptions import ImproperlyConfigured from django.utils.module_loading import import_by_path __all__ = [ 'get_cache', 'cache', 'DEFAULT_CACHE_ALIAS', 'InvalidCacheBackendError', 'CacheKeyWarning', 'BaseCache', ] DEFAULT_CACHE_ALIAS = 'default' if DEFAULT_CACHE_ALIAS not in settings.CACHES: raise ImproperlyConfigured("You must define a '%s' cache" % DEFAULT_CACHE_ALIAS) def get_cache(backend, **kwargs): """ Function to load a cache backend dynamically. This is flexible by design to allow different use cases: To load a backend that is pre-defined in the settings:: cache = get_cache('default') To load a backend with its dotted import path, including arbitrary options:: cache = get_cache('django.core.cache.backends.memcached.MemcachedCache', **{