Compare commits
3 commits
3c9e7bca64
...
212b18a47a
Author | SHA1 | Date | |
---|---|---|---|
212b18a47a | |||
53a63877a1 | |||
07a49badda |
5 changed files with 76 additions and 9 deletions
8
app.py
8
app.py
|
@ -10,7 +10,7 @@ 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
|
||||
from services.EventNotifyerService import EventNotifyerService
|
||||
|
||||
class App:
|
||||
def __init__(self):
|
||||
|
@ -27,7 +27,7 @@ class App:
|
|||
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)
|
||||
self.scheduler.add_job(id='notifyUpcommingEvents', func=self.notifyUpcommingEvents, trigger='interval', seconds=1)
|
||||
|
||||
|
||||
def set_config(self):
|
||||
|
@ -63,9 +63,9 @@ class App:
|
|||
def SchadulerConfig(object):
|
||||
SCHEDULER_API_ENABLED = True
|
||||
|
||||
def locate_upcoming_events(self):
|
||||
def notifyUpcommingEvents(self):
|
||||
with self.app.app_context():
|
||||
print(EventNotifyerServer.locate_upcoming_events())
|
||||
EventNotifyerService.notifyUpcommingEvents()
|
||||
|
||||
def run(self):
|
||||
with self.app.app_context():
|
||||
|
|
32
migrations/versions/30ce6cd40d05_adda_col_notifyed_bool.py
Normal file
32
migrations/versions/30ce6cd40d05_adda_col_notifyed_bool.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
"""AddA_col_Notifyed_Bool
|
||||
|
||||
Revision ID: 30ce6cd40d05
|
||||
Revises: 04e41f293ec2
|
||||
Create Date: 2024-01-09 10:46:50.964332
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '30ce6cd40d05'
|
||||
down_revision = '04e41f293ec2'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('user_event', schema=None) as batch_op:
|
||||
batch_op.drop_column('notified')
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table('user_event', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('notified', sa.BOOLEAN(), nullable=True))
|
||||
|
||||
# ### end Alembic commands ###
|
|
@ -10,7 +10,6 @@ class UserEventAssociation(db.Model):
|
|||
__tablename__ = 'user_event'
|
||||
user_id = Column(String(36), ForeignKey('user.id'), primary_key=True)
|
||||
event_id = Column(Integer, ForeignKey('event.id'), primary_key=True)
|
||||
notified = Column(Boolean, default=False)
|
||||
|
||||
class Event(db.Model):
|
||||
id = Column(db.Integer, primary_key=True)
|
||||
|
|
|
@ -1,10 +1,37 @@
|
|||
# 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
|
||||
from services.UserService import UserService
|
||||
from models import UserEventAssociation, db
|
||||
|
||||
class EventNotifyerServer:
|
||||
class EventNotifyerService:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def locate_upcoming_events():
|
||||
return EventService.get_all_upcomming_events()
|
||||
def notifyUpcommingEvents():
|
||||
# this method will get the upcoming events from the event service
|
||||
# foreach event, locate the users with the same location as the event but not the users that already been notifyed about the event
|
||||
# foreach user, append to UserEventAssociation table that the user is been notified about the event
|
||||
events = EventService.get_upcoming_events()
|
||||
for event in events:
|
||||
# locate users with the same location as the event but not the users that already been notifyed about the event (users with UserEventAssociation)
|
||||
users = UserService.get_users_with_same_location_but_not_been_notifyed(event['location'])
|
||||
if not users:
|
||||
print("No user to notify")
|
||||
continue
|
||||
for user in users:
|
||||
print("Found user to notify:", user)
|
||||
if notify_user(user['email'], event):
|
||||
user_event_association = UserEventAssociation(
|
||||
user_id=user['id'],
|
||||
event_id=event['id']
|
||||
)
|
||||
db.session.add(user_event_association)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def notify_user(userEmail, event):
|
||||
# send email to user is the email been sent, return true; otherwise, return false
|
||||
return True
|
||||
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
from flask_bcrypt import Bcrypt
|
||||
from models import db, User
|
||||
from models import db, User, UserEventAssociation
|
||||
|
||||
|
||||
bcrypt = Bcrypt()
|
||||
|
||||
|
@ -35,3 +36,11 @@ class UserService:
|
|||
if user and bcrypt.check_password_hash(user.password_hash, data['password']):
|
||||
return user
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_users_with_same_location_but_not_been_notifyed(location):
|
||||
users = User.query.filter_by(location=location).all()
|
||||
users = [user for user in users if not UserEventAssociation.query.filter_by(user_id=user.id).first()]
|
||||
return [user.to_dict() for user in users]
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue