def get_log_file_path(self, root_dir): """Returns valid log file path Relative path is interpreted as relative to the ${root_dir} Absolute path will be accepted as is. """ if self.options.logging_settings: return None raw_path = self.options.log_file_path if os.path.isabs(raw_path): return raw_path prompt = 'Enter the ' + bold('path to the log file') default_value = self.parser.get_default('log_file_path') validator = OptionValidator(self.console, self.parser, option_name='log_file_path', default_value=default_value, prompt=prompt, validator=LogFilePathValidator, cli_error_messages=\ {'invalid': 'value of --log-file-path is invalid'}, interactive_error_messages=\ {'invalid': 'Path is invalid'}) value = validator.get_value() if os.path.isabs(value): return value return os.path.join(root_dir, value)
def get_db_engine(self): """Dialog asking for a value 1-4""" if self.options.database_settings: return '' choices = [ch[0] for ch in const.DATABASE_ENGINE_CHOICES] if not self.options.interactive: cli_value = self.options.database_engine if not cli_value: return const.DATABASE_ENGINE_CODES[const.SQLITE] if cli_value not in choices: raise ValidationError( 'Invaild choice for the --db-engine parameter') num_choices = [(str(idx + 1), item) for (idx, item) in enumerate(choices)] prompt = 'Select the ' + bold('database engine') + ': ' + \ ', '.join(['%s - %s' % ch for ch in num_choices]) + '.' num_choice = self.console.choice_dialog(prompt, choices=dict(num_choices), default='sqlite') if num_choice == 'sqlite': # a hack num_choice = '2' choice = dict(num_choices)[num_choice] return const.DATABASE_ENGINE_CODES[choice]
def get_db_password(self, db_engine): """Asks user to enter the database password""" if self.options.database_settings: return '' if 'sqlite' in db_engine: return '' if not self.options.interactive: if not self.options.database_password: raise ValidationError('--db-password parameter is required') return self.options.database_password prompt = 'Enter ' + bold('database password') + '.' return self.console.simple_dialog(prompt, required=True)
def get_db_user(self, db_engine): """Asks for the database user name""" if self.options.database_settings: return '' if 'sqlite' in db_engine: return '' if not self.options.interactive: cli_value = self.options.database_user if not cli_value: raise ValidationError('--db-user parameter is required') return cli_value prompt = 'Enter ' + bold('database user name') + '.' return self.console.simple_dialog(prompt, required=True)
def get_email_port(self): """Returns the mail server port number""" if self.options.interactive and not self.options.email_port: return '' validator = OptionValidator(self.console, self.parser, option_name='email_port', default_value='', required=False, prompt='Enter the ' + bold('mail server port number'), validator=PortNumberValidator, cli_error_messages=\ {'invalid': 'value of --email-port is invalid'}, interactive_error_messages=\ {'invalid': 'Port is invalid'}) return validator.get_value()
def get_server_email(self, default_value): """Returns server email""" if self.options.email_settings: return '' validator = OptionValidator(self.console, self.parser, required=False, default_value=default_value, option_name='server_email', prompt='Enter the ' + bold('server email'), validator=EmailValidator, cli_error_messages=\ {'invalid': 'value of --server-email is invalid'}, interactive_error_messages=\ {'invalid': 'Invalid email address'}) return validator.get_value()
def get_default_from_email(self): """Returns DEFAULT_FROM_EMAIL email""" if self.options.email_settings: return '' validator = OptionValidator(self.console, self.parser, required=False, default_value=self.prev_params['admin_email'], option_name='default_from_email', prompt='Enter the ' + bold('default from email'), validator=EmailValidator, cli_error_messages=\ {'invalid': 'value of --default-from-email is invalid'}, interactive_error_messages=\ {'invalid': 'Invalid email address'}) return validator.get_value()
def get_db_port(self, db_engine): """Returns port number, default value is empty string""" if self.options.database_settings: return '' if 'sqlite' in db_engine: return '' validator = OptionValidator(self.console, self.parser, option_name='database_port', default_value='', prompt='Enter the ' + bold('database port'), validator=PortNumberValidator, cli_error_messages=\ {'invalid': 'value of --db-port is invalid'}, interactive_error_messages=\ {'invalid': 'Port is invalid'}) return validator.get_value()
def get_db_host(self, db_engine): """Returns host name for the db settings, default value is empty string""" if self.options.database_settings: return '' if 'sqlite' in db_engine: return '' validator = OptionValidator(self.console, self.parser, option_name='database_host', default_value='', prompt='Enter the ' + bold('database host name'), validator=DomainNameValidator, cli_error_messages=\ {'invalid': 'value of --db-host is invalid'}, interactive_error_messages=\ {'invalid': 'Database host is invalid'}) return validator.get_value()
def get_email_host(self): """Returns host name of the email server""" if self.options.email_settings: return '' if self.options.interactive and not self.options.email_host: return '' validator = OptionValidator(self.console, self.parser, option_name='email_host', default_value='', required=False, prompt='Enter the ' + bold('mail server host name'), validator=DomainNameValidator, cli_error_messages=\ {'invalid': 'value of --email-host is invalid'}, interactive_error_messages=\ {'invalid': 'Host name is invalid'}) return validator.get_value()
def get_timezone(self): """Returns valid timezone for the TIME_ZONE setting""" default_tz = self.parser.get_default('timezone') if self.options.interactive and self.options.timezone == default_tz: return self.options.timezone tz_prompt = 'Enter the ' + bold('time zone') + '\n' + \ 'Allowed timezone values are here:\n' + \ 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones' validator = OptionValidator(self.console, self.parser, option_name='timezone', default_value=default_tz, prompt=tz_prompt, required=True, validator=TimezoneValidator, cli_error_messages=\ {'invalid': 'value of --timezone is invalid'}, interactive_error_messages=\ {'invalid': 'Timezone is invalid'}) return validator.get_value()
def get_language_code(self): """Returns valid language code""" default_lang_code = self.parser.get_default('language_code') is_default = self.options.language_code == default_lang_code if self.options.interactive and is_default: return self.options.language_code language_code_prompt = 'Enter the ' + bold('language code') + '\n' + \ 'Allowed language code values are here:\n' + \ 'https://github.com/django/django/blob/master/django/conf/global_settings.py' validator = OptionValidator(self.console, self.parser, option_name='language_code', default_value=default_lang_code, prompt=language_code_prompt, required=True, validator=LanguageCodeValidator, cli_error_messages=\ {'invalid': 'value of --language-code is invalid'}, interactive_error_messages=\ {'invalid': 'Language code is invalid'}) return validator.get_value()
def get_db_name(self, db_engine): """Asks user to enter the database name""" if self.options.database_settings: return '' if not self.options.interactive: cli_value = self.options.database_name if not cli_value: if 'sqlite' in db_engine: return const.DEFAULT_SQLITE_DB_NAME raise ValidationError('--db-name parameter is required') return cli_value if 'sqlite' in db_engine: prompt = get_sqlite_db_path_prompt(self.prev_params['proj_dir']) db_path = self.console.simple_dialog( prompt, default=const.DEFAULT_SQLITE_DB_NAME) if os.path.isabs(db_path): return db_path return os.path.join(self.prev_params['proj_dir'], db_path) return self.console.simple_dialog('Enter ' + bold('database name') + '.', required=True)
def get_sqlite_db_path_prompt(project_dir): """Returns prompt string for the sqlite db path""" return 'Enter the ' + console.bold('SQLite database file path') + '.\n' + \ ('If path is relative, %s will be prepended.\n' % project_dir) + \ 'Absolute path will be used as given.'
DEFAULT_PROJECT_NAME = 'askbot_site' DEFAULT_MEDIA_ROOT_SUBDIR = 'upfiles' DEFAULT_STATIC_ROOT_SUBDIR = 'static' SQLITE = 'sqlite' DEFAULT_SQLITE_DB_NAME = 'db.sqlite' DATABASE_ENGINE_CHOICES = (('postgresql', 'PostgreSQL'), (SQLITE, 'SQLite'), ('mysql', 'MySQL'), ('oracle', 'Oracle')) DATABASE_ENGINE_CODES = { 'postgresql': 'django.db.backends.postgresql_psycopg2', SQLITE: 'django.db.backends.sqlite3', 'mysql': 'django.db.backends.mysql', 'oracle': 'django.db.backends.oracle' } ROOT_DIR_HELP = 'the ' + bold('Root') + \ ' directory path (relative or absolute).\n' + \ 'This directory will contain the Django project\'s manage.py file' PROJ_NAME_HELP = 'the ' + bold('Project') + \ ' directory name.\nWill be a subdirectory within the ' + \ bold('Root') + ' for the settings.py, urls.py files' MEDIA_ROOT_HELP = 'value of the ' + bold('MEDIA_ROOT') + \ ' setting for the settings.py file.\n ' + \ 'This directory is for the user uploaded files.\n ' + \ 'Default is /upfiles within the ' + bold('Root') + ' directory.' LOG_FILE_PATH_HELP = 'Path to the log file. ' + \ 'If path is absolute - will be used as is. ' + \ 'Relative path will be appended to ${ROOT_DIR}.'