Skip to content

Commit

Permalink
Wd 10014 credentials sign ups donot end up in marketo (canonical#13706)
Browse files Browse the repository at this point in the history
* move credentials signup to separate view in credentials
  • Loading branch information
abhigyanghosh30 authored Apr 15, 2024
1 parent f0d6079 commit c9b208b
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 45 deletions.
8 changes: 8 additions & 0 deletions templates/credentials/sign-up.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
{% block meta_copydoc %}{% endblock meta_copydoc %}

{% block content %}
{% if error %}
<div class="p-notification--negative">
<div class="p-notification__content">
<h5 class="p-notification__title">Error</h5>
<p class="p-notification__message">{{ error }}</p>
</div>
</div>
{% endif %}
{% with
formid="3801",
lpId="7140",
Expand Down
2 changes: 1 addition & 1 deletion templates/shared/_credentials-contact-us-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>Test our certification program</h1>
</div>
<div class="row">
<div class="col-8">
<form action="/marketo/submit" onsubmit="getCustomFields();" method="post" id="mktoForm_{{formid}}">
<form action="/credentials/sign-up" onsubmit="getCustomFields();" method="post" id="mktoForm_{{formid}}">
<fieldset class="u-no-margin--bottom">
<h3>About you</h3>
<ul class="p-list">
Expand Down
4 changes: 3 additions & 1 deletion webapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,9 @@ def takeovers_index():
app.add_url_rule("/credentials", view_func=cred_home)
app.add_url_rule("/credentials/self-study", view_func=cred_self_study)
app.add_url_rule("/credentials/syllabus", view_func=cred_syllabus_data)
app.add_url_rule("/credentials/sign-up", view_func=cred_sign_up)
app.add_url_rule(
"/credentials/sign-up", view_func=cred_sign_up, methods=["GET", "POST"]
)
app.add_url_rule(
"/credentials/schedule",
view_func=cred_schedule,
Expand Down
122 changes: 119 additions & 3 deletions webapp/shop/cred/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,126 @@ def cred_self_study(**_):

@shop_decorator(area="cred", permission="user", response="html")
def cred_sign_up(**_):
sign_up_open = True
return flask.render_template(
"credentials/sign-up.html", sign_up_open=sign_up_open
if flask.request.method == "GET":
sign_up_open = True
return flask.render_template(
"credentials/sign-up.html", sign_up_open=sign_up_open
)

form_fields = {}
for key in flask.request.form:
values = flask.request.form.getlist(key)
value = ", ".join(values)
if value:
form_fields[key] = value
if "utm_content" in form_fields:
form_fields["utmcontent"] = form_fields.pop("utm_content")
# Check honeypot values are not set
honeypots = {}
honeypots["name"] = flask.request.form.get("name")
honeypots["website"] = flask.request.form.get("website")
if honeypots["name"] is not None and honeypots["website"] is not None:
if honeypots["name"] != "" and honeypots["website"] != "":
raise BadRequest("Unexpected honeypot fields (name, website)")
else:
form_fields["grecaptcharesponse"] = "no-recaptcha"
form_fields.pop("website", None)
form_fields.pop("name", None)

form_fields.pop("thankyoumessage", None)
form_fields.pop("g-recaptcha-response", None)
return_url = form_fields.pop("returnURL", None)

encode_lead_comments = (
form_fields.pop("Encode_Comments_from_lead__c", "yes") == "yes"
)
if encode_lead_comments and "Comments_from_lead__c" in form_fields:
encoded_comment = html.escape(form_fields["Comments_from_lead__c"])
form_fields["Comments_from_lead__c"] = encoded_comment

visitor_data = {
"userAgentString": flask.request.headers.get("User-Agent"),
}
referrer = flask.request.referrer
client_ip = flask.request.headers.get(
"X-Real-IP", flask.request.remote_addr
)

if client_ip and ":" not in client_ip:
visitor_data["leadClientIpAddress"] = client_ip

payload = {
"formId": form_fields.pop("formid"),
"input": [
{
"leadFormFields": form_fields,
"visitorData": visitor_data,
"cookie": flask.request.args.get("mkt"),
}
],
}

try:
marketo_api.submit_form(payload).json()
except Exception:
flask.current_app.extensions["sentry"].captureException(
extra={"payload": payload}
)

return (
flask.render_template(
"credentials/sign-up.html", error="Something went wrong"
),
400,
)

service_account_info = {
"token_uri": "https://oauth2.googleapis.com/token",
"client_email": os.getenv("GOOGLE_SERVICE_ACCOUNT_EMAIL"),
"private_key": os.getenv("GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY").replace(
"\\n", "\n"
),
"scopes": ["https://www.googleapis.com/auth/spreadsheets.readonly"],
}

credentials = service_account.Credentials.from_service_account_info(
service_account_info,
)

service = build("sheets", "v4", credentials=credentials)

sheet = service.spreadsheets()
sheet.values().append(
spreadsheetId="1i9dT558_YYxxdPpDTG5VYewezb5gRUziMG77BtdUZGU",
range="Sheet1",
valueInputOption="RAW",
body={
"values": [
[
form_fields.get("firstName"),
form_fields.get("lastName"),
form_fields.get("email"),
form_fields.get("Job_Role__c"),
form_fields.get("title"),
form_fields.get("Comments_from_lead__c"),
form_fields.get("canonicalUpdatesOptIn"),
]
]
},
).execute()

if return_url:
# Personalize thank-you page
flask.session["form_details"] = {
"name": flask.request.form.get("firstName"),
"email": flask.request.form.get("email"),
}
return flask.redirect(return_url)

if referrer:
return flask.redirect(f"/thank-you?referrer={referrer}")
else:
return flask.redirect("/thank-you")


@shop_decorator(area="cred", permission="user", response="html")
Expand Down
40 changes: 0 additions & 40 deletions webapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
DocParser,
)

from google.oauth2 import service_account
from googleapiclient.discovery import build

# Local
from webapp.login import user_info
Expand Down Expand Up @@ -999,44 +997,6 @@ def marketo_submit():
400,
)

if payload["formId"] == "3801":
service_account_info = {
"token_uri": "https://oauth2.googleapis.com/token",
"client_email": os.getenv("GOOGLE_SERVICE_ACCOUNT_EMAIL"),
"private_key": os.getenv(
"GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY"
).replace("\\n", "\n"),
"scopes": [
"https://www.googleapis.com/auth/spreadsheets.readonly"
],
}

credentials = service_account.Credentials.from_service_account_info(
service_account_info,
)

service = build("sheets", "v4", credentials=credentials)

sheet = service.spreadsheets()
sheet.values().append(
spreadsheetId="1i9dT558_YYxxdPpDTG5VYewezb5gRUziMG77BtdUZGU",
range="Sheet1",
valueInputOption="RAW",
body={
"values": [
[
form_fields.get("firstName"),
form_fields.get("lastName"),
form_fields.get("email"),
form_fields.get("Job_Role__c"),
form_fields.get("title"),
form_fields.get("Comments_from_lead__c"),
form_fields.get("canonicalUpdatesOptIn"),
]
]
},
).execute()

# Send enrichment data
try:
marketo_api.submit_form(enriched_payload).json()
Expand Down

0 comments on commit c9b208b

Please sign in to comment.