Beispiel #1
0
def create_saved_search():
  """Create a saved search.

  Endpoint creating saved search with provided parameters. Request payload
  should contain saved search `name`, `search_type` and `object_type`. Also it
  could contain saved search `filters`. If there will any error during saved
  search creation, 400 status code and corresponding error will be returned.

  Returns:
    Flask Response object containing JSON representation of created saved
    search or error message if error occurred.
  """
  user = login.get_current_user(use_external_user=False)

  data = request.get_json()

  try:
    search = SavedSearch(
        data.get("name"),
        data.get("object_type"),
        user,
        data.get("search_type"),
        data.get("filters"),
    )
  except ValidationError as error:
    return make_error_response(error.message, 400, force_json=True)

  user.saved_searches.append(search)
  db.session.commit()

  return json_success_response(search)
  def setUpClass(cls):
    """
      Set up read-only test data to test GET requests.
    """

    super(TestSavedSearchDelete, cls).setUpClass()

    with app.app_context():
      cls._person_0 = Person(
          name="Aniki",
          email="aniki_baniki_{}@test.com".format(random()),
      )
      db.session.add(cls._person_0)
      db.session.flush()

      saved_search = SavedSearch(
          name="test_ss_1",
          object_type="Assessment",
          user=cls._person_0,
          search_type="GlobalSearch"
      )
      cls._person_0.saved_searches.append(saved_search)
      db.session.flush()

      cls._user_role = setup_user_role(cls._person_0)
      db.session.commit()

      cls._client, cls._headers = get_client_and_headers(
          app, cls._person_0,
      )

      cls._saved_search_id = saved_search.id
Beispiel #3
0
def create_saved_search():
    user = login.get_current_user(use_external_user=False)

    data = request.get_json()

    try:
        search = SavedSearch(
            data.get("query"),
            data.get("name"),
            data.get("object_type"),
            user,
        )
    except ValidationError as error:
        return make_error_response(error.message, 400, force_json=True)

    user.saved_searches.append(search)
    db.session.commit()

    return json_success_response(search)
Beispiel #4
0
    def setUpClass(cls):
        """
      Set up read-only test data to test GET requests.
    """

        super(TestSavedSearchDelete, cls).setUpClass()

        cls._person_0 = Person(
            name="Aniki",
            email="aniki_baniki_{}@test.com".format(random()),
        )
        db.session.add(cls._person_0)
        db.session.flush()

        saved_search = SavedSearch(
            name="test_ss_1",
            query=[{
                "object_name": "Assessment",
                "filters": {
                    "expression": {}
                },
                "limit": [0, 10],
                "order_by": [{
                    "name": "updated_at",
                    "desc": True
                }]
            }],
            object_type="Assessment",
            user=cls._person_0,
        )
        cls._person_0.saved_searches.append(saved_search)
        db.session.flush()

        cls._user_role = setup_user_role(cls._person_0)
        db.session.commit()

        cls._client, cls._headers = get_client_and_headers(
            app,
            cls._person_0,
        )

        cls._saved_search_id = saved_search.id
Beispiel #5
0
    def setUpClass(cls):
        """
      Set up read-only test data to test GET requests.
    """

        super(TestSavedSearchGet, cls).setUpClass()

        email_0 = "aniki_baniki_{}@test.com".format(random())
        cls._person_0 = Person(name="Aniki", email=email_0)

        email_1 = "baniki_aniki_{}@test.com".format(random())
        cls._person_1 = Person(name="Baniki", email=email_1)

        db.session.add(cls._person_0)
        db.session.add(cls._person_1)

        db.session.flush()

        cls._valid_query = [{
            "object_name": "Assessment",
            "filters": {
                "expression": {}
            },
            "limit": [0, 10],
            "order_by": [{
                "name": "updated_at",
                "desc": True
            }]
        }]

        locked_time = {
            "year": 2025,
            "month": 1,
            "day": 25,
            "minute": 0,
            "second": 0,
        }

        with freeze_time(datetime(**locked_time)) as frozen_time:
            for i in range(1, 5):
                user = cls._person_0 if i != 4 else cls._person_1

                saved_search = SavedSearch(
                    name="test_ss_{}".format(i),
                    query=cls._valid_query,
                    object_type="Assessment",
                    user=user,
                )
                user.saved_searches.append(saved_search)
                db.session.flush()
                #
                # we need to test the order in which saved
                # searches are returned in response to GET
                # request (descending order by created_at)
                # created_at precision is seconds
                #
                locked_time["second"] = i

                frozen_time.move_to(datetime(**locked_time))

        cls._user_role = setup_user_role(cls._person_0)
        db.session.commit()

        cls._client, cls._headers = get_client_and_headers(
            app,
            cls._person_0,
        )
Beispiel #6
0
    def setUpClass(cls):
        """
      Set up read-only test data to test GET requests.
    """

        super(TestSavedSearchGet, cls).setUpClass()

        with app.app_context():
            email_0 = "aniki_baniki_{}@test.com".format(random())
            cls._person_0 = Person(name="Aniki", email=email_0)

            email_1 = "baniki_aniki_{}@test.com".format(random())
            cls._person_1 = Person(name="Baniki", email=email_1)

            db.session.add(cls._person_0)
            db.session.add(cls._person_1)

            db.session.flush()

            locked_time = {
                "year": 2025,
                "month": 1,
                "day": 25,
                "minute": 0,
                "second": 0,
            }

            with freeze_time(datetime(**locked_time)) as frozen_time:
                for i in range(1, 5):
                    user = cls._person_0 if i != 4 else cls._person_1

                    saved_search = SavedSearch(
                        name="test_ss_{}".format(i),
                        object_type="Assessment",
                        user=user,
                        search_type=cls.SAVED_SEARCH_TYPE)
                    user.saved_searches.append(saved_search)
                    db.session.flush()
                    #
                    # we need to test the order in which saved
                    # searches are returned in response to GET
                    # request (descending order by created_at)
                    # created_at precision is seconds
                    #
                    locked_time["second"] = i

                    frozen_time.move_to(datetime(**locked_time))

                saved_search_program = SavedSearch(
                    name="test_program_ss",
                    object_type="Program",
                    user=cls._person_0,
                    search_type=cls.SAVED_SEARCH_TYPE)
                cls._person_0.saved_searches.append(saved_search_program)
                db.session.flush()

            cls._user_role = setup_user_role(cls._person_0)
            db.session.commit()

            cls._client, cls._headers = get_client_and_headers(
                app,
                cls._person_0,
            )