Ticket #3475: invalid_app_and_project_names.patch

File invalid_app_and_project_names.patch, 2.4 KB (added by frankie@…, 17 years ago)

This patch check for invalid project and app names in the way previously mentioned.

  • django/core/management.py

     
    2525# which has been installed.
    2626PROJECT_TEMPLATE_DIR = os.path.join(django.__path__[0], 'conf', '%s_template')
    2727
    28 INVALID_PROJECT_NAMES = ('django', 'site', 'test')
    29 
    3028# Set up the terminal color scheme.
    3129class dummy: pass
    3230style = dummy()
     
    5048if sys.platform == 'win32' or sys.platform == 'Pocket PC' or not sys.stdout.isatty():
    5149    disable_termcolors()
    5250
     51def _is_valid_project_or_app_name(name):
     52    valid_name = False
     53    try:
     54        eval(name)
     55    except NameError:
     56        try:
     57            __import__(name)
     58        except ImportError:
     59            valid_name = True
     60        except:
     61            pass
     62    except:
     63        pass
     64    return valid_name
     65
    5366def _is_valid_dir_name(s):
    5467    return bool(re.search(r'^\w+$', s))
    5568
     
    707720def startproject(project_name, directory):
    708721    "Creates a Django project for the given project_name in the given directory."
    709722    from random import choice
    710     if project_name in INVALID_PROJECT_NAMES:
    711         sys.stderr.write(style.ERROR("Error: '%r' conflicts with the name of an existing Python module and cannot be used as a project name. Please try another name.\n" % project_name))
     723    if not _is_valid_project_or_app_name(project_name):
     724        sys.stderr.write(style.ERROR("Error: '%r' conflicts with the name of an existing Python module or keyword and cannot be used as a project name. Please try another name.\n" % project_name))
    712725        sys.exit(1)
    713726    _start_helper('project', project_name, directory)
    714727    # Create a random SECRET_KEY hash, and put it in the main settings.
     
    726739    "Creates a Django app for the given app_name in the given directory."
    727740    # Determine the project_name a bit naively -- by looking at the name of
    728741    # the parent directory.
     742    if not _is_valid_project_or_app_name(app_name):
     743        sys.stderr.write(style.ERROR("Error: '%r' conflicts with the name of an existing Python module or keyword and cannot be used as a application name. Please try another name.\n" % app_name))
     744        sys.exit(1)
    729745    project_dir = os.path.normpath(os.path.join(directory, '..'))
    730746    project_name = os.path.basename(project_dir)
    731747    if app_name == os.path.basename(directory):
Back to Top