本文整理汇总了Python中treemap.lib.object_caches.udf_defs函数的典型用法代码示例。如果您正苦于以下问题:Python udf_defs函数的具体用法?Python udf_defs怎么用?Python udf_defs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了udf_defs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_udfc_search_fields
def get_udfc_search_fields(self, user):
from treemap.models import InstanceUser
from treemap.udf import UDFModel
from treemap.util import to_object_name, leaf_models_of_class
from treemap.lib.perms import udf_write_level, READ, WRITE
try:
iu = self.instanceuser_set.get(user__pk=user.pk)
except InstanceUser.DoesNotExist:
iu = None
data = DotDict({"models": set(), "udfc": {}})
for clz in leaf_models_of_class(UDFModel):
model_name = clz.__name__
for k, v in clz.collection_udf_settings.items():
udfds = (u for u in udf_defs(self, model_name) if u.name == k)
for udfd in udfds:
if udf_write_level(iu, udfd) in (READ, WRITE):
_base_nest_path = "udfc.%s." % (to_object_name(k))
ids_nest_path = "%sids.%s" % (_base_nest_path, to_object_name(model_name))
models_nest_path = "%smodels.%s" % (_base_nest_path, to_object_name(model_name))
data[ids_nest_path] = udfd.pk
data[models_nest_path] = {"udfd": udfd, "fields": udfd.datatype_dict[0]["choices"]}
p = "udfc.%s." % to_object_name(k)
data[p + "action_verb"] = v["action_verb"]
data[p + "range_field_key"] = v["range_field_key"]
data[p + "action_field_key"] = v["action_field_key"]
data["models"] |= {clz}
return data
示例2: get_udfc_search_fields
def get_udfc_search_fields(self, user):
from treemap.models import InstanceUser
from treemap.udf import UDFModel
from treemap.util import to_object_name, leaf_models_of_class
from treemap.lib.perms import udf_write_level, READ, WRITE
try:
iu = self.instanceuser_set.get(user__pk=user.pk)
except InstanceUser.DoesNotExist:
iu = None
data = DotDict({'models': set(), 'udfc': {}})
for clz in (leaf_models_of_class(UDFModel)):
model_name = clz.__name__
items = (
(k, v) for k, v
in getattr(clz, 'collection_udf_settings', {}).iteritems())
for k, v in items:
udfds = (u for u in udf_defs(self, model_name) if u.name == k)
for udfd in udfds:
if udf_write_level(iu, udfd) in (READ, WRITE):
nest_path = ('udfc.%s.models.%s' %
(to_object_name(k),
to_object_name(model_name)))
data[nest_path] = {
'udfd': udfd,
'fields': udfd.datatype_dict[0]['choices']
}
p = 'udfc.%s.' % to_object_name(k)
data[p + 'action_verb'] = v['action_verb']
data[p + 'range_field_key'] = v['range_field_key']
data[p + 'action_field_key'] = v['action_field_key']
data['models'] |= {clz}
return data
示例3: udf_list
def udf_list(request, instance):
editable_udf_models = instance.editable_udf_models()
udf_models = \
sorted([{'name': clz.__name__,
'display_name': clz.display_name(instance)}
for clz in editable_udf_models['core']],
key=lambda model: model['name'],
reverse=True) + \
sorted([{'name': clz.__name__,
'display_name': clz.display_name(instance)}
for clz in editable_udf_models['gsi']],
key=lambda model: model['name'])
editable_gsi_models = [clz.__name__ for clz in editable_udf_models['gsi']]
udf_model_names = sorted([model['name'] for model in udf_models])
udfs = sorted([udf for udf in udf_defs(instance)
if udf.model_type in udf_model_names],
key=lambda udf: (udf.model_type, udf.iscollection,
udf.name))
def dict_update(d1, d2):
d1.update(d2)
return d1
udf_models = [dict_update(model, {
'specs': [{'udf': udf, 'datatype': _get_type_display(udf)}
for udf in udfs if udf.model_type == model['name']]
}) for model in udf_models]
return {
"udf_models": udf_models,
"editable_gsi_models": editable_gsi_models
}
示例4: _commit_tree_data
def _commit_tree_data(self, data, plot, tree, tree_edited):
for tree_attr, field_name in TreeImportRow.TREE_MAP.iteritems():
value = data.get(field_name, None)
if value:
tree_edited = True
if tree is None:
tree = Tree(instance=plot.instance)
setattr(tree, tree_attr, value)
ie = self.import_event
tree_udf_defs = udf_defs(ie.instance, 'Tree')
for udf_def in tree_udf_defs:
udf_column_name = ie.get_udf_column_name(udf_def)
value = data.get(udf_column_name, None)
# Legitimate values could be falsey
if value is not None:
tree_edited = True
if tree is None:
tree = Tree(instance=plot.instance)
tree.udfs[udf_def.name] = \
self._import_value_to_udf_value(udf_def, value)
if tree_edited:
tree.plot = plot
tree.save_with_system_user_bypass_auth()
tree.plot.update_updated_fields(ie.owner)
示例5: _get_udfc_search_fields
def _get_udfc_search_fields(self):
from treemap.util import to_object_name
empty_udfc = {to_object_name(n_k):
{to_object_name(m_k): {'fields': [], 'udfd': None}
for m_k in UDFC_MODELS}
for n_k in UDFC_NAMES}
udfds = []
for model_name in UDFC_MODELS:
for udfd in udf_defs(self, model_name):
if udfd.name in UDFC_NAMES:
udfds.append(udfd)
udfc = deepcopy(empty_udfc)
for udfd in udfds:
udfd_info = {
'udfd': udfd,
'fields': udfd.datatype_dict[0]['choices']
}
name_dict = udfc[to_object_name(udfd.name)]
name_dict[to_object_name(udfd.model_type)] = udfd_info
return udfc
示例6: alert_identifiers
def alert_identifiers():
def identifier(udf):
model_name = udf.model_type.lower()
return 'udf:%(model)s:%(pk)s.Status' % {
'model': model_name, 'pk': udf.pk}
return [identifier(udf) for udf in udf_defs(instance)
if udf.iscollection and udf.name == 'Alerts']
示例7: _values_for_model
def _values_for_model(instance, job, table, model, select, select_params, prefix=None):
if prefix:
prefix += "__"
else:
prefix = ""
prefixed_names = []
model_class = safe_get_model_class(model)
dummy_instance = model_class()
for field_name in (
perm.field_name
for perm in field_permissions(job.user, instance, model)
if perm.permission_level >= FieldPermission.READ_ONLY
):
prefixed_name = prefix + field_name
if field_name.startswith("udf:"):
name = field_name[4:]
if name in model_class.collection_udf_settings.keys():
field_definition_id = None
for udfd in udf_defs(instance, model):
if udfd.iscollection and udfd.name == name:
field_definition_id = udfd.id
if field_definition_id is None:
continue
select[
prefixed_name
] = """
WITH formatted_data AS (
SELECT concat('(', data, ')') as fdata
FROM %s
WHERE field_definition_id = %s and model_id = %s.id
)
SELECT array_to_string(array_agg(fdata), ', ', '*')
FROM formatted_data
""" % (
UserDefinedCollectionValue._meta.db_table,
field_definition_id,
table,
)
else:
select[prefixed_name] = "{0}.udfs->%s".format(table)
select_params.append(name)
else:
if not model_hasattr(dummy_instance, field_name):
# Exception will be raised downstream if you look for
# a field on a model that no longer exists but still
# has a stale permission record. Here we check for that
# case and don't include the field if it does not exist.
continue
prefixed_names.append(prefixed_name)
return prefixed_names
示例8: extra_select_and_values_for_model
def extra_select_and_values_for_model(
instance, job, table, model, prefix=None):
if prefix:
prefix += '__'
else:
prefix = ''
perms = permissions(job.user, instance, model)
extra_select = {}
prefixed_names = []
dummy_instance = safe_get_model_class(model)()
for perm in (perm for perm in perms
if perm.permission_level >= FieldPermission.READ_ONLY):
field_name = perm.field_name
prefixed_name = prefix + field_name
if field_name in _UDFC_FIELDS:
field_definition_id = None
for udfd in udf_defs(instance, model):
if udfd.iscollection and udfd.name == field_name[4:]:
field_definition_id = udfd.id
if field_definition_id is None:
continue
extra_select[prefixed_name] = (
"""
WITH formatted_data AS (
SELECT concat('(', data, ')') as fdata
FROM %s
WHERE field_definition_id = %s and model_id = %s.id
)
SELECT array_to_string(array_agg(fdata), ', ', '*')
FROM formatted_data
"""
% (UserDefinedCollectionValue._meta.db_table,
field_definition_id, table))
elif field_name.startswith('udf:'):
name = field_name[4:]
extra_select[prefixed_name] = "%s.udfs->'%s'" % (table, name)
else:
if not model_hasattr(dummy_instance, field_name):
# Exception will be raised downstream if you look for
# a field on a model that no longer exists but still
# has a stale permission record. Here we check for that
# case and don't include the field if it does not exist.
continue
prefixed_names.append(prefixed_name)
return (extra_select, prefixed_names)
示例9: validate_user_defined_fields
def validate_user_defined_fields(self):
ie = self.import_event
for udf_def in udf_defs(ie.instance):
column_name = ie.get_udf_column_name(udf_def)
value = self.datadict.get(column_name, None)
if value:
try:
udf_def.clean_value(value)
self.cleaned[column_name] = value
except ValidationError as ve:
self.append_error(
errors.INVALID_UDF_VALUE, column_name, str(ve))
示例10: info
def info(group):
group['fields'] = [
(field, labels.get(field),
templates.get(field, "treemap/field/tr.html"))
for field in group.get('field_keys', [])
]
group['collection_udfs'] = [
next(udf for udf in udf_defs(instance)
if udf.full_name == udf_name)
for udf_name in group.get('collection_udf_keys', [])
]
return group
示例11: get_disabled_cudfs
def get_disabled_cudfs(group):
if 'model' in group:
models = (to_model_name(group['model']), )
else:
models = ('Tree', 'Plot')
udfs = {udf.full_name for udf in udf_defs(instance)
if udf.iscollection and udf.model_type in models and
(group['header'] not in collection_groups
or udf.name == group['header'])}
disabled_cudfs = udfs - set(group['collection_udf_keys'])
return sorted(disabled_cudfs)
示例12: _get_fields
def _get_fields(instance, model_name):
Model = safe_get_model_class(model_name)
mobj = Model(instance=instance)
udfs = {udf.canonical_name
for udf in udf_defs(instance, model_name)
if not udf.iscollection}
concrete_fields = {
f.name for f in mobj._meta.get_fields(include_parents=False)
if _should_show_field(Model, f.name) and not f.is_relation}
model_fields = concrete_fields | udfs
model_fields = {'%s.%s' % (to_object_name(model_name), f)
for f in model_fields}
return model_fields
示例13: get_alert_field_info
def get_alert_field_info(identifier, instance):
from treemap.util import get_model_for_instance
alert_match = ALERT_IDENTIFIER_PATTERN.match(identifier)
if alert_match:
model_name, pk = alert_match.groups()
Model = get_model_for_instance(model_name, instance)
udf_def = next(udf for udf in udf_defs(instance) if udf.pk == int(pk))
display_name = force_text(Model.terminology(instance)['singular'])
return {
'identifier': identifier,
'search_type': 'DEFAULT',
'default_identifier': udf_def.full_name,
'label': 'Open %(model)s Alerts' % {'model': display_name},
}
return None
示例14: validate_user_defined_fields
def validate_user_defined_fields(self):
ie = self.import_event
for udf_def in udf_defs(ie.instance):
column_name = ie.get_udf_column_name(udf_def)
value = self.datadict.get(column_name, None)
if value:
try:
udf_def.clean_value(value)
self.cleaned[column_name] = value
except ValidationError as ve:
message = str(ve)
if isinstance(ve.message_dict, dict):
message = '\n'.join(
[unicode(m) for m in ve.message_dict.values()])
self.append_error(
errors.INVALID_UDF_VALUE, column_name, message)
示例15: get_display_model_name
def get_display_model_name(audit_name, instance=None):
if audit_name.startswith('udf:'):
try:
# UDF Collections store their model names in the audit table as
# udf:<pk of UserDefinedFieldDefinition>
pk = int(audit_name[4:])
if not instance:
udf_def = UserDefinedFieldDefinition.objects.get(pk=pk)
return udf_def.name
else:
for udf_def in udf_defs(instance):
if udf_def.pk == pk:
return udf_def.name
except (ValueError, UserDefinedFieldDefinition.DoesNotExist):
pass # If something goes wrong, just use the defaults
return audit_name