This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
custom_item_fields.py
95 lines (67 loc) · 2.88 KB
/
custom_item_fields.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (C) 2013 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Set of static methods to provide custom fields for a timeline item.
custom_item_fields uses timelineItem.sourceItemId of a timeline
item to enable custom fields (i.e. variables) for the timeline
item. Each custom field is a key->value pair encapsulated in a
dictionary (usually called fields). The dictionary is JSON
serialized and stored in the sourceItemId field of a timeline item.
Additionally, a possible use of a custom_item_fields is to use the
custom fields to render the HTML of a timeline item. This class
takes care of this by using jinja templates.
"""
__author__ = 'jewang.net (Jennifer Wang)'
import jinja2
import json
import os
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
KEY = 'sourceItemId'
def get_fields_from_json(json_input):
"""Returns a dictionary from json_input."""
return json.loads(json_input)
def get_fields_from_item(item):
"""Returns dictionary made from sourceItemId field timeline item.
Constructs a new dictionary holding custom fields from json_input
assuming that input JSON is in item['sourceItemId'].
"""
return get_fields_from_json(item.get(KEY, '{}'))
def get_json_from_fields(fields):
"""Returns JSON for custom fields to go in item['sourceItemId']."""
return json.dumps(fields)
def render_html_from_fields(fields, template_url):
"""Returns html rendering of timeline item with custom fields data."""
template = jinja_environment.get_template(template_url)
return template.render(fields)
def get(item, key):
"""Returns value associated with in input key."""
fields = get_fields_from_item(item)
return fields[key]
def set(item, key, val, template_url=''):
"""Sets key->value custom field in timeline item."""
return set_multiple(item, {key: val}, template_url)
def set_multiple(item, new_fields, template_url=''):
"""Sets key->value custom fields in timeline item.
To do this, this method updates a timeline item's sourceItemId
and, if enabled, template html.
Generating JSON and rendering html with each call could cause
performance issues on larger scale Glassware.
"""
fields = get_fields_from_item(item)
fields.update(new_fields)
item['sourceItemId'] = get_json_from_fields(fields)
if template_url:
item['html'] = render_html_from_fields(
fields, template_url)
return item