Commits

Thomas Waldmann committed 30634c5

user bookmarks: simplify code, make .bookmark property

to remove a bookmark, set it to None.

if a bookmark does not exist, None will be returned.

Comments (0)

Files changed (3)

MoinMoin/_tests/test_user.py

     # Bookmarks -------------------------------------------------------
 
     def test_bookmark(self):
-        name = u'Test_User_quicklink'
+        name = u'Test_User_bookmark'
         password = name
         self.createUser(name, password)
         theUser = user.User(name=name, password=password)
 
-        theUser.setBookmark(7)
-        result_added = theUser.getBookmark()
-        expected = 7
-        assert result_added == expected
+        # set / retrieve the bookmark
+        bookmark = 1234567
+        theUser.bookmark = bookmark
+        theUser = user.User(name=name, password=password)
+        result = theUser.bookmark
+        assert result == bookmark
+
         # delete the bookmark
-        result_success = theUser.delBookmark()
-        assert result_success == 0
-        result_deleted = theUser.getBookmark()
-        assert not result_deleted
-
-        # delBookmark should return 1 on failure
-        result_failure = theUser.delBookmark()
-        assert result_failure == 1
+        theUser.bookmark = None
+        theUser = user.User(name=name, password=password)
+        result = theUser.bookmark
+        assert result is None
 
     # Quicklinks ------------------------------------------------------
 

MoinMoin/apps/frontend/views.py

 def global_history():
     all_revs = bool(request.values.get('all'))
     idx_name = ALL_REVS if all_revs else LATEST_REVS
-    if flaskg.user.valid:
-        bookmark_time = flaskg.user.getBookmark()
-    else:
-        bookmark_time = None
     query = Term(WIKINAME, app.cfg.interwikiname)
+    bookmark_time = flaskg.user.bookmark
     if bookmark_time is not None:
         query = And([query, DateRange(MTIME, start=datetime.utcfromtimestamp(bookmark_time), end=None)])
     revs = flaskg.storage.search(query, idx_name=idx_name, sortedby=[MTIME], reverse=True, limit=1000)
                     tm = int(time.time())
         else:
             tm = int(time.time())
-
-        if tm is None:
-            flaskg.user.delBookmark()
-        else:
-            flaskg.user.setBookmark(tm)
+        flaskg.user.bookmark = tm
     else:
         flash(_("You must log in to use bookmarks."), "error")
     return redirect(url_for('.global_history'))
 
     # Bookmarks --------------------------------------------------------------
 
-    def setBookmark(self, tm):
+    def _set_bookmark(self, tm):
         """ Set bookmark timestamp.
 
-        :param tm: timestamp
+        :param tm: timestamp (int or None)
         """
         if self.valid:
-            self.profile[BOOKMARKS][self._cfg.interwikiname] = int(tm)
-            self.save()
+            if not (tm is None or isinstance(tm, int)):
+                raise ValueError('tm should be int or None')
+            if tm is None:
+                self.profile[BOOKMARKS].pop(self._cfg.interwikiname)
+            else:
+                self.profile[BOOKMARKS][self._cfg.interwikiname] = tm
+            self.save(force=True)
 
-    def getBookmark(self):
+    def _get_bookmark(self):
         """ Get bookmark timestamp.
 
-        :rtype: int
+        :rtype: int / None
         :returns: bookmark timestamp or None
         """
         bm = None
                 pass
         return bm
 
-    def delBookmark(self):
-        """ Removes bookmark timestamp.
-
-        :rtype: int
-        :returns: 0 on success, 1 on failure
-        """
-        if self.valid:
-            try:
-                del self.profile[BOOKMARKS][self._cfg.interwikiname]
-            except KeyError:
-                return 1
-            self.save()
-            return 0
-        return 1
+    bookmark = property(_get_bookmark, _set_bookmark)
 
     # Subscribed Items -------------------------------------------------------
 
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.