-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished view and service layer, also made modal work correctly
Signed-off-by: Trey <[email protected]>
- Loading branch information
Showing
10 changed files
with
217 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.urls import reverse | ||
from django.utils.crypto import get_random_string | ||
|
||
from backend.models import User, Organization, TeamMemberPermission | ||
from backend.types.emails import SingleEmailInput | ||
from backend.utils.dataclasses import BaseServiceResponse | ||
from settings.helpers import send_email | ||
|
||
|
||
class CreateUserServiceResponse(BaseServiceResponse[User]): ... | ||
|
||
|
||
def create_user_service( | ||
request, email: str, team: Organization, first_name: str, last_name: str, permissions: list[str] | ||
) -> CreateUserServiceResponse: | ||
|
||
if not first_name: | ||
return CreateUserServiceResponse(error_message="Please enter a valid first name") | ||
|
||
if not email: | ||
return CreateUserServiceResponse(error_message="Please enter a valid user email") | ||
|
||
if User.objects.filter(email=email).exists(): | ||
return CreateUserServiceResponse(error_message="This user already exists, invite them instead!") | ||
|
||
temporary_password = get_random_string(length=8) | ||
|
||
user: User = User.objects.create_user(email=email, first_name=first_name, last_name=last_name, username=email) | ||
user.set_password(temporary_password) | ||
user.awaiting_email_verification = False | ||
user.require_change_password = True | ||
user.save() | ||
|
||
send_email( | ||
SingleEmailInput( | ||
destination=email, | ||
subject="MyFinances | You have been invited to join an organization", | ||
content=f""" | ||
Hi {user.first_name or "User"}, | ||
You have been invited by {request.user.email} to join the organization {team.name}. | ||
Your account email is: {email} | ||
Your temporary password is: {temporary_password} | ||
We suggest that you change your password as soon as you login, however no other user including the organization have | ||
access to this password. | ||
Upon login, you will be added to the \"{team.name}\" organization. However, if required, you may leave at any point. | ||
Login to your new account using this link: | ||
{request.build_absolute_uri(reverse("auth:login"))} | ||
Didn't give permission to be added to this organization? You can safely ignore the email, no actions can be done on | ||
behalf of you without your permission. | ||
""", | ||
) | ||
) | ||
|
||
team.members.add(user) | ||
|
||
TeamMemberPermission.objects.create(user=user, team=team, scopes=permissions) | ||
|
||
return CreateUserServiceResponse(True, response=user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ | |
{% endfor %} | ||
</ul> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,67 @@ | ||
{% component_block "modal" id="modal_invite_user" start_open="true" title="Create User Account" %} | ||
{% fill "content" %} | ||
<form class="py-4" | ||
id="modal_invite_user-form" | ||
hx-post="{% url 'api:teams:invite' %}" | ||
hx-swap="none"> | ||
{% csrf_token %} | ||
<div class="form-control w-full"> | ||
<label class="label justify-start"> | ||
Users' Email | ||
<span class="tooltip tooltip-right ml-2" data-tip="The email address of the user you are about to create"> | ||
{% component_block "modal" id="modal_team_create_user" start_open="true" title="Create User Account" %} | ||
{% fill "content" %} | ||
<form class="py-4" | ||
id="modal_team_create_user-form" | ||
hx-post="{% url 'api:teams:create_user' %}" | ||
hx-swap="none"> | ||
{% csrf_token %} | ||
<div class="form-control w-full"> | ||
<label class="label justify-start"> | ||
Users' Email | ||
<span class="tooltip tooltip-right ml-2" | ||
data-tip="The email address of the user you are about to create"> | ||
<i class="fa fa-info-circle"></i> | ||
</span> | ||
<span class="required_star">*</span> | ||
</label> | ||
<input id="modal_input-email" | ||
name="email" | ||
type="email" | ||
class="input input-block input-bordered" | ||
required> | ||
</div> | ||
<div class="flex flex-row gap-4 w-full"> | ||
<div> | ||
<label class="label justify-start"> | ||
Users' First Name | ||
<span class="required_star">*</span> | ||
</label> | ||
<input id="modal_input-email" | ||
name="first_name" | ||
type="text" | ||
class="input input-block input-bordered" | ||
required> | ||
</div> | ||
<div> | ||
<label class="label justify-start"> | ||
Users' Last Name | ||
<span class="required_star">*</span> | ||
</label> | ||
<input id="modal_input-email" | ||
name="last_name" | ||
type="text" | ||
class="input input-block input-bordered" | ||
required> | ||
</div> | ||
</div> | ||
<input type="hidden" name="team_id" value="{{ team_id }}"> | ||
{% include "components/permissions/selector.html" %} | ||
<div class="modal-action"> | ||
<button type="submit" | ||
id="modal_invite_user-submit" | ||
class="btn btn-primary" | ||
x-on:invite_user_error.window="document.getElementById('modal_invite_user').close()"> | ||
Send Invite | ||
</button> | ||
<button type="button" | ||
_="on click call #modal_invite_user.close()" | ||
class="btn">Cancel | ||
</button> | ||
</div> | ||
</form> | ||
{% endfill %} | ||
<span class="required_star">*</span> | ||
</label> | ||
<input id="modal_input-email" | ||
name="email" | ||
type="email" | ||
class="input input-block input-bordered" | ||
required> | ||
</div> | ||
<div class="flex flex-row gap-4 w-full"> | ||
<div> | ||
<label class="label justify-start"> | ||
Users' First Name | ||
<span class="required_star">*</span> | ||
</label> | ||
<input id="modal_input-first_name" | ||
name="first_name" | ||
type="text" | ||
class="input input-block input-bordered" | ||
required> | ||
</div> | ||
<div> | ||
<label class="label justify-start"> | ||
Users' Last Name | ||
<span class="required_star">*</span> | ||
</label> | ||
<input id="modal_input-last_name" | ||
name="last_name" | ||
type="text" | ||
class="input input-block input-bordered" | ||
required> | ||
</div> | ||
</div> | ||
<input type="hidden" name="team_id" value="{{ team_id }}"> | ||
{% include "components/permissions/selector.html" %} | ||
<div class="w-full align-content-start align-items-center gap-1 text-start border-2 rounded-lg border-info p-3 mt-3"> | ||
<mark class="rounded pb-1 bg-info pt-0.5 px-1">Note:</mark> | ||
this will send the user instructions on how to login, and they will be granted the permissions above in | ||
your organization <strong>right away</strong>. Double-check the permissions, you can modify them at any point. | ||
</div> | ||
<div class="modal-action"> | ||
<button type="submit" | ||
id="modal_team_create_user-submit" | ||
class="btn btn-primary" | ||
x-on:team_create_user_error.window="document.getElementById('modal_team_create_user').close()"> | ||
Create account | ||
</button> | ||
<button type="button" | ||
_="on click call #modal_team_create_user.close()" | ||
class="btn">Cancel</button> | ||
</div> | ||
</form> | ||
{% endfill %} | ||
{% endcomponent_block %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.