From: Guillaume Pellerin Date: Thu, 22 Oct 2015 15:23:21 +0000 (+0200) Subject: make a CSV export generic function, add a command to export all items and collections X-Git-Tag: 1.6b~7^2~60 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=73364d568b3dab5a5f2da6d06f78cd977b1bdc67;p=telemeta.git make a CSV export generic function, add a command to export all items and collections --- diff --git a/telemeta/management/commands/telemeta-export-all-to-csv.py b/telemeta/management/commands/telemeta-export-all-to-csv.py new file mode 100644 index 00000000..750988b7 --- /dev/null +++ b/telemeta/management/commands/telemeta-export-all-to-csv.py @@ -0,0 +1,31 @@ +from optparse import make_option +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError +from django.contrib.auth.models import User +from django.template.defaultfilters import slugify + +import os +import timeside.core +from timeside.server.models import * +from timeside.core.tools.test_samples import generateSamples +from telemeta.models import * +from telemeta.util.unicode import * + + +class Command(BaseCommand): + help = "Export all items or collections metadata to a CSV file" + + def handle(self, *args, **options): + path = args[-1] + element_type = args[-2] + f = open(path, 'w') + if element_type == "item": + elements = MediaItem.objects.all() + elif element_type == "collection": + elements = MediaCollection.objects.all() + else: + raise TypeError('type should be "item" or "collection"') + writer = UnicodeWriter(f) + csv = CSVExport(writer) + csv.write(elements) + f.close() diff --git a/telemeta/models/item.py b/telemeta/models/item.py index 0afe1bd0..4c9ec630 100644 --- a/telemeta/models/item.py +++ b/telemeta/models/item.py @@ -251,7 +251,12 @@ class MediaItem(MediaResource): del metadata[key] metadata['url'] = self.get_url() - metadata['last_modification_date'] = unicode(self.get_revision().time) + revision = self.get_revision() + if revision: + time = unicode(revision.time) + else: + time = '' + metadata['last_modification_date'] = time metadata['collection'] = self.collection.get_url() keywords = [] diff --git a/telemeta/util/unicode.py b/telemeta/util/unicode.py index f935f740..152a9fad 100644 --- a/telemeta/util/unicode.py +++ b/telemeta/util/unicode.py @@ -45,11 +45,11 @@ def _stringify(s, encoding): elif type(s) != str: s=str(s) return s - + def _stringify_list(l, encoding): return [_stringify(s, encoding) for s in l] - - + + class UnicodeWriter(object): def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): self.writer = csv.writer(f) @@ -63,3 +63,33 @@ class UnicodeWriter(object): def writerows(self, rows): for row in rows: self.writerow(row) + + +class CSVExport(object): + + def __init__(self, writer): + self.writer = writer + + def write(self, elements): + tags = [] + element_dicts = [e.to_dict_with_more() for e in elements] + for e in element_dicts: + for key in e.keys(): + if not key in tags: + tags.append(key) + # code and title on the two first column + tags.remove('code') + tags.remove('title') + tags.sort() + tags.insert(0, 'title') + tags.insert(0, 'code') + self.writer.writerow(tags) + + for element in element_dicts: + data = [] + for tag in tags: + if tag in element.keys(): + data.append(element[tag]) + else: + data.append('') + self.writer.writerow(data) diff --git a/telemeta/views/core.py b/telemeta/views/core.py index 2846cce2..0ac49103 100644 --- a/telemeta/views/core.py +++ b/telemeta/views/core.py @@ -85,7 +85,7 @@ from telemeta.interop.oaidatasource import TelemetaOAIDataSource from telemeta.util.unaccent import unaccent from telemeta.util.unaccent import unaccent_icmp from telemeta.util.logger import Logger -from telemeta.util.unicode import UnicodeWriter +from telemeta.util.unicode import UnicodeWriter, CSVExport from telemeta.cache import TelemetaCache import pages from telemeta.forms import * diff --git a/telemeta/views/playlist.py b/telemeta/views/playlist.py index da3196e8..8ee10ef6 100644 --- a/telemeta/views/playlist.py +++ b/telemeta/views/playlist.py @@ -115,28 +115,7 @@ class PlaylistView(object): elements.append(collection) if elements: - tags = [] - element_dicts = [e.to_dict_with_more() for e in elements] - for e in element_dicts: - for key in e.keys(): - if not key in tags: - tags.append(key) - # code and title on the two first column - tags.remove('code') - tags.remove('title') - tags.sort() - tags.insert(0, 'title') - tags.insert(0, 'code') - writer.writerow(tags) - - for element in element_dicts: - data = [] - for tag in tags: - if tag in element.keys(): - data.append(element[tag]) - else: - data.append('') - writer.writerow(data) + csv = CSVExport(writer) + csv.write(elements) return response -