Skip to content

Commit

Permalink
feat : add account creation, change password, and email unsuscribptio…
Browse files Browse the repository at this point in the history
…n email
  • Loading branch information
Musoye committed May 26, 2023
1 parent 8bd5600 commit 5873372
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def put_user(user_id):
if not request.get_json():
abort(400, description="Not a JSON")

ignore = ['id', 'email', 'created_at', 'updated_at']
ignore = ['id', 'email', 'created_at', 'updated_at', 'password']

data = request.get_json()
for key, value in data.items():
Expand Down
1 change: 1 addition & 0 deletions message/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
mail = Mail()

from message.views.index import *
from message.views.create import *

def configure_mail(app):
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
Expand Down
Binary file modified message/views/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added message/views/__pycache__/create.cpython-310.pyc
Binary file not shown.
Binary file modified message/views/__pycache__/index.cpython-310.pyc
Binary file not shown.
83 changes: 83 additions & 0 deletions message/views/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/python3
""" Create for account confirmation and changing password """
from models.user import User
from models import storage
from message.views import notification, mail
from flask import jsonify, abort, request
from flask_mail import Message



@notification.route('/register', methods=['POST'])
def create_accout():
"""Mail for creation of a new account"""
if not request.get_json():
abort(400, description="Not a JSON")
if 'first_name' not in request.get_json():
abort(400, description='No first_name')
if 'email' not in request.get_json():
abort(400, description='No email')
if 'last_name' not in request.get_json():
abort(400, description='No last_name')
data = request.get_json()
name = data.get('first_name')
email = data.get('email')
l_name = data.get('last_name')
msg = Message('Time Master', sender='[email protected]', recipients=[email])
msg.body = '''Hello {name} {l_name},\nYou have take a new step in the mangement of your \
life. I cant't wait to see you flourish'''.format(name, l_name)
try:
mail.send(msg)
return jsonify({'status': 'sent', 'description': 'Email sent'}), 201
except:
pass
return jsonify({'status': 'error', 'description': 'Email not sent'}), 200

@notification.route('/password', methods=['POST'])
def change_password():
"""Mail for changing of passsword"""
if not request.get_json():
abort(400, description="Not a JSON")
if 'first_name' not in request.get_json():
abort(400, description='No first_name')
if 'email' not in request.get_json():
abort(400, description='No email')
if 'password' not in request.get_json():
abort(400, description='No password')
if 'user_id' not in request.get_json():
abort(400, description='No user_id')
data = request.get_json()
user_id = data.get('user_id')
user = storage.get(User, user_id)
if user is None:
abort(400, description='This user does not exist')
password = data.get('password')
user.password = password
user.save()
name = data.get('first_name')
email = data.get('email')
msg = Message('Time Master', sender='[email protected]', recipients=[email])
msg.body = "Hello {},\nThis is to inform you that your password has been changed sucessfully\
if you are not the one contact the admin email [email protected]".format(name)
try:
mail.send(msg)
return jsonify({'status': 'sent', 'user_id': user_id, 'description': 'Email sent'}), 201
except:
pass
return jsonify({'status': 'error', 'description': 'Email not sent'}), 200

@notification.route('/unsub', methods=['POST'])
def change_email_subscription():
"""Mail for changing subscription to false"""
if not request.get_json():
abort(400, description="Not a JSON")
if 'user_id' not in request.get_json():
abort(400, description='No user_id')
data = request.get_json()
user_id = data.get('user_id')
user = storage.get(User, user_id)
if user is None:
abort(400, description='This user does not exist')
user.is_suscribe = False
user.save()
return jsonify({'status': 'success'}), 201
22 changes: 9 additions & 13 deletions message/views/index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#!/usr/bin/python3
""" Index """
from models.project import Project
from models.task import Task
from models.user import User
from models import storage
""" Index: Projet and Task creatio mail"""
from message.views import notification,mail
from flask import jsonify, abort, request, current_app
from flask_mail import Message
Expand All @@ -26,14 +22,14 @@ def create_project():
email = data.get('email')
expiry = data.get('expiry_date')
msg = Message('Time Master', sender='[email protected]', recipients=[email])
msg.body = '''Hello Great Time Master,\n You have created a new project with the name {}.
The project will expire {}'''.format(name, expiry)
msg.body = '''Hello Great Time Master,\n You have created a new project \
with the name {}.The project will expire {}'''.format(name, expiry)
try:
mail.send(msg)
return jsonify({'status': 'sent'}), 201
return jsonify({'status': 'sent', 'description': 'Email sent'}), 201
except:
pass
return jsonify({'status': 'error'}), 200
return jsonify({'status': 'error', 'description': 'Email not sent'}), 200

@notification.route('/task', methods=['POST'])
def create_task():
Expand All @@ -54,11 +50,11 @@ def create_task():
project = data.get('project_name')
expiry = data.get('expiry_date')
msg = Message('Time Master', sender='[email protected]', recipients=[email])
msg.body = '''Hello Great Time Master,\n You have created a new task {} under the project {}.
The project will expire {}'''.format(name, project, expiry)
msg.body = "Hello Great Time Master,\n You have created a new task {}\
under the project {}.The project will expire {}".format(name, project, expiry)
try:
mail.send(msg)
return jsonify({'status': 'sent'}), 201
return jsonify({'status': 'sent', 'description': 'Email sent'}), 201
except:
pass
return jsonify({'status': 'error'}), 200
return jsonify({'status': 'error', 'description': 'Email not sent'}), 200

0 comments on commit 5873372

Please sign in to comment.