Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added server/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions server/notifications/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail

db = SQLAlchemy()
mail = Mail()
Binary file not shown.
Binary file added server/notifications/__pycache__/app.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions server/notifications/app.py
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 26 additions & 0 deletions server/notifications/models.py
Original file line number Diff line number Diff line change
@@ -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)
86 changes: 86 additions & 0 deletions server/notifications/routes.py
Original file line number Diff line number Diff line change
@@ -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('/<int:user_id>', 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/<int:notification_id>', 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)