diff MoinMoin/themes/__init__.py @ 2519:b942154b2d74

merged bootstrap and main repo
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 02 Feb 2014 17:14:23 +0100
parents f1d470ade3c0 7b7e07bb6f8a
children 07f3ccfdea45
line wrap: on
line diff
--- a/MoinMoin/themes/__init__.py	Tue Jan 21 02:25:35 2014 +0530
+++ b/MoinMoin/themes/__init__.py	Sun Feb 02 17:14:23 2014 +0100
@@ -22,9 +22,10 @@
 
 from MoinMoin.i18n import _, L_, N_
 from MoinMoin import wikiutil, user
-from MoinMoin.constants.keys import USERID, ADDRESS, HOSTNAME
+from MoinMoin.constants.keys import USERID, ADDRESS, HOSTNAME, REVID, ITEMID, NAME_EXACT
+from MoinMoin.constants.namespaces import NAMESPACE_DEFAULT, NAMESPACE_USERPROFILES, NAMESPACE_ALL
 from MoinMoin.search import SearchForm
-from MoinMoin.util.interwiki import split_interwiki, getInterwikiHome, is_local_wiki, is_known_wiki, url_for_item
+from MoinMoin.util.interwiki import split_interwiki, getInterwikiHome, is_local_wiki, is_known_wiki, url_for_item, CompositeName, split_fqname
 from MoinMoin.util.crypto import cache_key
 from MoinMoin.util.forms import make_generator
 from MoinMoin.util.clock import timed
@@ -217,18 +218,31 @@
                 'special.transclusions': "icon-edit", }
         return icon
 
-    def location_breadcrumbs(self, item_name):
+    def location_breadcrumbs(self, fqname):
         """
         Assemble the location using breadcrumbs (was: title)
 
         :rtype: list
-        :returns: location breadcrumbs items in tuple (segment_name, item_name, exists)
+        :returns: location breadcrumbs items in tuple (segment_name, fq_name, exists)
         """
         breadcrumbs = []
         current_item = ''
+        if not isinstance(fqname, CompositeName):
+            fqname = split_fqname(fqname)
+        if fqname.field != NAME_EXACT:
+            return [(fqname, fqname, bool(self.storage.get_item(**fqname.query)))]
+        namespace = fqname.namespace
+        fq_current = CompositeName(u'', NAME_EXACT, namespace)
+        fq_segment = CompositeName(u'', NAME_EXACT, namespace or '~')
+        breadcrumbs.append((fq_segment, fq_current, False))
+        item_name = fqname.value
+        if not item_name:
+            return breadcrumbs
         for segment in item_name.split('/'):
             current_item += segment
-            breadcrumbs.append((segment, current_item, self.storage.has_item(current_item)))
+            fq_current = CompositeName(namespace, NAME_EXACT, current_item)
+            fq_segment = CompositeName(namespace, NAME_EXACT, segment)
+            breadcrumbs.append((fq_segment, fq_current, bool(self.storage.get_item(**fq_current.query))))
             current_item += '/'
         return breadcrumbs
 
@@ -243,26 +257,30 @@
         breadcrumbs = []
         trail = user.get_trail()
         for interwiki_item_name in trail:
-            wiki_name, namespace, item_name = split_interwiki(interwiki_item_name)
+            wiki_name, namespace, field, item_name = split_interwiki(interwiki_item_name)
+            fqname = CompositeName(namespace, field, item_name)
             err = not is_known_wiki(wiki_name)
-            href = url_for_item(item_name, namespace=namespace, wiki_name=wiki_name)
+            href = url_for_item(wiki_name=wiki_name, **fqname.split)
             if is_local_wiki(wiki_name):
-                exists = self.storage.has_item(item_name)
+                exists = bool(self.storage.get_item(**fqname.query))
                 wiki_name = ''  # means "this wiki" for the theme code
             else:
                 exists = True  # we can't detect existance of remote items
-            breadcrumbs.append((wiki_name, item_name, href, exists, err))
+            if item_name:
+                breadcrumbs.append((wiki_name, fqname, href, exists, err))
         return breadcrumbs
 
-    def subitem_index(self, item_name):
+    def subitem_index(self, fqname):
         """
-        Get a list of subitems for the given item_name
+        Get a list of subitems for the given fqname
 
         :rtype: list
         :returns: list of item tuples (item_name, item_title, item_mime_type, has_children)
         """
         from MoinMoin.items import Item
-        item = Item.create(item_name)
+        if not isinstance(fqname, CompositeName):
+            fqname = split_fqname(fqname)
+        item = Item.create(fqname.fullname)
         return item.get_mixed_index()
 
     def userhome(self):
@@ -283,7 +301,7 @@
         else:
             # We cannot check if wiki pages exists in remote wikis
             exists = True
-        wiki_href = url_for_item(itemname, wiki_name=wikiname)
+        wiki_href = url_for_item(itemname, wiki_name=wikiname, namespace=NAMESPACE_USERPROFILES)
         return wiki_href, display_name, title, exists
 
     def split_navilink(self, text):
@@ -329,29 +347,38 @@
         if target.startswith("wiki:"):
             target = target[5:]
 
-        wiki_name, namespace, item_name = split_interwiki(target)
+        wiki_name, namespace, field, item_name = split_interwiki(target)
         if wiki_name == 'Self':
             wiki_name = ''
-        href = url_for_item(item_name, namespace=namespace, wiki_name=wiki_name)
+        href = url_for_item(item_name, namespace=namespace, wiki_name=wiki_name, field=field)
         if not title:
-            title = item_name
+            title = shorten_fqname(CompositeName(namespace, field, item_name))
         return href, title, wiki_name
 
     @timed()
-    def navibar(self, item_name):
+    def navibar(self, fqname):
         """
         Assemble the navibar
 
         :rtype: list
         :returns: list of tuples (css_class, url, link_text, title)
         """
+        if not isinstance(fqname, CompositeName):
+            fqname = split_fqname(fqname)
+        item_name = fqname.value
         current = item_name
         # Process config navi_bar
         items = []
         for cls, endpoint, args, link_text, title in self.cfg.navi_bar:
             if endpoint == "frontend.show_root":
                 endpoint = "frontend.show_item"
-                args['item_name'] = app.cfg.item_root
+                root_fqname = fqname.get_root_fqname()
+                default_root = app.cfg.root_mapping.get(NAMESPACE_DEFAULT, app.cfg.default_root)
+                args['item_name'] = root_fqname.fullname if fqname.namespace != NAMESPACE_ALL else default_root
+            elif endpoint in ["frontend.global_history", "frontend.global_tags"]:
+                args['namespace'] = fqname.namespace
+            elif endpoint == "frontend.index":
+                args['item_name'] = fqname.namespace
             items.append((cls, url_for(endpoint, **args), link_text, title))
 
         # Add user links to wiki links.
@@ -420,6 +447,31 @@
             url = url or url_for('frontend.login')
         return url
 
+    def get_fqnames(self, fqname):
+        """
+        Return the list of other fqnames associated with the item.
+        """
+        if fqname.field != NAME_EXACT:
+            return []
+        item = self.storage.get_item(**fqname.query)
+        fqnames = item.fqnames
+        fqnames.remove(fqname)
+        return fqnames or []
+
+    def get_namespaces(self, ns):
+        """
+        Return the list of tuples (composite name, namespace) referring to namespaces other
+        than the current namespace.
+        """
+        ns = u'' if ns.value == '~' else ns.value
+        namespace_root_mapping = []
+        for namespace, _ in app.cfg.namespace_mapping:
+            namespace = namespace.rstrip('/')
+            if namespace != ns:
+                fq_namespace = CompositeName(namespace, NAME_EXACT, u'')
+                namespace_root_mapping.append((namespace or '~', fq_namespace.get_root_fqname()))
+        return namespace_root_mapping
+
 
 def get_editor_info(meta, external=False):
     addr = meta.get(ADDRESS)
@@ -471,6 +523,28 @@
     return result
 
 
+def shorten_fqname(fqname, length=25):
+    """
+    Shorten fqname
+
+    Shorten a given long fqname so that it looks good depending upon whether
+    the field is a UUID or not.
+
+    :param fqname: fqname, namedtuple
+    :param length maximum length for shortened fqnames in case the field
+    is not a UUID.
+    :rtype: unicode
+    :returns: shortened fqname.
+    """
+    name = fqname.value
+    if len(name) > length:
+        if fqname.field in [REVID, ITEMID]:
+            name = shorten_id(name)
+        else:
+            name = shorten_item_name(name, length)
+    return name
+
+
 def shorten_item_name(name, length=25):
     """
     Shorten item names
@@ -565,6 +639,7 @@
 
 
 def setup_jinja_env():
+    app.jinja_env.filters['shorten_fqname'] = shorten_fqname
     app.jinja_env.filters['shorten_item_name'] = shorten_item_name
     app.jinja_env.filters['shorten_id'] = shorten_id
     app.jinja_env.filters['contenttype_to_class'] = contenttype_to_class
@@ -586,7 +661,7 @@
         'storage': flaskg.storage,
         'clock': flaskg.clock,
         'cfg': app.cfg,
-        'item_name': u'@NONAMEGIVEN',
+        'item_name': u'@NONAMEGIVEN',  # XXX can we just use u'' ?
         'url_for_item': url_for_item,
         'get_editor_info': lambda meta: get_editor_info(meta),
         'utctimestamp': lambda dt: utctimestamp(dt),