Skip to content

Commit

Permalink
subscribers: modify functions for 'user' argument
Browse files Browse the repository at this point in the history
* Necessary for bulk subscription feature where current_user undefined.
  • Loading branch information
GraemeWatt committed Oct 31, 2024
1 parent 98e46a0 commit 6980564
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 6 additions & 4 deletions hepdata/modules/records/subscribers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
from .models import Subscribers


def is_current_user_subscribed_to_record(recid):
if not current_user.is_authenticated:
return False
def is_current_user_subscribed_to_record(recid, user=None):
if not user:
user = current_user
if not current_user.is_authenticated:
return False

return Subscribers.query.filter(Subscribers.publication_recid == recid,
Subscribers.subscribers.contains(current_user)).count() > 0
Subscribers.subscribers.contains(user)).count() > 0


def get_users_subscribed_to_record(recid):
Expand Down
12 changes: 8 additions & 4 deletions hepdata/modules/records/subscribers/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ def list_subscriptions_for_user():


@blueprint.route('/subscribe/<int:recid>', methods=['POST'])
@login_required
def subscribe(recid):
def subscribe(recid, user=None):
if not user:
user = current_user
if not current_user.is_authenticated:
return jsonify({"success": False, "status_code": 500})

record_subscribers = get_or_create(db.session, Subscribers, publication_recid=recid)

try:
if not current_user in record_subscribers.subscribers:
record_subscribers.subscribers.append(current_user)
if not user in record_subscribers.subscribers:
record_subscribers.subscribers.append(user)

db.session.add(record_subscribers)
db.session.commit()
Expand Down

0 comments on commit 6980564

Please sign in to comment.