本文整理汇总了Python中model_report.utils.ReportRow.append方法的典型用法代码示例。如果您正苦于以下问题:Python ReportRow.append方法的具体用法?Python ReportRow.append怎么用?Python ReportRow.append使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类model_report.utils.ReportRow
的用法示例。
在下文中一共展示了ReportRow.append方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_rows
# 需要导入模块: from model_report.utils import ReportRow [as 别名]
# 或者: from model_report.utils.ReportRow import append [as 别名]
def get_rows(self,
request,
groupby_data=None,
filter_kwargs={},
filter_related_fields={}):
report_rows = []
def get_field_value(obj, field):
if isinstance(obj, (dict)):
return obj[field]
left_field = field.split("__")[0]
try:
right_field = "__".join(field.split("__")[1:])
except:
right_field = ''
if right_field:
return get_field_value(getattr(obj, left_field), right_field)
if hasattr(obj, 'get_%s_display' % left_field):
attr = getattr(obj, 'get_%s_display' % field)
else:
attr = getattr(obj, field)
if callable(attr):
attr = attr()
return attr
for kwarg, value in filter_kwargs.items():
if kwarg in self.override_field_filter_values:
filter_kwargs[kwarg] = self.override_field_filter_values.get(
kwarg)(self, value)
qs = self.get_query_set(filter_kwargs)
ffields = [
f if 'self.' not in f else 'pk'
for f in self.get_query_field_names()
if f not in filter_related_fields
]
extra_ffield = []
backend = settings.DATABASES['default']['ENGINE'].split('.')[-1]
for f in list(ffields):
if '__' in f:
for field, name in self.model_fields:
if name == f:
if 'fields.Date' in unicode(field):
fname, flookup = f.split('__')
if not flookup in ('year', 'month', 'day'):
break
if flookup == 'year':
if 'sqlite' in backend:
extra_ffield.append(
[f, "strftime('%%Y', " + fname + ")"])
elif 'postgres' in backend:
extra_ffield.append([
f, "cast(extract(year from " + fname +
") as integer)"
])
else:
raise NotImplemented # mysql
if flookup == 'month':
if 'sqlite' in backend:
extra_ffield.append(
[f, "strftime('%%m', " + fname + ")"])
elif 'postgres' in backend:
extra_ffield.append([
f, "cast(extract(month from " + fname +
") as integer)"
])
else:
raise NotImplemented # mysql
if flookup == 'day':
if 'sqlite' in backend:
extra_ffield.append(
[f, "strftime('%%d', " + fname + ")"])
elif 'postgres' in backend:
extra_ffield.append([
f, "cast(extract(day from " + fname +
") as integer)"
])
else:
raise NotImplemented # mysql
break
obfields = list(self.list_order_by)
if groupby_data and groupby_data['groupby']:
if groupby_data['groupby'] in obfields:
obfields.remove(groupby_data['groupby'])
obfields.insert(0, groupby_data['groupby'])
qs = self.filter_query(qs)
qs = qs.order_by(*obfields)
if extra_ffield:
qs = qs.extra(select=dict(extra_ffield))
qs = qs.values_list(*ffields)
qs_list = list(qs)
def get_with_dotvalues(resources):
# {1: 'field.method'}
dot_indexes = dict(
[(index, dot_field)
for index, dot_field in enumerate(self.get_fields())
if '.' in dot_field])
dot_indexes_values = {}
#.........这里部分代码省略.........
示例2: get_rows
# 需要导入模块: from model_report.utils import ReportRow [as 别名]
# 或者: from model_report.utils.ReportRow import append [as 别名]
def get_rows(self, request, groupby_data=None, filter_kwargs={}, filter_related_fields={}):
report_rows = []
def get_field_value(obj, field):
if isinstance(obj, (dict)):
return obj[field]
left_field = field.split("__")[0]
try:
right_field = "__".join(field.split("__")[1:])
except:
right_field = ""
if right_field:
return get_field_value(getattr(obj, left_field), right_field)
if hasattr(obj, "get_%s_display" % left_field):
attr = getattr(obj, "get_%s_display" % field)
else:
attr = getattr(obj, field)
if callable(attr):
attr = attr()
return attr
qs = self.get_query_set(filter_kwargs)
ffields = [
f if "self." not in f else "pk" for f in self.get_query_field_names() if f not in filter_related_fields
]
obfields = list(self.list_order_by)
if groupby_data and groupby_data["groupby"]:
if groupby_data["groupby"] in obfields:
obfields.remove(groupby_data["groupby"])
obfields.insert(0, groupby_data["groupby"])
qs = qs.order_by(*obfields)
qs = qs.values_list(*ffields)
qs_list = list(qs)
def get_with_dotvalues(resources):
# {1: 'field.method'}
dot_indexes = dict([(index, dot_field) for index, dot_field in enumerate(self.fields) if "." in dot_field])
dot_indexes_values = {}
dot_model_fields = [
(index, model_field[0]) for index, model_field in enumerate(self.model_fields) if index in dot_indexes
]
# [ 1, model_field] ]
for index, model_field in dot_model_fields:
model_ids = set([row[index] for row in resources])
if isinstance(model_field, (unicode, str)) and "self." in model_field:
model_qs = self.model.objects.filter(pk__in=model_ids)
else:
model_qs = model_field.rel.to.objects.filter(pk__in=model_ids)
div = {}
method_name = dot_indexes[index].split(".")[1]
for obj in model_qs:
method_value = getattr(obj, method_name)
if callable(method_value):
method_value = method_value()
div[obj.pk] = method_value
dot_indexes_values[index] = div
del model_qs
if dot_indexes_values:
new_resources = []
for index_row, old_row in enumerate(resources):
new_row = []
for index, actual_value in enumerate(old_row):
if index in dot_indexes_values:
new_value = dot_indexes_values[index][actual_value]
else:
new_value = actual_value
new_row.append(new_value)
new_resources.append(new_row)
resources = new_resources
return resources
def compute_row_totals(row_config, row_values):
total_row = self.get_empty_row_asdict(self.fields, ReportValue(" "))
for k, v in total_row.items():
if k in row_config:
fun = row_config[k]
value = fun(row_values[k])
value = ReportValue(value)
if k in self.override_field_values:
value.to_value = self.override_field_values[k]
if k in self.override_field_formats:
value.format = self.override_field_formats[k]
total_row[k] = value
row = self.reorder_dictrow(total_row)
row = ReportRow(row)
row.is_total = True
return row
def compute_row_header(row_config):
header_row = self.get_empty_row_asdict(self.fields, ReportValue(""))
for k, fun in row_config.items():
if hasattr(fun, "caption"):
value = fun.caption
else:
value = " "
header_row[k] = value
row = self.reorder_dictrow(header_row)
#.........这里部分代码省略.........
示例3: get_rows
# 需要导入模块: from model_report.utils import ReportRow [as 别名]
# 或者: from model_report.utils.ReportRow import append [as 别名]
def get_rows(self, groupby_data=None, filter_kwargs={}, filter_related_fields={}):
report_rows = []
for selected_field, field_value in filter_kwargs.items():
if selected_field in self.override_field_filter_values:
filter_kwargs[selected_field] = self.override_field_filter_values.get(selected_field)(self, field_value)
qs = self.get_query_set(filter_kwargs)
ffields = [f if 'self.' not in f else 'pk' for f in self.get_query_field_names() if f not in filter_related_fields]
ffields_include_self = [f for f in self.get_query_field_names() if f not in filter_related_fields]
extra_ffield = []
backend = settings.DATABASES['default']['ENGINE'].split('.')[-1]
for f in list(ffields):
if '__' in f:
for field, name in self.model_fields:
if name == f:
if is_date_field(field):
fname, flookup = f.rsplit('__', 1)
fname = fname.split('__')[-1]
if not flookup in ('year', 'month', 'day'):
break
if flookup == 'year':
if 'sqlite' in backend:
extra_ffield.append([f, "strftime('%%Y', " + fname + ")"])
elif 'postgres' in backend:
extra_ffield.append([f, "cast(extract(year from " + fname + ") as integer)"])
elif 'mysql' in backend:
extra_ffield.append([f, "YEAR(" + fname + ")"])
else:
raise NotImplemented # mysql
if flookup == 'month':
if 'sqlite' in backend:
extra_ffield.append([f, "strftime('%%m', " + fname + ")"])
elif 'postgres' in backend:
extra_ffield.append([f, "cast(extract(month from " + fname + ") as integer)"])
elif 'mysql' in backend:
extra_ffield.append([f, "MONTH(" + fname + ")"])
else:
raise NotImplemented # mysql
if flookup == 'day':
if 'sqlite' in backend:
extra_ffield.append([f, "strftime('%%d', " + fname + ")"])
elif 'postgres' in backend:
extra_ffield.append([f, "cast(extract(day from " + fname + ") as integer)"])
elif 'mysql' in backend:
extra_ffield.append([f, "DAY(" + fname + ")"])
else:
raise NotImplemented # mysql
break
obfields = list(self.list_order_by)
if groupby_data and groupby_data['groupby']:
if groupby_data['groupby'] in obfields:
obfields.remove(groupby_data['groupby'])
obfields.insert(0, groupby_data['groupby'])
qs = self.filter_query(qs)
qs = qs.order_by(*obfields)
if extra_ffield:
qs = qs.extra(select=dict(extra_ffield))
qs = qs.values_list(*ffields)
qs_list = list(qs)
qs_list = self.get_with_dotvalues(qs_list)
if self.model_m2m_fields:
qs_list = self.group_m2m_field_values(qs_list)
if groupby_data and groupby_data['groupby']:
groupby_field = groupby_data['groupby']
if groupby_field in self.override_group_value:
transform_fn = self.override_group_value.get(groupby_field)
groupby_fn = lambda x: transform_fn(x[ffields.index(groupby_field)])
else:
groupby_fn = lambda x: x[ffields.index(groupby_field)]
else:
groupby_fn = lambda x: None
qs_list.sort(key=groupby_fn)
g = groupby(qs_list, key=groupby_fn)
row_report_totals = self.get_empty_row_asdict(self.report_totals, [])
for grouper, resources in g:
rows = list()
row_group_totals = self.get_empty_row_asdict(self.group_totals, [])
for resource in resources:
row = ReportRow()
if isinstance(resource, (tuple, list)):
for index, value in enumerate(resource):
if ffields_include_self[index] in self.group_totals:
row_group_totals[ffields_include_self[index]].append(value)
elif ffields[index] in self.group_totals:
row_group_totals[ffields[index]].append(value)
elif ffields[index] in self.report_totals:
row_report_totals[ffields[index]].append(value)
value = self._get_value_text(index, value)
value = ReportValue(value)
if ffields[index] in self.override_field_values:
value.to_value = self.override_field_values[ffields[index]]
if ffields[index] in self.override_field_formats:
value.format = self.override_field_formats[ffields[index]]
row.append(value)
else:
#.........这里部分代码省略.........
示例4: get_rows
# 需要导入模块: from model_report.utils import ReportRow [as 别名]
# 或者: from model_report.utils.ReportRow import append [as 别名]
def get_rows(self, request, groupby_data=None, filter_kwargs={}, filter_related_fields={}):
report_rows = []
def get_field_value(obj, field):
if isinstance(obj, (dict)):
return obj[field]
left_field = field.split("__")[0]
try:
right_field = "__".join(field.split("__")[1:])
except:
right_field = ''
if right_field:
return get_field_value(getattr(obj, left_field), right_field)
if hasattr(obj, 'get_%s_display' % left_field):
attr = getattr(obj, 'get_%s_display' % field)
else:
attr = getattr(obj, field)
if callable(attr):
attr = attr()
return attr
qs = self.get_query_set(filter_kwargs)
ffields = [f if 'self.' not in f else 'pk' for f in self.get_query_field_names() if f not in filter_related_fields]
obfields = list(self.list_order_by)
if groupby_data and groupby_data['groupby']:
if groupby_data['groupby'] in obfields:
obfields.remove(groupby_data['groupby'])
obfields.insert(0, groupby_data['groupby'])
qs = self.filter_query(qs)
qs = qs.order_by(*obfields)
qs = qs.values_list(*ffields)
qs_list = list(qs)
def get_with_dotvalues(resources):
# {1: 'field.method'}
dot_indexes = dict([(index, dot_field) for index, dot_field in enumerate(self.get_fields()) if '.' in dot_field])
dot_indexes_values = {}
dot_model_fields = [(index, model_field[0]) for index, model_field in enumerate(self.model_fields) if index in dot_indexes]
# [ 1, model_field] ]
for index, model_field in dot_model_fields:
model_ids = set([row[index] for row in resources])
if isinstance(model_field, (unicode, str)) and 'self.' in model_field:
model_qs = self.model.objects.filter(pk__in=model_ids)
else:
model_qs = model_field.rel.to.objects.filter(pk__in=model_ids)
div = {}
method_name = dot_indexes[index].split('.')[1]
for obj in model_qs:
method_value = getattr(obj, method_name)
if callable(method_value):
method_value = method_value()
div[obj.pk] = method_value
dot_indexes_values[index] = div
del model_qs
if dot_indexes_values:
new_resources = []
for index_row, old_row in enumerate(resources):
new_row = []
for index, actual_value in enumerate(old_row):
if index in dot_indexes_values:
new_value = dot_indexes_values[index][actual_value]
else:
new_value = actual_value
new_row.append(new_value)
new_resources.append(new_row)
resources = new_resources
return resources
def compute_row_totals(row_config, row_values, is_group_total=False, is_report_total=False):
total_row = self.get_empty_row_asdict(self.get_fields(), ReportValue(' '))
for k, v in total_row.items():
if k in row_config:
fun = row_config[k]
value = fun(row_values[k])
if k in self.get_m2m_field_names():
value = ReportValue([value, ])
value = ReportValue(value)
value.is_value = False
value.is_group_total = is_group_total
value.is_report_total = is_report_total
if k in self.override_field_values:
value.to_value = self.override_field_values[k]
if k in self.override_field_formats:
value.format = self.override_field_formats[k]
value.is_m2m_value = (k in self.get_m2m_field_names())
total_row[k] = value
row = self.reorder_dictrow(total_row)
row = ReportRow(row)
row.is_total = True
return row
def compute_row_header(row_config):
header_row = self.get_empty_row_asdict(self.get_fields(), ReportValue(''))
for k, fun in row_config.items():
if hasattr(fun, 'caption'):
value = force_unicode(fun.caption)
else:
value = ' '
header_row[k] = value
#.........这里部分代码省略.........