Basic support for xml data files

This commit is contained in:
Fabien Meghazi 2014-05-30 11:35:28 +02:00
parent 7f984dd6ef
commit 10fb6525e9
10 changed files with 45 additions and 15 deletions

View File

@ -84,7 +84,7 @@ class ScaffoldModule(object):
self.path = functools.partial(os.path.join, directory(path))
self.created = not os.path.exists(self.path())
directory(path, create=True)
self.module_name = self.path().split(os.path.sep)[-1]
self.module = self.path().split(os.path.sep)[-1]
if self.created:
manifest_base = os.path.splitext(MANIFEST)[0]
self.render_file('%s.jinja2' % manifest_base, self.path('%s.py' % manifest_base))
@ -96,11 +96,17 @@ class ScaffoldModule(object):
die("Model `%s` already exists !" % model_file)
self.add_init_import(self.path('__init__.py'), 'models')
self.add_init_import(self.path('models', '__init__.py'), model_module)
self.render_file('models.jinja2', model_file, model=model)
self.render_file('ir.model.access.jinja2', self.path('security', 'ir.model.access.csv'),
if_exists='append', model=model)
self.append_manifest_list('data', 'security/ir.model.access.csv')
demo_file = '%s_demo.xml' % self.module
self.append_xml_data('record.jinja2', self.path(demo_file),
model=model)
self.append_manifest_list('demo', demo_file)
def add_controller(self, controller):
controller_module = snake(controller)
controller_file = self.path('controllers', '%s.py' % controller_module)
@ -115,7 +121,7 @@ class ScaffoldModule(object):
def add_webclient_structure(self):
self.append_manifest_list('depends', 'web')
prefix = '%s.%%s' % self.module_name
prefix = '%s.%%s' % self.module
for ext in ('js', 'css', 'xml'):
self.render_file('webclient_%s.jinja2' % ext,
self.path('static', 'src', ext, prefix % ext))
@ -129,7 +135,7 @@ class ScaffoldModule(object):
return False
def get_manifest(self, key=None, default=None):
manifest = load_manifest(self.module_name, self.path())
manifest = load_manifest(self.module, self.path())
if key:
return manifest.get(key, default)
else:
@ -173,10 +179,25 @@ class ScaffoldModule(object):
outdir = os.path.dirname(dest)
if not os.path.exists(outdir):
os.makedirs(outdir)
content = self.env.get_template(template).render(module_name=self.module_name, **kwargs)
content = self.env.get_template(template).render(module=self.module, **kwargs)
with open(dest, mode) as f:
f.write(content)
def append_xml_data(self, template, dest, **kwargs):
if not os.path.exists(dest):
self.render_file('xmldata.jinja2', dest, **kwargs)
with open(dest, 'r') as f:
data = f.read()
m = re.search('(^\s*)?</data>', data, re.MULTILINE)
content = self.env.get_template(template).render(module=self.module, **kwargs)
if not m:
warn("Could not add data in `%s`. You should add this by yourself:"
"\n\n%s\n" % (dest, content))
else:
data = data[:m.start()] + content + m.group() + data[m.end():]
with open(self.path(dest), 'w') as f:
f.write(data)
def snake(s):
""" snake cases ``s``

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
{
'name': "{{ module_name }}",
'name': "{{ module }}",
'summary': """
Short (1 phrase/line) summary of the module's purpose, used as

View File

@ -3,11 +3,11 @@ from openerp import http
from openerp.addons.web.controllers import main
class {{ controller }}(main.Home):
@http.route('/{{ module_name }}/{{ controller }}', auth='public')
@http.route('/{{ module }}/{{ controller }}', auth='public')
def index(self):
return "Hello, world!"
{% if has_model %}
@http.route('/{{ module_name }}/{{ controller }}/<model("{{ module_name }}.{{ controller }}"):{{ controller }}>'], type='http', auth='public')
@http.route('/{{ module }}/{{ controller }}/<model("{{ module }}.{{ controller }}"):{{ controller }}>'], type='http', auth='public')
def {{ controller }}(self, {{ controller }}, **kw):
return "Hello, %r!" % {{ controller }}
{% endif %}

View File

@ -1,6 +1,6 @@
{% if file_created -%}id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink{%- endif %}
{% if model -%}
access_{{ module_name }}_{{ model }},{{- '' -}}
access_{{ module_name }}_{{ model }},{{- '' -}}
model_{{ module_name }}_{{ model }},,1,0,0,0
access_{{ module }}_{{ model }},{{- '' -}}
access_{{ module }}_{{ model }},{{- '' -}}
model_{{ module }}_{{ model }},,1,0,0,0
{%- endif %}

View File

@ -2,7 +2,7 @@
from openerp.osv import orm, fields
class {{ model }}(orm.Model):
_name = "{{ module_name }}.{{ model }}"
_name = "{{ module }}.{{ model }}"
_columns = {
'name': fields.char(),

View File

@ -0,0 +1,4 @@
<record id="{{ module }}.{{ model }}_demo" model="{{ model }}">
<field name="name">My first record for model '{{ module }}.{{ model }}'</field>
</record>

View File

@ -1,4 +1,4 @@
@charset "utf-8";
.openerp .oe_{{ module_name }} {
.openerp .oe_{{ module }} {
background: white;
}

View File

@ -1,4 +1,4 @@
openerp.{{ module_name }} = function (instance) {
openerp.{{ module }} = function (instance) {
var _t = instance.web._t,
_lt = instance.web._lt;

View File

@ -1,6 +1,6 @@
<template>
<t t-name="{{ module_name }}.MyWidget">
<div class="oe_{{ module_name }}">
<t t-name="{{ module }}.MyWidget">
<div class="oe_{{ module }}">
Hello World!
</div>
</t>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data{% if noupdate %} noupdate="1"{% endif %}>
</data>
</openerp>