Skip to content

Commit

Permalink
add sorting to bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
n00rsy committed May 4, 2023
1 parent 81e312c commit 55586cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
34 changes: 22 additions & 12 deletions pybossa/view/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"""
import json

from enum import Enum
from itsdangerous import BadData
from markdown import markdown

Expand Down Expand Up @@ -1156,22 +1157,27 @@ def add_metadata(name):
return redirect(url_for('account.profile', name=name))


def bookmarks_dict_to_array(bookmarks_dict):
def bookmarks_dict_to_array(bookmarks_dict, order_by, desc):
order_by = "name" if order_by is None else order_by
desc = False if desc is None else desc

bookmarks_array = []
for name, meta in bookmarks_dict.items():
b = {'name': name}
b.update(meta)
bookmarks_array.append(b)

bookmarks_array.sort(key=lambda b: b[order_by], reverse=desc)
return bookmarks_array


def get_bookmarks(user_name, short_name):
def get_bookmarks(user_name, short_name, order_by, desc):
taskbrowse_bookmarks = cached_users.get_taskbrowse_bookmarks(user_name)
proj_bookmarks = taskbrowse_bookmarks.get(short_name, {})
return bookmarks_dict_to_array(proj_bookmarks)
return bookmarks_dict_to_array(proj_bookmarks, order_by, desc)


def add_bookmark(user_name, short_name, bookmark_name, bookmark_url):
def add_bookmark(user_name, short_name, bookmark_name, bookmark_url, order_by, desc):
user = user_repo.get_by_name(name=user_name)
taskbrowse_bookmarks = user.info.get('taskbrowse_bookmarks', {})
proj_bookmarks = taskbrowse_bookmarks.get(short_name, {})
Expand All @@ -1198,10 +1204,10 @@ def add_bookmark(user_name, short_name, bookmark_name, bookmark_url):

user_repo.update(user)
cached_users.delete_taskbrowse_bookmarks(user)
return bookmarks_dict_to_array(proj_bookmarks)
return bookmarks_dict_to_array(proj_bookmarks, order_by, desc)


def delete_bookmark(user_name, short_name, bookmark_name):
def delete_bookmark(user_name, short_name, bookmark_name, order_by, desc):
user = user_repo.get_by_name(name=user_name)
taskbrowse_bookmarks = user.info.get('taskbrowse_bookmarks', {})
proj_bookmarks = taskbrowse_bookmarks.get(short_name, {})
Expand All @@ -1218,7 +1224,7 @@ def delete_bookmark(user_name, short_name, bookmark_name):
user.info['taskbrowse_bookmarks'] = taskbrowse_bookmarks
user_repo.update(user)
cached_users.delete_taskbrowse_bookmarks(user)
return bookmarks_dict_to_array(proj_bookmarks)
return bookmarks_dict_to_array(proj_bookmarks, order_by, desc)


@blueprint.route('/<user_name>/taskbrowse_bookmarks/<short_name>', methods=['GET', 'POST', 'DELETE'])
Expand All @@ -1228,26 +1234,30 @@ def taskbrowse_bookmarks(user_name, short_name):
if current_user.name != user_name:
return abort(404)

order_by = request.args.get('order_by', None, type=str)
desc_str = request.args.get('desc', None).lower()
desc = True if desc_str == 'true' else False if desc_str == 'false' else None

# get bookmarks for project from cache
if request.method == 'GET':
res_bookmarks = get_bookmarks(user_name, short_name)
res_bookmarks = get_bookmarks(user_name, short_name, order_by, desc)

# add a bookmark
elif request.method == 'POST':
bookmark_name = request.json.get('name', None)
bookmark_name = str(request.json.get('name', None))
bookmark_url = request.json.get('url', None)
try:
res_bookmarks = add_bookmark(user_name, short_name, bookmark_name, bookmark_url)
res_bookmarks = add_bookmark(user_name, short_name, bookmark_name, bookmark_url, order_by, desc)
except ValueError as e:
error_msg = str(e)
current_app.logger.exception(f'Bad request: {error_msg}, project: {short_name}, bookmark_name:{bookmark_name}')
return jsonify({"description": error_msg}), 400

# delete a bookmark
elif request.method == 'DELETE':
bookmark_name = request.json.get('name', None)
bookmark_name = str(request.json.get('name', None))
try:
res_bookmarks = delete_bookmark(user_name, short_name, bookmark_name)
res_bookmarks = delete_bookmark(user_name, short_name, bookmark_name, order_by, desc)
except ValueError as e:
error_msg = str(e)
current_app.logger.exception(f'Bad request: {error_msg}, project: {short_name}, bookmark_name:{bookmark_name}')
Expand Down
6 changes: 5 additions & 1 deletion pybossa/view/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from pybossa.model.project_stats import ProjectStats
from pybossa.model.webhook import Webhook
from pybossa.model.blogpost import Blogpost
from pybossa.view.account import get_bookmarks
from pybossa.util import (Pagination, admin_required, get_user_id_or_ip, rank,
handle_content_type, redirect_content_type,
get_avatar_url, admin_or_subadmin_required,
Expand Down Expand Up @@ -1781,6 +1782,8 @@ def get_users_completed(task):
# Populate list of user names for "Completed By" column.
get_users_fullname(page_tasks, lambda task: get_users_completed(task), 'completed_users')

taskbrowse_bookmarks = get_bookmarks(current_user.name, short_name, None, None)

valid_user_preferences = app_settings.upref_mdata.get_valid_user_preferences() \
if app_settings.upref_mdata else {}
language_options = valid_user_preferences.get('languages')
Expand Down Expand Up @@ -1812,7 +1815,8 @@ def get_users_completed(task):
can_know_task_is_gold=can_know_task_is_gold,
allow_taskrun_edit=allow_taskrun_edit,
regular_user=regular_user,
admin_subadmin_coowner=admin_subadmin_coowner)
admin_subadmin_coowner=admin_subadmin_coowner,
taskbrowse_bookmarks=taskbrowse_bookmarks)


return handle_content_type(data)
Expand Down

0 comments on commit 55586cb

Please sign in to comment.