]> git.parisson.com Git - telemeta.git/commitdiff
make a CSV export generic function, add a command to export all items and collections
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Thu, 22 Oct 2015 15:23:21 +0000 (17:23 +0200)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Thu, 22 Oct 2015 15:23:21 +0000 (17:23 +0200)
telemeta/management/commands/telemeta-export-all-to-csv.py [new file with mode: 0644]
telemeta/models/item.py
telemeta/util/unicode.py
telemeta/views/core.py
telemeta/views/playlist.py

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 (file)
index 0000000..750988b
--- /dev/null
@@ -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()
index 0afe1bd0d0ec27ccd4f9a7bb0ce2fa987d978c82..4c9ec630265962022f19c03e5f537a51cea6f4dc 100644 (file)
@@ -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 = []
index f935f740b63fc5fa7d7fdc3710e4a33a1b188c2c..152a9fad27fe2d15bc84dfc0a60aff7f45d2d240 100644 (file)
@@ -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)
index 2846cce2e0526df7ff6586d948295ad1f2dd9210..0ac491038c98e036ccbe2b339982f1c9dd883d9e 100644 (file)
@@ -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 *
index da3196e8212dec8bef9128d241f9e9864d90cd2e..8ee10ef60aa0bee7d242107be3c07683ff383311 100644 (file)
@@ -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
-