Skip to content

Commit

Permalink
Merge pull request #733 from nrvikas/1.29
Browse files Browse the repository at this point in the history
For Release 1.29-RC3
  • Loading branch information
nrvikas authored Jun 12, 2019
2 parents f900c51 + 3c3eb8c commit b001e19
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "superdesk",
"license": "GPL-3.0",
"dependencies": {
"superdesk-core": "superdesk/superdesk-client-core#715197d",
"superdesk-core": "superdesk/superdesk-client-core#baff55fb",
"superdesk-planning": "superdesk/superdesk-planning#1.29-aap",
"superdesk-analytics": "superdesk/superdesk-analytics#e4188fd"
}
Expand Down
69 changes: 34 additions & 35 deletions server/aap/macros/abs_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,29 @@ def expand_token(token, item, template_map):
abs_web_service_token = app.config.get('ABS_WEB_SERVICE_TOKEN')
url_suffix = '/all?dimensionAtObservation=allDimensions&detail=DataOnly&APIKey='

# logger.info('ABS request : {}'.format(url_prefix + token_map.get(token) + url_suffix + abs_web_service_token))
r = requests.get(url_prefix + token_map.get(token) + url_suffix + abs_web_service_token, verify=False)
# convert the token in the item to one that is jinja compliant
jinja_token = re.sub('\\.|/|\\+|#', '_', token)
item['body_html'] = item.get('body_html').replace(token, jinja_token)
item['headline'] = item.get('headline').replace(token, jinja_token)
item['abstract'] = item.get('abstract').replace(token, jinja_token)

# Get the token for the primary value
temp_token = re.sub('\\.|/|\\+', '_', token)
value_token = temp_token.split('#')[0] + '__' if '#' in token else temp_token

# If we have handled the value we don't need to do it again
if template_map.get(value_token):
return

data_identifier = token_map.get(token).split('#')[0] if '#' in token_map.get(token) else token_map.get(token)
# logger.info('ABS request : {}'.format(url_prefix + data_identifier + url_suffix + abs_web_service_token))
r = requests.get(url_prefix + data_identifier + url_suffix + abs_web_service_token, verify=False)
if r.status_code == 200:
# logger.info('Response Text [{}]'.format(r.text))
try:
response = json.loads(r.text)
except:
logger.error('Exception parsing json')
logger.error('Exception parsing json for {}'.format(data_identifier))
return

# get the number of dimensions in the dataset
Expand All @@ -67,21 +82,10 @@ def expand_token(token, item, template_map):
value = str(round(raw_value, 2))
else:
value = str(response['dataSets'][0]['observations'][dimension_key][0])

# convert the token to one that is jinja compliant
jinja_token = re.sub('\\.|/|\\+', '_', token)
item['body_html'] = item.get('body_html').replace(token, jinja_token)
item['headline'] = item.get('headline').replace(token, jinja_token)
item['abstract'] = item.get('abstract').replace(token, jinja_token)

template_map[jinja_token] = value
template_map[value_token] = value

# the token for the period
jinja_token = jinja_token[:-1] + 'PERIOD__'
item['body_html'] = item.get('body_html').replace(token[:-2] + '#PERIOD__', jinja_token)
item['headline'] = item.get('headline').replace(token[:-2] + '#PERIOD__', jinja_token)
item['abstract'] = item.get('abstract').replace(token[:-2] + '#PERIOD__', jinja_token)
template_map[jinja_token] = last_period_name
template_map[value_token[:-1] + 'PERIOD__'] = last_period_name

# calculate the change from the preceeding value
last_period_index -= 1
Expand All @@ -96,31 +100,21 @@ def expand_token(token, item, template_map):
adjective = 'rose'
else:
adjective = 'held steady'
jinja_token = jinja_token.replace('_PERIOD__', '_ADJECTIVE__')
item['body_html'] = item.get('body_html').replace(token[:-2] + '#ADJECTIVE__', jinja_token)
item['headline'] = item.get('headline').replace(token[:-2] + '#ADJECTIVE__', jinja_token)
item['abstract'] = item.get('abstract').replace(token[:-2] + '#ADJECTIVE__', jinja_token)
template_map[jinja_token] = adjective

template_map[value_token[:-1] + 'ADJECTIVE__'] = adjective

if isinstance(prev_value, float):
value = str(round(prev_value, 2))
else:
value = str(response['dataSets'][0]['observations'][dimension_key][0])
jinja_token = jinja_token.replace('_ADJECTIVE__', '_PREV__')
item['body_html'] = item.get('body_html').replace(token[:-2] + '#PREV__', jinja_token)
item['headline'] = item.get('headline').replace(token[:-2] + '#PREV__', jinja_token)
item['abstract'] = item.get('abstract').replace(token[:-2] + '#PREV__', jinja_token)
template_map[jinja_token] = value

template_map[value_token[:-1] + 'PREV__'] = value

