Alessandro Molina avatar Alessandro Molina committed ee59272 Draft

Compatibility with TG2.2 and TW2, also expose pluggables interface

Comments (0)

Files changed (8)

tgext/tagging/__init__.py

 from model import setup_model
 from widgets import TagList, TagCloud
-from controllers import TaggingController
+from controllers import TaggingController
+
+try:
+    #This is here to make TW2 serve the image
+    from tw2.core import Link
+    register_delete_image = Link(modname='tgext.tagging', filename='static/delete.png')
+except ImportError:
+    pass
+
+def plugme(app_config, options):
+    app_models = dict(DBSession=app_config.model.DBSession,
+                      DeclarativeBase=app_config.model.DeclarativeBase,
+                      metadata=app_config.model.metadata,
+                      User=app_config.model.User)
+    app_config.model.Tag, app_config.model.Tagging = setup_model(app_models)
+
+    return dict(appid='tgext.tagging', plug_controller=False)

tgext/tagging/controllers/controller.py

     pos = request.path.rfind(method)-1
     return request.path[:pos]
 
+Tagging = None
+
 class TaggingController(TGController):
     def __init__(self, session, model, allow_edit=None):
         super(TaggingController, self).__init__()
 
     @expose(content_type='text/plain')
     def add(self, target_id, tags, *args, **kw):
-        from tgext.tagging.model.tagging import Tagging
+        global Tagging
+        if Tagging is None:
+            from tgext.tagging.model.tagging import Tagging
 
         if not can_edit(self.allow_edit):
             return 'DENIED'
 
     @expose(content_type='text/plain')
     def remove(self, target_id, tags, *args, **kw):
-        from tgext.tagging.model.tagging import Tagging
+        global Tagging
+        if Tagging is None:
+            from tgext.tagging.model.tagging import Tagging
 
         if not can_edit(self.allow_edit):
             return 'DENIED'
         Tagging.del_tags(obj, tags)
         return 'OK'
 
-    @expose('genshi:tgext.tagging.templates.taglist')
+    @expose('genshi:tgext.tagging.templates.tags')
     def tags(self, target_id):
-        from tgext.tagging.model.tagging import Tagging
+        global Tagging
+        if Tagging is None:
+            from tgext.tagging.model.tagging import Tagging
 
         obj = self.session.query(self.model).get(target_id)
         value = {}
 
         editmode = can_edit(self.allow_edit)
         base_url = get_tagging_base_url(request, 'tags')
-        return dict(value=value, tagging_url=base_url, editmode=editmode, tg_url=url)
+        return dict(value=value, tagging_url=base_url, editmode=editmode)
 
     @expose('genshi:tgext.tagging.templates.search')
     def search(self, tags):
-        from tgext.tagging.model.tagging import Tagging
+        global Tagging
+        if Tagging is None:
+            from tgext.tagging.model.tagging import Tagging
 
         items = Tagging.items_for_tags(self.model, tags)
         return dict(items=items, tags=tags)

tgext/tagging/templates/tagcloud.html

 <div xmlns:py="http://genshi.edgewall.org/"
-     class="tagging_tags_cloud ${css_class}"
-     id="${id}">
+     class="tagging_tags_cloud ${w.css_class}"
+     id="${w.compound_id}">
     <ul class="tagging_tags">
-        <li py:for="tag in value">
-            <a class="tagging_tag tagging_tag_${(6 * (tag[1] - weight_min)) / (weight_range)}"
+        <li py:for="tag in w.value">
+            <a class="tagging_tag tagging_tag_${(6 * (tag[1] - w.weight_min)) / (w.weight_range)}"
                title="${tag[0]}"
-               href="${tg_url('%s/search' % tagging_url, params=dict(tags=tag[0]))}">
+               href="${w.tg_url('%s/search' % w.tagging_url, params=dict(tags=tag[0]))}">
                 ${Markup(tag[0])}
             </a>
         </li>

tgext/tagging/templates/taglist.html

 <div xmlns:py="http://genshi.edgewall.org/"
-     class="tagging_tags_block ${css_class}"
-     id="${id}">
+     class="tagging_tags_block ${w.css_class}"
+     id="${w.compound_id}">
     <ul class="tagging_tags">
-        <li py:for="tag in value['tag_list']">
-            <a py:if="editmode" class="tagging_tag_delete" href="#"
-               onclick="return tagging_delete_tag(this, '${tagging_url}', '${value['target_id']}', '${tag[0]}');">
+        <li py:for="tag in w.value['tag_list']">
+            <a py:if="w.editmode" class="tagging_tag_delete" href="#"
+               onclick="return tagging_delete_tag(this, '${w.tagging_url}', '${w.value['target_id']}', '${tag[0]}');">
                 &nbsp;
             </a>
 
             <a class="tagging_tag" title="${tag[0]}"
