Browse Source

Added ability to get the group associated with the resource

main
Lakshmi Vyasarajan 14 years ago
parent
commit
3bcd2904c2
3 changed files with 41 additions and 2 deletions
  1. +22
    -1
      hyde/ext/plugins/grouper.py
  2. +13
    -0
      hyde/tests/ext/test_grouper.py
  3. +6
    -1
      hyde/util.py

+ 22
- 1
hyde/ext/plugins/grouper.py View File

@@ -7,7 +7,7 @@ import re
from hyde.model import Expando from hyde.model import Expando
from hyde.plugin import Plugin from hyde.plugin import Plugin
from hyde.site import Node, Resource from hyde.site import Node, Resource
from hyde.util import add_method, pairwalk
from hyde.util import add_method, add_property, pairwalk


from collections import namedtuple from collections import namedtuple
from functools import partial from functools import partial
@@ -42,6 +42,10 @@ class Group(Expando):
'walk_resources_grouped_by_%s' % self.name, 'walk_resources_grouped_by_%s' % self.name,
Group.walk_resources, Group.walk_resources,
group=self) group=self)
add_property(Resource,
'%s_group' % self.name,
Group.get_resource_group,
group=self)
add_method(Resource, add_method(Resource,
'walk_%s_groups' % self.name, 'walk_%s_groups' % self.name,
Group.walk_resource_groups, Group.walk_resource_groups,
@@ -57,6 +61,23 @@ class Group(Expando):
else: else:
return super(Group, self).set_expando(key, value) return super(Group, self).set_expando(key, value)


@staticmethod
def get_resource_group(resource, group):
"""
This method gets attached to the resource object.
Returns group and its ancestors that the resource
belongs to, in that order.
"""
try:
group_name = getattr(resource.meta, group.root.name)
except AttributeError:
group_name = None

return next((g for g in group.walk_groups()
if g.name == group_name), None) \
if group_name \
else None

@staticmethod @staticmethod
def walk_resource_groups(resource, group): def walk_resource_groups(resource, group):
""" """


+ 13
- 0
hyde/tests/ext/test_grouper.py View File

@@ -130,6 +130,19 @@ class TestGrouperSingleLevel(object):
plugin_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_plugins()] plugin_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_plugins()]
assert plugin_resources == self.plugins assert plugin_resources == self.plugins


def test_resource_group(self):

groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])

for name, group in groups.items():
pages = getattr(self, name)
for page in pages:
res = self.s.content.resource_from_relative_path('blog/' + page)
assert hasattr(res, 'section_group')
res_group = getattr(res, 'section_group')
print "%s, %s=%s" % (page, group.name, res_group.name)
assert res_group == group

def test_resource_belongs_to(self): def test_resource_belongs_to(self):


groups = dict([(g.name, g) for g in self.s.grouper['section'].groups]) groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])


+ 6
- 1
hyde/util.py View File

@@ -92,11 +92,16 @@ def make_method(method_name, method_):
method__.__name__ = method_name method__.__name__ = method_name
return method__ return method__


def add_property(obj, method_name, method_, *args, **kwargs):
from functools import partial
m = make_method(method_name, partial(method_, *args, **kwargs))
setattr(obj, method_name, property(m))

def add_method(obj, method_name, method_, *args, **kwargs): def add_method(obj, method_name, method_, *args, **kwargs):
from functools import partial from functools import partial
m = make_method(method_name, partial(method_, *args, **kwargs)) m = make_method(method_name, partial(method_, *args, **kwargs))
setattr(obj, method_name, m) setattr(obj, method_name, m)
def pairwalk(iterable): def pairwalk(iterable):
a, b = tee(iterable) a, b = tee(iterable)
next(b, None) next(b, None)

Loading…
Cancel
Save