Django values_list

  1. ## https://www.jb51.net/article/183872.htm
  2. alarm_columns = (
  3. "create_time", "policy__name", "node",
  4. "policy__level", "status", "comment"
  5. )
  6. tmp_values = objs.values_list(*alarm_columns)

Django translation

  1. from django.utils.translation import ugettext as _
  2. ## https://blog.csdn.net/zhuyue14/article/details/87618067
  3. values = []
  4. for tmp_value in tmp_values:
  5. value = list(tmp_value)
  6. value[0] = '{0:%Y-%m-%d %H:%M:%S}'.format(
  7. value[0].astimezone(fixed_offset)
  8. )
  9. value[3] = _(
  10. level_map[value[3]]
  11. ) if value[3] in level_map else _(
  12. Policy.level_value(value[3])
  13. )
  14. value[4] = _(
  15. status_map[value[4]]
  16. ) if value[4] in status_map else ""
  17. values.append(value)
  18. def translate(self, param):
  19. if isinstance(param, str):
  20. return _(param)
  21. if isinstance(param, list):
  22. ret = []
  23. for item in param:
  24. if isinstance(item, str):
  25. ret.append(_(item))
  26. elif isinstance(item, list):
  27. ret.append(self.translate(item))
  28. else:
  29. return ret
  30. return None

namedtuple

  1. from collections import namedtuple
  2. """Returns a new subclass of tuple with named fields.
  3. ##def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
  4. >>> Point = namedtuple('Point', ['x', 'y'])
  5. >>> Point.__doc__ # docstring for the new class
  6. 'Point(x, y)'
  7. >>> p = Point(11, y=22) # instantiate with positional args or keywords
  8. >>> p[0] + p[1] # indexable like a plain tuple
  9. 33
  10. >>> x, y = p # unpack like a regular tuple
  11. >>> x, y
  12. (11, 22)
  13. >>> p.x + p.y # fields also accessible by name
  14. 33
  15. >>> d = p._asdict() # convert to a dictionary
  16. >>> d['x']
  17. 11
  18. >>> Point(**d) # convert from a dictionary
  19. Point(x=11, y=22)
  20. >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
  21. Point(x=100, y=22)
  22. """
  23. context_tuple = namedtuple(
  24. "context",
  25. [
  26. 'data', 'start_time', 'end_time',
  27. 'creator', 'create_time', 'operator'
  28. ]
  29. )
  30. return context_tuple(
  31. values,
  32. start_time.astimezone(fixed_offset),
  33. end_time.astimezone(fixed_offset),
  34. data['creator'],
  35. create_time.astimezone(fixed_offset),
  36. settings.LICO.ARCH
  37. )._asdict()

html 说明

  1. {% comment %} https://www.runoob.com/tags/ref-byfunc.html {% endcomment %}
  2. {% comment %} <html> 定义一个 HTML 文档 {% endcomment %}
  3. {% comment %} <title> 为文档定义一个标题 {% endcomment %}
  4. {% comment %} <body> 定义文档的主体 {% endcomment %}
  5. {% comment %} <h1> to <h6> 定义 HTML 标题 {% endcomment %}
  6. {% comment %} <span> 定义文档中的节。 {% endcomment %}
  7. {% comment %} <table> 定义一个表格 {% endcomment %}
  8. {% comment %} <caption> 定义表格标题。 {% endcomment %}
  9. {% comment %} <thead> 定义表格中的表头内容。 {% endcomment %}
  10. {% comment %} <th> 定义表格中的表头单元格 {% endcomment %}
  11. {% comment %} <tr> 定义表格中的行。 {% endcomment %}
  12. {% comment %} <td> 定义表格中的单元。 {% endcomment %}
  13. {% comment %} <tbody> 定义表格中的主体内容。 {% endcomment %}
  14. {% comment %} <tfoot> 定义表格中的表注内容(脚注). {% endcomment %}

测试验证

  1. from lico.core.accounting.views.deposit import *
  2. import requests
  3. import time
  4. request = requests
  5. request.data = {
  6. "language" : "sc",
  7. "bill_group" : 1,
  8. "start_time" : "1615169036",
  9. "end_time" : str(int(time.time())),
  10. "timezone_offset" :"-480"
  11. }
  12. # filename = "billing_group_details.html"
  13. # filename = "billing_group_details.pdf"
  14. filename = "billing_group_details.xlsx"
  15. aa=DepositReportView()
  16. response = aa.post(request, filename)
  17. ## 这三种方式 都可以写 ; postman 也有 Save to a file 选项
  18. # with open("daiyi.test.html","wb") as file:
  19. # with open("daiyi.test.pdf","wb") as file:
  20. with open("daiyi.test.xlsx","wb") as file:
  21. for data in response:
  22. file.write(data)
  23. """
  24. https://10.240.208.84/api/download/alert/report/alert_statistics.xlsx/
  25. Request URL: https://10.240.208.84/api/download/alert/report/alert_details.xlsx/
  26. Request Method: POST
  27. timezone_offset: -480
  28. creator: hpcadmin
  29. language: en
  30. start_time: 1612800000
  31. end_time: 1616083199
  32. page_direction: landscape
  33. event_level: 0
  34. ## they are requst_body
  35. """

deposits_report.htmlstyle.cssdeposit.pyreportbase.py