def handle_subscribe_requests(request, search_params: dict, msg_subscribed, msg_unsubscribed, msg_already_subscribed): if 'subscribe' in request.POST: if request.user.is_anonymous: messages.error(request, 'In order to subscribe to new results, you need to log in') raise NeedsLoginError(reverse('account_login') + '?next=' + request.path) if UserAlert.user_has_alert(request.user, search_params): messages.info(request, msg_already_subscribed) else: alert = UserAlert() alert.user = request.user alert.set_search_params(search_params) alert.last_match = datetime.now() # Prevent getting notifications about old entries alert.save() messages.success(request, msg_subscribed) if 'unsubscribe' in request.POST and request.user: if request.user.is_anonymous: messages.error(request, 'In order to subscribe to new results, you need to log in') raise NeedsLoginError(reverse('account_login') + '?next=' + request.path) alert = UserAlert.find_user_alert(request.user, search_params) if alert: alert.delete() messages.success(request, msg_unsubscribed)
def _create_user_with_alerts(self, email, alerts): newuser = User() newuser.email = email newuser.username = email newuser.is_active = 1 newuser.save() for alert in alerts: alert_object = UserAlert() alert_object.search_string = alert alert_object.last_match = None alert_object.user = newuser alert_object.save()
def test_import_json(send_mail_function): """This test runs with elasticsearch if available and otherwise uses saved responses""" # Create the base state old = load_ris_data("importer/test-data/amtzell_old.json") body = Body(name=old.meta.name, short_name=old.meta.name, ags=old.meta.ags) body.save() import_data(body, old) actual = make_db_snapshot() expected = json.loads( Path("importer/test-data/amtzell_old_db.json").read_text()) assert expected == actual last_notification = timezone.now() # Create notification user = User(username="******", email="*****@*****.**") user.save() UserProfile.objects.create(user=user) user_alert = UserAlert( user=user, search_string="Digitalisierungsstrategie", created=datetime.fromisoformat("2008-01-01T12:00:00+01:00"), ) user_alert.save() # Import the new data new = load_ris_data("importer/test-data/amtzell_new.json") import_data(body, new) actual = make_db_snapshot() expected = json.loads( Path("importer/test-data/amtzell_new_db.json").read_text()) assert expected == actual # Check that the notification was sent elasticsearch_mock = ElasticsearchMock({ "importer/test-data/notification_request.json": "importer/test-data/notification_response.json" }) if is_es_online(): context = contextlib.nullcontext() else: context = mock.patch( "elasticsearch_dsl.search.get_connection", new=lambda _alias: elasticsearch_mock, ) with context: if is_es_online(): notifier = NotifyUsers(last_notification) else: notifier = NotifyUsers( datetime.fromisoformat("2020-05-17T12:07:37.887853+00:00")) notifier.notify_all() assert send_mail_function.call_count == 1 assert send_mail_function.call_args[0][0] == "*****@*****.**" assert "Digitalisierungsstrategie" in send_mail_function.call_args[0][ 2] assert "Digitalisierungsstrategie" in send_mail_function.call_args[0][ 3]