Ticket #101: createview.diff

File createview.diff, 2.4 KB (added by ricardo@…, 19 years ago)

Very crude "createview" implementation

  • django/bin/django-admin.py

    old new  
    395395        sys.exit(1)
    396396runserver.args = '[optional port number]'
    397397
     398DJANGO_VIEW_TEMPLATE = """\
     399# from {{ model_qname }} import {{ model_name }}
     400from django.utils.httpwrappers import HttpResponse
     401
     402# Place here your methods
     403"""
     404
     405DJANGO_VIEW_METHOD_TEMPLATE = """
     406def {{ method_name }}(request):
     407    return HttpResponse("{{ method_name }} method")
     408"""
     409
     410def create_view(mod, filename, *methods):
     411    "Generates a skeleton view module"
     412    model_qname = mod.__name__
     413    model_name = model_qname.split('.')[-1]
     414
     415    app_dir = os.path.normpath(os.path.join(mod.__file__, '../../'))
     416    app_name = os.path.basename(app_dir)
     417    url_file = os.path.join(app_dir, 'urls', app_name + '.py')
     418    view_file = os.path.join(app_dir, 'views', filename + '.py')
     419
     420    if os.path.exists(view_file):
     421        sys.stderr.write("Error: %s exists\n" % view_file)
     422
     423    try:
     424        fp = open(view_file, 'w')
     425        vt = DJANGO_VIEW_TEMPLATE
     426        vt = vt.replace('{{ model_qname }}', model_qname)
     427        vt = vt.replace('{{ model_name }}', model_name)
     428        fp.write(vt)
     429
     430        for method_name in methods:
     431            fp.write(DJANGO_VIEW_METHOD_TEMPLATE.replace('{{ method_name }}',
     432                                                         method_name))
     433    except IOError:
     434        sys.stderr.write("Error: I/O error while trying to create '%s' view\n" % filename)
     435
     436create_view.args = '[app viewFileName optionalMethodName ...]'
     437
    398438def usage():
    399439    sys.stderr.write("Usage: %s [action]\n" % sys.argv[0])
    400440
     
    421461    'startproject': startproject,
    422462    'init': init,
    423463    'install': install,
     464    'createview': create_view,
    424465}
    425466
    426467if __name__ == "__main__":
     
    449490    elif action == 'dbcheck':
    450491        from django.core import meta
    451492        mod_list = meta.get_all_installed_modules()
     493    elif action == 'createview':
     494        from django.core import meta
     495        try:
     496            mod = meta.get_app(sys.argv[2])
     497            view = sys.argv[3]
     498        except ImportError, e:
     499            sys.stderr.write("Error: %s. Are you sure your INSTALLED_APPS setting is correct?\n" % e)
     500            sys.exit(1)
     501        except IndexError:
     502            usage()
     503        create_view(mod, view, *sys.argv[4:])
     504        sys.exit(0)
    452505    else:
    453506        from django.core import meta
    454507        try:
Back to Top