added EventNotifyer

This commit is contained in:
Kfir Dayan 2024-01-08 22:03:51 +02:00
parent a63b31da04
commit e718ae905d
3 changed files with 43 additions and 1 deletions

19
app.py
View file

@ -9,6 +9,8 @@ from middlewares.errorHandlers import handle_auth_error, handle_invalid_token
from flask_jwt_extended.exceptions import NoAuthorizationError from flask_jwt_extended.exceptions import NoAuthorizationError
from jwt.exceptions import InvalidTokenError from jwt.exceptions import InvalidTokenError
from datetime import timedelta from datetime import timedelta
from flask_apscheduler import APScheduler
from services.EventNotifyerService import EventNotifyerServer
class App: class App:
def __init__(self): def __init__(self):
@ -18,6 +20,15 @@ class App:
self.set_up_jwt() self.set_up_jwt()
self.register_blueprints() self.register_blueprints()
self.setup_error_handlers() self.setup_error_handlers()
self.scheduler = APScheduler()
def setup_scheduler(self):
self.app.config.from_object(self.SchadulerConfig())
self.scheduler.init_app(self.app)
self.scheduler.start()
# Schedule the job
self.scheduler.add_job(id='locate_upcoming_events', func=self.locate_upcoming_events, trigger='interval', seconds=1)
def set_config(self): def set_config(self):
self.app.config.from_object(config.Config) self.app.config.from_object(config.Config)
@ -44,6 +55,7 @@ class App:
def run(self): def run(self):
with self.app.app_context(): with self.app.app_context():
db.create_all() db.create_all()
self.setup_scheduler() # Setup scheduler
self.app.run(debug=True) self.app.run(debug=True)
def print_endpoints(self): def print_endpoints(self):
@ -54,6 +66,13 @@ class App:
print(f" Function: {function_name}") print(f" Function: {function_name}")
def SchadulerConfig(object):
SCHEDULER_API_ENABLED = True
def locate_upcoming_events(self):
with self.app.app_context():
print(EventNotifyerServer.locate_upcoming_events())
app_class_instance = App() app_class_instance = App()
app_instance = app_class_instance.app app_instance = app_class_instance.app

View file

@ -0,0 +1,10 @@
# this Class is for the scheduler
#this class will have a function that locates the upcomming events Using the Event service.
from services.EventService import EventService
class EventNotifyerServer:
def __init__(self):
pass
def locate_upcoming_events():
return EventService.get_all_upcomming_events()

View file

@ -4,6 +4,7 @@ from services.UserService import UserService
from datetime import datetime from datetime import datetime
from flask import g from flask import g
from sqlalchemy import func from sqlalchemy import func
from datetime import timedelta
class EventService: class EventService:
@staticmethod @staticmethod
@ -134,4 +135,16 @@ class EventService:
db.session.delete(user_event_association) db.session.delete(user_event_association)
db.session.commit() db.session.commit()
return {'message': 'User successfully removed from the event'} return {'message': 'User successfully removed from the event'}
@staticmethod
def get_all_upcomming_events():
now = datetime.now()
upcoming_deadline = now + timedelta(minutes=30)
events = Event.query.filter(
Event.duedate <= upcoming_deadline,
Event.deleted == False
).all()
return [event.to_dict() for event in events]