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, BookmarkedPosts, BookmarkedThreads, Threads, Posts, Badges, BadgeUploads from functools import wraps bp = Blueprint('hyperapi', __name__, url_prefix='/hyperapi/') def user_required(view_func): @wraps(view_func) def wrapper(*args, **kwargs): if get_active_user().is_guest(): return 'Your account must be approved by a moderator before you may perform this action.', 403 return view_func(*args, **kwargs) return wrapper @bp.get('/bookmarks/dropdown/') @hard_login_required @user_required def get_bookmark_dropdown(): user = get_active_user() concept_kind = request.args.get('concept_kind', 'thread') try: concept_id = int(request.args.get('concept_id', 0)) except ValueError: return 'error', 400 is_thread = concept_kind == 'thread' if is_thread: target_thread = Threads.find({'id': concept_id}) if not target_thread: return 'This thread no longer exists. Please refresh the page.', 404 else: target_post = Posts.find({'id': concept_id}) if not target_post: return 'This post no longer exists. Please refresh the page.', 404 collections = BookmarkCollections.get_for_user(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, 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 if not Threads.find({'id': thread_id}): return 'error', 404 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 if not Posts.find({'id': post_id}): return 'error', 404 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 @bp.get('/badges/editor/') @hard_login_required @user_required def badge_editor(): user = get_active_user() badges = Badges.get_for_user(user.id) badge_uploads = BadgeUploads.get_for_user(user.id) return render_template('hyper/badge_editor.html', badges=badges, badge_uploads=badge_uploads)