Skip to content

Commit

Permalink
Merge pull request #271 from DevDataPlatform/create-org-flow-changes
Browse files Browse the repository at this point in the history
Create org flow changes
  • Loading branch information
fatchat authored Aug 11, 2023
2 parents f168cd0 + 8b8579f commit a631b17
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PREFECT_PROXY_API_URL=
PREFECT_HTTP_TIMEOUT=5

SIGNUPCODE=
CREATEORG_CODE=
FRONTEND_URL=

SENDGRID_APIKEY=
Expand Down
5 changes: 1 addition & 4 deletions ddpui/api/client/dashboard_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ninja import NinjaAPI
from ninja.errors import HttpError, ValidationError
from ninja.errors import ValidationError

from ninja.responses import Response
from pydantic.error_wrappers import ValidationError as PydanticValidationError
Expand Down Expand Up @@ -50,9 +50,6 @@ def get_dashboard(request):
"""Fetch all flows/pipelines created in an organization"""
orguser = request.orguser

if orguser.org is None:
raise HttpError(400, "register an organization first")

org_data_flows = (
OrgDataFlow.objects.filter(org=orguser.org).exclude(cron=None).all()
)
Expand Down
69 changes: 52 additions & 17 deletions ddpui/api/client/user_org_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,20 @@ def post_login(request):
request_obj = json.loads(request.body)
token = views.obtain_auth_token(request)
if "token" in token.data:
org = None
orguser = OrgUser.objects.filter(user__email=request_obj["username"]).first()
if orguser.org is not None:
org = orguser.org.name
user = User.objects.filter(email=request_obj["username"]).first()

# check if all the orgusers for this user have email verified
email_verified = OrgUser.objects.filter(user=user, email_verified=True).exists()
if email_verified:
OrgUser.objects.filter(user=user, email_verified=False).update(
email_verified=True
)

return {
"token": token.data["token"],
"org": org,
"email": str(orguser),
"role_slug": slugify(OrgUserRole(orguser.role).name),
"active": orguser.user.is_active,
"email": user.email,
"email_verified": email_verified,
"active": user.is_active,
}

return token
Expand Down Expand Up @@ -312,14 +316,13 @@ def post_transfer_ownership(request, payload: OrgUserNewOwner):

@user_org_api.post("/organizations/", response=OrgSchema, auth=auth.FullAccess())
def post_organization(request, payload: OrgSchema):
"""creates a new org and attaches it to the requestor"""
"""creates a new org & new orguser (if required) and attaches it to the requestor"""
orguser: OrgUser = request.orguser
if orguser.org:
raise HttpError(400, "orguser already has an associated org")
org = Org.objects.filter(name=payload.name).first()
if org:
raise HttpError(400, "client org with this name already exists")
org = Org.objects.create(**payload.dict())

org = Org(name=payload.name)
org.slug = slugify(org.name)[:20]
org.save()
logger.info(f"{orguser.user.email} created new org {org.name}")
Expand All @@ -329,8 +332,19 @@ def post_organization(request, payload: OrgSchema):
# delete the org or we won't be able to create it once airbyte comes back up
org.delete()
raise HttpError(400, "could not create airbyte workspace") from error
orguser.org = org
orguser.save()

# create a new orguser if the org is already there
if orguser.org is None:
orguser.org = org
orguser.save()
else:
orguser = OrgUser.objects.create(
user=orguser.user,
role=OrgUserRole.ACCOUNT_MANAGER,
email_verified=True,
org=org,
)

return OrgSchema(name=org.name, airbyte_workspace_id=new_workspace.workspaceId)


Expand Down Expand Up @@ -507,7 +521,7 @@ def post_organization_user_invite(request, payload: InvitationSchema):
)
logger.info(
f"Invited {invited_email} to join {orguser.org.name} "
f"with invite code {payload.invite_code}",
f"with invite code {invitation.invite_code}",
)
return InvitationSchema.from_invitation(invitation)

Expand Down Expand Up @@ -700,6 +714,27 @@ def post_reset_password(
return {"success": 1}


@user_org_api.get("/users/verify_email/resend", auth=auth.AnyOrgUser())
def get_verify_email_resend(request): # pylint: disable=unused-argument
"""this api is hit when the user is logged in but the email is still not verified"""
redis = Redis()
token = uuid4()

redis_key = f"email-verification:{token.hex}"
orguserid_bytes = str(request.orguser.id).encode("utf8")

redis.set(redis_key, orguserid_bytes)

FRONTEND_URL = os.getenv("FRONTEND_URL")
reset_url = f"{FRONTEND_URL}/verifyemail/?token={token.hex}"
try:
sendgrid.send_signup_email(request.user.email, reset_url)
except Exception as error:
raise HttpError(400, "failed to send email") from error

return {"success": 1}


@user_org_api.post("/users/verify_email/")
def post_verify_email(
request, payload: VerifyEmailSchema
Expand All @@ -718,7 +753,7 @@ def post_verify_email(
logger.error("no orguser having id %s", orguserid_str)
raise HttpError(400, "could not look up request from this token")

orguser.email_verified = True
orguser.save()
# verify email for all the orgusers
OrgUser.objects.filter(user_id=orguser.user.id).update(email_verified=True)

return {"success": 1}
11 changes: 0 additions & 11 deletions ddpui/tests/api_tests/test_user_org_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,6 @@ def test_post_transfer_ownership_db_error(


# ================================================================================
def test_post_organization_has_org(orguser):
"""failing test, user already has an org"""
mock_request = Mock()
mock_request.orguser = orguser

payload = OrgSchema(name="some-name")
with pytest.raises(HttpError) as excinfo:
post_organization(mock_request, payload)
assert str(excinfo.value) == "orguser already has an associated org"


def test_post_organization_orgexists(orguserwithoutorg, org_without_workspace):
"""failing test, org name is already in use"""
mock_request = Mock()
Expand Down

0 comments on commit a631b17

Please sign in to comment.