Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "main.py",
"django": true,
"justMyCode": true,
"pythonPath": "/opt/homebrew/bin/python3.10"
}
]
}
1 change: 1 addition & 0 deletions database/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class EmailList(db.Model):
__tablename__ = 'email_list'

email = db.Column(db.String(255), primary_key=True, autoincrement=False)
source = db.Column(db.String(255), index=True)
unsubscribed = db.Column(db.Boolean())
created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
ip_addr = db.Column(db.String(100))
Expand Down
4 changes: 3 additions & 1 deletion logic/emails/mailing_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def mass_unsubscribe_sendgrid_contact(emails):
# Inserts or updates an entry in the email_list table.
# Returns True if a new entry was added, False if the entry already existed.
# Raises an exception in case of an error.
def add_contact(email, first_name, last_name, ip_addr, country_code):
def add_contact(email, first_name, last_name, ip_addr, country_code, source=None):
if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email):
raise Exception('Invalid email')

Expand All @@ -89,6 +89,7 @@ def add_contact(email, first_name, last_name, ip_addr, country_code):
row.last_name = last_name
row.ip_addr = ip_addr
row.country_code = country_code
row.source = source
else:
# Insert a new entry.
new_contact = True
Expand All @@ -99,6 +100,7 @@ def add_contact(email, first_name, last_name, ip_addr, country_code):
row.unsubscribed = False
row.ip_addr = ip_addr
row.country_code = country_code
row.source = source
db.session.add(row)
db.session.commit()
except Exception as e:
Expand Down
29 changes: 29 additions & 0 deletions migrations/versions/17de7a1bad16_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""empty message

Revision ID: 17de7a1bad16
Revises: bbf13d065442
Create Date: 2023-04-21 00:18:06.044764

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '17de7a1bad16'
down_revision = 'bbf13d065442'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('email_list', sa.Column('source', sa.String(length=255), nullable=True))
op.create_index(op.f('ix_email_list_source'), 'email_list', ['source'], unique=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_email_list_source'), table_name='email_list')
op.drop_column('email_list', 'source')
# ### end Alembic commands ###
49 changes: 45 additions & 4 deletions views/web_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from datetime import datetime
import os
import re
import sys
import calendar
import random

from flask_cors import CORS, cross_origin
from app import app
Expand Down Expand Up @@ -247,7 +244,7 @@ def join_mailing_list():
# Add an entry to the email_list table.
log("Adding to mailing list")
new_contact = mailing_list.add_contact(
email, first_name, last_name, ip_addr, country_code
email, first_name, last_name, ip_addr, country_code, "originprotocol.com"
)

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

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

@cross_origin()
@app.route("/oeth-subscribe", methods=["POST"], strict_slashes=False)
def oeth_subscribe():
Copy link
Contributor

@shahthepro shahthepro Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since only source differs between this function and the other one, why one extract this into a common function to reduce code duplication?

if not "email" in request.form:
return jsonify(success=False, message=gettext("Missing email"))
email = request.form["email"]
if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email):
return jsonify(success=False, message=gettext("Invalid email"))

# optional fields
source = request.form.get("source") or None
eth_address = request.form.get("eth_address") or None
first_name = request.form.get("first_name") or None
last_name = request.form.get("last_name") or None
full_name = request.form.get("name") or None
if not full_name and (first_name or last_name):
full_name = " ".join(filter(None, (first_name, last_name)))
ip_addr = request.form.get("ip_addr") or get_real_ip()
country_code = request.form.get("country_code") or get_country(ip_addr)

new_user = False

log("Updating mailing list for", email, eth_address)
try:
# Add an entry to the email_list table.
log("Adding to mailing list")
new_contact = mailing_list.add_contact(
email, first_name, last_name, ip_addr, country_code, source
)

# Add the entry to the Sendgrid contact list.
if new_contact:
new_user = True

except Exception as err:
log("Failure: %s" % err)
return jsonify(success=False, message=str(err))

if not new_user:
return jsonify(success=True, message=gettext("You're already registered!"))

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


@app.route("/mailing-list/unsubscribe", methods=["GET"], strict_slashes=False)
def unsubscribe():
email = request.args.get("email")
Expand Down