Changeset 854:b98ce3951f0a

Show
Ignore:
Timestamp:
03/09/10 19:14:55 (6 months ago)
Author:
dread
Branch:
default
Message:

[wui][model]: (ticket:255) Display package relationships on package read.

Location:
ckan
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • ckan/lib/package_saver.py

    r824 r854  
    1717                       log_message=None, 
    1818                       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.''' 
    2023        pkg = cls._preview_pkg(fs, original_name, record_id, 
    2124                               log_message, author) 
     
    2528    @classmethod 
    2629    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.''' 
    2835        c.pkg = pkg 
    2936        notes_formatted = ckan.misc.MarkdownFormat().to_html(pkg.notes) 
     
    3340        c.pkg_author_link = cls._person_email_link(c.pkg.author, c.pkg.author_email, "Author") 
    3441        c.pkg_maintainer_link = cls._person_email_link(c.pkg.maintainer, c.pkg.maintainer_email, "Maintainer") 
     42        c.package_relationships = pkg.relationships_printable() 
    3543        # c.auth_for_change_state and c.auth_for_edit may also used 
    3644        # return render('package/read') 
     
    5765            fs.model.groups 
    5866            fs.model.ratings 
     67            fs.model.relationships_as_subject 
     68            fs.model.relationships_as_object 
    5969        except ValidationException, e: 
    6070            # remove everything from session so nothing can get saved accidentally 
  • ckan/model/core.py

    r827 r854  
    207207        else: 
    208208            raise NotImplementedError, 'Package relationship type: %r' % type_ 
    209              
    210              
     209 
    211210        rel = package_relationship.PackageRelationship( 
    212211            subject=subject, 
     
    219218    def relationships(self): 
    220219        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 
    221254 
    222255class Tag(DomainObject): 
  • ckan/model/package_relationship.py

    r811 r854  
    44from core import DomainObject, Package 
    55from 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. 
     9try: 
     10    from pylons.i18n import _ 
     11    _('') 
     12except: 
     13    def _(txt): 
     14        return txt 
     15 
    616 
    717package_relationship_table = Table('package_relationship', metadata, 
     
    1727class PackageRelationship(vdm.sqlalchemy.RevisionedObjectMixin, 
    1828                          DomainObject): 
     29    # List of (type, corresponding_reverse_type) 
     30    # e.g. (A "depends_on" B, B has a "dependency_of" A) 
    1931    types = [(u'depends_on', u'dependency_of'), 
    2032             (u'derives_from', u'has_derivation'), 
    2133             (u'child_of', u'parent_of'), 
    2234             ] 
     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')} 
    2344 
    2445    @classmethod 
     
    4364            if rev == reverse_type: 
    4465                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 
    4681mapper(PackageRelationship, package_relationship_table, properties={ 
    4782    'subject':relation(Package, primaryjoin=\ 
  • ckan/templates/package/read_core.html

    r657 r854  
    2929        <dd>${"Resources available for download" if c.pkg.resources else "No resources given"}</dd> 
    3030      </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> 
    3139    </div> 
    3240 

0.9.0.3 © 2008-2010 agile42 all rights reserved (this page was served in: 0.579573 sec.)