Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
add user send email resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mirakel committed Apr 12, 2020
1 parent a296a12 commit 08ef552
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 0 additions & 1 deletion api/resources/district.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from flask import request
from flask_restful import Resource
from injector import inject
from http import HTTPStatus
Expand Down
1 change: 0 additions & 1 deletion api/resources/province.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from flask import request
from flask_restful import Resource
from injector import inject
from http import HTTPStatus
Expand Down
35 changes: 35 additions & 0 deletions api/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ def post(self):
return {"message": "Email already used"}, HTTPStatus.BAD_REQUEST


class UserSendEmail(Resource):
@inject
def __init__(self, user_repository: IUserRepository, email_method_service: IEmailMethodService):
self.user_repository = user_repository
self.email_method_service = email_method_service

def post(self):
data = request.get_json()

try:
user = self.user_repository.get_user_by_email(data['email'])
expires = timedelta(hours=24)
token = create_access_token(str(user.email), expires_delta=expires)
url = EnvironmentConfig.HOST_URL + 'verify_account/' + token

body_html = render_template("messages/confirm_email.html", full_name=user.full_name(), confirm_url=url)
body_text = render_template("messages/confirm_email.txt", full_name=user.full_name(), confirm_url=url)

data_message = {
'to': user.email,
'subject': 'Por favor, verifique su dirección de correo electrónico',
'sender': ("Data Science Research Perú", "[email protected]"),
'content_html': body_html,
'content_text': body_text
}

if self.email_method_service.send_message(data_message):
return user.to_dict(), HTTPStatus.OK
else:
return {"message": "Error sending the message"}, HTTPStatus.INTERNAL_SERVER_ERROR
except Exception:
pass
return {"message": "El correo electrónico no se encuentra registrado"}, HTTPStatus.BAD_REQUEST


class UserVerifyAccount(Resource):
@inject
def __init__(self, user_repository: IUserRepository):
Expand Down
3 changes: 2 additions & 1 deletion api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_restful import Api
from api.resources.covid_epidemiology import CovidSirDResource, CovidSeirDResource, CovidSeaichurDResource
from api.resources.user import UserListResource, UserLoginResource, UserForgotPasswordResource, \
UserResetPasswordResource, UserVerifyAccount
UserResetPasswordResource, UserVerifyAccount, UserSendEmail
from api.resources.department import DepartmentListResource
from api.resources.province import ProvinceListResource
from api.resources.district import DistrictListResource
Expand All @@ -19,6 +19,7 @@
api_rest.add_resource(UserLoginResource, '/user/login')
api_rest.add_resource(UserForgotPasswordResource, '/user/forgot-password')
api_rest.add_resource(UserResetPasswordResource, '/user/reset-password')
api_rest.add_resource(UserSendEmail, '/user/resend-email')
api_rest.add_resource(DepartmentListResource, '/departments')
api_rest.add_resource(ProvinceListResource, *['/provinces', '/<int:department_id>/provinces'])
api_rest.add_resource(DistrictListResource, *['/districts', '/<int:province_id>/districts'])
Expand Down

0 comments on commit 08ef552

Please sign in to comment.