class PoiseApplicationPython::Resources::Django::Resource

An ‘application_django` resource to configure Django applications.

@since 4.0.0 @provides application_django @action deploy @example

application '/srv/myapp' do
  git '...'
  pip_requirements
  django do
    database do
      host node['db_host']
    end
  end
  gunicorn do
    port 8080
  end
end

Private Instance Methods

default_local_settings_options() click to toggle source

Default value for {#local_settings_options}. Adds Django settings data from the resource to be rendered in the local settings template.

@return [Hash]

# File lib/poise_application_python/resources/django.rb, line 156
def default_local_settings_options
  {}.tap do |options|
    options[:allowed_hosts] = Array(allowed_hosts)
    options[:databases] = {}
    options[:databases]['default'] = database.inject({}) do |memo, (key, value)|
      key = key.to_s.upcase
      # Deal with engine aliases here too, just in case.
      value = resolve_engine(value) if key == 'ENGINE'
      memo[key] = value
      memo
    end
    options[:debug] = debug
    options[:secret_key] = secret_key
  end
end
default_local_settings_path() click to toggle source

Default value for {#local_settings_path}, local_settings.py next to the configured {#settings_module}.

@return [String, nil]

# File lib/poise_application_python/resources/django.rb, line 176
def default_local_settings_path
  # If no settings module, no default local settings.
  return unless settings_module
  settings_path = PoisePython::Utils.module_to_path(settings_module, path)
  ::File.expand_path(::File.join('..', 'local_settings.py'), settings_path)
end
default_manage_path() click to toggle source

Default value for {#manage_path}, searches for manage.py in the application path.

@return [String, nil]

# File lib/poise_application_python/resources/django.rb, line 187
def default_manage_path
  find_file('manage.py')
end
default_settings_module() click to toggle source

Default value for {#settings_module}, searches for settings.py in the application path.

@return [String, nil]

# File lib/poise_application_python/resources/django.rb, line 195
def default_settings_module
  settings_path = find_file('settings.py')
  if settings_path
    PoisePython::Utils.path_to_module(settings_path, path)
  else
    nil
  end
end
default_wsgi_module() click to toggle source

Default value for {#wsgi_module}, searchs for wsgi.py in the application path.

@return [String, nil]

# File lib/poise_application_python/resources/django.rb, line 208
def default_wsgi_module
  wsgi_path = find_file('wsgi.py')
  if wsgi_path
    PoisePython::Utils.path_to_module(wsgi_path, path)
  else
    nil
  end
end
find_file(name) click to toggle source

Search for a file somewhere under the application path. Prefers files closer to the root, then sort alphabetically for stability.

@param name [String] Filename to search for. @return [String, nil]

# File lib/poise_application_python/resources/django.rb, line 244
def find_file(name)
  num_separators = lambda do |path|
    if ::File::ALT_SEPARATOR && path.include?(::File::ALT_SEPARATOR)
      # :nocov:
      path.count(::File::ALT_SEPARATOR)
      # :nocov:
    else
      path.count(::File::SEPARATOR)
    end
  end
  Dir[::File.join(path, '**', name)].min do |a, b|
    cmp = num_separators.call(a) <=> num_separators.call(b)
    if cmp == 0
      cmp = a <=> b
    end
    cmp
  end
end
parse_database_url(url) click to toggle source

Format a URL for DATABASES.

@return [Hash]

# File lib/poise_application_python/resources/django.rb, line 220
def parse_database_url(url)
  parsed = URI(url)
  {}.tap do |db|
    # Store this for use later in #set_state, and maybe future use by
    # Django in some magic world where operability happens.
    db[:URL] = url
    db[:ENGINE] = resolve_engine(parsed.scheme)
    # Strip the leading /.
    path = parsed.path ? parsed.path[1..-1] : parsed.path
    # If we are using SQLite, make it an absolute path.
    path = ::File.expand_path(path, self.path) if db[:ENGINE].include?('sqlite')
    db[:NAME] = path if path && !path.empty?
    db[:USER] = parsed.user if parsed.user && !parsed.user.empty?
    db[:PASSWORD] = parsed.password if parsed.password && !parsed.password.empty?
    db[:HOST] = parsed.host if parsed.host && !parsed.host.empty?
    db[:PORT] = parsed.port if parsed.port && !parsed.port.empty?
  end
end
resolve_engine(name) click to toggle source

Resolve Django database engine from shortname to dotted module.

@param name [String, nil] Engine name. @return [String, nil]

# File lib/poise_application_python/resources/django.rb, line 267
def resolve_engine(name)
  if name && !name.empty? && !name.include?('.')
    ENGINE_ALIASES[name] || "django.db.backends.#{name}"
  else
    name
  end
end