diff --git a/config.py b/config.py index e52b75d..f672974 100644 --- a/config.py +++ b/config.py @@ -32,14 +32,13 @@ # user_id for anonymous users - fix in deployment config BOOTSTRAP_USER_ID = 0 +NECTAR_REFERRERS = ["dev.scixplorer.org"] + # import endpoints HARBOUR_MYADS_IMPORT_ENDPOINT = 'https://api.adsabs.harvard.edu/v1/harbour/myads/classic/%s' MYADS_DAILY_TIME_RANGE = 2 # days MYADS_WEEKLY_TIME_RANGE = 6 # days -# scixplorer host -SCIXPLORER_HOST = 'scixplorer.org' - # arXiv categories and sub-categories ALLOWED_ARXIV_CLASSES = ['astro-ph', 'astro-ph.GA', 'astro-ph.CO', 'astro-ph.EP', 'astro-ph.HE', 'astro-ph.IM', 'astro-ph.SR', diff --git a/vault_service/tests/test_user.py b/vault_service/tests/test_user.py index 0736cdf..61b87f7 100644 --- a/vault_service/tests/test_user.py +++ b/vault_service/tests/test_user.py @@ -944,7 +944,7 @@ def test_scixplorer_referrer_updates_all_notifications(self): self.assertFalse(notification1.scix_ui) self.assertFalse(notification2.scix_ui) - # Create a third notification WITH Scixplorer Host (query type) + # Create a third notification WITH Scixplorer Referrer (query type) r = self.client.post( url_for('user.myads_notifications'), data=json.dumps({ @@ -957,9 +957,9 @@ def test_scixplorer_referrer_updates_all_notifications(self): content_type='application/json', headers={ 'Authorization': 'secret', - 'X-api-uid': '42', - }, - environ_overrides={'HTTP_HOST': self.app.config['SCIXPLORER_HOST']} + 'X-api-uid': '42', + 'Referer': 'https://dev.scixplorer.org/search' + } ) self.assertStatus(r, 200) self.assertTrue(r.json['name'] == 'Scixplorer Query') @@ -974,5 +974,36 @@ def test_scixplorer_referrer_updates_all_notifications(self): self.assertTrue(notification2.scix_ui) self.assertTrue(notification3.scix_ui) + # Create a fourth notification WITHOUT Scixplorer Host (query type) + r = self.client.post( + url_for('user.myads_notifications'), + data=json.dumps({ + 'name': 'Scixplorer Query', + 'qid': qid, + 'stateful': True, + 'frequency': 'daily', + 'type': 'query' + }), + content_type='application/json', + headers={ + 'Authorization': 'secret', + 'X-api-uid': '42', + } + ) + self.assertStatus(r, 200) + self.assertTrue(r.json['name'] == 'Scixplorer Query') + fourth_notification_id = r.json['id'] + + # Verify ALL notifications now have scix_ui=True + with self.app.session_scope() as session: + notification1 = session.query(MyADS).filter_by(id=first_notification_id).first() + notification2 = session.query(MyADS).filter_by(id=second_notification_id).first() + notification3 = session.query(MyADS).filter_by(id=third_notification_id).first() + notification4 = session.query(MyADS).filter_by(id=fourth_notification_id).first() + self.assertTrue(notification1.scix_ui) + self.assertTrue(notification2.scix_ui) + self.assertTrue(notification3.scix_ui) + self.assertTrue(notification4.scix_ui) + if __name__ == '__main__': unittest.main() diff --git a/vault_service/views/user.py b/vault_service/views/user.py index cc8d2e7..b0ac6f2 100644 --- a/vault_service/views/user.py +++ b/vault_service/views/user.py @@ -294,7 +294,7 @@ def _create_myads_notification(payload=None, headers=None, user_id=None): except KeyError: return json.dumps({'msg': 'No notification type passed'}), 400 - scix_ui_header = current_app.config['SCIXPLORER_HOST'] in request.headers.get('Host', '') + scix_ui_header = urlparse.urlparse(request.referrer).netloc in current_app.config.get("NECTAR_REFERRERS", ["dev.scixplorer.org"]) with current_app.session_scope() as session: try: @@ -407,15 +407,15 @@ def _create_myads_notification(payload=None, headers=None, user_id=None): # qid is an int in the myADS table myads_id = setup.id - # If user is coming from scixplorer but existing notifications don't have scix_ui set to True, update them - if scix_ui_header: - # Check if there are any of user's notifications with scix_ui=False - existing_notifications = session.query(MyADS).filter_by(user_id=user_id).filter_by(scix_ui=False).all() - current_app.logger.info(f'Total notifications to update: {len(existing_notifications)}') - if existing_notifications: - for notification in existing_notifications: - current_app.logger.info(f'Updating notification: {notification.id} for user: {user_id}') - notification.scix_ui = True + # If scix_ui_header is True or any of the notifications have scix_ui=True, update all notifications to scix_ui=True + existing_notifications = session.query(MyADS).filter_by(user_id=user_id).all() + notifications_scix_ui_is_false = [notification for notification in existing_notifications if notification.scix_ui == False] + if scix_ui_header or (len(notifications_scix_ui_is_false) < len(existing_notifications)): + current_app.logger.info(f'Total notifications to update: {len(notifications_scix_ui_is_false)}') + + for notification in notifications_scix_ui_is_false: + current_app.logger.info(f'Updating notification: {notification.id} for user: {user_id}') + notification.scix_ui = True session.commit() except exc.StatementError as e: