From e718ae905d486afeee1ad9313ad61ba8d106e101 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Mon, 8 Jan 2024 22:03:51 +0200 Subject: [PATCH] added EventNotifyer --- app.py | 19 +++++++++++++++++++ services/EventNotifyerService.py | 10 ++++++++++ services/EventService.py | 15 ++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 services/EventNotifyerService.py diff --git a/app.py b/app.py index 16404c7..f9cd141 100644 --- a/app.py +++ b/app.py @@ -9,6 +9,8 @@ from middlewares.errorHandlers import handle_auth_error, handle_invalid_token from flask_jwt_extended.exceptions import NoAuthorizationError from jwt.exceptions import InvalidTokenError from datetime import timedelta +from flask_apscheduler import APScheduler +from services.EventNotifyerService import EventNotifyerServer class App: def __init__(self): @@ -18,6 +20,15 @@ class App: self.set_up_jwt() self.register_blueprints() 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): self.app.config.from_object(config.Config) @@ -44,6 +55,7 @@ class App: def run(self): with self.app.app_context(): db.create_all() + self.setup_scheduler() # Setup scheduler self.app.run(debug=True) def print_endpoints(self): @@ -54,6 +66,13 @@ class App: 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_instance = app_class_instance.app diff --git a/services/EventNotifyerService.py b/services/EventNotifyerService.py new file mode 100644 index 0000000..2ff50d9 --- /dev/null +++ b/services/EventNotifyerService.py @@ -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() \ No newline at end of file diff --git a/services/EventService.py b/services/EventService.py index c5408ff..cf5eb59 100644 --- a/services/EventService.py +++ b/services/EventService.py @@ -4,6 +4,7 @@ from services.UserService import UserService from datetime import datetime from flask import g from sqlalchemy import func +from datetime import timedelta class EventService: @staticmethod @@ -134,4 +135,16 @@ class EventService: db.session.delete(user_event_association) db.session.commit() - return {'message': 'User successfully removed from the event'} \ No newline at end of file + 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] \ No newline at end of file