Changeset 1035 in products


Ignore:
Timestamp:
Apr 15, 2009 2:23:56 PM (15 years ago)
Author:
koval
Message:

portlet now has configurable parameters and tags are queried from catalog

Location:
quintagroup.portlet.cumulus/trunk/quintagroup/portlet/cumulus
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.portlet.cumulus/trunk/quintagroup/portlet/cumulus/configure.zcml

    r1003 r1035  
    2020 
    2121    <!-- Register the portlet --> 
    22  
    23     <!-- If the portlet has no configurable parameters, you can remove 
    24         the EditForm declaration in cumulusportlet.py and delete 
    25         the 'editview' attribute from this statement. 
    26     --> 
    2722 
    2823    <plone:portlet 
  • quintagroup.portlet.cumulus/trunk/quintagroup/portlet/cumulus/cumulusportlet.pt

    r1003 r1035  
    22    i18n:domain="quintagroup.portlet.cumulus"> 
    33 
    4     <dt class="portletHeader" style="border-top: none;"> 
    5 <!--        <span class="portletTopLeft"></span> 
     4    <dt class="portletHeader"> 
     5        <span class="portletTopLeft"></span> 
    66        Header 
    7         <span class="portletTopRight"></span>--> 
     7        <span class="portletTopRight"></span> 
    88    </dt> 
    99 
    1010    <dd class="portletItem odd"> 
    1111        <script type="text/javascript" src="swfobject.js" 
    12             tal:attributes="src string:${context/absolute_url}/++resource++swfobject.js"> 
     12            tal:attributes="src string:${view/portal_url}/++resource++swfobject.js"> 
    1313        </script> 
    1414        <div id="comulus"> 
    1515        </div> 
    16  
    1716        <script type="text/javascript" tal:replace="structure view/getScript"> 
    1817        </script> 
    1918    </dd> 
    2019 
    21 <!--    <dd class="portletFooter"> 
     20    <dd class="portletFooter"> 
    2221        <span class="portletBottomLeft"></span> 
    2322        Footer 
    2423        <span class="portletBottomRight"></span> 
    25     </dd>--> 
     24    </dd> 
    2625 
    2726</dl> 
  • quintagroup.portlet.cumulus/trunk/quintagroup/portlet/cumulus/cumulusportlet.py

    r1003 r1035  
    1 import cgi 
     1import urllib 
    22 
    33from zope.interface import implements 
     4from zope.component import getMultiAdapter 
    45 
    56from plone.portlets.interfaces import IPortletDataProvider 
     
    1415 
    1516class ICumulusPortlet(IPortletDataProvider): 
    16     """A portlet 
    17  
    18     It inherits from IPortletDataProvider because for this portlet, the 
    19     data that is being rendered and the portlet assignment itself are the 
    20     same. 
    21     """ 
    22  
    23     # TODO: Add any zope.schema fields here to capture portlet configuration 
    24     # information. Alternatively, if there are no settings, leave this as an 
    25     # empty interface - see also notes around the add form and edit form 
    26     # below. 
    27  
    28     # some_field = schema.TextLine(title=_(u"Some field"), 
    29     #                              description=_(u"A field to use"), 
    30     #                              required=True) 
    31  
     17    """ A cumulus tag cloud portlet. 
     18    """ 
     19    # options for generating <embed ... /> tag 
     20    width = schema.Int( 
     21        title=_(u'Width of the Flash tag cloud'), 
     22        description=_(u'Width in pixels (500 or more is recommended).'), 
     23        required=True, 
     24        default=550) 
     25 
     26    height = schema.Int( 
     27        title=_(u'Height of the Flash tag cloud'), 
     28        description=_(u'Height in pixels (ideally around 3/4 of the width).'), 
     29        required=True, 
     30        default=375) 
     31 
     32    tcolor = schema.TextLine( 
     33        title=_(u'Color of the tags'), 
     34        description=_(u'This and next 3 fields should be 6 character hex color values without the # prefix (000000 for black, ffffff for white).'), 
     35        required=True, 
     36        default=u'ffffff') 
     37 
     38    tcolor2 = schema.TextLine( 
     39        title=_(u'Optional second color for gradient'), 
     40        description=_(u'When this color is available, each tag\'s color will be from a gradient between the two. This allows you to create a multi-colored tag cloud.'), 
     41        required=False, 
     42        default=u'ffffff') 
     43 
     44    hicolor = schema.TextLine( 
     45        title=_(u'Optional highlight color'), 
     46        description=_(u'Color of the tag when mouse is over it.'), 
     47        required=False, 
     48        default=u'ffffff') 
     49 
     50    bgcolor = schema.TextLine( 
     51        title=_(u'Background color'), 
     52        description=_(u'The hex value for the background color you\'d like to use. This options has no effect when \'Use transparent mode\' is selected.'), 
     53        required=True, 
     54        default=u'333333') 
     55 
     56    trans = schema.Bool( 
     57        title=_(u'Use transparent mode'), 
     58        description=_(u'Switches on Flash\'s wmode-transparent setting.'), 
     59        required=True, 
     60        default=False) 
     61 
     62    speed = schema.Int( 
     63        title=_(u'Rotation speed'), 
     64        description=_(u'Speed of the sphere. Options between 25 and 500 work best.'), 
     65        required=True, 
     66        default=100) 
     67 
     68    distr = schema.Bool( 
     69        title=_(u'Distribute tags evenly on sphere'), 
     70        description=_(u'When enabled, the movie will attempt to distribute the tags evenly over the surface of the sphere.'), 
     71        required=True, 
     72        default=True) 
     73 
     74    compmode = schema.Bool( 
     75        title=_(u'Use compatibility mode?'), 
     76        description=_(u'Enabling this option switches the plugin to a different way of embedding Flash into the page. Use this if your page has markup errors or if you\'re having trouble getting tag cloud to display correctly.'), 
     77        required=True, 
     78        default=False) 
     79 
     80    # options for generating tag cloud data 
     81    smallest = schema.Int( 
     82        title=_(u'Smallest tag size'), 
     83        description=_(u'The text size of the tag with the smallest count value (units given by unit parameter).'), 
     84        required=True, 
     85        default=8) 
     86 
     87    largest = schema.Int( 
     88        title=_(u'Largest tag size'), 
     89        description=_(u'The text size of the tag with the highest count value (units given by the unit parameter).'), 
     90        required=True, 
     91        default=22) 
     92 
     93    unit = schema.TextLine( 
     94        title=_(u'Unit of measure'), 
     95        description=_(u'Unit of measure as pertains to the smallest and largest values. This can be any CSS length value, e.g. pt, px, em, %.'), 
     96        required=True, 
     97        default=u'pt') 
    3298 
    3399class Assignment(base.Assignment): 
     
    40106    implements(ICumulusPortlet) 
    41107 
    42     # TODO: Set default values for the configurable parameters here 
    43  
    44     # some_field = u"" 
    45  
    46     # TODO: Add keyword parameters for configurable parameters here 
    47     # def __init__(self, some_field=u""): 
    48     #    self.some_field = some_field 
    49  
    50     def __init__(self): 
    51         pass 
     108    width    = 550; 
     109    height   = 375; 
     110    tcolor   = u'ffffff' 
     111    tcolor2  = u'ffffff' 
     112    hicolor  = u'ffffff' 
     113    bgcolor  = u'333333' 
     114    speed    = 100 
     115    trans    = False 
     116    distr    = True 
     117    compmode = False 
     118 
     119    smallest = 8 
     120    largest  = 22 
     121    unit     = u'pt' 
     122 
     123    def __init__(self, **kw): 
     124        for k, v in kw.items(): 
     125            setattr(self, k, v) 
    52126 
    53127    @property 
     
    56130        "manage portlets" screen. 
    57131        """ 
    58         return "Cumulus portlet" 
     132        return _("Cumulus portlet") 
    59133 
    60134 
    61135class Renderer(base.Renderer): 
    62136    """Portlet renderer. 
    63  
    64     This is registered in configure.zcml. The referenced page template is 
    65     rendered, and the implicit variable 'view' will refer to an instance 
    66     of this class. Other methods can be added and referenced in the template. 
    67137    """ 
    68138 
    69139    render = ViewPageTemplateFile('cumulusportlet.pt') 
    70140 
    71     def getTags(self): 
     141    def __init__(self, context, request, view, manager, data): 
     142        base.Renderer.__init__(self, context, request, view, manager, data) 
     143        portal_state = getMultiAdapter((context, request), name=u'plone_portal_state') 
     144        self.portal_url = portal_state.portal_url() 
     145 
     146    def getScript(self): 
     147        params = { 
     148            'url': self.portal_url + '/++resource++tagcloud.swf', 
     149            'tagcloud': self.getTagCloud(), 
     150            'width': self.data.width, 
     151            'height': self.data.height, 
     152            'tcolor': self.data.tcolor, 
     153            'tcolor2': self.data.tcolor2 or self.data.tcolor, 
     154            'hicolor': self.data.hicolor or self.data.tcolor, 
     155            'bgcolor': self.data.bgcolor, 
     156            'speed': self.data.speed, 
     157            'trans': self.data.trans and 'so.addParam("wmode", "transparent");' or '', 
     158            'distr': self.data.distr and 'true' or 'false', 
     159        } 
     160        return """<script type="text/javascript"> 
     161            var so = new SWFObject("%(url)s", "tagcloudflash", "%(width)s", "%(height)s", "9", "#%(bgcolor)s"); 
     162            %(trans)s 
     163            so.addParam("allowScriptAccess", "always"); 
     164            so.addVariable("tcolor", "0x%(tcolor)s"); 
     165            so.addVariable("tcolor2", "0x%(tcolor2)s"); 
     166            so.addVariable("hicolor", "0x%(hicolor)s"); 
     167            so.addVariable("tspeed", "%(speed)s"); 
     168            so.addVariable("distr", "%(distr)s"); 
     169            so.addVariable("mode", "tags"); 
     170            so.addVariable("tagcloud", "%(tagcloud)s"); 
     171            so.write("comulus"); 
     172        </script>""" % params 
     173 
     174    def getTagCloud(self): 
    72175        tags = '<tags>' 
    73         for i in range(10): 
    74             tags += "<a href='#' style='10'>test%d</a>" % i 
     176        for tag in self.getTags(): 
     177            tags += '<a href="%s" title="%s entries" style="font-size: %.1f%s;">%s</a>' % \ 
     178                (tag['url'], tag['number_of_entries'], tag['size'], self.data.unit, tag['name']) 
    75179        tags += '</tags>' 
    76         return cgi.escape(tags) 
    77  
    78     def getScript(self): 
    79         tags = self.getTags() 
    80         return """<script type="text/javascript"> 
    81             var so = new SWFObject("++resource++tagcloud.swf", "tagcloud", "200", "200", "7", "#ffffff"); 
    82             so.addParam("wmode", "transparent"); 
    83             so.addVariable("tcolor", "0x333333"); 
    84             so.addVariable("mode", "tags"); 
    85             so.addVariable("distr", "true"); 
    86             so.addVariable("tspeed", "100"); 
    87             so.addVariable("tagcloud", "%s"); 
    88             so.write("comulus"); 
    89         </script>""" % tags 
     180        return urllib.quote(tags) 
     181 
     182    def getTags(self, settings=None): 
     183        plone_tools = getMultiAdapter((self.context, self.request), name=u'plone_tools') 
     184        cat = plone_tools.catalog() 
     185        index = cat._catalog.getIndex('Subject') 
     186        tags = [] 
     187        number_of_entries = [] 
     188        for name in index._index.keys(): 
     189            tags.append(name) 
     190            try: 
     191                number_of_entries.append(len(index._index[name])) 
     192            except TypeError: 
     193                number_of_entries.append(1) 
     194        min_number = min(number_of_entries) 
     195        max_number = max(number_of_entries) 
     196        distance = float(max_number - min_number) or 1 
     197        step = (self.data.largest - self.data.smallest) / distance 
     198 
     199        result = [] 
     200        for name, number in zip(tags, number_of_entries): 
     201            size = self.data.smallest + step * (number - min_number) 
     202            result.append({ 
     203                'name': name, 
     204                'size': size, 
     205                'number_of_entries': number, 
     206                'url': '#' 
     207            }) 
     208        return result 
    90209 
    91210class AddForm(base.AddForm): 
     
    101220        return Assignment(**data) 
    102221 
    103  
    104 # NOTE: If this portlet does not have any configurable parameters, you 
    105 # can use the next AddForm implementation instead of the previous. 
    106  
    107 # class AddForm(base.NullAddForm): 
    108 #     """Portlet add form. 
    109 #     """ 
    110 #     def create(self): 
    111 #         return Assignment() 
    112  
    113  
    114 # NOTE: If this portlet does not have any configurable parameters, you 
    115 # can remove the EditForm class definition and delete the editview 
    116 # attribute from the <plone:portlet /> registration in configure.zcml 
    117  
    118  
    119222class EditForm(base.EditForm): 
    120223    """Portlet edit form. 
Note: See TracChangeset for help on using the changeset viewer.