[Zope3-checkins] CVS: Products3/demo/smileyservice/browser - __init__.py:1.1 configure.zcml:1.1 overview.pt:1.1 smiley.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Fri Aug 22 18:27:35 EDT 2003


Update of /cvs-repository/Products3/demo/smileyservice/browser
In directory cvs.zope.org:/tmp/cvs-serv5789/smileyservice/browser

Added Files:
	__init__.py configure.zcml overview.pt smiley.py 
Log Message:
Initial checkin of the Smiley Service. :-)

This service registers smiley text representations and their corresponding
url to an image representation. This implements a global and a local 
version. In order to make the implementation more interesting, I also support
themes, which are basically collections of similar icons. (Thanks goes to
dreamcatcher, who gave me the idea.)

In the local version of the service I implemented Themes using the new 
named utility/service pattern.

Since the service is pretty much useless without being used, I also extended
the messageboard example by another step using this service. This also 
keeps a common theme is the Devel Cookbook, which is good (thanks to 
philliKON for the tip). The new step will follow soon. This also gives me
a good base for a vocabulary and vocabulary widget, which I assume will be-
come step 14.

Finally, this product was written for the Zope Devel Cookbook. It will be 
covered in the "New Registries via Services" and "Writing Local Services".


=== Added File Products3/demo/smileyservice/browser/__init__.py ===


=== Added File Products3/demo/smileyservice/browser/configure.zcml ===
<configure xmlns="http://namespaces.zope.org/browser">

<!-- Smiley Service -->

  <menuItem
      for="zope.app.interfaces.container.IAdding"
      menu="add_service"
      action="zope.services.SmileyService"
      title="Smiley Service" />

  <page
      name="overview.html"
      menu="zmi_views" title="Overview"
      for="zopeproducts.demo.smileyservice.interfaces.ISmileyService"
      permission="zope.ManageServices"
      class=".smiley.SmileyServiceOverview"
      template="overview.pt" />

  <defaultView
      for="zopeproducts.demo.smileyservice.interfaces.ISmileyService"
      name="overview.html" />

<!-- Smiley Menu -->

  <view
      name="+"
      for="zopeproducts.demo.smileyservice.interfaces.ITheme"
      permission="zope.ManageContent"
      class="zope.app.browser.container.adding.Adding" />

  <addform
      label="Add Smiley Theme (Registration)"
      for="zopeproducts.demo.smileyservice.interfaces.ITheme"
      name="addRegistration.html"
      schema="zope.app.interfaces.services.utility.IUtilityRegistration"
      class="zope.app.browser.services.utility.AddRegistration"
      permission="zope.ManageServices"
      content_factory="zope.app.services.utility.UtilityRegistration"
      arguments="name interface componentPath"
      set_after_add="status"
      fields="name interface componentPath permission status" />

  <page
       name="contents.html"
       menu="zmi_views" title="Contents"
       for="zopeproducts.demo.smileyservice.interfaces.ITheme"
       permission="zope.ManageServices"
       class="zope.app.browser.container.contents.Contents" 
       attribute="contents"/>

  <!-- Menu entry for "add component" menu -->
  <menuItem
      for="zope.app.interfaces.container.IAdding"
      menu="add_component"
      action="zope.services.SmileyService.Theme"
      title="Smiley Theme"
      description="A Smiley Theme"
      permission="zope.ManageServices" />

  <!-- Menu entry for "add utility" menu -->
  <menuItem
      for="zope.app.interfaces.container.IAdding"
      menu="add_utility"
      action="zope.services.SmileyService.Theme"
      title="Smiley Theme"
      description="A Smiley Theme"
      permission="zope.ManageServices" />

<!-- Smiley (just an Image) -->
  <menuItem
      menu="zmi_actions" title="Add Smiley"
      for="zopeproducts.demo.smileyservice.interfaces.ITheme"
      action="./contents.html?type_name=AddSmiley" />

  <addform
      name="AddSmiley"
      schema="zope.app.content.image.IImage"
      label="Add a Smiley"
      class="zope.app.browser.content.file.FileUpload"
      fields="data"
      content_factory="zope.app.content.image.Image"
      keyword_arguments="data"
      permission="zope.ManageServices" />

</configure>

