def add_default_permissions(instance, roles=None, models=None): if roles is None: roles = Role.objects.filter(instance=instance) if models is None: models = leaf_subclasses(Authorizable) for role in roles: _add_default_permissions(models, role, instance)
def _make_permissions(field_permission): def make_model_perms(Model): return tuple((Model._meta.object_name, field_name, field_permission) for field_name in Model().tracked_fields) models = leaf_subclasses(Authorizable) model_permissions = [make_model_perms(Model) for Model in models] permissions = sum(model_permissions, ()) # flatten return permissions
def _make_permissions(field_permission): def make_model_perms(Model): return tuple( (Model._meta.object_name, field_name, field_permission) for field_name in Model().tracked_fields) models = leaf_subclasses(MapFeature) + [Tree, TreePhoto, Species] model_permissions = [make_model_perms(Model) for Model in models] permissions = sum(model_permissions, ()) # flatten return permissions
def _make_permissions(field_permission): def make_model_perms(Model): return tuple( (Model._meta.object_name, field_name, field_permission) for field_name in Model().tracked_fields) models = leaf_subclasses(Authorizable) model_permissions = [make_model_perms(Model) for Model in models] permissions = sum(model_permissions, ()) # flatten return permissions
def _get_model_class(class_dict, cls, model_name): """ Convert a model name (as a string) into the model class """ if model_name.startswith('udf:'): from udf import UserDefinedCollectionValue return UserDefinedCollectionValue if not class_dict: # One-time load of class dictionary for c in leaf_subclasses(cls): class_dict[c.__name__] = c return class_dict[model_name]
def subclass_dict(cls): return {C.map_feature_type: C for C in leaf_subclasses(MapFeature)}
def set_map_feature_updated_at(): models = [Model.map_feature_type for Model in leaf_subclasses(MapFeature)] if not models: raise Exception("Could not find any map_feature subclasses") models_in = "('%s')" % "','".join(models) # For a baseline, pull the most recent change to the MapFeature itself. # This depends on the fact that all the MapFeature subclasses share the # same id pool and ids do not overlap. # NOTE: This MUST be run first. Additional update queries compare # dates against the updated_at values set by this statement. execute_sql(""" UPDATE treemap_mapfeature SET updated_at = a.updated_at FROM ( SELECT model_id as id, MAX(created) AS updated_at FROM treemap_audit WHERE treemap_audit.model IN %s GROUP BY model_id ) a WHERE a.id = treemap_mapfeature.id;""" % models_in) # If the tree associated with a MapFeature has been updated more # recently than the MapFeature, copy the tree's most recent # update date to the MapFeature execute_sql(""" UPDATE treemap_mapfeature SET updated_at = GREATEST(treemap_mapfeature.updated_at, b.updated_at) FROM ( SELECT plot_id as id, a.updated_at FROM treemap_tree JOIN ( SELECT model_id as tree_id, MAX(created) AS updated_at FROM treemap_audit WHERE treemap_audit.model = 'Tree' GROUP BY model_id ) a ON a.tree_id = treemap_tree.id ) b WHERE b.id = treemap_mapfeature.id;""") # If a photo associated with a Tree or MapFeature has been updated more # recently than the MapFeature, copy the photo's most recent # update date to the MapFeature. TreePhoto is a subclass of # MapFeaturePhoto so they share the same id range. execute_sql(""" UPDATE treemap_mapfeature SET updated_at = GREATEST(treemap_mapfeature.updated_at, b.updated_at) FROM ( SELECT map_feature_id as id, a.updated_at FROM treemap_mapfeaturephoto JOIN ( SELECT model_id as photo_id, MAX(created) AS updated_at FROM treemap_audit WHERE treemap_audit.model in ('TreePhoto', 'MapFeaturePhoto') GROUP BY model_id ) a ON a.photo_id = treemap_mapfeaturephoto.id ) b WHERE b.id = treemap_mapfeature.id; """)
def get_filterable_audit_models(): map_features = [c.__name__ for c in leaf_subclasses(MapFeature)] models = map_features + ['Tree'] return {model.lower(): model for model in models}