[WIP] models and controllers for scaffolding

This commit is contained in:
Fabien Meghazi 2014-05-28 17:10:56 +02:00
parent ba85b80f32
commit ce764477e6
4 changed files with 92 additions and 37 deletions

View File

@ -20,32 +20,69 @@ class Scaffold(Command):
'openerp.cli', 'scaffold'))
env.filters['snake'] = snake
self.env = env
self.manifest = '__openerp__'
def scaffold(self, args):
# TODO: make this function callable even if the module already
# exists. (update mode for scaffolding)
from pudb import set_trace;set_trace() ############################## Breakpoint ##############################
args.dependency = 'base'
if args.web:
args.dependency = 'web'
elif args.theme:
args.dependency = 'website'
dest = os.path.abspath(os.path.expanduser(args.dest))
# TODO: update dependencies according to --web and --theme
# if args.web:
# args.dependency = 'web'
# elif args.theme:
# args.dependency = 'website'
module_name = snake(args.module)
module = functools.partial(os.path.join, dest, module_name)
dest = directory(args.dest)
if args.init:
module_name = snake(args.init)
module = functools.partial(os.path.join, dest, module_name)
if os.path.exists(module()):
die("Can't initialize module in `%s`: Directory already exists." % module())
else:
module_name = dest.split(os.path.sep)[-1]
# find the module's root directory
while not os.path.exists(os.path.join(dest, '%s.py' % self.manifest)):
new_dest = os.path.abspath(os.path.join(dest, os.pardir))
if dest == new_dest:
die("Can't find module directory. Please `cd` to it's path or use --dest")
module_name = dest.split(os.path.sep)[-1]
dest = new_dest
module = functools.partial(os.path.join, dest)
args.module = module_name
if os.path.exists(module()):
message = "The path `%s` already exists." % module()
die(message)
if args.init:
self.dump('%s.jinja2' % self.manifest, module('%s.py' % self.manifest), config=args)
self.dump('__openerp__.jinja2', module('__openerp__.py'), config=args)
self.dump('__init__.jinja2', module('__init__.py'), modules=[
args.controller and 'controllers',
args.model and 'models'
])
self.dump('ir.model.access.jinja2', module('security', 'ir.model.access.csv'), config=args)
if args.model:
model_module = snake(args.model)
model_file = module('models', '%s.py' % model_module)
if os.path.exists(model_file):
die("Model `%s` already exists !" % model_file)
self.add_init_import(module('__init__.py'), 'models')
self.add_init_import(module('models', '__init__.py'), model_module)
self.dump('models.jinja2', model_file, config=args)
if args.controller:
controller_module = snake(args.controller)
controller_file = module('controllers', '%s.py' % controller_module)
if os.path.exists(controller_file):
die("Controller `%s` already exists !" % controller_file)
self.add_init_import(module('__init__.py'), 'controllers')
self.add_init_import(module('controllers', '__init__.py'), controller_module)
self.dump('controllers.jinja2', module('controllers', controller_file), config=args)
# self.dump('ir.model.access.jinja2', module('security', 'ir.model.access.csv'), config=args)
return
def add_init_import(self, path, module):
if not os.path.exists(path):
self.dump('__init__.jinja2', path, modules=[module])
else:
with open(path, "r") as f:
lines = f.readlines()
# TODO: regex
if ('import %s' % module) in lines:
return
with open(path, "a") as f:
f.write('\nimport %s' % module)
def dump(self, template, dest, **kwargs):
outdir = os.path.dirname(dest)
@ -61,15 +98,24 @@ class Scaffold(Command):
prog="%s scaffold" % sys.argv[0].split(os.path.sep)[-1],
description=self.__doc__
)
parser.add_argument('module', help="Name of the module to generate")
parser.add_argument('dest', nargs='?', help='Directory where the module should be created (default to current directory)', default=".")
parser.add_argument('--init', type=identifier, help='Initialize a new Odoo module')
parser.add_argument('--dest', default=".",
help='Directory where the module should be created/updated (default to current directory)')
parser.add_argument('--model', type=identifier, help="Name of the model to add")
parser.add_argument('--controller', type=identifier, help="Name of the controller to add")
parser.add_argument('--web', action='store_true', default=False,
help="Generate structure for a webclient module")
parser.add_argument('--theme', action='store_true', default=False,
help="Generate structure for a Website theme")
if not args:
sys.exit(parser.print_help())
args = parser.parse_args(args=args)
self.scaffold(args)
def snake(s):

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
{% for module in modules if module -%}
import {{ module }}
{% endfor %}
import {{ module }}{% endfor %}

View File

@ -1,26 +1,32 @@
# -*- coding: utf-8 -*-
{
'name': "{{ config.module }}",
# short description, used as subtitles on modules listings
'summary': """
Short (1 phrase/line) summary of the module's purpose, used as
subtitle on modules listing or apps.openerp.com""",
# long description of module purpose
'description': """
""",
# Who you are
'author': "Acme Corp.",
'website': "http://www.example.com",
# categories can be used to filter modules in modules listing
'description': """
Long description of module's purpose
""",
'author': "Your Company",
'website': "http://www.yourcompany.com",
# Categories can be used to filter modules in modules listing
# Check <odoo>/addons/base/module/module_data.xml of the full list
'category': 'Uncategorized',
'version': '0.1',
# any module necessary for this one to work correctly
'depends': ['{{ config.dependency }}'],
'depends': ['base'],
'data': [
{{- "'security/ir.model.access.csv'" if config.model -}}
# {{- "'security/ir.model.access.csv'" if config.model -}}
],
'demo': [
],
'tests': [
],
}

View File

@ -1,9 +1,13 @@
# -*- coding: utf-8 -*-
from openerp import http
from openerp.addons.web.controllers import main
class {{ config.controller }}(main.Home):
@http.route('/', auth='none')
@http.route('/{{ config.module }}/{{ config.controller }}', auth='public')
def index(self):
return "Hello, world!"
{% if config.model %}
@http.route('/{{ config.module }}/{{ config.controller }}/{{ config.model }}/<model("{{ config.module }}.{{ config.model }}"):{{ config.model }}>'], type='http', auth='public')
def {{ config.model }}(self, {{ config.model }}, **kw):
return "Hello, %r!" % {{ config.model }}
{% endif %}