30 lines
778 B

from django.http import HttpResponse
from django.utils.encoding import force_str
class OvvpFile:
def __init__(self):
# { header: value, ... }
self.headers = {}
# [ 'column-name', ... ]
self.columns = []
# [ { column: value, ...}, ...]
self.rows = []
def to_lines(self):
# header
for hk in sorted(self.headers.keys()):
yield f'{hk}\t{self.headers[hk]}\n'
yield '\n'
# columns
yield '\t'.join(self.columns) + '\n'
# rows
for r in self.rows:
yield '\t'.join([force_str(r[c]) for c in self.columns]) + '\n'
def to_string(self):
return ''.join(self.to_lines())
# Pozn: tohle je ta jediná funkce, která se reálně používá…
def to_HttpResponse(self):
return HttpResponse(self.to_string(), content_type='text/plain; charset=utf-8')