Skip to content

Commit

Permalink
Добавил уведомления на почту при регистрации
Browse files Browse the repository at this point in the history
  • Loading branch information
Puzanovim committed May 24, 2021
1 parent 0fd6322 commit 2c2bf9f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/accounts/client_account/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ...users.logic import all_users, get_or_404
from ...users.models import users
from ...users.schemas import UserCreate, OwnerCreate, UserUpdate
from ...service import check_dict
from ...service import check_dict, send_mail


async def get_clients():
Expand Down Expand Up @@ -130,5 +130,8 @@ async def add_owner(client_id: int, owner: OwnerCreate):
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS,
)

message = f"Добро пожаловать в UDV Service Desk!\n\nВаш логин в системе: {owner.email}\nВаш пароль: {owner.password}"
await send_mail(owner.email, "Вы зарегистрированы в системе", message)
updated_owner = await update_owner(client_id, created_owner.id)
return updated_owner
4 changes: 3 additions & 1 deletion src/accounts/developer_account/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pydantic.types import UUID4

from src.db.db import database
from src.service import send_mail
from src.users.logic import all_users, delete_user, update_user
from src.users.models import users
from src.users.schemas import UserCreate, DeveloperCreate, UserUpdate
Expand All @@ -28,7 +29,8 @@ async def add_developer(user: UserCreate):
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS,
)

message = f"Добро пожаловать в UDV Service Desk!\n\nВаш логин в системе: {user.email}\nВаш пароль: {user.password}"
await send_mail(user.email, "Вы зарегистрированы в системе", message)
return created_developer


Expand Down
12 changes: 6 additions & 6 deletions src/accounts/employee_account/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from src.db.db import database
from src.reference_book.models import licences
from src.service import send_mail
from src.users.logic import all_users, update_user, delete_user
from src.users.models import users
from src.users.schemas import UserCreate, EmployeeCreate, UserUpdate
Expand Down Expand Up @@ -39,19 +40,18 @@ async def count_allowed_employees(client_id: int):


async def add_employee(id: int, user: UserCreate):
employee = EmployeeCreate(
**user.dict(),
client_id=id,
is_owner=False,
date_reg=datetime.utcnow())
user.client_id = id
user.is_owner = False
employee = EmployeeCreate(**user.dict())
try:
created_user = await all_users.create_user(employee, safe=True)
except Exception:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS,
)

message = f"Добро пожаловать в UDV Service Desk!\n\nВаш логин в системе: {user.email}\nВаш пароль: {user.password}"
await send_mail(user.email, "Вы зарегистрированы в системе", message)
return created_user


Expand Down
25 changes: 25 additions & 0 deletions src/service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import asyncio
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

from src.config import MAIL_LOGIN, MAIL_PWD


async def check_dict(result):
if result:
return dict(result)
return None


async def send_mail(to_addr: str, subject: str, msg: str):
multipart_msg = MIMEMultipart()
multipart_msg['From'] = MAIL_LOGIN
multipart_msg['To'] = to_addr
multipart_msg['Subject'] = subject
multipart_msg.attach(MIMEText(msg, 'plain'))

smtp_obj = smtplib.SMTP('smtp.gmail.com', 587)
smtp_obj.starttls()
smtp_obj.login(MAIL_LOGIN, MAIL_PWD)
smtp_obj.send_message(multipart_msg)
smtp_obj.quit()
print("письмо отправлено")

if __name__ == '__main__':
print("Hello")
asyncio.run(send_mail("[email protected]", "HELLO", "Hello Hello"))

0 comments on commit 2c2bf9f

Please sign in to comment.