2018-01-12 03:40:56 +00:00
|
|
|
import flask; from flask import request
|
|
|
|
|
|
|
|
import etiquette
|
|
|
|
|
2018-07-19 01:37:21 +00:00
|
|
|
from .. import common
|
2018-01-12 03:40:56 +00:00
|
|
|
from .. import decorators
|
|
|
|
from .. import jsonify
|
|
|
|
|
|
|
|
site = common.site
|
|
|
|
session_manager = common.session_manager
|
|
|
|
|
|
|
|
# Individual bookmarks #############################################################################
|
|
|
|
|
2018-07-29 08:25:53 +00:00
|
|
|
@site.route('/bookmark/<bookmark_id>.json')
|
|
|
|
def get_bookmark_json(bookmark_id):
|
|
|
|
bookmark = common.P_bookmark(bookmark_id, response_type='json')
|
2021-01-01 20:56:05 +00:00
|
|
|
response = bookmark.jsonify()
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
2018-07-29 08:25:53 +00:00
|
|
|
@site.route('/bookmark/<bookmark_id>/edit', methods=['POST'])
|
|
|
|
def post_bookmark_edit(bookmark_id):
|
|
|
|
bookmark = common.P_bookmark(bookmark_id, response_type='json')
|
2018-01-12 03:40:56 +00:00
|
|
|
# Emptystring is okay for titles, but not for URL.
|
|
|
|
title = request.form.get('title', None)
|
|
|
|
url = request.form.get('url', None) or None
|
2020-02-20 08:34:28 +00:00
|
|
|
bookmark.edit(title=title, url=url, commit=True)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2021-01-01 20:56:05 +00:00
|
|
|
response = bookmark.jsonify()
|
2018-01-12 03:40:56 +00:00
|
|
|
response = jsonify.make_json_response(response)
|
|
|
|
return response
|
|
|
|
|
|
|
|
# Bookmark listings ################################################################################
|
|
|
|
|
|
|
|
@site.route('/bookmarks')
|
|
|
|
def get_bookmarks_html():
|
|
|
|
bookmarks = list(common.P.get_bookmarks())
|
2019-08-14 20:40:52 +00:00
|
|
|
return common.render_template(request, 'bookmarks.html', bookmarks=bookmarks)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
@site.route('/bookmarks.json')
|
|
|
|
def get_bookmarks_json():
|
2021-01-01 20:56:05 +00:00
|
|
|
bookmarks = [b.jsonify() for b in common.P.get_bookmarks()]
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_json_response(bookmarks)
|
2018-09-23 22:05:05 +00:00
|
|
|
|
|
|
|
# Bookmark create and delete #######################################################################
|
|
|
|
|
|
|
|
@site.route('/bookmarks/create_bookmark', methods=['POST'])
|
|
|
|
@decorators.required_fields(['url'], forbid_whitespace=True)
|
|
|
|
def post_bookmark_create():
|
|
|
|
url = request.form['url']
|
|
|
|
title = request.form.get('title', None)
|
|
|
|
user = session_manager.get(request).user
|
2020-02-20 08:34:28 +00:00
|
|
|
bookmark = common.P.new_bookmark(url=url, title=title, author=user, commit=True)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = bookmark.jsonify()
|
2018-09-23 22:05:05 +00:00
|
|
|
response = jsonify.make_json_response(response)
|
|
|
|
return response
|
|
|
|
|
|
|
|
@site.route('/bookmark/<bookmark_id>/delete', methods=['POST'])
|
|
|
|
def post_bookmark_delete(bookmark_id):
|
|
|
|
bookmark = common.P_bookmark(bookmark_id, response_type='json')
|
2020-02-20 08:34:28 +00:00
|
|
|
bookmark.delete(commit=True)
|
2018-09-23 22:05:05 +00:00
|
|
|
return jsonify.make_json_response({})
|