def get_saved_queries_created_by_app(): """Return saved queries created by the app. Returns: """ return SavedQuery.objects(user_id=ExploreExampleAppConfig.name).all()
def get_all(): """Get all Saved Query Returns: """ return SavedQuery.get_all()
def get_by_id(query_id): """Get a Saved Query Args: query_id: Returns: """ return SavedQuery.get_by_id(query_id)
def test_not_owner_returns_http_403(self, mock_saved_query_get_by_id): mock_saved_query_get_by_id.return_value = SavedQuery(user_id="1") mock_user = create_mock_user("2") response = RequestMock.do_request_delete(SavedQueryDetail.as_view(), mock_user, param={"pk": "0"}, data=None) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def get_all_by_user_and_template(user_id, template_id): """Gets a saved query by user id and template id Args: user_id: template_id: Returns: """ return SavedQuery.get_all_by_user_and_template(user_id, template_id)
def test_staff_returns_http_204(self, mock_saved_query_delete, mock_saved_query_get_by_id): mock_saved_query_get_by_id.return_value = SavedQuery(user_id="1") mock_saved_query_delete.return_value = None mock_user = create_mock_user("5", is_staff=True) response = RequestMock.do_request_delete(SavedQueryDetail.as_view(), mock_user, param={"pk": "0"}, data=None) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def insert_sub_elements_query(request): """Inserts a query for a sub element in the query builder Args: request: Returns: """ form_values = json.loads(request.POST["formValues"]) template_id = request.POST["templateID"] criteria_id = request.POST["criteriaID"] # get template template = template_api.get(template_id, request=request) # get template namespaces namespaces = get_namespaces(template.content) # get default prefix default_prefix = get_default_prefix(namespaces) # keep only selected fields form_values = [field for field in form_values if field["selected"] is True] errors = check_query_form(form_values, template_id, request=request) if len(errors) == 0: query = sub_elements_to_query(form_values, namespaces, default_prefix, request) displayed_query = sub_elements_to_pretty_query(form_values, namespaces, request) ui_id = "ui" + criteria_id[4:] temporary_query = SavedQuery( user_id=ExploreExampleAppConfig.name, template=template, query=json.dumps(query), displayed_query=displayed_query, ) saved_query_api.upsert(temporary_query) response_dict = { "criteriaID": criteria_id, "prettyQuery": displayed_query, "uiID": ui_id, "queryID": str(temporary_query.id), } else: return HttpResponseBadRequest("<br/>".join(errors), content_type="application/javascript") return HttpResponse(json.dumps(response_dict), content_type="application/javascript")
def post(self, request): """Save a query and update the html display Args: request: Returns: """ form_values = json.loads(request.POST["formValues"]) template_id = request.POST["templateID"] # Check that the user can save a query if "_auth_user_id" not in request.session: error = "You have to login to save a query." return HttpResponseBadRequest( error, content_type="application/javascript") # Check that the query is valid errors = check_query_form(form_values, template_id, request=request) if len(errors) == 0: try: query = self.fields_to_query_func(form_values, template_id, request=request) displayed_query = fields_to_pretty_query(form_values) # save the query in the data base saved_query = SavedQuery( user_id=str(request.user.id), template=template_api.get(template_id, request=request), query=json.dumps(query), displayed_query=displayed_query, ) saved_query_api.upsert(saved_query) except MongoQueryException as e: errors = [str(e)] return HttpResponseBadRequest( _render_errors(errors), content_type="application/javascript") else: return HttpResponseBadRequest( _render_errors(errors), content_type="application/javascript") return HttpResponse(json.dumps({}), content_type="application/javascript")
def generate_data_collection(self): """ Generate a Data collection. Returns: """ template = Template(filename="filename", content="<xml />", hash="hash").save() query_data_1 = "{\"$or\": [{\"list_content\": {\"$elemMatch\": {\"path\": \"/.*chemical-element-type/\", \"" \ "value\": \"Ac\"}}}, {\"list_content\": {\"$elemMatch\": " \ "{\"path\": \"/.*chemical-element-type.#text/\", \"value\": \"Ac\"}}}]}" self.data_1 = SavedQuery(user_id="1", template=template.id, query=query_data_1, displayed_query="Element is Ac").save() self.data_collection = [self.data_1]
def _create_saved_query(user_id=None, template=None): """Returns a saved query Args: user_id: template: Returns: """ if user_id is None: user_id = ObjectId() if template is None: template = ObjectId() return SavedQuery(id=ObjectId(), user_id=user_id, template=template, query="{}", displayed_query="query")
def generate_data_collection(self): """Generate a Data collection. Returns: """ template = Template(filename="filename", content="<xml />", hash="hash").save() query_data_1 = ( '{"$or": [{"list_content": {"$elemMatch": {"path": "/.*chemical-element-type/", "' 'value": "Ac"}}}, {"list_content": {"$elemMatch": ' '{"path": "/.*chemical-element-type.#text/", "value": "Ac"}}}]}') self.data_1 = SavedQuery( user_id="1", template=template.id, query=query_data_1, displayed_query="Element is Ac", ).save() self.data_collection = [self.data_1]