From 9539c48b7b9b52ffc79b371082d19d61caf706e4 Mon Sep 17 00:00:00 2001 From: Michiel Scholten Date: Sat, 17 Mar 2018 15:42:39 +0100 Subject: [PATCH] Split fetching of bookmarks into generic and view-specific functions --- digimarks.py | 36 ++++++++++++++++++++++++++++++------ templates/base.html | 4 ++-- templates/bookmarks.html | 8 ++++---- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/digimarks.py b/digimarks.py index dd702d9..38f3ebb 100644 --- a/digimarks.py +++ b/digimarks.py @@ -429,10 +429,7 @@ def index(): return render_template('index.html', theme=theme) -@app.route('/', methods=['GET', 'POST']) -@app.route('//filter/', methods=['GET', 'POST']) -@app.route('//sort/', methods=['GET', 'POST']) -def bookmarks(userkey, filtermethod = None, sortmethod = None): +def get_bookmarks(userkey, filtermethod=None, sortmethod=None): """ User homepage, list their bookmarks, optionally filtered and/or sorted """ #return object_list('bookmarks.html', Bookmark.select()) #user = User.select(key=userkey) @@ -442,7 +439,7 @@ def bookmarks(userkey, filtermethod = None, sortmethod = None): #else: # abort(404) message = request.args.get('message') - tags = get_cached_tags(userkey) + bookmarktags = get_cached_tags(userkey) filter_text = '' if request.form: @@ -475,11 +472,19 @@ def bookmarks(userkey, filtermethod = None, sortmethod = None): else: bookmarks = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.status == Bookmark.VISIBLE).order_by(Bookmark.created_date.desc()) + return bookmarks, bookmarktags, filter_text, message + + +@app.route('/', methods=['GET', 'POST']) +@app.route('//filter/', methods=['GET', 'POST']) +@app.route('//sort/', methods=['GET', 'POST']) +def bookmarks_page(userkey, filtermethod=None, sortmethod=None): + bookmarks, bookmarktags, filter_text, message = get_bookmarks(userkey, filtermethod, sortmethod) theme = get_theme(userkey) return render_template('bookmarks.html', bookmarks=bookmarks, userkey=userkey, - tags=tags, + tags=bookmarktags, filter_text=filter_text, message=message, theme=theme, @@ -488,6 +493,25 @@ def bookmarks(userkey, filtermethod = None, sortmethod = None): ) +@app.route('/api/v1/', methods=['GET', 'POST']) +@app.route('/api/v1//filter/', methods=['GET', 'POST']) +@app.route('/api/v1//sort/', methods=['GET', 'POST']) +def bookmarks_json(userkey, filtermethod=None, sortmethod=None): + bookmarks, bookmarktags, filter_text, message = get_bookmarks(userkey, filtermethod, sortmethod) + + bookmarkslist = [] + + for bookmark in bookmarks: + bookmarkslist.append(bookmark) + + the_data = { + 'bookmarks': bookmarks, + 'tags': bookmarktags, + 'filter_text': filter_text, + 'message': message, + 'userkey': userkey, + } + return jsonify(the_data) #@app.route('//') #def viewbookmark(userkey, urlhash): diff --git a/templates/base.html b/templates/base.html index f170a76..588fe7a 100644 --- a/templates/base.html +++ b/templates/base.html @@ -37,7 +37,7 @@