def detail_link(thing): """ Get a link to a detail view that can be shown for an object with this type For example, a 'treephoto' instance provides a link to the given tree. """ name = thing.__class__.__name__ nameLower = name.lower() if nameLower in MODEL_DETAILS: return MODEL_DETAILS[nameLower](thing) elif MapFeature.has_subclass(name): return MODEL_DETAILS['mapfeature'](thing) else: return None
def safe_get_model_class(model_string): """ In a couple of cases we want to be able to convert a string into a valid django model class. For instance, if we have 'Plot' we want to get the actual class for 'treemap.models.Plot' in a safe way. This function returns the class represented by the given model if it exists in 'treemap.models' """ from treemap.models import MapFeature # All of our models live in 'treemap.models', so # we can start with that namespace models_module = __import__('treemap.models') if hasattr(models_module.models, model_string): return getattr(models_module.models, model_string) elif MapFeature.has_subclass(model_string): return MapFeature.get_subclass(model_string) else: raise ValidationError(trans('invalid model type'))
def safe_get_model_class(model_string): """ In a couple of cases we want to be able to convert a string into a valid django model class. For instance, if we have 'Plot' we want to get the actual class for 'treemap.models.Plot' in a safe way. This function returns the class represented by the given model if it exists in 'treemap.models' """ from treemap.models import MapFeature # All of our models live in 'treemap.models', so # we can start with that namespace models_module = __import__('treemap.models') if hasattr(models_module.models, model_string): return getattr(models_module.models, model_string) elif MapFeature.has_subclass(model_string): return MapFeature.get_subclass(model_string) else: raise ValidationError(trans('invalid model type: "%s"') % model_string)