1 | #!/usr/bin/python
|
---|
2 |
|
---|
3 | ###################################################################################################
|
---|
4 | # Scaffolding the whole project
|
---|
5 | # Author: Javier Nievas (javinievas@gmail.com)
|
---|
6 | # Date: 6th August 2006
|
---|
7 | ###################################################################################################
|
---|
8 | # BASED ON...
|
---|
9 | # Script based on the work of:
|
---|
10 | # - Lllama
|
---|
11 | # - Akaihola
|
---|
12 | # - David Warry
|
---|
13 | ###################################################################################################
|
---|
14 | # INTRODUCTION...
|
---|
15 | # The original work lives in: http://code.djangoproject.com/wiki/ScaffoldScript
|
---|
16 | # I have extended the scaffold to work for the whole project and also generate automatic urls
|
---|
17 | # I have also put together the generation of forms and manipulators in the same file (scaffold.py)
|
---|
18 | ###################################################################################################
|
---|
19 | # Add the following line to your urls.py if you want to use automatic generated urls
|
---|
20 | # from _urls import urlpatterns
|
---|
21 | #
|
---|
22 | # The templates are generated under your app folder, in a templates/app_label folder
|
---|
23 | #
|
---|
24 | # This script was tested under Windows XP, using Django 0.96-pre
|
---|
25 | # You may have to change some code to match your own installation
|
---|
26 | ###################################################################################################
|
---|
27 |
|
---|
28 | from optparse import OptionParser
|
---|
29 | import sys
|
---|
30 | import os
|
---|
31 | from django.shortcuts import render_to_response
|
---|
32 | from django.template.loader import get_template
|
---|
33 | from scaffold import renderForm, readfile
|
---|
34 | from django.db import models
|
---|
35 | from django.template import Context, Template
|
---|
36 |
|
---|
37 | TEMPLATES_FILES_PATTERN = "_%s_form.html"
|
---|
38 | APPS_FOLDER = r"../apps"
|
---|
39 | TEMPLATE_PATH_PATTERN = r"%s/%s/templates/%s"
|
---|
40 | URLS_PATH_PATTERN = r"%s/%s"
|
---|
41 |
|
---|
42 | def writefile(fichero, cadena):
|
---|
43 | f = open(fichero,"wt")
|
---|
44 | pre = f.write(cadena)
|
---|
45 | f.close()
|
---|
46 |
|
---|
47 | urls_prefix="""
|
---|
48 | # SCAFFOLDING AUTOGENERATED URLS (by javinievas@gmail.com)
|
---|
49 | # Add the following line to your urls.py if you want to use automatic generated urls
|
---|
50 | # from _urls import urlpatterns
|
---|
51 |
|
---|
52 | from django.conf.urls.defaults import *
|
---|
53 | from django.conf import settings
|
---|
54 | from contratacion.models import *
|
---|
55 |
|
---|
56 | urlpatterns = patterns('')
|
---|
57 |
|
---|
58 | """
|
---|
59 | def meta_generate(app, options):
|
---|
60 | list = models.get_models(app_mod=app.models)
|
---|
61 | list_names = [i.__name__ for i in list]
|
---|
62 | list_names_lower = [i.lower() for i in list_names]
|
---|
63 |
|
---|
64 | template_path = TEMPLATE_PATH_PATTERN % (APPS_FOLDER, options.app, options.app)
|
---|
65 | urls_path = URLS_PATH_PATTERN % (APPS_FOLDER, options.app,)
|
---|
66 | urls = ""
|
---|
67 | print template_path
|
---|
68 |
|
---|
69 | print list_names
|
---|
70 | print "Building forms..."
|
---|
71 | for i, name in enumerate(list_names):
|
---|
72 | template_file = os.path.join(template_path, TEMPLATES_FILES_PATTERN % list_names_lower[i])
|
---|
73 | print "Form for '%s' in: \n\t'%s'" % (name, template_file)
|
---|
74 | template_content = renderForm(options.app, name)
|
---|
75 | writefile(template_file, template_content)
|
---|
76 | urls = urls + genera_urls(app.__name__,name)
|
---|
77 |
|
---|
78 | print "\nBuilding _urls.py"
|
---|
79 | writefile(os.path.join(urls_path, "_urls.py"), urls_prefix + urls)
|
---|
80 | print "\nScaffolding completed!"
|
---|
81 |
|
---|
82 | def genera_urls(app,modelo):
|
---|
83 | template = """
|
---|
84 | # URLS of {{modelo}}
|
---|
85 |
|
---|
86 | urlpatterns += patterns('django.views.generic.create_update',
|
---|
87 | (r'^{{modelo|lower}}/new/$', 'create_object', dict({'model':{{modelo}}})),
|
---|
88 | (r'^{{modelo|lower}}/(?P<object_id>\d+)/edit/$', 'update_object', dict({'model':{{modelo}}})),
|
---|
89 | (r'^{{modelo|lower}}/(?P<object_id>\d+)/delete/$', 'delete_object', dict({'model':{{modelo}}})),
|
---|
90 | )
|
---|
91 | urlpatterns += patterns('{{app}}.views',
|
---|
92 | (r'^{{modelo|lower}}/list/$','{{modelo|lower}}_list'),
|
---|
93 | )
|
---|
94 | info_dict = {
|
---|
95 | 'queryset': {{modelo}}.objects.all(),
|
---|
96 | }
|
---|
97 | urlpatterns += patterns('django.views.generic.list_detail',
|
---|
98 | (r'^{{modelo|lower}}/(?P<object_id>\d+)/$', 'object_detail', dict(info_dict)),
|
---|
99 | )
|
---|
100 | """
|
---|
101 | t = Template(template)
|
---|
102 | return t.render(Context({'modelo': modelo,'app':app}))
|
---|
103 |
|
---|
104 | if __name__ == '__main__':
|
---|
105 | try:
|
---|
106 | import settings
|
---|
107 | except ImportError:
|
---|
108 | print "Settings file not found. Place this file in the same place as manage.py"
|
---|
109 | sys.exit()
|
---|
110 |
|
---|
111 | p = OptionParser()
|
---|
112 |
|
---|
113 | p.add_option("-p", "--project", dest="project", help="The name of the project")
|
---|
114 | p.add_option("-a", "--app", dest="app", help="The app which contains the models")
|
---|
115 | options, args = p.parse_args()
|
---|
116 |
|
---|
117 | if not (options.app):
|
---|
118 | p.print_help()
|
---|
119 | sys.exit()
|
---|
120 |
|
---|
121 | app_models = __import__("%s.models" % options.app)
|
---|
122 | meta_generate(app_models, options)
|
---|
123 |
|
---|