Skip to content

Commit d533061

Browse files
authored
Merge pull request #659 from OriginProtocol/oeth-subscribe
add endpoint for oeth subscriptions and add source column the email_l…
2 parents 0371271 + bab0812 commit d533061

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Django",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "main.py",
12+
"django": true,
13+
"justMyCode": true,
14+
"pythonPath": "/opt/homebrew/bin/python3.10"
15+
}
16+
]
17+
}

database/db_models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class EmailList(db.Model):
1313
__tablename__ = 'email_list'
1414

1515
email = db.Column(db.String(255), primary_key=True, autoincrement=False)
16+
source = db.Column(db.String(255), index=True)
1617
unsubscribed = db.Column(db.Boolean())
1718
created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
1819
ip_addr = db.Column(db.String(100))

logic/emails/mailing_list.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def mass_unsubscribe_sendgrid_contact(emails):
7373
# Inserts or updates an entry in the email_list table.
7474
# Returns True if a new entry was added, False if the entry already existed.
7575
# Raises an exception in case of an error.
76-
def add_contact(email, first_name, last_name, ip_addr, country_code):
76+
def add_contact(email, first_name, last_name, ip_addr, country_code, source=None):
7777
if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email):
7878
raise Exception('Invalid email')
7979

@@ -89,6 +89,7 @@ def add_contact(email, first_name, last_name, ip_addr, country_code):
8989
row.last_name = last_name
9090
row.ip_addr = ip_addr
9191
row.country_code = country_code
92+
row.source = source
9293
else:
9394
# Insert a new entry.
9495
new_contact = True
@@ -99,6 +100,7 @@ def add_contact(email, first_name, last_name, ip_addr, country_code):
99100
row.unsubscribed = False
100101
row.ip_addr = ip_addr
101102
row.country_code = country_code
103+
row.source = source
102104
db.session.add(row)
103105
db.session.commit()
104106
except Exception as e:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""empty message
2+
3+
Revision ID: 17de7a1bad16
4+
Revises: bbf13d065442
5+
Create Date: 2023-04-21 00:18:06.044764
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
from sqlalchemy.dialects import postgresql
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '17de7a1bad16'
14+
down_revision = 'bbf13d065442'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
op.add_column('email_list', sa.Column('source', sa.String(length=255), nullable=True))
21+
op.create_index(op.f('ix_email_list_source'), 'email_list', ['source'], unique=False)
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
op.drop_index(op.f('ix_email_list_source'), table_name='email_list')
28+
op.drop_column('email_list', 'source')
29+
# ### end Alembic commands ###

views/web_views.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
from datetime import datetime
33
import os
44
import re
5-
import sys
6-
import calendar
7-
import random
85

96
from flask_cors import CORS, cross_origin
107
from app import app
@@ -247,7 +244,7 @@ def join_mailing_list():
247244
# Add an entry to the email_list table.
248245
log("Adding to mailing list")
249246
new_contact = mailing_list.add_contact(
250-
email, first_name, last_name, ip_addr, country_code
247+
email, first_name, last_name, ip_addr, country_code, "originprotocol.com"
251248
)
252249

253250
# If it is a new contact and not a backfill, send a welcome email.
@@ -275,6 +272,50 @@ def join_mailing_list():
275272

276273
return jsonify(success=True, message=gettext("Thanks for signing up!"))
277274

275+
@cross_origin()
276+
@app.route("/oeth-subscribe", methods=["POST"], strict_slashes=False)
277+
def oeth_subscribe():
278+
if not "email" in request.form:
279+
return jsonify(success=False, message=gettext("Missing email"))
280+
email = request.form["email"]
281+
if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email):
282+
return jsonify(success=False, message=gettext("Invalid email"))
283+
284+
# optional fields
285+
source = request.form.get("source") or None
286+
eth_address = request.form.get("eth_address") or None
287+
first_name = request.form.get("first_name") or None
288+
last_name = request.form.get("last_name") or None
289+
full_name = request.form.get("name") or None
290+
if not full_name and (first_name or last_name):
291+
full_name = " ".join(filter(None, (first_name, last_name)))
292+
ip_addr = request.form.get("ip_addr") or get_real_ip()
293+
country_code = request.form.get("country_code") or get_country(ip_addr)
294+
295+
new_user = False
296+
297+
log("Updating mailing list for", email, eth_address)
298+
try:
299+
# Add an entry to the email_list table.
300+
log("Adding to mailing list")
301+
new_contact = mailing_list.add_contact(
302+
email, first_name, last_name, ip_addr, country_code, source
303+
)
304+
305+
# Add the entry to the Sendgrid contact list.
306+
if new_contact:
307+
new_user = True
308+
309+
except Exception as err:
310+
log("Failure: %s" % err)
311+
return jsonify(success=False, message=str(err))
312+
313+
if not new_user:
314+
return jsonify(success=True, message=gettext("You're already registered!"))
315+
316+
return jsonify(success=True, message=gettext("Thanks for signing up!"))
317+
318+
278319
@app.route("/mailing-list/unsubscribe", methods=["GET"], strict_slashes=False)
279320
def unsubscribe():
280321
email = request.args.get("email")

0 commit comments

Comments
 (0)