def process_requested_options(self, requested_options): """Convert requested options to usable types. Args: requested_options: QueryDict of requested_options (QueryDict). Method does the following: - Update all values through str_to_bool utility function. - Raises 404 error is requested option cannot be found. - Raises 404 is option given with invalid value. Returns: QueryDict of converted requested options (QueryDict). """ requested_options = requested_options.copy() for option in self.valid_options.keys(): values = requested_options.getlist(option) if not values: raise QueryParameterMissingError(option) for (i, value) in enumerate(values): update_value = str_to_bool(value) if update_value not in self.valid_options[option]: raise QueryParameterInvalidError(option, value) values[i] = update_value requested_options.setlist(option, values) return requested_options
def data(self): """Create a image for Piano Keys resource. Returns: A list of dictionaries for each resource page. """ label_type = self.options["label"].value image_path = "static/img/resources/piano-keys/keyboard.png" image = Image.open(image_path) if str_to_bool(label_type): self.label_keys(image, label_type) image = image.rotate(90, expand=True) return {"type": "image", "data": image}
def retrieve_query_parameter(request, parameter, valid_options=None): """Retrieve the query parameter. If the parameter cannot be found, or is not found in the list of valid options, then a 404 error is raised. Args: request: Request object (Request). parameter: Parameter to retrieve (str). valid_options: If provided, a list of valid options (list of str). Returns: String value of parameter. """ value = request.get(parameter, None) if value is None: raise Http404("{} parameter not specified.".format(parameter)) value = str_to_bool(value) if valid_options and value not in valid_options: raise Http404("{} parameter not valid.".format(parameter)) return value
def test_true(self): self.assertTrue(str_to_bool("True"))
def test_other_value(self): self.assertEqual(str_to_bool("example"), "example")
def test_no(self): self.assertFalse(str_to_bool("no"))
def test_yes(self): self.assertTrue(str_to_bool("yes"))
def test_false(self): self.assertFalse(str_to_bool("False"))