Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
RUN apt-get update
RUN apt-get install -y cron
COPY . /code/
8 changes: 8 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_crontab',
'django_filters',
'corsheaders',
'rest_framework',
Expand Down Expand Up @@ -251,6 +252,7 @@
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
DEFAULT_FROM_EMAIL = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')


Expand All @@ -270,3 +272,9 @@

AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')

CRONJOBS = [
# TODO: fix "no crontab for root" error in heroku?
# ('*/1 * * * *', 'weekly_digest.cron.weekly_digest_job','>> ./file.log'), # for testing
# ('0 4 * * 1', 'weekly_digest.cron.weekly_digest_job'), # every monday at 4am
]
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:

web:
build: .
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py crontab add && python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
ports:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Faker==13.15.1
boto3==1.18.36
django-storages==1.11.1
Pillow==9.2.0
django-crontab==0.7.1
17 changes: 17 additions & 0 deletions weekly_digest/cron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.core.mail import send_mail
from django.conf import settings


def weekly_digest_job(self, *args, **options):
subscribed_users = WeeklyDigestSubscription.objects.filter(
subscribed=True
).all()

send_mail(
'subject',
'Here is the message.',
settings.EMAIL_HOST_USER,
['daniel.ashcraft@gmail.com'],
fail_silently=False,
)
print('Successfully sent')
17 changes: 17 additions & 0 deletions weekly_digest/tests/views/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ def test_weekly_digest_put_request(self):
self.assertEqual(response_data['email'], self.existing_email_address)
self.assertEqual(response_data['subscribed'], False)

def test_weekly_digest_patch_request(self):
request_data = {
'subscribed': 'False',
}

response = self.client.patch(
f"/api/v1/weekly_digests/{self.weekly_digest_subscription.id}/",
json.dumps(request_data),
content_type="application/json",
)
response_data = response.json()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response_data['email'], self.weekly_digest_subscription.email)
self.assertEqual(response_data['subscribed'], False)


def test_weekly_digest_get_request(self):
response = self.client.get(
f"/api/v1/weekly_digests/{self.weekly_digest_subscription.id}/",
Expand All @@ -87,6 +103,7 @@ def test_weekly_digest_delete_request(self):
)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)


def test_trigger_digest_email_request_week_events(self):
Event.objects.create(
event_name='Today Event',
Expand Down