add ability to bookmark posts and threads, courtesy of bitty
This commit is contained in:
53
app/routes/hyperapi.py
Normal file
53
app/routes/hyperapi.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from flask import Blueprint, render_template, abort, request
|
||||
from .users import get_active_user, is_logged_in
|
||||
from ..models import BookmarkCollections, BookmarkedPosts, BookmarkedThreads
|
||||
from functools import wraps
|
||||
|
||||
bp = Blueprint('hyperapi', __name__, url_prefix='/hyperapi/')
|
||||
|
||||
def login_required(view_func):
|
||||
@wraps(view_func)
|
||||
def dec(*args, **kwargs):
|
||||
if not is_logged_in():
|
||||
abort(403)
|
||||
return view_func(*args, **kwargs)
|
||||
return dec
|
||||
|
||||
def account_required(view_func):
|
||||
@wraps(view_func)
|
||||
def dec(*args, **kwargs):
|
||||
if get_active_user().is_guest():
|
||||
abort(403)
|
||||
return view_func(*args, **kwargs)
|
||||
return dec
|
||||
|
||||
@bp.errorhandler(403)
|
||||
def handle_403(e):
|
||||
return "<h1>forbidden</h1>", 403
|
||||
|
||||
|
||||
@bp.get('bookmarks-dropdown/<bookmark_type>')
|
||||
@login_required
|
||||
@account_required
|
||||
def bookmarks_dropdown(bookmark_type):
|
||||
collections = BookmarkCollections.findall({'user_id': get_active_user().id})
|
||||
concept_id = request.args.get('id')
|
||||
require_reload = bool(int(request.args.get('require_reload', default=0)))
|
||||
if bookmark_type.lower() == 'thread':
|
||||
selected = next(filter(lambda bc: bc.has_thread(concept_id), collections), None)
|
||||
elif bookmark_type.lower() == 'post':
|
||||
selected = next(filter(lambda bc: bc.has_post(concept_id), collections), None)
|
||||
else:
|
||||
abort(400)
|
||||
return
|
||||
|
||||
if selected:
|
||||
if bookmark_type.lower() == 'thread':
|
||||
memo = BookmarkedThreads.find({'collection_id': selected.id, 'thread_id': int(concept_id)}).note
|
||||
else:
|
||||
memo = BookmarkedPosts.find({'collection_id': selected.id, 'post_id': int(concept_id)}).note
|
||||
else:
|
||||
memo = ''
|
||||
|
||||
|
||||
return render_template('components/bookmarks_dropdown.html', collections=collections, id=concept_id, selected=selected, type=bookmark_type, memo=memo, require_reload=require_reload)
|
||||
Reference in New Issue
Block a user