Django values_list
## https://www.jb51.net/article/183872.htm
alarm_columns = (
"create_time", "policy__name", "node",
"policy__level", "status", "comment"
)
tmp_values = objs.values_list(*alarm_columns)
Django translation
from django.utils.translation import ugettext as _
## https://blog.csdn.net/zhuyue14/article/details/87618067
values = []
for tmp_value in tmp_values:
value = list(tmp_value)
value[0] = '{0:%Y-%m-%d %H:%M:%S}'.format(
value[0].astimezone(fixed_offset)
)
value[3] = _(
level_map[value[3]]
) if value[3] in level_map else _(
Policy.level_value(value[3])
)
value[4] = _(
status_map[value[4]]
) if value[4] in status_map else ""
values.append(value)
def translate(self, param):
if isinstance(param, str):
return _(param)
if isinstance(param, list):
ret = []
for item in param:
if isinstance(item, str):
ret.append(_(item))
elif isinstance(item, list):
ret.append(self.translate(item))
else:
return ret
return None
namedtuple
from collections import namedtuple
"""Returns a new subclass of tuple with named fields.
##def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
>>> Point = namedtuple('Point', ['x', 'y'])
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p[0] + p[1] # indexable like a plain tuple
33
>>> x, y = p # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y # fields also accessible by name
33
>>> d = p._asdict() # convert to a dictionary
>>> d['x']
11
>>> Point(**d) # convert from a dictionary
Point(x=11, y=22)
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
Point(x=100, y=22)
"""
context_tuple = namedtuple(
"context",
[
'data', 'start_time', 'end_time',
'creator', 'create_time', 'operator'
]
)
return context_tuple(
values,
start_time.astimezone(fixed_offset),
end_time.astimezone(fixed_offset),
data['creator'],
create_time.astimezone(fixed_offset),
settings.LICO.ARCH
)._asdict()
html 说明
{% comment %} https://www.runoob.com/tags/ref-byfunc.html {% endcomment %}
{% comment %} <html> 定义一个 HTML 文档 {% endcomment %}
{% comment %} <title> 为文档定义一个标题 {% endcomment %}
{% comment %} <body> 定义文档的主体 {% endcomment %}
{% comment %} <h1> to <h6> 定义 HTML 标题 {% endcomment %}
{% comment %} <span> 定义文档中的节。 {% endcomment %}
{% comment %} <table> 定义一个表格 {% endcomment %}
{% comment %} <caption> 定义表格标题。 {% endcomment %}
{% comment %} <thead> 定义表格中的表头内容。 {% endcomment %}
{% comment %} <th> 定义表格中的表头单元格 {% endcomment %}
{% comment %} <tr> 定义表格中的行。 {% endcomment %}
{% comment %} <td> 定义表格中的单元。 {% endcomment %}
{% comment %} <tbody> 定义表格中的主体内容。 {% endcomment %}
{% comment %} <tfoot> 定义表格中的表注内容(脚注). {% endcomment %}
测试验证
from lico.core.accounting.views.deposit import *
import requests
import time
request = requests
request.data = {
"language" : "sc",
"bill_group" : 1,
"start_time" : "1615169036",
"end_time" : str(int(time.time())),
"timezone_offset" :"-480"
}
# filename = "billing_group_details.html"
# filename = "billing_group_details.pdf"
filename = "billing_group_details.xlsx"
aa=DepositReportView()
response = aa.post(request, filename)
## 这三种方式 都可以写 ; postman 也有 Save to a file 选项
# with open("daiyi.test.html","wb") as file:
# with open("daiyi.test.pdf","wb") as file:
with open("daiyi.test.xlsx","wb") as file:
for data in response:
file.write(data)
"""
https://10.240.208.84/api/download/alert/report/alert_statistics.xlsx/
Request URL: https://10.240.208.84/api/download/alert/report/alert_details.xlsx/
Request Method: POST
timezone_offset: -480
creator: hpcadmin
language: en
start_time: 1612800000
end_time: 1616083199
page_direction: landscape
event_level: 0
## they are requst_body
"""
deposits_report.htmlstyle.cssdeposit.pyreportbase.py