From f4d3415c11032afde1588839654a661cefc52118 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Tue, 24 Oct 2023 16:11:08 +0200 Subject: [PATCH] First work to bring back Community Update functionality --- app/corpora/followers/json_routes.py | 116 +++++++++--------- app/main/routes.py | 24 ++-- app/templates/_sidenav.html.j2 | 11 +- app/templates/corpora/corpus.html.j2 | 32 ++--- app/templates/users/settings/settings.html.j2 | 24 ++-- app/users/json_routes.py | 44 +++---- app/users/routes.py | 58 ++++----- 7 files changed, 154 insertions(+), 155 deletions(-) diff --git a/app/corpora/followers/json_routes.py b/app/corpora/followers/json_routes.py index db6bb635..87299862 100644 --- a/app/corpora/followers/json_routes.py +++ b/app/corpora/followers/json_routes.py @@ -12,65 +12,65 @@ from ..decorators import corpus_follower_permission_required from . import bp -# @bp.route('//followers', methods=['POST']) -# @corpus_follower_permission_required('MANAGE_FOLLOWERS') -# @content_negotiation(consumes='application/json', produces='application/json') -# def create_corpus_followers(corpus_id): -# usernames = request.json -# if not (isinstance(usernames, list) or all(isinstance(u, str) for u in usernames)): -# abort(400) -# corpus = Corpus.query.get_or_404(corpus_id) -# for username in usernames: -# user = User.query.filter_by(username=username, is_public=True).first_or_404() -# user.follow_corpus(corpus) -# db.session.commit() -# response_data = { -# 'message': f'Users are now following "{corpus.title}"', -# 'category': 'corpus' -# } -# return response_data, 200 +@bp.route('//followers', methods=['POST']) +@corpus_follower_permission_required('MANAGE_FOLLOWERS') +@content_negotiation(consumes='application/json', produces='application/json') +def create_corpus_followers(corpus_id): + usernames = request.json + if not (isinstance(usernames, list) or all(isinstance(u, str) for u in usernames)): + abort(400) + corpus = Corpus.query.get_or_404(corpus_id) + for username in usernames: + user = User.query.filter_by(username=username, is_public=True).first_or_404() + user.follow_corpus(corpus) + db.session.commit() + response_data = { + 'message': f'Users are now following "{corpus.title}"', + 'category': 'corpus' + } + return response_data, 200 -# @bp.route('//followers//role', methods=['PUT']) -# @corpus_follower_permission_required('MANAGE_FOLLOWERS') -# @content_negotiation(consumes='application/json', produces='application/json') -# def update_corpus_follower_role(corpus_id, follower_id): -# role_name = request.json -# if not isinstance(role_name, str): -# abort(400) -# cfr = CorpusFollowerRole.query.filter_by(name=role_name).first() -# if cfr is None: -# abort(400) -# cfa = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=follower_id).first_or_404() -# cfa.role = cfr -# db.session.commit() -# response_data = { -# 'message': f'User "{cfa.follower.username}" is now {cfa.role.name}', -# 'category': 'corpus' -# } -# return response_data, 200 +@bp.route('//followers//role', methods=['PUT']) +@corpus_follower_permission_required('MANAGE_FOLLOWERS') +@content_negotiation(consumes='application/json', produces='application/json') +def update_corpus_follower_role(corpus_id, follower_id): + role_name = request.json + if not isinstance(role_name, str): + abort(400) + cfr = CorpusFollowerRole.query.filter_by(name=role_name).first() + if cfr is None: + abort(400) + cfa = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=follower_id).first_or_404() + cfa.role = cfr + db.session.commit() + response_data = { + 'message': f'User "{cfa.follower.username}" is now {cfa.role.name}', + 'category': 'corpus' + } + return response_data, 200 -# @bp.route('//followers/', methods=['DELETE']) -# def delete_corpus_follower(corpus_id, follower_id): -# cfa = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=follower_id).first_or_404() -# if not ( -# current_user.id == follower_id -# or current_user == cfa.corpus.user -# or CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=current_user.id).first().role.has_permission('MANAGE_FOLLOWERS') -# or current_user.is_administrator()): -# abort(403) -# if current_user.id == follower_id: -# flash(f'You are no longer following "{cfa.corpus.title}"', 'corpus') -# response = make_response() -# response.status_code = 204 -# else: -# response_data = { -# 'message': f'"{cfa.follower.username}" is not following "{cfa.corpus.title}" anymore', -# 'category': 'corpus' -# } -# response = jsonify(response_data) -# response.status_code = 200 -# cfa.follower.unfollow_corpus(cfa.corpus) -# db.session.commit() -# return response +@bp.route('//followers/', methods=['DELETE']) +def delete_corpus_follower(corpus_id, follower_id): + cfa = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=follower_id).first_or_404() + if not ( + current_user.id == follower_id + or current_user == cfa.corpus.user + or CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=current_user.id).first().role.has_permission('MANAGE_FOLLOWERS') + or current_user.is_administrator()): + abort(403) + if current_user.id == follower_id: + flash(f'You are no longer following "{cfa.corpus.title}"', 'corpus') + response = make_response() + response.status_code = 204 + else: + response_data = { + 'message': f'"{cfa.follower.username}" is not following "{cfa.corpus.title}" anymore', + 'category': 'corpus' + } + response = jsonify(response_data) + response.status_code = 200 + cfa.follower.unfollow_corpus(cfa.corpus) + db.session.commit() + return response diff --git a/app/main/routes.py b/app/main/routes.py index 3be92196..080616ec 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -78,15 +78,15 @@ def terms_of_use(): ) -# @bp.route('/social-area') -# @register_breadcrumb(bp, '.social_area', 'groupSocial Area') -# @login_required -# def social_area(): -# corpora = Corpus.query.filter(Corpus.is_public == True, Corpus.user != current_user).all() -# users = User.query.filter(User.is_public == True, User.id != current_user.id).all() -# return render_template( -# 'main/social_area.html.j2', -# title='Social Area', -# corpora=corpora, -# users=users -# ) +@bp.route('/social-area') +@register_breadcrumb(bp, '.social_area', 'groupSocial Area') +@login_required +def social_area(): + corpora = Corpus.query.filter(Corpus.is_public == True, Corpus.user != current_user).all() + users = User.query.filter(User.is_public == True, User.id != current_user.id).all() + return render_template( + 'main/social_area.html.j2', + title='Social Area', + corpora=corpora, + users=users + ) diff --git a/app/templates/_sidenav.html.j2 b/app/templates/_sidenav.html.j2 index 06fc0b97..765e566e 100644 --- a/app/templates/_sidenav.html.j2 +++ b/app/templates/_sidenav.html.j2 @@ -3,13 +3,12 @@
- {#
+
user-image -
#} - {# Change col s12 to col s5 to show user image #} -
+
+
{% if current_user.username|length > 18 %} {{ current_user.username[:15] + '...' }} @@ -71,7 +70,7 @@
  • Corpus Analysis
  • - {#
  • +
  • rocket_launchSocial Area -
  • #} +
  • Account
  • diff --git a/app/templates/corpora/corpus.html.j2 b/app/templates/corpora/corpus.html.j2 index b6e600b9..e31ed50c 100644 --- a/app/templates/corpora/corpus.html.j2 +++ b/app/templates/corpora/corpus.html.j2 @@ -65,14 +65,14 @@ - {#
  • - {# {% if cfr.has_permission('MANAGE_FOLLOWERS') %} + {% if cfr.has_permission('MANAGE_FOLLOWERS') %} Social
    @@ -82,7 +82,7 @@ linkShare link
    - {% endif %} #} + {% endif %}
    @@ -102,7 +102,7 @@ - {# {% if cfr.has_permission('MANAGE_FOLLOWERS') %} + {% if cfr.has_permission('MANAGE_FOLLOWERS') %}
    @@ -111,7 +111,7 @@
    - {% endif %} #} + {% endif %} @@ -121,7 +121,7 @@ {{ super() }} {% if current_user == corpus.user or current_user.is_administrator() %} -{#