backend for bookmark menu
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from flask import Blueprint, render_template, request, url_for
|
||||
from ..auth import get_active_user, is_logged_in, hard_login_required
|
||||
from ..models import BookmarkCollections
|
||||
from ..models import BookmarkCollections, BookmarkedPosts, BookmarkedThreads
|
||||
from functools import wraps
|
||||
|
||||
bp = Blueprint('hyperapi', __name__, url_prefix='/hyperapi/')
|
||||
@@ -26,22 +26,87 @@ def get_bookmark_dropdown():
|
||||
is_thread = concept_kind == 'thread'
|
||||
collections = BookmarkCollections.findall({'user_id': user.id})
|
||||
in_collection = None
|
||||
note = ''
|
||||
for collection in collections:
|
||||
callable = collection.has_thread if is_thread else collection.has_post
|
||||
if callable(concept_id):
|
||||
in_collection = collection.id
|
||||
concept = 'thread_id' if is_thread else 'post_id'
|
||||
note = (BookmarkedThreads if is_thread else BookmarkedPosts).find({'collection_id': in_collection, concept: concept_id}).note
|
||||
break
|
||||
submit_url = url_for('.bookmark_thread' if is_thread else '.bookmark_post')
|
||||
return render_template('hyper/bookmark_dropdown.html', collections=collections, in_collection=in_collection, is_thread=is_thread, concept_id=concept_id, submit_url=submit_url)
|
||||
return render_template('hyper/bookmark_dropdown.html', collections=collections, in_collection=in_collection, is_thread=is_thread, concept_id=concept_id, submit_url=submit_url, note=note)
|
||||
|
||||
@bp.post('/bookmarks/thread/')
|
||||
@hard_login_required
|
||||
@user_required
|
||||
def bookmark_thread():
|
||||
user = get_active_user()
|
||||
try:
|
||||
thread_id = int(request.form['concept_id'])
|
||||
target_collection_id = int(request.form['target_collection'])
|
||||
except ValueError, KeyError:
|
||||
return 'error', 400
|
||||
|
||||
if target_collection_id == -1:
|
||||
bt = BookmarkedThreads.get_for_user(thread_id, user.id)
|
||||
if bt:
|
||||
bt.delete()
|
||||
return '', 204
|
||||
|
||||
target_collection = BookmarkCollections.find({'id': target_collection_id})
|
||||
note = request.form.get('note', '')
|
||||
if not target_collection:
|
||||
return 'error', 400
|
||||
|
||||
if int(user.id) != int(target_collection.user_id):
|
||||
return 'error', 400
|
||||
|
||||
bt = BookmarkedThreads.get_for_user(thread_id, user.id)
|
||||
if bt:
|
||||
bt.update({'collection_id': target_collection_id, 'note': note})
|
||||
else:
|
||||
BookmarkedThreads.create({
|
||||
'collection_id': target_collection_id,
|
||||
'thread_id': thread_id,
|
||||
'note': note,
|
||||
})
|
||||
|
||||
return '', 204
|
||||
|
||||
@bp.post('/bookmarks/post/')
|
||||
@hard_login_required
|
||||
@user_required
|
||||
def bookmark_post():
|
||||
user = get_active_user()
|
||||
try:
|
||||
post_id = int(request.form['concept_id'])
|
||||
target_collection_id = int(request.form['target_collection'])
|
||||
except ValueError, KeyError:
|
||||
return 'error', 400
|
||||
|
||||
if target_collection_id == -1:
|
||||
bp = BookmarkedPosts.get_for_user(post_id, user.id)
|
||||
if bp:
|
||||
bp.delete()
|
||||
return '', 204
|
||||
|
||||
target_collection = BookmarkCollections.find({'id': target_collection_id})
|
||||
note = request.form.get('note', '')
|
||||
if not target_collection:
|
||||
return 'error', 400
|
||||
|
||||
if int(user.id) != int(target_collection.user_id):
|
||||
return 'error', 400
|
||||
|
||||
bp = BookmarkedPosts.get_for_user(post_id, user.id)
|
||||
if bp:
|
||||
bp.update({'collection_id': target_collection_id, 'note': note})
|
||||
else:
|
||||
BookmarkedPosts.create({
|
||||
'collection_id': target_collection_id,
|
||||
'post_id': post_id,
|
||||
'note': note,
|
||||
})
|
||||
|
||||
return '', 204
|
||||
|
||||
Reference in New Issue
Block a user