Compare commits
No commits in common. "212b18a47a53275df0d47bce0fc74021f9123631" and "3c9e7bca64191dbc93a29b9ecb0c404db428e208" have entirely different histories.
212b18a47a
...
3c9e7bca64
5 changed files with 9 additions and 76 deletions
8
app.py
8
app.py
|
@ -10,7 +10,7 @@ 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 flask_apscheduler import APScheduler
|
||||||
from services.EventNotifyerService import EventNotifyerService
|
from services.EventNotifyerService import EventNotifyerServer
|
||||||
|
|
||||||
class App:
|
class App:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -27,7 +27,7 @@ class App:
|
||||||
self.scheduler.init_app(self.app)
|
self.scheduler.init_app(self.app)
|
||||||
self.scheduler.start()
|
self.scheduler.start()
|
||||||
# Schedule the job
|
# Schedule the job
|
||||||
self.scheduler.add_job(id='notifyUpcommingEvents', func=self.notifyUpcommingEvents, trigger='interval', seconds=1)
|
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):
|
||||||
|
@ -63,9 +63,9 @@ class App:
|
||||||
def SchadulerConfig(object):
|
def SchadulerConfig(object):
|
||||||
SCHEDULER_API_ENABLED = True
|
SCHEDULER_API_ENABLED = True
|
||||||
|
|
||||||
def notifyUpcommingEvents(self):
|
def locate_upcoming_events(self):
|
||||||
with self.app.app_context():
|
with self.app.app_context():
|
||||||
EventNotifyerService.notifyUpcommingEvents()
|
print(EventNotifyerServer.locate_upcoming_events())
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
with self.app.app_context():
|
with self.app.app_context():
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
"""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,6 +10,7 @@ class UserEventAssociation(db.Model):
|
||||||
__tablename__ = 'user_event'
|
__tablename__ = 'user_event'
|
||||||
user_id = Column(String(36), ForeignKey('user.id'), primary_key=True)
|
user_id = Column(String(36), ForeignKey('user.id'), primary_key=True)
|
||||||
event_id = Column(Integer, ForeignKey('event.id'), primary_key=True)
|
event_id = Column(Integer, ForeignKey('event.id'), primary_key=True)
|
||||||
|
notified = Column(Boolean, default=False)
|
||||||
|
|
||||||
class Event(db.Model):
|
class Event(db.Model):
|
||||||
id = Column(db.Integer, primary_key=True)
|
id = Column(db.Integer, primary_key=True)
|
||||||
|
|
|
@ -1,37 +1,10 @@
|
||||||
# this Class is for the scheduler
|
# this Class is for the scheduler
|
||||||
#this class will have a function that locates the upcomming events Using the Event service.
|
#this class will have a function that locates the upcomming events Using the Event service.
|
||||||
from services.EventService import EventService
|
from services.EventService import EventService
|
||||||
from services.UserService import UserService
|
|
||||||
from models import UserEventAssociation, db
|
|
||||||
|
|
||||||
class EventNotifyerService:
|
class EventNotifyerServer:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
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 locate_upcoming_events():
|
||||||
def notify_user(userEmail, event):
|
return EventService.get_all_upcomming_events()
|
||||||
# send email to user is the email been sent, return true; otherwise, return false
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from flask_bcrypt import Bcrypt
|
from flask_bcrypt import Bcrypt
|
||||||
from models import db, User, UserEventAssociation
|
from models import db, User
|
||||||
|
|
||||||
|
|
||||||
bcrypt = Bcrypt()
|
bcrypt = Bcrypt()
|
||||||
|
|
||||||
|
@ -36,11 +35,3 @@ class UserService:
|
||||||
if user and bcrypt.check_password_hash(user.password_hash, data['password']):
|
if user and bcrypt.check_password_hash(user.password_hash, data['password']):
|
||||||
return user
|
return user
|
||||||
return None
|
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