Skip to content

Commit

Permalink
add support for emailing everyone about tng
Browse files Browse the repository at this point in the history
  • Loading branch information
hpiwowar committed Mar 21, 2016
1 parent 12806ce commit e77aa6e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 4 deletions.
27 changes: 27 additions & 0 deletions daily.py
Expand Up @@ -403,6 +403,31 @@ def email_report_to_live_profiles(url_slug=None, min_url_slug=None, max_emails=N



def email_all_profiles_about_tng(url_slug=None, min_url_slug=None, max_emails=None):
number_emails_sent = 0

q = profile_query(url_slug, min_url_slug)

for profile in windowed_query(q, Profile.url_slug, 25):

logger.debug(u"in email_all_profiles_about_tng for {url_slug}".format(
url_slug=profile.url_slug))

try:
if not profile.email or (u"@" not in profile.email):
pass
logger.info(u"not sending, no email address for {url_slug}".format(url_slug=profile.url_slug))
else:
logger.info(u"emailing {url_slug} about tng".format(url_slug=profile.url_slug))
tasks.send_tng_email(profile)

except Exception as e:
logger.warning(u"EXCEPTION in email_all_profiles_about_tng for {url_slug}, skipping to next profile. Error {e}".format(
url_slug=profile.url_slug, e=e))
pass


return

def build_refsets(save_after_every_profile=False):
refset_builder = RefsetBuilder()
Expand Down Expand Up @@ -1120,6 +1145,8 @@ def debug_biblio_for_live_profiles(args):
def main(function, args):
if function=="emailreports":
email_report_to_live_profiles(args["url_slug"], args["min_url_slug"], args["max_emails"])
elif function=="email_tng":
email_all_profiles_about_tng(args["url_slug"], args["min_url_slug"], args["max_emails"])
elif function=="dedup":
dedup_everyone(args["url_slug"], args["min_url_slug"])
elif function=="productdeets":
Expand Down
45 changes: 44 additions & 1 deletion tasks.py
Expand Up @@ -11,6 +11,7 @@
from totalimpactwebapp.json_sqlalchemy import JSONAlchemy
from totalimpactwebapp.cards_factory import *
from totalimpactwebapp import notification_report
from totalimpactwebapp import tng_report
from totalimpactwebapp.drip_email import drip_email_context
import emailer

Expand Down Expand Up @@ -105,13 +106,55 @@ def send_email_if_new_diffs(profile):
return status


def send_tng_email(profile, now=None):
print "at top of send_tng_email"
status = "started"
if not now:
now = datetime.datetime.utcnow()

if os.getenv("ENVIRONMENT", "testing") == "production":
email = profile.email
else:
email = "heather@impactstory.org"
profile.last_email_sent = now
db.session.merge(profile)

try:
db.session.commit()
except InvalidRequestError:
logger.info(u"rollback, trying again to update profile object in send_email_report for {url_slug}".format(url_slug=profile.url_slug))
db.session.rollback()
db.session.commit()

print "here now"

report = tng_report.make(profile)
if report["profile"]:

if profile.is_live:
if profile.orcid_id:
msg = emailer.send(email, "News about live Impactstory; you have an orcid", "tng_announcement", report)
else:
msg = emailer.send(email, "News about live Impactstory; you don't have an orcid", "tng_announcement", report)
else:
if profile.orcid_id:
msg = emailer.send(email, "News about trial Impactstory; you have an orcid", "tng_announcement", report)
else:
msg = emailer.send(email, "News about trial Impactstory; you don't have an orcid", "tng_announcement", report)

status = "email sent"
logger.info(u"SENT EMAIL to {url_slug}!!".format(url_slug=profile.url_slug))

return status



def send_email_report(profile, now=None):
status = "started"
if not now:
now = datetime.datetime.utcnow()
report = notification_report.make(profile)
db.session.merge(profile)
report = notification_report.make(profile)

if report["cards"]:
if os.getenv("ENVIRONMENT", "testing") == "production":
Expand Down
7 changes: 4 additions & 3 deletions totalimpactwebapp/countries_info.py
Expand Up @@ -9960,6 +9960,7 @@
country_iso_by_name = {}
for country in countries_info_list:
country_iso_by_name[country["name"]["common"]] = country["cca2"]
country_iso_by_name[country["name"]["official"]] = country["cca2"]
for country_spelling in country["altSpellings"]:
country_iso_by_name[country_spelling] = country["cca2"]
# country_iso_by_name[country["name"]["official"]] = country["cca2"]
# for country_spelling in country["altSpellings"]:
# country_iso_by_name[country_spelling] = country["cca2"]

33 changes: 33 additions & 0 deletions totalimpactwebapp/templates/tng_announcement.html
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>Your Impactstory report</title>
<style type="text/css">
{{ css }}
</style>
<link rel="stylesheet" href="https://impactstory.org/static/less.emails/css/new-metrics.css"/>
</head>
<body>
<div class="report-header">
<img class="logo" src="https://impactstory.org/static/img/impactstory-logo-sideways.png" alt="Impactstory logo"/>
<h1>Announcement</h1>

<h2>
<img src="https://www.gravatar.com/avatar/{{ profile.email_hash }}?s=110&d=mm" alt="user avatar"/>
<span class="name">{{ profile.given_name }} {{ profile.surname }}</span>
<a class="link-to-profile" href="https://impactstory.org/{{ profile.url_slug }}">impactstory.org/{{ profile.url_slug }}</a>
<div class="clearfix"></div>
</h2>
</div>

<div class="body">
orcid if they have one: {{ profile.orcid_id }}
</div>

<div class="footer">
<span class="change-settings">
<--a class="unsubscribe" href="https://impactstory.org/settings/notifications">Unsubscribe</--a> |
</span>
</div>
</body>
</html>
39 changes: 39 additions & 0 deletions totalimpactwebapp/tng_report.py
@@ -0,0 +1,39 @@
from totalimpactwebapp.cards_factory import *
import os


def make(profile):

response = {
"profile": profile,
"css": get_css(),
}
return response


def get_css():
path = os.path.join(
os.path.dirname(__file__),
'static/less.emails/css/new-metrics.css'
)
file = open(path, "r")
return file.read()



















0 comments on commit e77aa6e

Please sign in to comment.