Random ...
 
December 2017
S M T W T F S
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Tags ...
Links



python+zope++:: Seeding tag clouds with Zope
Posted at 08.Sep,2006 15:05  Comments 0 /
Technorati tag(s):

Tag clouds are the in thing nowadays. COREBlog has no tag cloud capabilities and I wanted that for my site too, so I lifted off the tag codes from Bitakora.

This trick/hack can also be used to seed tag clouds for any cataloged zope objects.

We must have Catalog with at least one Keyword Index field. For COREBlog Entry, I use subtitle since there's no tag field.

We must have four Script (Python) lifted off Bitakora, tag, tag_all_html, tag_html and tagsAndPixels

The tag script is for displaying all objects found in the catalog with the tag search; it will call either tag_all_html or tag_html:

 ##id = tag
 request=context.REQUEST
 ts = request['traverse_subpath']

 if len(ts)==0:
    return context.tag_all_html(context, request)
 else:
    request.set('tagid',ts[0])
    return context.tag_html(context, request)

The tag_all_html is for displaying our tag clouds:

    <dtml-in "tagsAndPixels().items()">
    <a href="/blogs/kedai/tag/<dtml-var sequence-key>"  style="font-size:<dtml-var sequence-item>em;" class="PopularTag"><dtml-var sequence-key></a>&nbsp;
    </dtml-in>

You have to replace the necessary URL (in this case /blogs/kedai) depending on your setup.

The tag_html is for displaying the tags set:

 <dtml-if "REQUEST.has_key('tagid')">

 <dtml-in "searchResults(subtitle=tagid,meta_type='COREBlog Entry', sort_order='reverse')" >
 <dtml-if sequence-start>
    <b>Entries with tag: <em><dtml-var tagid></em></b>
 </dtml-if>
 <br>&bull; <a href="<dtml-var getURL>"><dtml-var title></a> 

 <dtml-else>
    <p>There are no posts tagged <em><dtml-var tagid></em></p>
 </dtml-in>

 <dtml-else>

 <h3>Tag</h3>

 <p>

 <dtml-let pxk="tagsAndPixels()"
          pxks="pxk.keys()">
 <dtml-call "pxks.sort()">

 <dtml-in "pxks">
 <a href="/blogs/kedai/tag/<dtml-var sequence-item>" style="font-size:<dtml-var "pxk[_['sequence-item']]">px;" ><dtml-var sequence-item></a>&nbsp;
 </dtml-in>
 </dtml-let>
 </p>
 </dtml-if>

Since COREBlog subclasses ZCatalog, I don't need to specifically call any Catalog. So, if you do need some catalog, add your Catalog instance. Usually one just need to:

     <dtml-in "path.to.Catalog.searchResults(subtitle=tagid,
      meta_type='COREBlog Entry', sort_order='reverse')" >

And replace meta_type with your object meta_type.

The tagsAndPixel script is the one that processes all the keywords found in the Catalog:

  tags = [tag for tag in context.uniqueValuesFor('subtitle') if tag.strip()]
 zenbat = {}
 for tag in tags:

   tagkop = context.searchResults(subtitle=tag,  meta_type='COREBlog Entry')
    zenbat[tag] = len(tagkop)

 maxpx = 4.20 
 minpx = 0.80
 difpx = maxpx-minpx

 if zenbat.values():
    maxnum = max(zenbat.values())
    minnum = max(zenbat.values())

 else:
    maxnum = 0
    minnum = 0

 hiz = {}
 for k,v in zenbat.items():
    hiz[k] = float(difpx*v/maxnum)+minpx

 return hiz
 #return printed

Replace subtitle with your KeywordIndex field.

The note re Catalog also applies here. Usually one just need to:

  cat = getattr(context, 'Catalog')

and replace all occurrence of context to cat.

Play around with maxnum and minnum for your tag cloud.

See the right column for what I've done for my-zope.

Have fun!


Bookmark and Share

Is this entry helpful? Comments/Donate/Click some google ads.  
Trackback is http://myzope.kedai.com.my/blogs/kedai/100/tbping 

Comments
Post a comment