diff --git a/server/__pycache__/__init__.cpython-310.pyc b/server/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..60a980bd Binary files /dev/null and b/server/__pycache__/__init__.cpython-310.pyc differ diff --git a/server/notifications/__init__.py b/server/notifications/__init__.py new file mode 100644 index 00000000..83805179 --- /dev/null +++ b/server/notifications/__init__.py @@ -0,0 +1,5 @@ +from flask_sqlalchemy import SQLAlchemy +from flask_mail import Mail + +db = SQLAlchemy() +mail = Mail() diff --git a/server/notifications/__pycache__/__init__.cpython-310.pyc b/server/notifications/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..5f73c9a7 Binary files /dev/null and b/server/notifications/__pycache__/__init__.cpython-310.pyc differ diff --git a/server/notifications/__pycache__/app.cpython-310.pyc b/server/notifications/__pycache__/app.cpython-310.pyc new file mode 100644 index 00000000..a28f3ad2 Binary files /dev/null and b/server/notifications/__pycache__/app.cpython-310.pyc differ diff --git a/server/notifications/__pycache__/models.cpython-310.pyc b/server/notifications/__pycache__/models.cpython-310.pyc new file mode 100644 index 00000000..f6d1dddf Binary files /dev/null and b/server/notifications/__pycache__/models.cpython-310.pyc differ diff --git a/server/notifications/__pycache__/routes.cpython-310.pyc b/server/notifications/__pycache__/routes.cpython-310.pyc new file mode 100644 index 00000000..86b294b1 Binary files /dev/null and b/server/notifications/__pycache__/routes.cpython-310.pyc differ diff --git a/server/notifications/app.py b/server/notifications/app.py new file mode 100644 index 00000000..74b9300d --- /dev/null +++ b/server/notifications/app.py @@ -0,0 +1,24 @@ +from flask import Flask +from . import db, mail +from .models import User, Notification, Post, Cohort +from .routes import init_app + +def create_app(): + app = Flask(__name__) + app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost/moringa_alumni' + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + app.config['MAIL_SERVER'] = 'smtp.example.com' + app.config['MAIL_PORT'] = 587 + app.config['MAIL_USE_TLS'] = True + app.config['MAIL_USERNAME'] = 'your-email@example.com' + app.config['MAIL_PASSWORD'] = 'your-email-password' + app.config['MAIL_DEFAULT_SENDER'] = 'your-email@example.com' + + db.init_app(app) + mail.init_app(app) + init_app(app) + return app + +if __name__ == '__main__': + app = create_app() + app.run(debug=True) diff --git a/server/notifications/models.py b/server/notifications/models.py new file mode 100644 index 00000000..ee1ee0e6 --- /dev/null +++ b/server/notifications/models.py @@ -0,0 +1,26 @@ +from . import db +from datetime import datetime + +class User(db.Model): + id = db.Column(db.Integer, primary_key=True) + email = db.Column(db.String(120), unique=True, nullable=False) + notifications = db.relationship('Notification', backref='user', lazy=True) + +class Notification(db.Model): + id = db.Column(db.Integer, primary_key=True) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + message = db.Column(db.String(250), nullable=False) + timestamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + is_read = db.Column(db.Boolean, default=False, nullable=False) + +class Post(db.Model): + id = db.Column(db.Integer, primary_key=True) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + content = db.Column(db.String(500), nullable=False) + timestamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + +class Cohort(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(100), nullable=False) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + timestamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) diff --git a/server/notifications/routes.py b/server/notifications/routes.py new file mode 100644 index 00000000..0dea43be --- /dev/null +++ b/server/notifications/routes.py @@ -0,0 +1,86 @@ +from flask import Blueprint, request, jsonify +from .models import db, Notification, Cohort, Post, User + +notifications_bp = Blueprint('notifications', __name__, url_prefix='/notifications') + +@notifications_bp.route('/send', methods=['POST']) +def send_notification_route(): + try: + data = request.get_json() + user_id = data.get('user_id') + message = data.get('message') + if not user_id or not message: + return jsonify({'error': 'Invalid input data'}), 400 + notification = Notification(user_id=user_id, message=message) + db.session.add(notification) + db.session.commit() + return jsonify({'message': 'Notification sent successfully'}), 200 + except Exception as e: + print(f"An error occurred: {e}") + return jsonify({'error': 'An error occurred while sending notification'}), 500 + +@notifications_bp.route('/', methods=['GET']) +def get_notifications(user_id): + try: + notifications = Notification.query.filter_by(user_id=user_id).all() + return jsonify([{ + 'id': n.id, + 'message': n.message, + 'timestamp': n.timestamp.strftime("%Y-%m-%d %H:%M:%S"), + 'is_read': n.is_read + } for n in notifications]), 200 + except Exception as e: + print(f"An error occurred: {e}") + return jsonify({'error': 'An error occurred while retrieving notifications'}), 500 + +@notifications_bp.route('/read/', methods=['PATCH']) +def read_notification(notification_id): + try: + notification = Notification.query.get(notification_id) + if not notification: + return jsonify({'error': 'Notification not found'}), 404 + notification.is_read = True + db.session.commit() + return jsonify({'message': 'Notification marked as read'}), 200 + except Exception as e: + print(f"An error occurred: {e}") + return jsonify({'error': 'An error occurred while marking notification as read'}), 500 + +@notifications_bp.route('/cohorts/join', methods=['POST']) +def join_cohort(): + try: + data = request.get_json() + user_id = data.get('user_id') + name = data.get('name') + if not user_id or not name: + return jsonify({'error': 'Invalid input data'}), 400 + new_cohort = Cohort(name=name, user_id=user_id) + db.session.add(new_cohort) + db.session.commit() + message = "You have joined a new cohort" + notification = Notification(user_id=user_id, message=message) + db.session.add(notification) + db.session.commit() + return jsonify({'message': 'Joined cohort successfully'}), 200 + except Exception as e: + print(f"An error occurred: {e}") + return jsonify({'error': 'An error occurred while joining cohort'}), 500 + +@notifications_bp.route('/posts', methods=['POST']) +def create_post(): + try: + data = request.get_json() + user_id = data.get('user_id') + content = data.get('content') + if not user_id or not content: + return jsonify({'error': 'Invalid input data'}), 400 + new_post = Post(user_id=user_id, content=content) + db.session.add(new_post) + db.session.commit() + return jsonify({'message': 'Post created successfully'}), 201 + except Exception as e: + print(f"An error occurred: {e}") + return jsonify({'error': 'An error occurred while creating post'}), 500 + +def init_app(app): + app.register_blueprint(notifications_bp)