--- /dev/null
+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()
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 = []
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)
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)
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 *
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
-