def perform_search(self, alert: UserAlert) -> List[dict]:
        if self.override_since is not None:
            since = self.override_since
        elif alert.last_match is not None:
            since = alert.last_match
        else:
            since = timezone.now() - self.fallback_timeframe

        search = MainappSearch(
            alert.get_search_params(),
            extra_filter=[Q("range", modified={"gte": since.isoformat()})],
        )
        executed = search.execute()
        return [parse_hit(hit) for hit in executed.hits]
    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()
示例#3
0
    def perform_search(self, alert: UserAlert) -> List[dict]:
        if self.override_since is not None:
            since = self.override_since
        elif alert.last_match is not None:
            since = alert.last_match
        else:
            since = timezone.now() - self.fallback_timeframe

        params = alert.get_search_params()
        params["after"] = str(since)
        mainapp_search = MainappSearch(params)

        executed = mainapp_search.execute()
        results = [parse_hit(hit) for hit in executed.hits]

        return results
示例#4
0
    def perform_search(self, alert: UserAlert, override_since=None):
        if override_since is not None:
            since = override_since
        else:
            if alert.last_match is not None:
                since = alert.last_match
            else:
                since = timezone.now() - datetime.timedelta(days=14)

        params = alert.get_search_params()
        params["after"] = str(since)
        mainapp_search = MainappSearch(params)

        executed = mainapp_search.execute()
        results = [parse_hit(hit) for hit in executed.hits]

        return results
    def perform_search(self, alert: UserAlert, override_since=None):
        if override_since is not None:
            since = override_since
        else:
            if alert.last_match is not None:
                since = alert.last_match
            else:
                since = timezone.now() - datetime.timedelta(days=14)

        options, s, errors = params_to_query(alert.get_search_params())
        s = add_modified_since(s, since)

        self.stdout.write("Alert: %s since %s" % (alert.search_string, since))
        results = []
        executed = s.execute()
        for hit in executed:
            result = hit.__dict__['_d_']  # Extract the raw fields from the hit
            result["type"] = hit.meta.doc_type.replace("_document", "").replace("_", "-")
            results.append(result)

        return results
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]
def is_subscribed_to_search(user, params: dict):
    if not user.pk:
        return False
    else:
        return UserAlert.user_has_alert(user, params)
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 = (
                timezone.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)