Skip to content

Commit

Permalink
Merge pull request hexya-erp#124 from hexya-erp/dev
Browse files Browse the repository at this point in the history
Several improvments in view inheritance
  • Loading branch information
npiganeau committed Jun 22, 2020
2 parents 464749f + ed63e7d commit 6dcd5b7
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 138 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[![Go Report Card](https://goreportcard.com/badge/hexya-erp/hexya)](https://goreportcard.com/report/hexya-erp/hexya)
[![codecov](https://codecov.io/gh/hexya-erp/hexya/branch/master/graph/badge.svg)](https://codecov.io/gh/hexya-erp/hexya)
[![godoc reference](https://godoc.org/github.com/hexya-erp/hexya?status.png)](https://godoc.org/github.com/hexya-erp/hexya)
[![Gitter](https://badges.gitter.im/hexya-erp/community.svg)](https://gitter.im/hexya-erp/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

# Hexya
Expand Down
4 changes: 2 additions & 2 deletions src/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (t *Template) updateFromXML(templateXML *TemplateXML) {
if err != nil {
log.Panic("Unable to read inheritance specs", "error", err, "arch", string(templateXML.Content))
}
content, err := xmlutils.XMLToElement(string(t.hWebContent))
content, err := xmlutils.XMLToDocument(string(t.hWebContent))
if err != nil {
log.Panic("Error while reading base template content", "error", err, "template", t.ID, "content", string(t.hWebContent))
}
Expand All @@ -123,7 +123,7 @@ func (t *Template) updateFromXML(templateXML *TemplateXML) {
log.Panic("Error while applying template extension specs", "error", err, "specTmpl", templateXML.ID,
"specs", string(templateXML.Content), "template", t.ID, "content", string(t.hWebContent))
}
ncs, err := xmlutils.ElementToXMLNoIndent(newContent)
ncs, err := xmlutils.DocumentToXMLNoIndent(newContent)
if err != nil {
log.Panic("Error while converting back to XML", "error", err, "content", newContent,
"specTmpl", templateXML.ID, "template", t.ID)
Expand Down
213 changes: 143 additions & 70 deletions src/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,22 @@ var tmplDef2 = `
var tmplDef3 = `
<template inherit_id="my_other_id">
<div name="position_info" position="inside">
<t t-esc="CompanyName"/>
</div>
<xpath expr="//t[@t-esc='Email']" position="after">
<t t-esc="Phone"/>
</xpath>
<t t-esc="CompanyName"/>
</div>
<xpath expr="//t[@t-esc='Email']" position="after"><t t-esc="Phone"/></xpath>
</template>
`

var tmplDef4 = `
<template inherit_id="my_other_id">
<div name="contact_data" position="before">
<div>
<div>
<t t-esc="Address"/>
</div>
<hr/>
</div>
</div>
<h1 position="replace">
<h2><t t-esc="Name"/></h2>
<h2><t t-esc="Name"/></h2>
</h1>
</template>
`
Expand All @@ -76,34 +74,39 @@ var tmplDef5 = `

var tmplDef8 = `
<template inherit_id="my_other_id" id="new_base_view" priority="13" optional="disabled" page="True">
<xpath expr="//t[@t-esc='Email']" position="after">
<t t-raw="Fax"/>
</xpath>
<xpath expr="//t[@t-esc='Email']" position="after"><t t-raw="Fax"/></xpath>
</template>
`

var tmplDef9 = `
<template inherit_id="new_base_view">
<xpath expr="//t[@t-raw='Fax']" position="before">
<t t-raw="Mobile"/>
<xpath expr="//t[@t-raw='Fax']" position="before"><t t-raw="Mobile"/></xpath>
</template>
`

var tmplDef10 = `
<template inherit_id="my_other_id">
<xpath expr="." position="inside">
<div>1</div>
<div>2</div>
</xpath>
</template>
`

func loadView(xml string) {
func loadTemplate(xml string) {
elt, err := xmlutils.XMLToElement(xml)
if err != nil {
panic(err)
}
LoadFromEtree(elt)
}

func TestViews(t *testing.T) {
func TestTemplates(t *testing.T) {
Convey("Setting two languages", t, func() {
i18n.Langs = []string{"fr", "de"}
})
Convey("Creating Template 1", t, func() {
loadView(tmplDef1)
loadTemplate(tmplDef1)
BootStrap()
So(len(Registry.collection.templates), ShouldEqual, 1)
So(Registry.collection.GetByID("my_id"), ShouldNotBeNil)
Expand All @@ -125,8 +128,8 @@ func TestViews(t *testing.T) {
`)
})
Convey("Creating Template 2", t, func() {
loadView(tmplDef1)
loadView(tmplDef2)
loadTemplate(tmplDef1)
loadTemplate(tmplDef2)
BootStrap()
So(len(Registry.collection.templates), ShouldEqual, 2)
So(Registry.collection.GetByID("my_other_id"), ShouldNotBeNil)
Expand All @@ -150,9 +153,9 @@ func TestViews(t *testing.T) {
`)
})
Convey("Inheriting Template 2", t, func() {
loadView(tmplDef1)
loadView(tmplDef2)
loadView(tmplDef3)
loadTemplate(tmplDef1)
loadTemplate(tmplDef2)
loadTemplate(tmplDef3)
BootStrap()
So(len(Registry.collection.templates), ShouldEqual, 2)
So(Registry.collection.GetByID("my_id"), ShouldNotBeNil)
Expand All @@ -170,89 +173,110 @@ func TestViews(t *testing.T) {
`)
template2 := Registry.collection.GetByID("my_other_id")
So(string(template2.Content("")), ShouldEqual,
`{% set _1 = _0 %}<div>
`{% set _1 = _0 %}
<div>
<h1>Name</h1>
<div name="position_info">
{{ Function }}
{{ CompanyName }}</div>
{{ CompanyName }}
</div>
<div name="contact_data">
{{ Email }}{{ Phone }}
</div>
</div>`)
</div>
`)
})

Convey("More inheritance on Template 2", t, func() {
loadView(tmplDef1)
loadView(tmplDef2)
loadView(tmplDef3)
loadView(tmplDef4)
loadTemplate(tmplDef1)
loadTemplate(tmplDef2)
loadTemplate(tmplDef3)
loadTemplate(tmplDef4)
BootStrap()
So(len(Registry.collection.templates), ShouldEqual, 2)
So(Registry.collection.GetByID("my_id"), ShouldNotBeNil)
So(Registry.collection.GetByID("my_other_id"), ShouldNotBeNil)
template2 := Registry.collection.GetByID("my_other_id")
So(string(template2.Content("")), ShouldEqual,
`{% set _1 = _0 %}<div>
`{% set _1 = _0 %}
<div>
<h2>{{ Name }}</h2>
<div name="position_info">
{{ Function }}
{{ CompanyName }}</div>
{{ CompanyName }}
</div>
<div>
{{ Address }}
</div><hr/><div name="contact_data">
</div>
<hr/>
<div name="contact_data">
{{ Email }}{{ Phone }}
</div>
</div>`)
</div>
`)

})
Convey("Modifying inherited modifications on Template 2", t, func() {
loadView(tmplDef1)
loadView(tmplDef2)
loadView(tmplDef3)
loadView(tmplDef4)
loadView(tmplDef5)
loadTemplate(tmplDef1)
loadTemplate(tmplDef2)
loadTemplate(tmplDef3)
loadTemplate(tmplDef4)
loadTemplate(tmplDef5)
BootStrap()
So(len(Registry.collection.templates), ShouldEqual, 2)
So(Registry.collection.GetByID("my_id"), ShouldNotBeNil)
So(Registry.collection.GetByID("my_other_id"), ShouldNotBeNil)
template2 := Registry.collection.GetByID("my_other_id")
So(string(template2.Content("")), ShouldEqual,
`{% set _1 = _0 %}<div>
`{% set _1 = _0 %}
<div>
<h2>{{ Name }}</h2>
<div name="position_info">
{{ Function }}
{{ CompanyName }}</div>
{{ CompanyName }}
</div>
<div name="address" string="Address">
{{ Address }}
</div><hr/><div name="contact_data">
</div>
<hr/>
<div name="contact_data">
{{ Email }}{{ Phone }}
</div>
</div>`)
</div>
`)
})
Convey("Create new base template from inheritance", t, func() {
loadView(tmplDef1)
loadView(tmplDef2)
loadView(tmplDef3)
loadView(tmplDef4)
loadView(tmplDef5)
loadView(tmplDef8)
loadTemplate(tmplDef1)
loadTemplate(tmplDef2)
loadTemplate(tmplDef3)
loadTemplate(tmplDef4)
loadTemplate(tmplDef5)
loadTemplate(tmplDef8)
BootStrap()
So(Registry.collection.GetByID("my_other_id"), ShouldNotBeNil)
So(Registry.collection.GetByID("new_base_view"), ShouldNotBeNil)
template2 := Registry.collection.GetByID("my_other_id")
newTemplate := Registry.collection.GetByID("new_base_view")
So(string(template2.Content("")), ShouldEqual,
`{% set _1 = _0 %}<div>
`{% set _1 = _0 %}
<div>
<h2>{{ Name }}</h2>
<div name="position_info">
{{ Function }}
{{ CompanyName }}</div>
{{ CompanyName }}
</div>
<div name="address" string="Address">
{{ Address }}
</div><hr/><div name="contact_data">
</div>
<hr/>
<div name="contact_data">
{{ Email }}{{ Phone }}
</div>
</div>`)
</div>
`)
So(template2.Priority, ShouldEqual, 12)
So(template2.Page, ShouldBeFalse)
So(template2.Optional, ShouldBeTrue)
Expand All @@ -262,56 +286,105 @@ func TestViews(t *testing.T) {
So(newTemplate.Optional, ShouldBeTrue)
So(newTemplate.OptionalDefault, ShouldBeFalse)
So(string(newTemplate.Content("")), ShouldEqual,
`{% set _1 = _0 %}<div>
`{% set _1 = _0 %}
<div>
<h2>{{ Name }}</h2>
<div name="position_info">
{{ Function }}
{{ CompanyName }}</div>
{{ CompanyName }}
</div>
<div name="address" string="Address">
{{ Address }}
</div><hr/><div name="contact_data">
</div>
<hr/>
<div name="contact_data">
{{ Email }}{{ Fax|safe }}{{ Phone }}
</div>
</div>`)
</div>
`)
})
Convey("Inheriting new base template from inheritance", t, func() {
Registry.collection = newCollection()
loadView(tmplDef1)
loadView(tmplDef2)
loadView(tmplDef3)
loadView(tmplDef4)
loadView(tmplDef5)
loadView(tmplDef8)
loadView(tmplDef9)
loadTemplate(tmplDef1)
loadTemplate(tmplDef2)
loadTemplate(tmplDef3)
loadTemplate(tmplDef4)
loadTemplate(tmplDef5)
loadTemplate(tmplDef8)
loadTemplate(tmplDef9)
BootStrap()
So(Registry.collection.GetByID("my_other_id"), ShouldNotBeNil)
So(Registry.collection.GetByID("new_base_view"), ShouldNotBeNil)
template2 := Registry.collection.GetByID("my_other_id")
newTemplate := Registry.collection.GetByID("new_base_view")
So(string(template2.Content("")), ShouldEqual,
`{% set _1 = _0 %}<div>
`{% set _1 = _0 %}
<div>
<h2>{{ Name }}</h2>
<div name="position_info">
{{ Function }}
{{ CompanyName }}</div>
{{ CompanyName }}
</div>
<div name="address" string="Address">
{{ Address }}
</div><hr/><div name="contact_data">
</div>
<hr/>
<div name="contact_data">
{{ Email }}{{ Phone }}
</div>
</div>`)
</div>
`)
So(string(newTemplate.Content("")), ShouldEqual,
`{% set _1 = _0 %}<div>
`{% set _1 = _0 %}
<div>
<h2>{{ Name }}</h2>
<div name="position_info">
{{ Function }}
{{ CompanyName }}</div>
{{ CompanyName }}
</div>
<div name="address" string="Address">
{{ Address }}
</div><hr/><div name="contact_data">
</div>
<hr/>
<div name="contact_data">
{{ Email }}{{ Mobile|safe }}{{ Fax|safe }}{{ Phone }}
</div>
</div>`)
</div>
`)
})
Convey("Inherited modifications on root element", t, func() {
loadTemplate(tmplDef1)
loadTemplate(tmplDef2)
loadTemplate(tmplDef3)
loadTemplate(tmplDef4)
loadTemplate(tmplDef5)
loadTemplate(tmplDef10)
BootStrap()
So(Registry.collection.GetByID("my_other_id"), ShouldNotBeNil)
template2 := Registry.collection.GetByID("my_other_id")
So(string(template2.Content("")), ShouldEqual,
`{% set _1 = _0 %}
<div>
<h2>{{ Name }}</h2>
<div name="position_info">
{{ Function }}
{{ CompanyName }}
</div>
<div name="address" string="Address">
{{ Address }}
</div>
<hr/>
<div name="contact_data">
{{ Email }}{{ Phone }}
</div>
</div>
<div>1</div>
<div>2</div>
`)
})
Convey("Testing gin loading", t, func() {
inst := Registry.Instance("my_id", pongo2.Context{
Expand Down
Loading

0 comments on commit 6dcd5b7

Please sign in to comment.