def get_content_type(ct_name): """ A helper function that returns ContentType object based on its slugified verbose_name_plural. Results of this function is cached to improve performance. :Parameters: - `ct_name`: Slugified verbose_name_plural of the target model. :Exceptions: - `Http404`: if no matching ContentType is found """ try: ct = CONTENT_TYPE_MAPPING[ct_name] except KeyError: for model in get_models(): if ct_name == slugify(model._meta.verbose_name_plural): ct = ContentType.objects.get_for_model(model) CONTENT_TYPE_MAPPING[ct_name] = ct break else: raise Http404 return ct
def test_by_brute_force(self): for m in get_models(): if issubclass(m, Publishable): ct = ContentType.objects.get_for_model(m) tools.assert_equals(ct, get_content_type(slugify(m._meta.verbose_name_plural)))