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
|
|
|
|
21
|
|
|
class SetEncoder(json.JSONEncoder): |
22
|
|
|
def default(self, obj): |
23
|
|
|
if isinstance(obj, set): |
24
|
|
|
return list(obj) |
25
|
|
|
return json.JSONEncoder.default(self, obj) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def _serialize_value_to_csv(value): |
29
|
|
|
if type(value) is list: |
30
|
|
|
vallist = [_serialize_value_to_csv(v) for v in value] |
31
|
|
|
serialized = ", ".join(vallist) |
32
|
|
|
elif type(value) is dict: |
33
|
|
|
vallist = [_serialize_value_to_csv(v) for v in value.values()] |
34
|
|
|
serialized = ", ".join(vallist) |
35
|
|
|
else: |
36
|
|
|
serialized = "%s" % value |
37
|
|
|
|
38
|
|
|
return serialized |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def export_entity_csv(entity): |
42
|
|
|
response = HttpResponse(content_type='text/csv') |
43
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.csv' |
44
|
|
|
% slugify(entity)) |
45
|
|
|
writer = csv.writer(response) |
46
|
|
|
edict = entity.to_dict() |
47
|
|
|
|
48
|
|
|
writer.writerow(edict.keys()) |
49
|
|
|
# Write data to CSV file |
50
|
|
|
row = [] |
51
|
|
|
for _, value in edict.items(): |
52
|
|
|
row.append(_serialize_value_to_csv(value)) |
53
|
|
|
row_ascii = [v.encode("ascii", "ignore") for v in row] |
54
|
|
|
|
55
|
|
|
writer.writerow(row_ascii) |
56
|
|
|
# Return CSV file to browser as download |
57
|
|
|
return response |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def export_entity_json(entity): |
61
|
|
|
# Return JS file to browser as download |
62
|
|
|
serialized = json.dumps(entity.to_dict(), cls=SetEncoder) |
63
|
|
|
response = HttpResponse(serialized, content_type='application/json') |
64
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.json' |
65
|
|
|
% slugify(entity)) |
66
|
|
|
return response |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
class Dict2XML(object): |
70
|
|
|
""" http://stackoverflow.com/questions/1019895/serialize-python-dictionary-to-xml """ |
71
|
|
|
doc = Document() |
72
|
|
|
|
73
|
|
|
def __init__(self, structure): |
74
|
|
|
if len(structure) == 1: |
75
|
|
|
root_name = str(structure.keys()[0]) |
76
|
|
|
self.root = self.doc.createElement(root_name) |
77
|
|
|
|
78
|
|
|
self.doc.appendChild(self.root) |
79
|
|
|
self.build(self.root, structure[root_name]) |
80
|
|
|
|
81
|
|
|
def build(self, father, structure): |
82
|
|
|
if type(structure) == dict: |
83
|
|
|
for k in structure: |
84
|
|
|
tag = self.doc.createElement(k) |
85
|
|
|
father.appendChild(tag) |
86
|
|
|
self.build(tag, structure[k]) |
87
|
|
|
|
88
|
|
View Code Duplication |
elif type(structure) == list: |
|
|
|
|
89
|
|
|
grand_father = father.parentNode |
90
|
|
|
tag_name = father.tagName |
91
|
|
|
grand_father.removeChild(father) |
92
|
|
|
for l in structure: |
93
|
|
|
tag = self.doc.createElement(tag_name) |
94
|
|
|
self.build(tag, l) |
95
|
|
|
grand_father.appendChild(tag) |
96
|
|
|
else: |
97
|
|
|
if type(structure) == unicode: |
98
|
|
|
data = structure.encode("ascii", errors="xmlcharrefreplace") |
99
|
|
|
else: |
100
|
|
|
data = str(structure) |
101
|
|
|
tag = self.doc.createTextNode(data) |
102
|
|
|
father.appendChild(tag) |
103
|
|
|
|
104
|
|
|
def __str__(self): |
105
|
|
|
return self.doc.toprettyxml(indent=" ") |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def export_entity_xml(entity): |
109
|
|
|
entity_xml = Dict2XML({"Entity": entity.to_dict()}) |
110
|
|
|
|
111
|
|
|
# Return XML file to browser as download |
112
|
|
|
response = HttpResponse(str(entity_xml), content_type='application/xml') |
113
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.xml' |
114
|
|
|
% slugify(entity)) |
115
|
|
|
return response |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
export_entity_modes = { |
119
|
|
|
'csv': export_entity_csv, |
120
|
|
|
'json': export_entity_json, |
121
|
|
|
'xml': export_entity_xml, |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
def export_entity(mode, entity): |
126
|
|
|
if mode in export_entity_modes: |
127
|
|
|
return export_entity_modes[mode](entity) |
128
|
|
|
else: |
129
|
|
|
content = "Error 400, Format %s is not supported" % mode |
130
|
|
|
return HttpResponseBadRequest(content) |
131
|
|
|
|