예제 #1
0
 def get_relationships(self, with_package=None, type=None, active=True,
                       direction='both'):
     '''Returns relationships this package has.
     Keeps stored type/ordering (not from pov of self).'''
     assert direction in ('both', 'forward', 'reverse')
     if with_package:
         assert isinstance(with_package, Package)
     from ckan.model.package_relationship import PackageRelationship
     forward_filters = [PackageRelationship.subject==self]
     reverse_filters = [PackageRelationship.object==self]
     if with_package:
         forward_filters.append(PackageRelationship.object==with_package)
         reverse_filters.append(PackageRelationship.subject==with_package)
     if active:
         forward_filters.append(PackageRelationship.state==core.State.ACTIVE)
         reverse_filters.append(PackageRelationship.state==core.State.ACTIVE)
     if type:
         forward_filters.append(PackageRelationship.type==type)
         reverse_type = PackageRelationship.reverse_type(type)
         reverse_filters.append(PackageRelationship.type==reverse_type)
     q = meta.Session.query(PackageRelationship)
     if direction == 'both':
         q = q.filter(or_(
         and_(*forward_filters),
         and_(*reverse_filters),
         ))
     elif direction == 'forward':
         q = q.filter(and_(*forward_filters))
     elif direction == 'reverse':
         q = q.filter(and_(*reverse_filters))
     return q.all()
예제 #2
0
파일: package.py 프로젝트: wrinklenose/ckan
 def get_relationships_printable(self):
     '''Returns a list of tuples describing related packages, including
     non-direct relationships (such as siblings).
     @return: e.g. [(annakarenina, u"is a parent"), ...]
     '''
     from ckan.model.package_relationship import PackageRelationship
     rel_list = []
     for rel in self.get_relationships():
         if rel.subject == self:
             type_printable = PackageRelationship.make_type_printable(
                 rel.type)
             rel_list.append((rel.object, type_printable, rel.comment))
         else:
             type_printable = PackageRelationship.make_type_printable(\
                 PackageRelationship.forward_to_reverse_type(
                     rel.type)
                 )
             rel_list.append((rel.subject, type_printable, rel.comment))
     # sibling types
     # e.g. 'gary' is a child of 'mum', looking for 'bert' is a child of 'mum'
     # i.e. for each 'child_of' type relationship ...
     for rel_as_subject in self.get_relationships(direction='forward'):
         if rel_as_subject.state != core.State.ACTIVE:
             continue
         # ... parent is the object
         parent_pkg = rel_as_subject.object
         # Now look for the parent's other relationships as object ...
         for parent_rel_as_object in parent_pkg.get_relationships(
                 direction='reverse'):
             if parent_rel_as_object.state != core.State.ACTIVE:
                 continue
             # and check children
             child_pkg = parent_rel_as_object.subject
             if (child_pkg != self
                     and parent_rel_as_object.type == rel_as_subject.type
                     and child_pkg.state == core.State.ACTIVE):
                 type_printable = PackageRelationship.inferred_types_printable[
                     'sibling']
                 rel_list.append((child_pkg, type_printable, None))
     return sorted(list(set(rel_list)))