Skip to content

Commit

Permalink
Merge pull request #419 from DalgoT4D/403-demo-accounts
Browse files Browse the repository at this point in the history
403 demo accounts
  • Loading branch information
fatchat authored Jan 8, 2024
2 parents 3ec1253 + d34fcfb commit e6ab242
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 5 deletions.
13 changes: 12 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ SENDGRID_YOUVE_BEEN_ADDED_TEMPLATE=

PREFECT_NOTIFICATIONS_WEBHOOK_KEY=

SUPERSET_USAGE_DASHBOARD_API_URL=
SUPERSET_USAGE_DASHBOARD_API_URL=



# DEMO ACCOUNTS
DEMO_SIGNUPCODE=

DEMO_AIRBYTE_SOURCE_TYPES=

DEMO_SENDGRID_SIGNUP_TEMPLATE=
DEMO_SUPERSET_USERNAME=
DEMO_SUPERSET_PASSWORD=
9 changes: 9 additions & 0 deletions ddpui/api/airbyte_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def get_airbyte_source_definitions(request):
raise HttpError(400, "create an airbyte workspace first")

res = airbyte_service.get_source_definitions(orguser.org.airbyte_workspace_id)

# filter source definitions for demo account
allowed_sources = os.getenv("DEMO_AIRBYTE_SOURCE_TYPES")
if orguser.org.is_demo is True and allowed_sources:
res["sourceDefinitions"] = [
source_def
for source_def in res["sourceDefinitions"]
if source_def["name"] in allowed_sources.split(",")
]
logger.debug(res)
return res["sourceDefinitions"]

Expand Down
3 changes: 3 additions & 0 deletions ddpui/api/user_org_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ def delete_organization_warehouses_v1(request):
if orguser.org is None:
raise HttpError(400, "create an organization first")

if orguser.org.is_demo:
raise HttpError(403, "insufficient permissions")

delete_warehouse_v1(orguser.org)

return {"success": 1}
Expand Down
21 changes: 19 additions & 2 deletions ddpui/core/orguserfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
VerifyEmailSchema,
DeleteOrgUserPayload,
)
from ddpui.models.org import Org
from ddpui.models.orgtnc import OrgTnC
from ddpui.utils import sendgrid
from ddpui.utils import helpers
Expand Down Expand Up @@ -70,7 +71,7 @@ def signup_orguser(payload: OrgUserCreate):
"""create an orguser and send an email"""

signupcode = payload.signupcode
if signupcode != os.getenv("SIGNUPCODE"):
if signupcode not in [os.getenv("SIGNUPCODE"), os.getenv("DEMO_SIGNUPCODE")]:
return None, "That is not the right signup code"

if User.objects.filter(email=payload.email).exists():
Expand All @@ -82,11 +83,20 @@ def signup_orguser(payload: OrgUserCreate):
if not helpers.isvalid_email(payload.email):
return None, "that is not a valid email address"

is_demo = True if (signupcode == os.getenv("DEMO_SIGNUPCODE")) else False
demo_org = None # common demo org
if is_demo:
demo_org = Org.objects.filter(is_demo=True).first()
if demo_org is None:
return None, "demo org has not been setup"

user = User.objects.create_user(
username=payload.email, email=payload.email, password=payload.password
)
UserAttributes.objects.create(user=user)
orguser = OrgUser.objects.create(user=user, role=OrgUserRole.ACCOUNT_MANAGER)
orguser = OrgUser.objects.create(
user=user, role=OrgUserRole.ACCOUNT_MANAGER, org=demo_org
)
orguser.save()
logger.info(
f"created user [account-manager] "
Expand All @@ -106,6 +116,7 @@ def signup_orguser(payload: OrgUserCreate):
sendgrid.send_signup_email(payload.email, reset_url)
except Exception:
return None, "failed to send email"

return from_orguser(orguser), None


Expand Down Expand Up @@ -428,6 +439,12 @@ def verify_email(payload: VerifyEmailSchema):
OrgUser.objects.filter(user_id=orguser.user.id).update(email_verified=True)
UserAttributes.objects.filter(user=orguser.user).update(email_verified=True)

if orguser.org.is_demo:
try:
sendgrid.send_demo_account_post_verify_email(orguser.user.email)
except Exception:
return None, "failed to send email"

return None, None


Expand Down
17 changes: 17 additions & 0 deletions ddpui/migrations/0038_org_is_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.7 on 2024-01-08 05:52

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("ddpui", "0037_userattributes_is_consultant"),
]

operations = [
migrations.AddField(
model_name="org",
name="is_demo",
field=models.BooleanField(default=False),
),
]
5 changes: 4 additions & 1 deletion ddpui/models/org.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ class Org(models.Model):
viz_login_type = models.CharField(
choices=OrgVizLoginType.choices(), max_length=50, null=True
)
is_demo = models.BooleanField(default=False)

def __str__(self) -> str:
return f"Org[{self.slug}|{self.name}|{self.airbyte_workspace_id}]"
demostr = "demo=" + ("yes" if self.is_demo else "no")
return f"Org[{self.slug}|{self.name}|{self.airbyte_workspace_id}|{demostr}]"


class OrgPrefectBlock(models.Model):
Expand Down Expand Up @@ -112,6 +114,7 @@ class OrgSchema(Schema):
viz_url: str = None
viz_login_type: str = None
tnc_accepted: bool = None
is_demo: bool = False


class OrgWarehouse(models.Model):
Expand Down
1 change: 1 addition & 0 deletions ddpui/models/org_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class OrgUserResponse(Schema):
role: int
role_slug: str
wtype: str | None
is_demo: bool = False


class Invitation(models.Model):
Expand Down
8 changes: 7 additions & 1 deletion ddpui/tests/api_tests/test_airbyte_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ def test_get_airbyte_source_definitions_without_airbyte_workspace(

@patch(
"ddpui.ddpairbyte.airbyte_service.get_source_definitions",
return_value={"sourceDefinitions": [1, 2, 3]},
return_value={
"sourceDefinitions": [
{"name": "name1"},
{"name": "name2"},
{"name": "name3"},
]
},
)
def test_get_airbyte_source_definitions_success(org_with_workspace):
"""tests GET /source_definitions"""
Expand Down
1 change: 1 addition & 0 deletions ddpui/utils/orguserhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def from_orguser(orguser: OrgUser):
role=orguser.role,
role_slug=slugify(OrgUserRole(orguser.role).name),
wtype=warehouse.wtype if warehouse else None,
is_demo=orguser.org.is_demo if orguser.org else False,
)
if orguser.org:
response.org.tnc_accepted = OrgTnC.objects.filter(org=orguser.org).exists()
Expand Down
17 changes: 17 additions & 0 deletions ddpui/utils/sendgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,20 @@ def send_youve_been_added_email(to_email: str, added_by: str, org_name: str) ->
"url": os.getenv("FRONTEND_URL"),
},
)


def send_demo_account_post_verify_email(to_email: str) -> None:
"""
sends the following in the email
- the demo superset credentials for the demo account
- data source credentials
- link to documentation
"""
send_template_message(
os.getenv("DEMO_SENDGRID_SIGNUP_TEMPLATE"),
to_email,
{
"username": os.getenv("DEMO_SUPERSET_USERNAME"),
"password": os.getenv("DEMO_SUPERSET_PASSWORD"),
},
)

0 comments on commit e6ab242

Please sign in to comment.