-               href="${tg_url('%s/search' % tagging_url, params=dict(tags=tag[0]))}">
+               href="${w.tg_url('%s/search' % w.tagging_url, params=dict(tags=tag[0]))}">
                 ${Markup(tag[0])}
             </a>
         </li>
     </ul>
 
-    <a py:if="editmode" class="tagging_add_tag_button" href="#"
+    <a py:if="w.editmode" class="tagging_add_tag_button" href="#"
        onclick="jQuery(this).parent().find('.tagging_add_tag_form').slideToggle(); return false;">
         Add a Tag
     </a>
     <div class="tagging_add_tag_form" style="display:none">
-        <form onsubmit="return tagging_add_tag(this, '${tagging_url}', '${value['target_id']}');" method="post">
-            <input type="hidden" value="${value['target_id']}" name="target_id" />
+        <form onsubmit="return tagging_add_tag(this, '${w.tagging_url}', '${w.value['target_id']}');" method="post">
+            <input type="hidden" value="${w.value['target_id']}" name="target_id" />
             <input type="text" name="tags" />
             <input type="submit" value="Add" />
             <input type="reset" Value="Cancel" />

tgext/tagging/templates/tags.html

+<div xmlns:py="http://genshi.edgewall.org/"
+     class="tagging_tags_block">
+    <ul class="tagging_tags">
+        <li py:for="tag in value['tag_list']">
+            <a py:if="editmode" class="tagging_tag_delete" href="#"
+               onclick="return tagging_delete_tag(this, '${tagging_url}', '${value['target_id']}', '${tag[0]}');">
+                &nbsp;
+            </a>
+
+            <a class="tagging_tag" title="${tag[0]}"
+               href="${tg.url('%s/search' % tagging_url, params=dict(tags=tag[0]))}">
+                ${Markup(tag[0])}
+            </a>
+        </li>
+    </ul>
+
+    <a py:if="editmode" class="tagging_add_tag_button" href="#"
+       onclick="jQuery(this).parent().find('.tagging_add_tag_form').slideToggle(); return false;">
+        Add a Tag
+    </a>
+    <div class="tagging_add_tag_form" style="display:none">
+        <form onsubmit="return tagging_add_tag(this, '${tagging_url}', '${value['target_id']}');" method="post">
+            <input type="hidden" value="${value['target_id']}" name="target_id" />
+            <input type="text" name="tags" />
+            <input type="submit" value="Add" />
+            <input type="reset" Value="Cancel" />
+        </form>
+    </div>
+</div>

tgext/tagging/widgets/tagcloud.py

-from tw.api import Widget, JSLink, CSSLink
-from tw.jquery import jquery_js
+from tw_compat import TW1, TW2, FakeW
 import tg
 
-class TagCloud(Widget):
-    template = "genshi:tgext.tagging.templates.tagcloud"
-    javascript = [jquery_js, JSLink(modname='tgext.tagging', filename='static/tagging.js')]
-    css = [CSSLink(modname='tgext.tagging', filename='static/tagging.css')]
+if TW2:
+    from tw2.core import Param
 
-    params = {'tagging_url': 'Url of the tagging controller'}
+    class TagCloud(TW2.Widget):
+        template = "genshi:tgext.tagging.templates.tagcloud"
+        resources= [TW2.jquery_js, TW2.JSLink(modname='tgext.tagging', filename='static/tagging.js'),
+                    TW2.CSSLink(modname='tgext.tagging', filename='static/tagging.css')]
 
-    tagging_url = '/tagging'
+        tagging_url = Param(description='Url of the tagging controller', default='/tagging')
 
-    def update_params(self, d):
-        super(TagCloud, self).update_params(d)
-        d['tg_url'] = tg.url
-        d['weight_min'] = 0
-        d['weight_max'] = max((t[1] for t in d['value']))
-        d['weight_range'] = d['weight_max'] - d['weight_min']
+        def prepare(self):
+            super(TagCloud, self).prepare()
 
+            self.tg_url = tg.url
+            self.weight_min = 0
+            try:
+                self.weight_max = max((t[1] for t in self.value))
+            except ValueError:
+                self.weight_max = 0
+            self.weight_range = self.weight_max - self.weight_min
+    
+if not TW2:
+    class TagCloud(TW1.Widget):
+        template = "genshi:tgext.tagging.templates.tagcloud"
+        javascript = [TW1.jquery_js, TW1.JSLink(modname='tgext.tagging', filename='static/tagging.js')]
+        css = [TW1.CSSLink(modname='tgext.tagging', filename='static/tagging.css')]
+    
+        params = {'tagging_url': 'Url of the tagging controller'}
+    
+        tagging_url = '/tagging'
+    
+        def update_params(self, d):
+            super(TagCloud, self).update_params(d)
+            d['tg_url'] = tg.url
+            d['weight_min'] = 0
+            try:
+                d['weight_max'] = max((t[1] for t in d['value']))
+            except ValueError:
+                d['weight_max'] = 0
+            d['weight_range'] = d['weight_max'] - d['weight_min']
+            d['w'] = FakeW(d)
+    
+            
         
