|
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
|
|
|
def export_summary_csv(qs, relation, filename, counters): |
|
22
|
|
|
response = HttpResponse(content_type='text/csv') |
|
23
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.csv' |
|
24
|
|
|
% slugify(filename)) |
|
25
|
|
|
writer = csv.writer(response, quoting=csv.QUOTE_NONNUMERIC) |
|
26
|
|
|
labels = ['name'] |
|
27
|
|
|
labels.extend([label for (label, _) in counters]) |
|
28
|
|
|
writer.writerow(labels) |
|
29
|
|
|
# Write data to CSV file |
|
30
|
|
|
for obj in qs: |
|
31
|
|
|
row = [unicode(obj).encode('utf-8')] |
|
32
|
|
|
for _, counter_filter in counters: |
|
33
|
|
|
row.append(getattr(obj, relation).filter(**counter_filter).count()) |
|
34
|
|
|
writer.writerow(row) |
|
35
|
|
|
# Return CSV file to browser as download |
|
36
|
|
|
return response |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def export_summary_json(qs, relation, filename, counters): |
|
40
|
|
|
objs = {} |
|
41
|
|
|
for obj in qs: |
|
42
|
|
|
item = {} |
|
43
|
|
|
for counter_label, counter_filter in counters: |
|
44
|
|
|
item[counter_label] = getattr(obj, relation).filter(**counter_filter).count() |
|
45
|
|
|
objs[unicode(obj)] = item |
|
46
|
|
|
# Return JS file to browser as download |
|
47
|
|
|
serialized = json.dumps(objs) |
|
48
|
|
|
response = HttpResponse(serialized, content_type='application/json') |
|
49
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.json' |
|
50
|
|
|
% slugify(filename)) |
|
51
|
|
|
return response |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def export_summary_xml(qs, relation, filename, counters): |
|
55
|
|
|
xml = Document() |
|
56
|
|
|
root = xml.createElement(filename) |
|
57
|
|
|
# Write data to CSV file |
|
58
|
|
|
for obj in qs: |
|
59
|
|
|
item = xml.createElement("federation") |
|
60
|
|
|
item.setAttribute("name", unicode(obj)) |
|
61
|
|
|
for counter_label, counter_filter in counters: |
|
62
|
|
|
val = getattr(obj, relation).filter(**counter_filter).count() |
|
63
|
|
|
element = xml.createElement(counter_label) |
|
64
|
|
|
xmlval = xml.createTextNode(unicode(val)) |
|
65
|
|
|
element.appendChild(xmlval) |
|
66
|
|
|
item.appendChild(element) |
|
67
|
|
|
root.appendChild(item) |
|
68
|
|
|
xml.appendChild(root) |
|
69
|
|
|
|
|
70
|
|
|
# Return XML file to browser as download |
|
71
|
|
|
response = HttpResponse(xml.toxml(), content_type='application/xml') |
|
72
|
|
|
response['Content-Disposition'] = ('attachment; filename=%s.xml' |
|
73
|
|
|
% slugify(filename)) |
|
74
|
|
|
return response |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
export_summary_modes = { |
|
78
|
|
|
'csv': export_summary_csv, |
|
79
|
|
|
'json': export_summary_json, |
|
80
|
|
|
'xml': export_summary_xml, |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def export_summary(mode, qs, relation, filename, counters): |
|
85
|
|
|
if mode in export_summary_modes: |
|
86
|
|
|
return export_summary_modes[mode](qs, relation, filename, counters) |
|
87
|
|
|
else: |
|
88
|
|
|
content = "Error 400, Format %s is not supported" % mode |
|
89
|
|
|
return HttpResponseBadRequest(content) |
|
90
|
|
|
|