Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
4b589295
Commit
4b589295
authored
Jan 28, 2011
by
Carl Whittaker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding test for flattened dictionary
parent
2825d34e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
6 deletions
+26
-6
pystache/view.py
+19
-5
tests/test_view.py
+7
-1
No files found.
pystache/view.py
View file @
4b589295
...
@@ -29,10 +29,10 @@ class View(object):
...
@@ -29,10 +29,10 @@ class View(object):
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
self
.
template
=
template
self
.
template
=
template
self
.
context
=
context
or
{}
context
=
context
or
{}
self
.
context
.
update
(
**
kwargs
)
context
.
update
(
**
kwargs
)
self
.
context_list
=
[
self
.
context
]
self
.
context_list
=
[
context
]
def
get
(
self
,
attr
,
default
=
None
):
def
get
(
self
,
attr
,
default
=
None
):
attr
=
get_or_attr
(
self
.
context_list
,
attr
,
getattr
(
self
,
attr
,
default
))
attr
=
get_or_attr
(
self
.
context_list
,
attr
,
getattr
(
self
,
attr
,
default
))
...
@@ -64,6 +64,13 @@ class View(object):
...
@@ -64,6 +64,13 @@ class View(object):
return
re
.
sub
(
'[A-Z]'
,
repl
,
template_name
)[
1
:]
return
re
.
sub
(
'[A-Z]'
,
repl
,
template_name
)[
1
:]
def
_get_context
(
self
):
context
=
{}
for
item
in
self
.
context_list
:
if
hasattr
(
item
,
'keys'
)
and
hasattr
(
item
,
'__getitem__'
):
context
.
update
(
item
)
return
context
def
render
(
self
,
encoding
=
None
):
def
render
(
self
,
encoding
=
None
):
return
Template
(
self
.
get_template
(
self
.
template_name
),
self
)
.
render
(
encoding
=
encoding
)
return
Template
(
self
.
get_template
(
self
.
template_name
),
self
)
.
render
(
encoding
=
encoding
)
...
@@ -74,8 +81,14 @@ class View(object):
...
@@ -74,8 +81,14 @@ class View(object):
val
=
self
.
get
(
attr
,
None
)
val
=
self
.
get
(
attr
,
None
)
if
not
val
and
val
is
not
0
:
if
not
val
and
val
is
not
0
:
raise
KeyError
(
"
No such key '
%
s'.
"
%
attr
)
raise
KeyError
(
"
Key '
%
s' does not exist in View
"
%
attr
)
return
val
return
val
def
__getattr__
(
self
,
attr
):
if
attr
==
'context'
:
return
self
.
_get_context
()
raise
AttributeError
(
"Attribute '
%
s' does not exist in View"
%
attr
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
render
()
return
self
.
render
()
\ No newline at end of file
tests/test_view.py
View file @
4b589295
...
@@ -97,6 +97,12 @@ class TestView(unittest.TestCase):
...
@@ -97,6 +97,12 @@ class TestView(unittest.TestCase):
view
.
template
=
"{{#parent}}{{#children}}{{this}}{{/children}}{{/parent}}"
view
.
template
=
"{{#parent}}{{#children}}{{this}}{{/children}}{{/parent}}"
self
.
assertEquals
(
view
.
render
(),
'derp'
)
self
.
assertEquals
(
view
.
render
(),
'derp'
)
def
test_context_returns_a_flattened_dict
(
self
):
view
=
Simple
()
view
.
context_list
=
[{
'one'
:
'1'
},
{
'two'
:
'2'
},
object
()]
self
.
assertEqual
(
view
.
context
,
{
'one'
:
'1'
,
'two'
:
'2'
})
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment