mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Designate feature mixin classes & employ class_prepared signal to register features
This commit is contained in:
@ -57,21 +57,24 @@ class FeatureQuery:
|
||||
return query
|
||||
|
||||
|
||||
def register_features(model, features):
|
||||
if 'model_features' not in registry:
|
||||
registry['model_features'] = {
|
||||
f: collections.defaultdict(list) for f in EXTRAS_FEATURES
|
||||
}
|
||||
for feature in features:
|
||||
if feature not in EXTRAS_FEATURES:
|
||||
raise ValueError(f"{feature} is not a valid extras feature!")
|
||||
app_label, model_name = model._meta.label_lower.split('.')
|
||||
registry['model_features'][feature][app_label].append(model_name)
|
||||
|
||||
|
||||
def extras_features(*features):
|
||||
"""
|
||||
Decorator used to register extras provided features to a model
|
||||
"""
|
||||
def wrapper(model_class):
|
||||
# Initialize the model_features store if not already defined
|
||||
if 'model_features' not in registry:
|
||||
registry['model_features'] = {
|
||||
f: collections.defaultdict(list) for f in EXTRAS_FEATURES
|
||||
}
|
||||
for feature in features:
|
||||
if feature in EXTRAS_FEATURES:
|
||||
app_label, model_name = model_class._meta.label_lower.split('.')
|
||||
registry['model_features'][feature][app_label].append(model_name)
|
||||
else:
|
||||
raise ValueError('{} is not a valid extras feature!'.format(feature))
|
||||
register_features(model_class, features)
|
||||
return model_class
|
||||
return wrapper
|
||||
|
Reference in New Issue
Block a user