-    
-    
+        

tgext/tagging/widgets/taglist.py

-from tw.api import Widget, JSLink, CSSLink
-from tw.jquery import jquery_js
+from tw_compat import TW1, TW2, FakeW
 import tg
 
 from tgext.tagging.model.sautils import get_primary_key
+Tagging = None
 
-class TagList(Widget):
-    template = "genshi:tgext.tagging.templates.taglist"
-    javascript = [jquery_js, JSLink(modname='tgext.tagging', filename='static/tagging.js')]
-    css = [CSSLink(modname='tgext.tagging', filename='static/tagging.css')]
+if TW2:
+    from tw2.core import Param
 
-    params = {'editmode': 'Enable Tags removal and adding',
-              'tagging_url': 'Url of the tagging controller'}
+    class TagList(TW2.Widget):
+        template = "genshi:tgext.tagging.templates.taglist"
+        resources= [TW2.jquery_js, TW2.JSLink(modname='tgext.tagging', filename='static/tagging.js'),
+                    TW2.CSSLink(modname='tgext.tagging', filename='static/tagging.css')]
 
-    editmode = True
-    tagging_url = '/tagging'
+        editmode = Param(description='Enable Tags removal and adding', default=True)
+        tagging_url = Param(description='Url of the tagging controller', default='/tagging')
 
-    def update_params(self, d):
-        from tgext.tagging.model.tagging import Tagging
-        
-        super(TagList, self).update_params(d)
-        d['tg_url'] = tg.url
-        d['Tagging'] = Tagging
+        def prepare(self):
+            global Tagging
+            if Tagging is None:
+                from tgext.tagging.model.tagging import Tagging
 
-        if not isinstance(d['value'], dict):
-            d['value'] = {'target_id': getattr(d['value'], get_primary_key(d['value'].__class__)),
-                           'tag_list': Tagging.tag_cloud_for_object(d['value'])}
-    
-    
+            super(TagList, self).prepare()
+            self.tg_url = tg.url
+            self.Tagging = Tagging
+            if not isinstance(self.value, dict):
+                obj = self.value
+                self.value = {'target_id': getattr(obj, get_primary_key(obj.__class__)),
+                              'tag_list': Tagging.tag_cloud_for_object(obj)}
+if not TW2:
+    class TagList(TW1.Widget):
+        template = "genshi:tgext.tagging.templates.taglist"
+        javascript = [TW1.jquery_js, TW1.JSLink(modname='tgext.tagging', filename='static/tagging.js')]
+        css = [TW1.CSSLink(modname='tgext.tagging', filename='static/tagging.css')]
+
+        params = {'editmode': 'Enable Tags removal and adding',
+                  'tagging_url': 'Url of the tagging controller'}
+
+        editmode = True
+        tagging_url = '/tagging'
+
+        def update_params(self, d):
+            global Tagging
+            if Tagging is None:
+                from tgext.tagging.model.tagging import Tagging
+
+            super(TagList, self).update_params(d)
+            d['tg_url'] = tg.url
+            d['Tagging'] = Tagging
+            if not isinstance(d['value'], dict):
+                d['value'] = {'target_id': getattr(d['value'], get_primary_key(d['value'].__class__)),
+                               'tag_list': Tagging.tag_cloud_for_object(d['value'])}
+            d['w'] = FakeW(d)

tgext/tagging/widgets/tw_compat.py

+try:
+    from tw.api import JSLink as Tw1JSLink,\
+                      CSSLink as Tw1CSSLink, \
+                       Widget as Tw1Widget
+    class TW1:
+        JSLink = Tw1JSLink
+        CSSLink = Tw1CSSLink
+        Widget = Tw1Widget
+except ImportError:
+    TW1 = None
+
+if TW1:
+    from tw.jquery import jquery_js as tw1_jquery_js
+    TW1.jquery_js = tw1_jquery_js
+
+
+try:
+    from tw2.core.resources import JSLink as Tw2JSLink, \
+                                  CSSLink as Tw2CSSLink
+    from tw2.core import Widget as Tw2Widget
+    class TW2:
+        JSLink = Tw2JSLink
+        CSSLink = Tw2CSSLink
+        Widget = Tw2Widget
+except ImportError:
+    TW2 = None
+
+if TW2:
+    from tw2.jquery import jquery_js as tw2_jquery_js
+    TW2.jquery_js = tw2_jquery_js
+
+
+def is_tw2_form(w):
+    return hasattr(w, 'req')
+
+def FakeW(dict):
+    def __getattr__(self, name):
+        try:
+            return self[name]
+        except KeyError:
+            raise AttributeError()
+
+    @property
+    def compound_id(self):
+        return self['id']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.