Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for THDMI Japan signups #477

Merged
merged 13 commits into from
Nov 3, 2022
8 changes: 8 additions & 0 deletions microsetta_private_api/admin/email_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ class EmailMessage(Enum):
EventType.EMAIL,
EventSubtype.EMAIL_ADDRESS_INVALID
)
submit_interest_confirmation = (
gettext(
"We received your sign-up"),
"email/submit_interest_confirmation.jinja2",
("contact_name",),
EventType.EMAIL,
EventSubtype.EMAIL_SUBMIT_INTEREST_CONFIRMATION
)

def __init__(self, subject, html, required, event_type, event_sub):
self.subject = subject
Expand Down
21 changes: 21 additions & 0 deletions microsetta_private_api/api/_interested_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from microsetta_private_api.repo.interested_user_repo import InterestedUserRepo
from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.exceptions import RepoException
from microsetta_private_api.repo.campaign_repo import CampaignRepo
from microsetta_private_api.tasks import send_email


def create_interested_user(body):
Expand All @@ -29,6 +31,25 @@ def create_interested_user(body):
message="Failed to create interested user."
), 400

if interested_user_id:
wasade marked this conversation as resolved.
Show resolved Hide resolved
campaign_repo = CampaignRepo(t)
campaign_info =\
campaign_repo.get_campaign_by_id(interested_user.campaign_id)

if campaign_info.send_thdmi_confirmation:
try:
# Send a confirmation email
# TODO: Add more intelligent locale determination.
wasade marked this conversation as resolved.
Show resolved Hide resolved
# Punting on that since our current campaign use cases
# are only a single language.
send_email(interested_user.email,
"submit_interest_confirmation",
{"contact_name": interested_user.first_name},
campaign_info.language_key)
except: # noqa
# try our best to email
pass

t.commit()

# opening a new transaction for address verification so we don't lose the
Expand Down
6 changes: 5 additions & 1 deletion microsetta_private_api/api/microsetta_private_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,8 @@ paths:
type: string
'extension':
type: string
'send_thdmi_confirmation':
type: string
required:
- title
- associated_projects
Expand Down Expand Up @@ -1531,6 +1533,8 @@ paths:
type: string
'extension':
type: string
'send_thdmi_confirmation':
type: string
required:
- campaign_id
- title
Expand Down Expand Up @@ -2731,7 +2735,7 @@ components:
example: "standard"
language:
type: string
enum: ["en_US", "es_MX", "es_ES"]
enum: ["en_US", "es_MX", "es_ES", "ja_JP"]
example: "en_US"
creation_time:
type: string
Expand Down
4 changes: 2 additions & 2 deletions microsetta_private_api/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,7 @@ def test_edit_sample_locked(self):

# if sample date is less than 10 years
now = datetime.datetime.now()
delta = relativedelta(year=now.year-11)
delta = relativedelta(years=now.year-11)
date = now+delta
post_resp = self.client.put(
'%s?%s' % (base_url, self.default_lang_querystring),
Expand All @@ -1818,7 +1818,7 @@ def test_edit_sample_locked(self):

# if sample date is greater than 30 days
now = datetime.datetime.now()
delta = relativedelta(month=now.month+2)
delta = relativedelta(months=now.month+2)
date = now+delta
post_resp = self.client.put(
'%s?%s' % (base_url, self.default_lang_querystring),
Expand Down
1 change: 1 addition & 0 deletions microsetta_private_api/db/patches/0103.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE campaign.campaigns ADD COLUMN send_thdmi_confirmation BOOLEAN DEFAULT FALSE;
wasade marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions microsetta_private_api/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
EN_GB = "en_GB"
ES_MX = "es_MX"
ES_ES = "es_ES"
JA_JP = "ja_JP"

NEW_PARTICIPANT_KEY = "new_participant"
LANG_NAME_KEY = "lang_name"
Expand Down
3 changes: 2 additions & 1 deletion microsetta_private_api/model/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class Campaign(ModelBase):
def __init__(self, campaign_id, title, instructions, header_image,
permitted_countries, language_key, accepting_participants,
associated_projects, language_key_alt, title_alt,
instructions_alt):
instructions_alt, send_thdmi_confirmation):
self.campaign_id = campaign_id
self.title = title
self.instructions = instructions
Expand All @@ -328,6 +328,7 @@ def __init__(self, campaign_id, title, instructions, header_image,
self.language_key_alt = language_key_alt
self.title_alt = title_alt
self.instructions_alt = instructions_alt
self.send_thdmi_confirmation = send_thdmi_confirmation

def to_api(self):
return self.__dict__.copy()
2 changes: 2 additions & 0 deletions microsetta_private_api/model/log_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class EventSubtype(Enum):
EMAIL_PER_PROJECT_SUMMARY = "per_project_summary"
# for addresses that Melissa deems invalid
EMAIL_ADDRESS_INVALID = "address_invalid"
# for confirmation emails of interested user signups
EMAIL_SUBMIT_INTEREST_CONFIRMATION = "submit_interest_confirmation"


class LogEvent(ModelBase):
Expand Down
36 changes: 27 additions & 9 deletions microsetta_private_api/repo/campaign_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ def _campaign_to_row(c):
return (c.campaign_id, c.title, c.instructions, c.header_image,
c.permitted_countries, c.language_key,
c.accepting_participants, '',
c.language_key_alt, c.title_alt, c.instructions_alt)
c.language_key_alt, c.title_alt, c.instructions_alt,
c.send_thdmi_confirmation)

def _row_to_campaign(self, r):
associated_projects = ", ".join(self._get_projects(r['campaign_id']))
return Campaign(r['campaign_id'], r['title'], r['instructions'],
r['header_image'], r['permitted_countries'],
r['language_key'], r['accepting_participants'],
associated_projects, r['language_key_alt'],
r['title_alt'], r['instructions_alt'])
r['title_alt'], r['instructions_alt'],
r['send_thdmi_confirmation'])