prev_period_name = response.get('structure').get('dimensions').get('observation')[-1].get('values')[-2].get(
'name')
jinja_token = jinja_token.replace('_PREV__', '_PREVPERIOD__')
item['body_html'] = item.get('body_html').replace(token[:-2] + '#PREVPERIOD__', jinja_token)
item['headline'] = item.get('headline').replace(token[:-2] + '#PREVPERIOD__', jinja_token)
item['abstract'] = item.get('abstract').replace(token[:-2] + '#PREVPERIOD__', jinja_token)
template_map[jinja_token] = prev_period_name
template_map[value_token[:-1] + 'PREVPERIOD__'] = prev_period_name
else:
logger.info('ABS API returned {}'.format(r.status_code))
logger.info('ABS API returned {} for {}'.format(r.status_code, data_identifier))
time.sleep(.120)


Expand All @@ -129,13 +123,18 @@ def abs_expand(item, **kwargs):
# find the primary tokens, delimitered double underscores
tokens = re.findall('__(.*?)__', item['body_html'])
for t in tokens:
if '__' + t + '__' not in token_map and '#' not in t:
if '__' + t + '__' not in token_map:
token_map['__' + t + '__'] = t
for e in token_map:
if e in item['body_html'] or e in item['headline'] or e in item['abstract']:
expand_token(e, item, template_map)

item['body_html'] = render_template_string(item.get('body_html', ''), **template_map)
try:
item['body_html'] = render_template_string(item.get('body_html', ''), **template_map)
item['abstract'] = render_template_string(item.get('abstract', ''), **template_map)
item['headline'] = render_template_string(item.get('headline', ''), **template_map)
except Exception as ex:
logger.warning(ex)

return item

Expand All @@ -144,4 +143,4 @@ def abs_expand(item, **kwargs):
label = 'ABS indicator expand'
callback = abs_expand
access_type = 'frontend'
action_type = 'interactive'
action_type = 'direct'
4 changes: 2 additions & 2 deletions server/aap/macros/am_bob_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def am_bob_publish(item, **kwargs):
# If the item has a location/place prefix then try to remove that from the headline
if len(item.get('place', [])):
item['headline'] = re.sub('^' + item.get('place')[0].get('qcode', '') + ': ', '',
item.get('headline', ''), re.IGNORECASE)
item.get('headline', ''), flags=re.IGNORECASE)

html = item.get('body_html')
if html:
Expand All @@ -71,7 +71,7 @@ def am_bob_publish(item, **kwargs):
item['body_html'] = item.get('body_html', '').replace('<p>RTV</p>', '')
break
if re.search(r'^[A-Za-z0-9_]{2,8} RTV', par.text, re.IGNORECASE):
par.text = par.text.replace(' RTV', '')
par.text = par.text.replace(' RTV', '').replace('RAW', 'Reuters')
item['body_html'] = to_string(parsed, method='html')
break
count = ++count
Expand Down
2 changes: 1 addition & 1 deletion server/aap/macros/am_bob_publisher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ def test_agency_content(self):
self.assertEqual(item['slugline'], 'AM UK Gove')
self.assertEqual(item['place'], [{'country': '', 'qcode': 'UK', 'group': 'Rest Of World', 'name': 'UK',
'world_region': 'Europe', 'state': ''}])
self.assertIn('<p>RAW</p>', item['body_html'])
self.assertIn('<p>Reuters</p>', item['body_html'])
self.assertEqual(item['genre'], [{'name': 'AM Service', 'qcode': 'AM Service'}])
10 changes: 7 additions & 3 deletions server/templates/outlook_planning_template.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{% for item in items %}
<p>{% if item.get('place') %}{{ item.get('place')|join(', ') }} - {% endif %}{% if item.get('description_text') %}{{ item.description_text }}, {% endif %}{% if (item.get('event') or {}).get('location') %}{{ item.event.location[0].name }},
{% endif %}{% if item.get('schedule') %}{{ item.schedule }} {% endif %}({{ item.slugline }}) - {{ item.get('assignees')|join(', ') }}</p>
{% for item in items %}{% if item['published_archive_items']|length == 0 and item['text_assignees']|length == 0 %}<p>{% if item.get('place') %}{{ item.get('place')|join(', ') }} - {% endif %}{% if item.get('description_text') %}{{ item.description_text }} {% endif %}{% if (item.get('event') or {}).get('location') %}{{ item.event.location[0].name }},
{% endif %}{% if item.get('schedule') %}{{ item.schedule }} {% endif %}({{ item.slugline }})</p>{% endif %}
{% for i in item['published_archive_items'] %}<p>{% if item.get('place') %}{{ item.get('place')|join(', ') }} - {% endif %}{{ i['archive_text'] }} ({{i['archive_slugline']}}){% if not item.get('event') and item['text_assignees']|length > 0 %} TO COME ({{ item.slugline }}) - {{ item.get('text_assignees')|join(', ') }}{% endif %}</p>
{% endfor %}
{% if item['text_assignees']|length > 0 and ((item['event']) or item['published_archive_items']|length == 0) %}
<p>{% if item.get('place') %}{{ item.get('place')|join(', ') }} - {% endif %}{% if item.get('description_text') %}{{ item.description_text }} {% endif %}{% if (item.get('event') or {}).get('location') %}{{ item.event.location[0].name }},
{% endif %}{% if item.get('schedule') %}{{ item.schedule }} {% endif %}({{ item.slugline }}) - {{ item.get('text_assignees')|join(', ') }}</p>{% endif %}
{% endfor %}

0 comments on commit b001e19

Please sign in to comment.