return json.dumps(self.to_dict_with_more())
+ def to_row(self):
+ row = []
+ _dict = self.to_dict_with_more()
+ for tag in _dict:
+ if tag in _dict.keys():
+ row.append(_dict[tag])
+ else:
+ row.append('')
+ return row
+
class MediaCollectionRelated(MediaRelated):
"Collection related media"
verbose_name = _('collection identifier')
verbose_name_plural = _('collection identifiers')
unique_together = ('identifier', 'collection')
-
return [_stringify(s, encoding) for s in l]
+class StreamCSVException(Exception):
+ pass
+
+
class UnicodeCSVWriter(object):
- def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
+ def __init__(self, f, elements, dialect=csv.excel, encoding="utf-8", **kwds):
self.dialect = dialect
self.encoding = encoding
self.writer = csv.writer(f, dialect=dialect, **kwds)
self.line = 0
+ self.elements = elements
self.tags = []
-
+ self.get_tags(self.elements[0])
def get_tags(self, element):
- element_dict = element.to_dict_with_more()
+ _dict = element.to_dict_with_more()
- for key in element_dict.keys():
+ for key in _dict.keys():
if not key in self.tags:
self.tags.append(key)
self.tags.sort()
self.tags.insert(0, 'title')
self.tags.insert(0, 'code')
- self.init_tags = True
-
- def get_row(self, element):
- row = []
- element_dict = element.to_dict_with_more()
- for tag in self.tags:
- if tag in element_dict.keys():
- row.append(element_dict[tag])
- else:
- row.append('')
- return row
-
- def write_element(self, element):
- if not self.tags:
- yield self.writer.writerow(self.get_tags())
- row = self.get_row(element)
- yield self.writer.writerow(_stringify_list(row, self.encoding))
+
+ def output(self):
+ yield self.writer.writerow(self.tags)
+ for element in self.elements:
+ yield self.writer.writerow(_stringify_list(element.to_row(), self.encoding))
class Echo(object):
playlist = Playlist.objects.get(public_id=public_id)
elements = self.get_elements(playlist, resource_type)
pseudo_buffer = Echo()
- writer = UnicodeCSVWriter(pseudo_buffer)
- response = StreamingHttpResponse((writer.write_element(element) for element in elements), content_type='text/csv')
+ writer = UnicodeCSVWriter(pseudo_buffer, elements)
+ response = StreamingHttpResponse(writer.output(), content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename='+playlist.title+'_'+resource_type+'.csv'
return response