def _get_projects(self, campaign_id):
with self._transaction.cursor() as cur:
Expand Down Expand Up @@ -66,7 +68,7 @@ def get_all_campaigns(self):
"SELECT campaign_id, title, instructions, header_image, "
"permitted_countries, language_key, accepting_participants, "
"language_key_alt, title_alt, "
"instructions_alt "
"instructions_alt, send_thdmi_confirmation "
"FROM campaign.campaigns ORDER BY title"
)
rows = cur.fetchall()
Expand All @@ -86,24 +88,37 @@ def create_campaign(self, **kwargs):
title_alt = kwargs.get('title_alt')
instructions_alt = kwargs.get('instructions_alt')
extension = kwargs.get('extension')
send_thdmi_confirmation = kwargs.get('send_thdmi_confirmation')

with self._transaction.cursor() as cur:
cur.execute(
"INSERT INTO campaign.campaigns (title, instructions, "
"permitted_countries, language_key, accepting_participants, "
"language_key_alt, title_alt, "
"instructions_alt) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s) "
"instructions_alt, send_thdmi_confirmation) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) "
"RETURNING campaign_id",
(title, instructions, permitted_countries, language_key,
accepting_participants, language_key_alt, title_alt,
instructions_alt)
instructions_alt, send_thdmi_confirmation)
)
campaign_id = cur.fetchone()[0]

if campaign_id is None:
raise RepoException("Error inserting campaign into database")
else:
# This code is not ideal but the various campaign creation
wasade marked this conversation as resolved.
Show resolved Hide resolved
# methods pass several different datatypes in.
if isinstance(associated_projects, tuple):
associated_projects = list(associated_projects)
elif isinstance(associated_projects, str):
if "," in associated_projects:
associated_projects = associated_projects.split(",")
else:
associated_projects = [associated_projects, ]
wasade marked this conversation as resolved.
Show resolved Hide resolved
elif isinstance(associated_projects, int):
wasade marked this conversation as resolved.
Show resolved Hide resolved
associated_projects = [associated_projects, ]

cur.executemany(
"INSERT INTO campaign.campaigns_projects ("
"campaign_id,project_id"
Expand Down Expand Up @@ -132,17 +147,19 @@ def update_campaign(self, **kwargs):
title_alt = kwargs.get('title_alt')
instructions_alt = kwargs.get('instructions_alt')
extension = kwargs.get('extension')
send_thdmi_confirmation = kwargs.get('send_thdmi_confirmation')

with self._transaction.cursor() as cur:
cur.execute(
"UPDATE campaign.campaigns SET title = %s, instructions = %s, "
"permitted_countries = %s, language_key = %s, "
"accepting_participants = %s, language_key_alt = %s, "
"title_alt = %s, instructions_alt = %s "
"title_alt = %s, instructions_alt = %s, "
"send_thdmi_confirmation = %s "
"WHERE campaign_id = %s",
(title, instructions, permitted_countries, language_key,
accepting_participants, language_key_alt, title_alt,
instructions_alt, campaign_id)
instructions_alt, send_thdmi_confirmation, campaign_id)
)

self.update_header_image(campaign_id, extension)
Expand All @@ -156,7 +173,8 @@ def get_campaign_by_id(self, campaign_id):
"SELECT campaign_id, title, instructions, header_image, "
"permitted_countries, language_key, "
"accepting_participants, "
"language_key_alt, title_alt, instructions_alt "
"language_key_alt, title_alt, instructions_alt, "
"send_thdmi_confirmation "
"FROM campaign.campaigns WHERE campaign_id = %s",
(campaign_id,)
)
Expand Down
5 changes: 3 additions & 2 deletions microsetta_private_api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from microsetta_private_api.exceptions import RepoException
from microsetta_private_api.celery_utils import celery, init_celery
from microsetta_private_api.localization import EN_US, ES_MX, ES_ES
from microsetta_private_api.localization import EN_US, ES_MX, ES_ES, JA_JP


"""
Expand Down Expand Up @@ -85,7 +85,8 @@ def get_locale():
if not flask.has_request_context():
return EN_US

return request.accept_languages.best_match([EN_US, ES_ES, ES_MX],
return request.accept_languages.best_match([EN_US, ES_ES, ES_MX,
JA_JP],
default=EN_US)

init_celery(celery, app.app)
Expand Down
Loading