changeset 88:9f037347955c

fixed quicklink bug when interwiki is None, create simpler addQuicklink and removeQuicklink methods, simplify isQuicklinkedTo method imported from: moin--main--1.5--patch-90
author Nir Soffer <nirs@freeshell.org>
date Fri, 07 Oct 2005 22:10:55 +0000
parents ee1c9e86b393
children 4e44f6dfdfc2
files MoinMoin/user.py MoinMoin/wikiaction.py
diffstat 2 files changed, 77 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/user.py	Fri Oct 07 08:57:24 2005 +0000
+++ b/MoinMoin/user.py	Fri Oct 07 22:10:55 2005 +0000
@@ -600,32 +600,30 @@
         return self.subscribed_pages
 
     def isQuickLinkedTo(self, pagelist):
-        """
-        Check if user quicklink matches any page in pagelist.
+        """ Check if user quicklink matches any page in pagelist.
+
+        TODO: should return a bool, not int.        
         
         @param pagelist: list of pages to check for quicklinks
         @rtype: int
         @return: 1, if user has quicklinked any page in pagelist
                  0, if not
         """
-        quicklinked = 0
-        if self.valid:
-            interwikiname = self._cfg.interwikiname
-            for page in pagelist:
-                if page in self.quicklinks:
-                    quicklinked = 1
-                    break
-                # do also try our own interwiki name
-                if interwikiname:
-                    iwpage = "%s:%s" % (interwikiname, page)
-                    if iwpage in self.quicklinks:
-                        quicklinked = 1
-                        break
-        return quicklinked
-
+        if not self.valid:
+            return 0
+            
+        for pagename in pagelist:
+            if pagename in self.quicklinks:
+                return 1
+            interWikiName = self._interWikiName(pagename)
+            if interWikiName and interWikiName in self.quicklinks:
+                return 1
+        return 0
+        
     def isSubscribedTo(self, pagelist):
-        """
-        Check if user subscription matches any page in pagelist.
+        """ Check if user subscription matches any page in pagelist.
+        
+        TODO: should use _interWikiName and return a bool, not int.
         
         @param pagelist: list of pages to check for subscription
         @rtype: int
@@ -659,39 +657,68 @@
         else:
             return 0
 
-    def quicklinkPage(self, pagename, remove=False):
-        """ Add or remove a quicklink to a wiki page.
-
-        Note that you need to save the user data to make this stick!
+    def addQuicklink(self, pagename):
+        """ Adds a page to the user quicklinks 
+        
+        If the wiki has an interwiki name, all links are saved as
+        interwiki names. If not, as simple page name.
 
-        @param pagename: name of the page to quicklink
-        @param remove: unsubscribe pagename if set
-        @type remove: bool
+        @param pagename: page name
+        @type pagename: unicode
         @rtype: bool
-        @return: True, if quicklinks were added/removed.
+        @return: if pagename was added
         """
         changed = False
-        if self._cfg.interwikiname:
-            iwpagename = "%s:%s" % (self._cfg.interwikiname, pagename)
-        else:
-            iwpagename = None
-        if remove:
+        interWikiName = self._interWikiName(pagename)
+        if interWikiName:
             if pagename in self.quicklinks:
                 self.quicklinks.remove(pagename)
                 changed = True
-            if iwpagename in self.quicklinks:
-                self.quicklinks.remove(iwpagename)
+            if interWikiName not in self.quicklinks:
+                self.quicklinks.append(interWikiName)
                 changed = True
         else:
-            # add the interwiki name and remove non-interwiki name, if present!
-            if pagename in self.quicklinks:
-                self.quicklinks.remove(pagename)
+            if pagename not in self.quicklinks:
+                self.quicklinks.append(pagename)
                 changed = True
-            if iwpagename not in self.quicklinks:
-                self.quicklinks.append(iwpagename)
-                changed = True
+
+        if changed:
+            self.save()
         return changed
 
+    def removeQuicklink(self, pagename):
+        """ Remove a page from user quicklinks 
+        
+        Remove both interwiki and simple name from quicklinks.
+        
+        @param pagename: page name
+        @type pagename: unicode
+        @rtype: bool
+        @return: if pagename was removed
+        """
+        changed = False
+        interWikiName = self._interWikiName(pagename)
+        if interWikiName and interWikiName in self.quicklinks:
+            self.quicklinks.remove(interWikiName)
+            changed = True
+        if pagename in self.quicklinks:
+            self.quicklinks.remove(pagename)
+            changed = True        
+        
+        if changed:
+            self.save()
+        return changed
+
+    def _interWikiName(self, pagename):
+        """ Return the inter wiki name of a page name 
+
+        @param pagename: page name
+        @type pagename: unicode
+        """
+        if not self._cfg.interwikiname:
+            return None
+        return "%s:%s" % (self._cfg.interwikiname, pagename)
+
     def subscribePage(self, pagename, remove=False):
         """ Subscribe or unsubscribe to a wiki page.
 
--- a/MoinMoin/wikiaction.py	Fri Oct 07 08:57:24 2005 +0000
+++ b/MoinMoin/wikiaction.py	Fri Oct 07 22:10:55 2005 +0000
@@ -674,29 +674,21 @@
         
 
 def do_quicklink(pagename, request):
-    """ Add the current wiki page to the quicklinks property in
-        current user profile.
+    """ Add the current wiki page to the user quicklinks 
+    
+    TODO: what if add or remove quicklink fail? display an error message?
     """
     _ = request.getText
-    cfg = request.cfg
-    msg = ''
-
-    # check whether the user has a profile
-    if not request.user.valid:
-        msg = _('''You didn't create a user profile yet. '''
-                '''Select UserPreferences in the upper right corner to create a profile.''')
+    msg = None
 
-    # check whether already quicklinked
+    if not request.user.valid:
+        msg = _("You must login to add a quicklink.")    
     elif request.user.isQuickLinkedTo([pagename]):
-        if request.user.quicklinkPage(pagename, remove=True):
-            request.user.save()
-            msg = _('Your quicklink to this page has been removed.')
-            
-    # quicklink to current page
+        if request.user.removeQuicklink(pagename):
+            msg = _('Your quicklink to this page has been removed.')            
     else:
-        if request.user.quicklinkPage(pagename):
-            request.user.save()
-        msg = _('A quicklink to this page has been added for you.')
+        if request.user.addQuicklink(pagename):
+            msg = _('A quicklink to this page has been added for you.')
 
     Page(request, pagename).send_page(request, msg=msg)