=== Added File Products3/demo/smileyservice/browser/overview.pt ===
<html metal:use-macro="views/standard_macros/page">
<head>
  <title metal:fill-slot="title" i18n:translate="">Smiley Service</title>
</head>
<body>
<div metal:fill-slot="body">

  <h2>Local Smiley Themes</h2>
  <ul>
    <li tal:repeat="theme view/getLocalThemes">
      <b tal:content="theme/name" />
      <a href="" tal:attributes="href string:?expand=${theme/name}">Expand</a>
      <br />
      <div tal:condition="python: theme['name'] == view.request.get('expand')">
        <em>Local Smileys</em> 
        <ul>
          <li tal:repeat="smiley theme/local_smileys">
            <b tal:content="smiley/text"/> &#8594;
            <img src="" tal:attributes="src smiley/url"/>
          </li>          
        </ul>
        <em>Acquired Smileys</em>
        <ul>
          <li tal:repeat="smiley theme/acquired_smileys">
          <b tal:content="smiley/text"/> &#8594;
          <img src="" tal:attributes="src smiley/url"/>
          </li>          
        </ul>
      </div>
    </li>
  </ul>

  <h2>Acquired Smiley Themes</h2>
  <ul>
    <li tal:repeat="theme view/getAcquiredThemes">
      <b tal:content="theme/name" />
      <a href="" tal:attributes="href string:?expand=${theme/name}">Expand</a>
      <ul tal:condition="python: theme['name'] == view.request.get('expand')">
        <li tal:repeat="smiley theme/local_smileys">
          <b tal:content="smiley/text"/> &#8594;
          <img src="" tal:attributes="src smiley/url"/>
        </li>
        <li tal:repeat="smiley theme/acquired_smileys">
          <b tal:content="smiley/text"/> &#8594;
          <img src="" tal:attributes="src smiley/url"/>
        </li>
      </ul>
    </li>
  </ul>


</div>
</body>
</html>

=== Added File Products3/demo/smileyservice/browser/smiley.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Local Smiley Service Views

$Id: smiley.py,v 1.1 2003/08/22 21:27:34 srichter Exp $
"""

from zope.app import zapi
from zope.app.component.nextservice import queryNextService
from zopeproducts.demo.smileyservice.interfaces import ITheme
from zope.app.services.servicenames import Utilities
from zope.security.proxy import trustedRemoveSecurityProxy

class SmileyServiceOverview:        

    def getLocalThemes(self):
        themes_info = []
        utilities = zapi.getService(self.context, Utilities)
        for name, theme in utilities.getLocalUtilitiesFor(ITheme):
            themes_info.append(self._getInfoFromTheme(name, theme))
        return themes_info


    def getAcquiredThemes(self):
        themes = []

        service = zapi.getService(self.context, 'Smileys')
        local_themes = service.getLocalThemes() 

        utilities = queryNextService(self.context, Utilities)
        for name, theme in utilities.getUtilitiesFor(ITheme):
            if name not in local_themes:
                themes.append(self._getInfoFromTheme(name, theme))
        # Global Smiley Service
        service = zapi.getService(None, 'Smileys')
        for name in service.getThemes():
            if name not in local_themes:
                themes.append(self._getInfoFromTheme(
                    name, service.getSmileysMapping(self.request, name)))
        return themes
            

    def _getInfoFromTheme(self, name, theme):
        info = {}
        info['name'] = name
        info['local_smileys'] = self._getSmileyInfo(theme)

        next = queryNextService(theme, 'Smileys')
        if next is not None:
            try:
                acquired_theme = next.queryTheme(name, onlyLocal=True)
            except AttributeError:
                # We deal with a global smiley service
                acquired_theme = next.getSmileysMapping(self.request, name)

            localSmileyTexts = self.context.queryTheme(name, True, {}).keys()
            info['acquired_smileys'] = self._getSmileyInfo(
                acquired_theme, localSmileyTexts)
        else:
            info['acquired_smileys'] = None
            
        return info


    def _getSmileyInfo(self, theme, localSmileyTexts=()):
        smiley_items = []

        if ITheme.isImplementedBy(theme):
            smileys = theme.getSmileysMapping(self.request)
        else:
            smileys = theme

        for text, url in smileys.items():
            if text not in localSmileyTexts:
                smiley_items.append({'text': text, 'url': url})

        return smiley_items




More information about the Zope3-Checkins mailing list