Skip to content

Commit

Permalink
some more cleanup around CMS story template rendering to make sure cr…
Browse files Browse the repository at this point in the history
…edit is handled correctly and consistently for NHC as well as NWS. TODO: cleanup email template rendering, see #4
  • Loading branch information
JoeGermuska committed Sep 7, 2023
1 parent bc8dfea commit a41a040
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
40 changes: 19 additions & 21 deletions NHC.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from bs4 import BeautifulSoup

from util import Translator,load_parsed_data, save_parsed_data,contains_area
from util import Translator,load_parsed_data, render_template, save_parsed_data,contains_area
from jinja2 import Template

import logging
Expand Down Expand Up @@ -315,26 +315,24 @@ def writeNHC(bulletin) -> dict:
else:
for event in results['data']["practive"]:
eventtype= event["type"].replace(" ", "").lower()
with open("templates/story_templates/" + eventtype + ".html") as f:
template = Template(f.read())
new_story = template.render(data=results['data'], event=event, flags=results['flags'], signals=results['signals'])
soup = BeautifulSoup(new_story, 'html.parser')
p_tags= soup.find_all('p')

new_story= [re.sub(r'\s+', ' ', p.get_text(strip=True)) for p in p_tags if p.get_text(strip=True)]
with open("templates/email_templates/storypublished.html") as f:
emailtemplate = Template(f.read())
emailcontent = emailtemplate.render(data=results['data'], event=event, flags=results['flags'], signals=results['signals'])
emailcontent= BeautifulSoup(emailcontent, 'html.parser').find_all('p')
emailcontent= [re.sub(r'\s+', ' ', p.get_text(strip=True)) for p in emailcontent if p.get_text(strip=True)]
new_story={
"body": '\n'.join(new_story),
"headline": results['data']["headline"] ,
"event": event["type"],
"email": '\n'.join(emailcontent),
'image_code': IMAGE_CODES.get(eventtype)
}
generated_stories.append(new_story)
rendered = render_template(f"story_templates/{eventtype}.html",
data=results['data'],
event=event,
flags=results['flags'],
signals=results['signals'])
with open("templates/email_templates/storypublished.html") as f:
emailtemplate = Template(f.read())
emailcontent = emailtemplate.render(data=results['data'], event=event, flags=results['flags'], signals=results['signals'])
emailcontent= BeautifulSoup(emailcontent, 'html.parser').find_all('p')
emailcontent= [re.sub(r'\s+', ' ', p.get_text(strip=True)) for p in emailcontent if p.get_text(strip=True)]
new_story={
"body": rendered,
"headline": results['data']["headline"] ,
"event": event["type"],
"email": '\n'.join(emailcontent),
'image_code': IMAGE_CODES.get(eventtype)
}
generated_stories.append(new_story)
return {
"content": generated_stories,
"action":"post"
Expand Down
22 changes: 7 additions & 15 deletions NWS.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from util import load_parsed_data, save_parsed_data, contains_area, Translator
from util import load_parsed_data, render_template, save_parsed_data, contains_area, Translator
from jinja2 import Template, Environment, FileSystemLoader
from datetime import datetime
import requests
Expand Down Expand Up @@ -171,20 +171,12 @@ def generate_nws_stories(bulletin) :
generated_stories=[]

for event in data:

with open("templates/story_templates/NWS.html") as f :
template = Environment(loader=FileSystemLoader("templates/story_templates/")).from_string(f.read())
#Template(f.read())
new_story = template.render(data=event)
soup = BeautifulSoup(new_story, 'html.parser')
p_tags= soup.find_all('p') # TODO this seems uncomfortably confident that no one will ever change the templates to use other than p tags. isn't there another way to get content out of a soup?

new_story='\n'.join([ elem.get_text() for elem in p_tags])
generated_stories.append({
"content" : new_story,
"headline": event["headline"],
'image_code': event.get('image_code')
})
new_story = render_template('story_templates/NWS.html', data=event)
generated_stories.append({
"content" : new_story,
"headline": event["headline"],
'image_code': event.get('image_code')
})

return generated_stories

Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from smtplib import SMTP
from email.message import EmailMessage
import time
from jinja2 import Environment, FileSystemLoader, select_autoescape

import logging
logger = logging.getLogger('util')
Expand All @@ -17,6 +18,18 @@
SMTP_PASSWORD = os.environ['SMTP_PASSWORD']
SENDER_EMAIL = 'Weatherbot <[email protected]>'

JINJA_ENVIRONMENT = Environment(
loader=FileSystemLoader('templates'),
autoescape=select_autoescape()
)


def render_template(path, **kwargs):
"""Given a relative template path and any context for rendering the template,
do it all here, in one place. """
template = JINJA_ENVIRONMENT.get_template(path)
return template.render(**kwargs)

def send_email(recipients, subject, body , url=None, actually_send_email=False):
if recipients is not None:
msg = EmailMessage()
Expand Down

0 comments on commit a41a040

Please sign in to comment.