forked from jirikuncar/marc-json-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dojson_reverse.tpl
65 lines (56 loc) · 1.95 KB
/
dojson_reverse.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
#
# This file is part of DoJSON
# Copyright (C) 2015 CERN.
#
# DoJSON is free software; you can redistribute it and/or
# modify it under the terms of the Revised BSD License; see LICENSE
# file for more details.
"""To MARC 21 model definition."""
from dojson import utils
from ..model import to_marc21
{%- for tag, field in data if tag|length() == 3 %}
{%- if 'subfields' in field %}
{%- set indicator1 = get_indicator(1, field) %}
{%- set indicator2 = get_indicator(2, field) %}
@to_marc21.over('{{ tag }}', '^{{ clean_name(field.name) }}$')
{%- if field.repeatable %}
@utils.reverse_for_each_value
{%- endif %}
@utils.filter_values
def reverse_{{ clean_name(field.name) }}(self, key, value):
"""Reverse - {{ field.name }}."""
{%- if indicator1.get('name') %}
indicator_map1 = {{ tojson(reverse_indicator_dict(indicator1.get('values', {}))) }}
{%- endif %}
{%- if indicator2.get('name') %}
indicator_map2 = {{ tojson(reverse_indicator_dict(indicator2.get('values', {}))) }}
{%- endif %}
return {
{%- for code, subfield in field.get('subfields').iteritems() %}
{%- if subfield.get('repeatable', False) %}
'{{ code }}': utils.reverse_force_list(
value.get('{{ clean_name(subfield['name']) }}')
),
{%- else %}
'{{ code }}': value.get('{{ clean_name(subfield['name']) }}'),
{%- endif %}
{%- endfor %}
{%- if indicator1.get('name') %}
'$ind1': indicator_map1.get(value.get('{{ indicator1['name'] }}'), '_'),
{%- else %}
'$ind1': '_',
{%- endif %}
{%- if indicator2.get('name') %}
'$ind2': indicator_map2.get(value.get('{{ indicator2['name'] }}'), '_'),
{%- else %}
'$ind2': '_',
{%- endif %}
}
{%- else %}
@to_marc21.over('{{ tag }}', '^{{ clean_name(field.name) }}$')
def reverse_{{ clean_name(field.name) }}(self, key, value):
"""Reverse - {{ field.name }}."""
return [value]
{%- endif %}
{%- endfor %}