Changeset 854:b98ce3951f0a
- Timestamp:
- 03/09/10 19:14:55 (6 months ago)
- Branch:
- default
- Location:
- ckan
- Files:
-
- 1 added
- 4 modified
-
lib/package_saver.py (modified) (4 diffs)
-
model/core.py (modified) (2 diffs)
-
model/package_relationship.py (modified) (3 diffs)
-
templates/package/read_core.html (modified) (1 diff)
-
tests/functional/test_package_relationships.py (added)
Legend:
- Unmodified
- Added
- Removed
-
ckan/lib/package_saver.py
r824 r854 17 17 log_message=None, 18 18 author=None): 19 'Renders a package on the basis of a fieldset - perfect for preview' 19 '''Renders a package on the basis of a fieldset - perfect for 20 preview of form data. 21 Note that the actual calling of render('package/read') is left 22 to the caller.''' 20 23 pkg = cls._preview_pkg(fs, original_name, record_id, 21 24 log_message, author) … … 25 28 @classmethod 26 29 def render_package(cls, pkg): 27 'Renders a package' 30 '''Prepares for rendering a package. Takes a Package object and 31 formats it for the various context variables required to call 32 render. 33 Note that the actual calling of render('package/read') is left 34 to the caller.''' 28 35 c.pkg = pkg 29 36 notes_formatted = ckan.misc.MarkdownFormat().to_html(pkg.notes) … … 33 40 c.pkg_author_link = cls._person_email_link(c.pkg.author, c.pkg.author_email, "Author") 34 41 c.pkg_maintainer_link = cls._person_email_link(c.pkg.maintainer, c.pkg.maintainer_email, "Maintainer") 42 c.package_relationships = pkg.relationships_printable() 35 43 # c.auth_for_change_state and c.auth_for_edit may also used 36 44 # return render('package/read') … … 57 65 fs.model.groups 58 66 fs.model.ratings 67 fs.model.relationships_as_subject 68 fs.model.relationships_as_object 59 69 except ValidationException, e: 60 70 # remove everything from session so nothing can get saved accidentally -
ckan/model/core.py
r827 r854 207 207 else: 208 208 raise NotImplementedError, 'Package relationship type: %r' % type_ 209 210 209 211 210 rel = package_relationship.PackageRelationship( 212 211 subject=subject, … … 219 218 def relationships(self): 220 219 return self.relationships_as_subject + self.relationships_as_object 220 221 def relationships_printable(self): 222 '''Returns a list of tuples describing related packages, including 223 non-direct relationships (such as siblings). 224 @return: e.g. [(annakarenina, u"is a parent"), ...] 225 ''' 226 from package_relationship import PackageRelationship 227 rel_list = [] 228 # forward types 229 for rel_as_subject in self.relationships_as_subject: 230 type_printable = PackageRelationship.make_type_printable(rel_as_subject.type) 231 rel_list.append((rel_as_subject.object, type_printable)) 232 # reverse types 233 for rel_as_object in self.relationships_as_object: 234 type_printable = PackageRelationship.make_type_printable(\ 235 PackageRelationship.forward_to_reverse_type( 236 rel_as_object.type) 237 ) 238 rel_list.append((rel_as_object.subject, type_printable)) 239 # sibling types 240 # e.g. 'gary' is a child of 'mum', looking for 'bert' is a child of 'mum' 241 # i.e. for each 'child_of' type relationship ... 242 for rel_as_subject in self.relationships_as_subject: 243 # ... parent is the object 244 parent_pkg = rel_as_subject.object 245 # Now look for the parent's other relationships as object ... 246 for parent_rel_as_object in parent_pkg.relationships_as_object: 247 # and check children 248 child_pkg = parent_rel_as_object.subject 249 if child_pkg != self and \ 250 parent_rel_as_object.type == rel_as_subject.type: 251 type_printable = PackageRelationship.inferred_types_printable['sibling'] 252 rel_list.append((child_pkg, type_printable)) 253 return rel_list 221 254 222 255 class Tag(DomainObject): -
ckan/model/package_relationship.py
r811 r854 4 4 from core import DomainObject, Package 5 5 from types import make_uuid 6 7 # i18n only works when this is run as part of pylons, 8 # which isn't the case for paster commands. 9 try: 10 from pylons.i18n import _ 11 _('') 12 except: 13 def _(txt): 14 return txt 15 6 16 7 17 package_relationship_table = Table('package_relationship', metadata, … … 17 27 class PackageRelationship(vdm.sqlalchemy.RevisionedObjectMixin, 18 28 DomainObject): 29 # List of (type, corresponding_reverse_type) 30 # e.g. (A "depends_on" B, B has a "dependency_of" A) 19 31 types = [(u'depends_on', u'dependency_of'), 20 32 (u'derives_from', u'has_derivation'), 21 33 (u'child_of', u'parent_of'), 22 34 ] 35 36 types_printable = \ 37 [(_(u'depends on %s'), _(u'is a dependency of %s')), 38 (_(u'derives from %s'), _(u'has derivation %s')), 39 (_(u'is a child of %s'), _(u'is a parent of %s')), 40 ] 41 42 inferred_types_printable = \ 43 {'sibling':_('has sibling %s')} 23 44 24 45 @classmethod … … 43 64 if rev == reverse_type: 44 65 return fwd 45 66 67 @classmethod 68 def forward_to_reverse_type(self, forward_type): 69 for fwd, rev in self.types: 70 if fwd == forward_type: 71 return rev 72 73 @classmethod 74 def make_type_printable(self, type_): 75 for i, types in enumerate(self.types): 76 for j in range(2): 77 if type_ == types[j]: 78 return self.types_printable[i][j] 79 raise TypeError, type_ 80 46 81 mapper(PackageRelationship, package_relationship_table, properties={ 47 82 'subject':relation(Package, primaryjoin=\ -
ckan/templates/package/read_core.html
r657 r854 29 29 <dd>${"Resources available for download" if c.pkg.resources else "No resources given"}</dd> 30 30 </dl> 31 <py:if test="c.package_relationships"> 32 <h3>Related packages</h3> 33 <ul> 34 <py:for each="pkg, relationship_str in c.package_relationships"> 35 <li>${h.literal(relationship_str % (h.link_to(pkg.name, h.url_for(controller="package", action="read", id=pkg.name))))}</li> 36 </py:for> 37 </ul> 38 </py:if> 31 39 </div> 32 40