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
|
|
|
from django import template |
14
|
|
|
from django.template.base import Node, TemplateSyntaxError |
15
|
|
|
from django.template.defaultfilters import stringfilter |
16
|
|
|
from django.utils.safestring import mark_safe, SafeData |
17
|
|
|
from met.metadataparser.models import Federation |
18
|
|
|
from met.metadataparser.xmlparser import DESCRIPTOR_TYPES, DESCRIPTOR_TYPES_DISPLAY |
19
|
|
|
from met.metadataparser.query_export import export_modes |
20
|
|
|
from met.metadataparser.summary_export import export_summary_modes |
21
|
|
|
from urllib import urlencode |
22
|
|
|
|
23
|
|
|
register = template.Library() |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class AddGetParameter(Node): |
27
|
|
|
def __init__(self, values): |
28
|
|
|
self.values = values |
29
|
|
|
|
30
|
|
|
def render(self, context): |
31
|
|
|
req = template.resolve_variable('request', context) |
32
|
|
|
params = req.GET.copy() |
33
|
|
|
for key, value in self.values.items(): |
34
|
|
|
params[key] = value.resolve(context) |
35
|
|
|
return '?%s' % params.urlencode() |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
@register.tag() |
39
|
|
|
def add_get(parser, token): |
40
|
|
|
pairs = token.split_contents()[1:] |
41
|
|
|
values = {} |
42
|
|
|
for pair in pairs: |
43
|
|
|
s = pair.split('=', 1) |
44
|
|
|
values[s[0]] = parser.compile_filter(s[1]) |
45
|
|
|
return AddGetParameter(values) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
@register.inclusion_tag('metadataparser/bootstrap_form.html') |
49
|
|
|
def bootstrap_form(form, cancel_link='..', delete_link=True): |
50
|
|
|
return {'form': form, |
51
|
|
|
'cancel_link': cancel_link, |
52
|
|
|
'delete_link': delete_link} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
@register.inclusion_tag('metadataparser/bootstrap_searchform.html') |
56
|
|
|
def bootstrap_searchform(form): |
57
|
|
|
return {'form': form} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
@register.inclusion_tag('metadataparser/federations_summary_tag.html', takes_context=True) |
61
|
|
|
def federations_summary(context, queryname, counts, federations=None): |
62
|
|
|
if not federations: |
63
|
|
|
federations = Federation.objects.all() |
64
|
|
|
|
65
|
|
|
user = context.get('user', None) |
66
|
|
|
add_federation = user and user.has_perm('metadataparser.add_federation') |
67
|
|
|
|
68
|
|
|
return {'federations': federations, |
69
|
|
|
'add_federation': add_federation, |
70
|
|
|
'queryname': queryname, |
71
|
|
|
'counts': counts, |
72
|
|
|
'entity_types': DESCRIPTOR_TYPES} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
@register.inclusion_tag('metadataparser/interfederations_summary_tag.html', takes_context=True) |
76
|
|
|
def interfederations_summary(context, queryname, counts, federations=None): |
77
|
|
|
if not federations: |
78
|
|
|
federations = Federation.objects.all() |
79
|
|
|
|
80
|
|
|
user = context.get('user', None) |
81
|
|
|
add_federation = user and user.has_perm('metadataparser.add_federation') |
82
|
|
|
|
83
|
|
|
return {'federations': federations, |
84
|
|
|
'add_federation': add_federation, |
85
|
|
|
'queryname': queryname, |
86
|
|
|
'counts': counts, |
87
|
|
|
'entity_types': DESCRIPTOR_TYPES} |
88
|
|
|
|
89
|
|
|
@register.inclusion_tag('metadataparser/tag_entity_list.html', takes_context=True) |
90
|
|
|
def entity_list(context, entities, categories=None, pagination=None, curfed=None, show_total=True, append_query=None, onclick_page=None, onclick_export=None): |
91
|
|
|
request = context.get('request', None) |
92
|
|
|
lang = 'en' |
93
|
|
|
if request: |
94
|
|
|
lang = request.GET.get('lang', 'en') |
95
|
|
|
|
96
|
|
|
return {'request': request, |
97
|
|
|
'entities': entities, |
98
|
|
|
'categories': categories, |
99
|
|
|
'curfed': curfed, |
100
|
|
|
'show_filters': context.get('show_filters'), |
101
|
|
|
'append_query': append_query, |
102
|
|
|
'show_total': show_total, |
103
|
|
|
'lang': lang, |
104
|
|
|
'pagination': pagination, |
105
|
|
|
'onclick_page': onclick_page, |
106
|
|
|
'onclick_export': onclick_export, |
107
|
|
|
'entity_types': DESCRIPTOR_TYPES} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
@register.inclusion_tag('metadataparser/most_fed_entities_summary.html', takes_context=True) |
111
|
|
|
def most_fed_entity_list(context, entities, categories=None, pagination=None, curfed=None, show_total=True, append_query=None, onclick_page=None, onclick_export=None): |
112
|
|
|
request = context.get('request', None) |
113
|
|
|
lang = 'en' |
114
|
|
|
if request: |
115
|
|
|
lang = request.GET.get('lang', 'en') |
116
|
|
|
|
117
|
|
|
return {'request': request, |
118
|
|
|
'entities': entities, |
119
|
|
|
'categories': categories, |
120
|
|
|
'curfed': curfed, |
121
|
|
|
'show_filters': context.get('show_filters'), |
122
|
|
|
'append_query': append_query, |
123
|
|
|
'show_total': show_total, |
124
|
|
|
'lang': lang, |
125
|
|
|
'pagination': pagination, |
126
|
|
|
'onclick_page': onclick_page, |
127
|
|
|
'onclick_export': onclick_export, |
128
|
|
|
'entity_types': DESCRIPTOR_TYPES} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
@register.inclusion_tag('metadataparser/service_search_summary.html', takes_context=True) |
132
|
|
|
def service_search_result(context, entities, categories=None, pagination=None, curfed=None, show_total=True, append_query=None, onclick_page=None, onclick_export=None): |
133
|
|
|
request = context.get('request', None) |
134
|
|
|
lang = 'en' |
135
|
|
|
if request: |
136
|
|
|
lang = request.GET.get('lang', 'en') |
137
|
|
|
|
138
|
|
|
return {'request': request, |
139
|
|
|
'entities': entities, |
140
|
|
|
'categories': categories, |
141
|
|
|
'curfed': curfed, |
142
|
|
|
'show_filters': context.get('show_filters'), |
143
|
|
|
'append_query': append_query, |
144
|
|
|
'show_total': show_total, |
145
|
|
|
'lang': lang, |
146
|
|
|
'pagination': pagination, |
147
|
|
|
'onclick_page': onclick_page, |
148
|
|
|
'onclick_export': onclick_export, |
149
|
|
|
'entity_types': DESCRIPTOR_TYPES} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
|
153
|
|
|
@register.inclusion_tag('metadataparser/tag_entity_filters.html', takes_context=True) |
154
|
|
|
def entity_filters(context, entities, categories): |
155
|
|
|
entity_types = ('All', ) + DESCRIPTOR_TYPES |
156
|
|
|
request = context.get('request') |
157
|
|
|
entity_type = request.GET.get('entity_type', '') |
158
|
|
|
entity_category = request.GET.get('entity_category', '') |
159
|
|
|
rquery = request.GET.copy() |
160
|
|
|
for filt in 'entity_type', 'entity_category', 'page': |
161
|
|
|
if filt in rquery: |
162
|
|
|
rquery.pop(filt) |
163
|
|
|
if not entity_type: |
164
|
|
|
entity_type = 'All' |
165
|
|
|
if not entity_category: |
166
|
|
|
entity_category = 'All' |
167
|
|
|
query = urlencode(rquery) |
168
|
|
|
filter_base_path = request.path |
169
|
|
|
return {'filter_base_path': filter_base_path, |
170
|
|
|
'otherparams': query, |
171
|
|
|
'entity_types': entity_types, |
172
|
|
|
'entity_type': entity_type, |
173
|
|
|
'entity_category': entity_category, |
174
|
|
|
'entities': entities, |
175
|
|
|
'categories': categories} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
@register.simple_tag() |
179
|
|
|
def entity_filter_url(base_path, filt, otherparams=None): |
180
|
|
|
url = base_path |
181
|
|
|
if filt != 'All': |
182
|
|
|
url += '?entity_type=%s' % filt |
183
|
|
|
if otherparams: |
184
|
|
|
url += '&%s' % otherparams |
185
|
|
|
elif otherparams: |
186
|
|
|
url += '?%s' % otherparams |
187
|
|
|
|
188
|
|
|
return url |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
@register.simple_tag() |
192
|
|
|
def entitycategory_filter_url(base_path, filt, otherparams=None): |
193
|
|
|
url = base_path |
194
|
|
|
if filt != 'All': |
195
|
|
|
url += '?entity_category=%s' % filt |
196
|
|
|
if otherparams: |
197
|
|
|
url += '&%s' % otherparams |
198
|
|
|
elif otherparams: |
199
|
|
|
url += '?%s' % otherparams |
200
|
|
|
|
201
|
|
|
return url |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
@register.inclusion_tag('metadataparser/export-menu.html', takes_context=True) |
205
|
|
|
def export_menu(context, entities, append_query=None, onclick=None): |
206
|
|
|
request = context.get('request') |
207
|
|
|
copy_query = request.GET.copy() |
208
|
|
|
if 'page' in copy_query: |
209
|
|
|
copy_query.pop('page') |
210
|
|
|
query = copy_query.urlencode() |
211
|
|
|
base_path = request.path |
212
|
|
|
formats = [] |
213
|
|
|
for mode in export_modes.keys(): |
214
|
|
|
url = base_path |
215
|
|
|
if query: |
216
|
|
|
url += '?%s&format=%s' % (query, mode) |
217
|
|
|
else: |
218
|
|
|
url += '?format=%s' % (mode) |
219
|
|
|
if append_query: |
220
|
|
|
url += "&%s" % (append_query) |
221
|
|
|
formats.append({'url': url, 'label': mode, 'onclick': onclick}) |
222
|
|
|
|
223
|
|
|
return {'formats': formats} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
@register.inclusion_tag('metadataparser/export-menu.html') |
227
|
|
|
def export_summary_menu(query, onclick=None): |
228
|
|
|
formats = [] |
229
|
|
|
for mode in export_summary_modes.keys(): |
230
|
|
|
urlquery = {'format': mode, |
231
|
|
|
'export': query} |
232
|
|
|
url = "./?%(query)s" % {'query': urlencode(urlquery)} |
233
|
|
|
formats.append({'url': url, 'label': mode, 'onclick': onclick}) |
234
|
|
|
|
235
|
|
|
return {'formats': formats} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
@register.simple_tag() |
239
|
|
|
def entities_count(entity_qs, entity_type=None): |
240
|
|
|
if entity_type and entity_type != 'All': |
241
|
|
|
return entity_qs.filter(types__xmlname=entity_type).count() |
242
|
|
|
else: |
243
|
|
|
return entity_qs.count() |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
@register.simple_tag() |
247
|
|
|
def get_fed_total(totals, entity_type='All'): |
248
|
|
|
tot_count = 0 |
249
|
|
|
for curtotal in totals: |
250
|
|
|
if entity_type == 'All' or curtotal['types__xmlname'] == entity_type: |
251
|
|
|
tot_count += curtotal['types__xmlname__count'] |
252
|
|
|
return tot_count |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
@register.simple_tag() |
256
|
|
|
def get_fed_count(counts, federation='All', entity_type='All'): |
257
|
|
|
count = counts[entity_type] |
258
|
|
|
|
259
|
|
|
fed_count = 0 |
260
|
|
|
for curcount in count: |
261
|
|
|
if federation == 'All' or curcount['federations__id'] == federation: |
262
|
|
|
fed_count += curcount['federations__id__count'] |
263
|
|
|
return fed_count |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
@register.simple_tag() |
267
|
|
|
def get_fed_count_by_country(count, country='All'): |
268
|
|
|
fed_count = 0 |
269
|
|
|
for curcount in count: |
270
|
|
|
if country == 'All' or curcount['federations__country'] == country: |
271
|
|
|
fed_count += curcount['federations__country__count'] |
272
|
|
|
return fed_count |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
@register.simple_tag(takes_context=True) |
276
|
|
|
def l10n_property(context, prop, lang): |
277
|
|
|
if isinstance(prop, dict) and len(prop) > 0: |
278
|
|
|
if not lang: |
279
|
|
|
lang = context.get('LANGUAGE_CODE', None) |
280
|
|
|
if lang and lang in prop: |
281
|
|
|
return prop.get(lang) |
282
|
|
|
else: |
283
|
|
|
return prop[prop.keys()[0]] |
284
|
|
|
return prop |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
@register.simple_tag(takes_context=True) |
288
|
|
|
def organization_property(context, organizations, prop, lang): |
289
|
|
|
if not isinstance(organizations, list): |
290
|
|
|
return prop |
291
|
|
|
|
292
|
|
|
lang = lang or context.get('LANGUAGE_CODE', None) |
293
|
|
|
for organization in organizations: |
294
|
|
|
if prop in organization: |
295
|
|
|
val = organization[prop] |
296
|
|
|
if organization['lang'] == lang: |
297
|
|
|
val = organization[prop] |
298
|
|
|
|
299
|
|
|
return val |
300
|
|
|
|
301
|
|
|
|
302
|
|
|
@register.simple_tag() |
303
|
|
|
def get_property(obj, prop=None): |
304
|
|
|
uprop = unicode(prop) |
305
|
|
|
if not uprop: |
306
|
|
|
return '<a href="%(link)s">%(name)s</a>' % {"link": obj.get_absolute_url(), |
307
|
|
|
"name": unicode(obj)} |
308
|
|
|
if isinstance(obj, dict): |
309
|
|
|
return obj.get(prop, None) |
310
|
|
|
if getattr(getattr(obj, uprop, None), 'all', None): |
311
|
|
|
return '. '.join(['<a href="%(link)s">%(name)s</a>' % {"link": item.get_absolute_url(), |
312
|
|
|
"name": unicode(item)} |
313
|
|
|
for item in getattr(obj, uprop).all()]) |
314
|
|
|
if isinstance(getattr(obj, uprop, ''), list): |
315
|
|
|
return ', '.join(getattr(obj, uprop, [])) |
316
|
|
|
return getattr(obj, uprop, '') |
317
|
|
|
|
318
|
|
|
|
319
|
|
|
@register.simple_tag(takes_context=True) |
320
|
|
|
def active_url(context, pattern): |
321
|
|
|
request = context.get('request') |
322
|
|
|
if request.path == pattern: |
323
|
|
|
return 'active' |
324
|
|
|
return '' |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
@register.filter(name='display_etype') |
328
|
|
|
def display_etype(value, separator=', '): |
329
|
|
|
if isinstance(value, list): |
330
|
|
|
return separator.join(value) |
331
|
|
|
elif hasattr(value, 'all'): |
332
|
|
|
return separator.join([unicode(item) for item in value.all()]) |
333
|
|
|
else: |
334
|
|
|
if value in DESCRIPTOR_TYPES_DISPLAY: |
335
|
|
|
return DESCRIPTOR_TYPES_DISPLAY.get(value) |
336
|
|
|
else: |
337
|
|
|
return value |
338
|
|
|
|
339
|
|
|
|
340
|
|
|
@register.filter(name='mailto') |
341
|
|
|
def mailto(value): |
342
|
|
|
if value.startswith('mailto:'): |
343
|
|
|
return value |
344
|
|
|
else: |
345
|
|
|
return 'mailto:%s' % value |
346
|
|
|
|
347
|
|
|
|
348
|
|
|
@register.filter(name='wrap') |
349
|
|
|
def wrap(value, length): |
350
|
|
|
value = unicode(value) |
351
|
|
|
if len(value) > length: |
352
|
|
|
return "%s..." % value[:length] |
353
|
|
|
return value |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
class CanEdit(Node): |
357
|
|
|
child_nodelists = 'nodelist' |
358
|
|
|
|
359
|
|
|
def __init__(self, obj, nodelist): |
360
|
|
|
self.obj = obj |
361
|
|
|
self.nodelist = nodelist |
362
|
|
|
|
363
|
|
|
@classmethod |
364
|
|
|
def __repr__(cls): |
365
|
|
|
return "<CanEdit>" |
366
|
|
|
|
367
|
|
|
def render(self, context): |
368
|
|
|
obj = self.obj.resolve(context, True) |
369
|
|
|
user = context.get('user') |
370
|
|
|
if obj.can_edit(user, False): |
371
|
|
|
return self.nodelist.render(context) |
372
|
|
|
else: |
373
|
|
|
return '' |
374
|
|
|
|
375
|
|
|
|
376
|
|
|
def do_canedit(parser, token): |
377
|
|
|
bits = list(token.split_contents()) |
378
|
|
|
if len(bits) != 2: |
379
|
|
|
raise TemplateSyntaxError("%r takes 1 argument" % bits[0]) |
380
|
|
|
end_tag = 'end' + bits[0] |
381
|
|
|
nodelist = parser.parse((end_tag,)) |
382
|
|
|
obj = parser.compile_filter(bits[1]) |
383
|
|
|
token = parser.next_token() |
384
|
|
|
return CanEdit(obj, nodelist) |
385
|
|
|
|
386
|
|
|
|
387
|
|
|
@register.tag |
388
|
|
|
def canedit(parser, token): |
389
|
|
|
""" |
390
|
|
|
Outputs the contents of the block if user has edit pemission |
391
|
|
|
|
392
|
|
|
Examples:: |
393
|
|
|
|
394
|
|
|
{% canedit obj %} |
395
|
|
|
... |
396
|
|
|
{% endcanedit %} |
397
|
|
|
""" |
398
|
|
|
return do_canedit(parser, token) |
399
|
|
|
|
400
|
|
|
|
401
|
|
|
@register.filter |
402
|
|
|
@stringfilter |
403
|
|
|
def split(value, splitter='|'): |
404
|
|
|
if not isinstance(value, SafeData): |
405
|
|
|
value = mark_safe(value) |
406
|
|
|
return value.split(splitter) |
407
|
|
|
|