question as csv

This commit is contained in:
Holger Sielaff
2024-07-13 14:38:42 +02:00
parent e7b2dff233
commit 1531b0f6d3
6 changed files with 99 additions and 44 deletions

View File

@@ -62,13 +62,18 @@ class Quiz(AuthorAware, DateAware, PublishedAware):
return self.pdfpath
def to_csv(self, fh=None):
with open(self.csvpath, 'w') as cf:
items = self.to_view()['items']
keys = items[0].keys()
dw = csv.DictWriter(cf, fieldnames=list(keys))
dw.writeheader()
dw.writerows(items)
return self.csvpath
if fh:
cf = fh
else:
cf = open(self.csvpath, 'w')
items = self.to_view()['items']
keys = items[0].keys()
dw = csv.DictWriter(cf, fieldnames=list(keys))
dw.writeheader()
dw.writerows(items)
if not fh:
cf.close()
return cf if fh else self.csvpath
def to_zip(self, fh=None):
with zipfile.ZipFile(self.zippath, 'w') as zf: