2018-01-12 03:40:56 +00:00
|
|
|
import flask; from flask import request
|
|
|
|
|
2021-06-05 04:49:45 +00:00
|
|
|
from voussoirkit import flasktools
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
import etiquette
|
|
|
|
|
2018-07-19 01:37:21 +00:00
|
|
|
from .. import common
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
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):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2018-07-29 08:25:53 +00:00
|
|
|
bookmark = common.P_bookmark(bookmark_id, response_type='json')
|
2021-01-01 20:56:05 +00:00
|
|
|
response = bookmark.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2018-07-29 08:25:53 +00:00
|
|
|
@site.route('/bookmark/<bookmark_id>/edit', methods=['POST'])
|
|
|
|
def post_bookmark_edit(bookmark_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
bookmark = common.P_bookmark(bookmark_id, response_type='json')
|
|
|
|
# Emptystring is okay for titles, but not for URL.
|
|
|
|
title = request.form.get('title', None)
|
|
|
|
url = request.form.get('url', None) or None
|
|
|
|
bookmark.edit(title=title, url=url)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2021-01-01 20:56:05 +00:00
|
|
|
response = bookmark.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
response = flasktools.json_response(response)
|
2018-01-12 03:40:56 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
# Bookmark listings ################################################################################
|
|
|
|
|
2022-11-11 19:41:24 +00:00
|
|
|
@site.route('/bookmarks.atom')
|
|
|
|
def get_bookmarks_atom():
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-11-11 19:41:24 +00:00
|
|
|
bookmarks = common.P.get_bookmarks()
|
|
|
|
response = etiquette.helpers.make_atom_feed(
|
|
|
|
bookmarks,
|
|
|
|
feed_id='/bookmarks' + request.query_string.decode('utf-8'),
|
|
|
|
feed_title='bookmarks',
|
|
|
|
feed_link=request.url.replace('/bookmarks.atom', '/bookmarks'),
|
|
|
|
)
|
|
|
|
return flasktools.atom_response(response)
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
@site.route('/bookmarks')
|
|
|
|
def get_bookmarks_html():
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2018-01-12 03:40:56 +00:00
|
|
|
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():
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2021-01-01 20:56:05 +00:00
|
|
|
bookmarks = [b.jsonify() for b in common.P.get_bookmarks()]
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(bookmarks)
|
2018-09-23 22:05:05 +00:00
|
|
|
|
|
|
|
# Bookmark create and delete #######################################################################
|
|
|
|
|
|
|
|
@site.route('/bookmarks/create_bookmark', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['url'], forbid_whitespace=True)
|
2018-09-23 22:05:05 +00:00
|
|
|
def post_bookmark_create():
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2018-09-23 22:05:05 +00:00
|
|
|
url = request.form['url']
|
|
|
|
title = request.form.get('title', None)
|
|
|
|
user = session_manager.get(request).user
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
bookmark = common.P.new_bookmark(url=url, title=title, author=user)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = bookmark.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
response = flasktools.json_response(response)
|
2018-09-23 22:05:05 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
@site.route('/bookmark/<bookmark_id>/delete', methods=['POST'])
|
|
|
|
def post_bookmark_delete(bookmark_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
bookmark = common.P_bookmark(bookmark_id, response_type='json')
|
|
|
|
bookmark.delete()
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|