-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reminder notifications #121
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
52df0cc
Added tasks.py and updated models to send email notifications
nzeager 7f9bea5
Started implementing celery and redis
nzeager 5cc673d
Updated .env file and documentation
nzeager f29b553
Merged with main and resolved conflicts
nzeager fd270f8
Added mentee email to reminder notifications, cleaned up tasks.py and…
nzeager 8c3ec38
tasks.py now triggers an email for 15/60 minute notifications
nzeager 586183e
Started implementing celerys beat schedule to run every 5 minutes, cu…
nzeager aaac02e
Set up celery beat to run every 5 minutes, added celery beat command …
nzeager 30cfe50
test: adds notify task testing
esparr f474acf
chore: updates gitignore for redis dump
esparr 330eaef
chore: delete dump.rdb
esparr d1ebf1c
Merge pull request #123 from esparr/reminder-notify-test
nzeager fb624ec
Added intructions to run celery and redis to README
nzeager b7d10f6
Merged from main
nzeager 7854055
Added TODO comments about unit testing to models.py
nzeager File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,28 @@ python manage.py runserver | |
|
||
The app should now be running at http://localhost:8000/ | ||
|
||
## Run Celery and Redis locally | ||
|
||
Only needed if you want 15/60 minute reminders of scheduled sessions. | ||
|
||
Start the Redis server: | ||
|
||
```bash | ||
redis-server | ||
``` | ||
|
||
Start the Celery server: | ||
|
||
```bash | ||
celery -A config.celery worker --loglevel=info | ||
``` | ||
|
||
Start the Celery Beat server: | ||
|
||
```bash | ||
celery -A config.celery beat -l debug | ||
``` | ||
|
||
## Environment Variables | ||
|
||
1. Create a file named .env in the root directory of your project. This file will contain your environment variables. | ||
|
@@ -85,6 +107,8 @@ DEBUG=True | |
DJANGO_SUPERUSER_USERNAME=admin | ||
DJANGO_SUPERUSER_PASSWORD=admin_password | ||
[email protected] | ||
CELERY_BROKER_URL = local_redis_url | ||
CELERY_RESULT_BACKEND = local_redis_url | ||
``` | ||
|
||
- DATABASE_URL: This should be set to the URL of your database. Depending on your database type, this may include a username, password, host, and port. | ||
|
@@ -99,6 +123,10 @@ [email protected] | |
|
||
- DJANGO_SUPERUSER_EMAIL: This should be set to the email address you want to use for the Django superuser account. | ||
|
||
- CELERY_BROKER_URL: This should be set to your local redis url. | ||
|
||
- CELERY_RESULT_BACKEND: This should be set to your local redis url. | ||
|
||
3. Save the .env file. | ||
|
||
# Testing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from __future__ import absolute_import | ||
from .celery import app as celery_app | ||
|
||
__all__ = ('celery_app',) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import absolute_import | ||
import django | ||
from django.conf import settings | ||
import os | ||
import ssl | ||
from celery import Celery | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') | ||
# for production | ||
# app = Celery( | ||
# "config", | ||
# broker_use_ssl={ | ||
# 'ssl_cert_reqs': ssl.CERT_NONE | ||
# }, | ||
# redis_backend_use_ssl={ | ||
# 'ssl_cert_reqs': ssl.CERT_NONE | ||
# } | ||
# ) | ||
|
||
# for development | ||
app = Celery('config') | ||
|
||
app.conf.beat_schedule = { | ||
'notify-every-5-min': { | ||
'task': 'team_production_system.tasks.notify', | ||
'schedule': 300.0, | ||
}, | ||
} | ||
|
||
app.config_from_object('django.conf:settings', namespace='CELERY') | ||
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from celery import shared_task | ||
from datetime import datetime, timedelta | ||
from django.utils import timezone | ||
from .models import Session | ||
|
||
|
||
@shared_task | ||
# Redis will run this every 5 minutes | ||
def notify(): | ||
now = datetime.now(timezone.utc) | ||
# session = Session.objects.get(pk=session_pk) | ||
sessions = Session.objects.filter(status="Confirmed") | ||
for session in sessions: | ||
start_time = session.start_time | ||
|
||
# We check a range of times to have Redis run every 5 minutes | ||
if start_time - timedelta(minutes=60) \ | ||
<= now \ | ||
<= start_time - timedelta(minutes=55): | ||
if session.mentor.user.notification_settings.sixty_minute_alert: | ||
session.sixty_min_notify() | ||
elif start_time - timedelta(minutes=15) \ | ||
<= now \ | ||
<= start_time - timedelta(minutes=10): | ||
if session.mentor.user.notification_settings.fifteen_minute_alert: | ||
session.fifteen_min_notify() |
Empty file.
50 changes: 50 additions & 0 deletions
50
team_production_system/tests/unit_tests/tasks/test_tasks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
from datetime import timedelta | ||
from unittest.mock import MagicMock | ||
from ....models import Session | ||
from ....tasks import notify | ||
|
||
|
||
class NotifyTestCase(TestCase): | ||
def test_notify_sixty_min(self): | ||
# Create a mock session | ||
session = MagicMock() | ||
session.start_time = timezone.now() + timedelta(minutes=60) | ||
session.mentor.user.notification_settings.sixty_minute_alert = True | ||
|
||
# Create a MagicMock object for the Session.objects manager | ||
session_manager = MagicMock() | ||
|
||
# Set up the mock filter method to return a list of mock sessions | ||
session_manager.filter.return_value = [session] | ||
|
||
# Replace the Session.objects manager with the mock object | ||
Session.objects = session_manager | ||
|
||
# Call the notify function | ||
notify() | ||
|
||
# Check that the session's sixty_min_notify method was called | ||
session.sixty_min_notify.assert_called_once() | ||
|
||
def test_notify_fifteen_min(self): | ||
# Create a mock session | ||
session = MagicMock() | ||
session.start_time = timezone.now() + timedelta(minutes=15) | ||
session.mentor.user.notification_settings.fifteen_minute_alert = True | ||
|
||
# Create a MagicMock object for the Session.objects manager | ||
session_manager = MagicMock() | ||
|
||
# Set up the mock filter method to return a list of mock sessions | ||
session_manager.filter.return_value = [session] | ||
|
||
# Replace the Session.objects manager with the mock object | ||
Session.objects = session_manager | ||
|
||
# Call the notify function | ||
notify() | ||
|
||
# Check that the session's sixty_min_notify method was called | ||
session.fifteen_min_notify.assert_called_once() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's look into how to automate the production versus development environments for celery. Here is the Render documentation for Celery: https://render.com/docs/deploy-celery