1
|
|
|
################################################################# |
2
|
|
|
# MET v2 Metadate Explorer Tool |
3
|
|
|
# |
4
|
|
|
# This Software is Open Source. See License: https://github.com/TERENA/met/blob/master/LICENSE.md |
5
|
|
|
# Copyright (c) 2012, TERENA All rights reserved. |
6
|
|
|
# |
7
|
|
|
# This Software is based on MET v1 developed for TERENA by Yaco Sistemas, http://www.yaco.es/ |
8
|
|
|
# MET v2 was developed for TERENA by Tamim Ziai, DAASI International GmbH, http://www.daasi.de |
9
|
|
|
# Current version of MET has been revised for performance improvements by Andrea Biancini, |
10
|
|
|
# Consortium GARR, http://www.garr.it |
11
|
|
|
######################################################################################### |
12
|
|
|
|
13
|
|
|
import csv |
14
|
|
|
from xml.dom.minidom import Document |
15
|
|
|
|
16
|
|
|
from django.http import HttpResponse, HttpResponseBadRequest |
17
|
|
|
from django.template.defaultfilters import slugify |
18
|
|
|
import simplejson as json |
19
|
|
|
|
20
|
|
|
class SetEncoder(json.JSONEncoder): |
21
|
|
|
def default(self, obj): |
22
|
|
|
if isinstance(obj, set): |
23
|
|
|
return list(obj) |
24
|
|
|
return json.JSONEncoder.default(self, obj) |
25
|
|
|
|
26
|
|
|
def _serialize_value_to_csv(value): |
27
|
|
|
if type(value) is list: |
28
|
|
|
vallist = [_serialize_value_to_csv(v) for v in value] |
29
|
|
|
serialized = ", ".join(vallist) |
30
|
|
|
elif type(value) is dict: |
31
|
|
|
vallist = [_serialize_value_to_csv(v) for v in value.values()] |
32
|
|
|
serialized = ", ".join(vallist) |
33
|
|
|
else: |
34
|
|
|
serialized = "%s" % value |
35
|
|
|
|
36
|
|
|
return serialized |
37
|
|
|
|
38
|
|
|
def export_entity_csv(entity): |
39
|
|
|
response = HttpResponse(content_type='text/csv') |
40
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.csv' |
41
|
|
|
% slugify(entity)) |
42
|
|
|
writer = csv.writer(response) |
43
|
|
|
edict = entity.to_dict() |
44
|
|
|
|
45
|
|
|
writer.writerow(edict.keys()) |
46
|
|
|
# Write data to CSV file |
47
|
|
|
row = [] |
48
|
|
|
for _, value in edict.items(): |
49
|
|
|
row.append(_serialize_value_to_csv(value)) |
50
|
|
|
row_ascii = [v.encode("ascii", "ignore") for v in row] |
51
|
|
|
|
52
|
|
|
writer.writerow(row_ascii) |
53
|
|
|
# Return CSV file to browser as download |
54
|
|
|
return response |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def export_entity_json(entity): |
58
|
|
|
# Return JS file to browser as download |
59
|
|
|
serialized = json.dumps(entity.to_dict(), cls=SetEncoder) |
60
|
|
|
response = HttpResponse(serialized, content_type='application/json') |
61
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.json' |
62
|
|
|
% slugify(entity)) |
63
|
|
|
return response |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
class Dict2XML(object): |
67
|
|
|
""" http://stackoverflow.com/questions/1019895/serialize-python-dictionary-to-xml """ |
68
|
|
|
doc = Document() |
69
|
|
|
|
70
|
|
|
def __init__(self, structure): |
71
|
|
|
if len(structure) == 1: |
72
|
|
|
root_name = str(structure.keys()[0]) |
73
|
|
|
self.root = self.doc.createElement(root_name) |
74
|
|
|
|
75
|
|
|
self.doc.appendChild(self.root) |
76
|
|
|
self.build(self.root, structure[root_name]) |
77
|
|
|
|
78
|
|
|
def build(self, father, structure): |
79
|
|
|
if type(structure) == dict: |
80
|
|
|
for k in structure: |
81
|
|
|
tag = self.doc.createElement(k) |
82
|
|
|
father.appendChild(tag) |
83
|
|
|
self.build(tag, structure[k]) |
84
|
|
|
|
85
|
|
View Code Duplication |
elif type(structure) == list: |
|
|
|
|
86
|
|
|
grand_father = father.parentNode |
87
|
|
|
tag_name = father.tagName |
88
|
|
|
grand_father.removeChild(father) |
89
|
|
|
for l in structure: |
90
|
|
|
tag = self.doc.createElement(tag_name) |
91
|
|
|
self.build(tag, l) |
92
|
|
|
grand_father.appendChild(tag) |
93
|
|
|
else: |
94
|
|
|
if type(structure) == unicode: |
95
|
|
|
data = structure.encode("ascii", errors="xmlcharrefreplace") |
96
|
|
|
else: |
97
|
|
|
data = str(structure) |
98
|
|
|
tag = self.doc.createTextNode(data) |
99
|
|
|
father.appendChild(tag) |
100
|
|
|
|
101
|
|
|
def __str__(self): |
102
|
|
|
return self.doc.toprettyxml(indent=" ") |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
def export_entity_xml(entity): |
106
|
|
|
entity_xml = Dict2XML({"Entity": entity.to_dict()}) |
107
|
|
|
|
108
|
|
|
# Return XML file to browser as download |
109
|
|
|
response = HttpResponse(str(entity_xml), content_type='application/xml') |
110
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.xml' |
111
|
|
|
% slugify(entity)) |
112
|
|
|
return response |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
export_entity_modes = { |
116
|
|
|
'csv': export_entity_csv, |
117
|
|
|
'json': export_entity_json, |
118
|
|
|
'xml': export_entity_xml, |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def export_entity(mode, entity): |
123
|
|
|
if mode in export_entity_modes: |
124
|
|
|
return export_entity_modes[mode](entity) |
125
|
|
|
else: |
126
|
|
|
content = "Error 400, Format %s is not supported" % mode |
127
|
|
|
return HttpResponseBadRequest(content) |
128
|
|
|
|