Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
feat: Scheduler and Jobs Added
Browse files Browse the repository at this point in the history
  • Loading branch information
ritwickraj78 authored and SanketDG committed Jul 28, 2020
1 parent 25231b7 commit 97a6d46
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 2 deletions.
3 changes: 2 additions & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ django-imagekit==4.0.2
psycopg2==2.8.5
python3-openid==3.2.0
geoip2==4.0.1
django-ipware==3.0.0
django-ipware==3.0.0
django-apscheduler==0.3.1
25 changes: 24 additions & 1 deletion systers_portal/meetup/signals.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from django.db.models.signals import post_save, post_delete, post_migrate
from datetime import datetime, timedelta

from django.db.models.signals import post_save, post_delete, post_migrate, pre_save
from django.dispatch import receiver
from pinax.notifications.models import NoticeType

from meetup.models import Meetup
from meetup.constants import COMMUNITY_LEADER
from meetup.utils import (create_groups, assign_permissions, remove_groups)

from users.scheduler import scheduler
from meetup.utils import notify_location, notify_time


@receiver(post_save, sender=Meetup, dispatch_uid="manage_groups")
def manage_meetup_groups(sender, instance, created, **kwargs):
Expand Down Expand Up @@ -37,3 +42,21 @@ def create_notice_types(sender, **kwargs):
("your support request has been approved"))
NoticeType.create("new_meetup_request", ("New Meetup Request"),
("a user has added a meetup request"))


@receiver(pre_save, sender=Meetup, dispatch_uid="location_change")
def notify_change(sender, instance, **kwargs):
obj = Meetup.objects.filter(pk=instance.pk)
if obj:
if obj[0].date != instance.date or obj[0].time != instance.time:
name = "Time for {0} Change Notify".format(instance.title)
scheduler.add_job(notify_time, "date",
run_date=datetime.now() + timedelta(minutes=5),
args=[instance],
id=name, replace_existing=True)
if obj[0].meetup_location != instance.meetup_location:
name = "Location for {0} Change Notify".format(instance.title)
scheduler.add_job(notify_location, "date",
run_date=datetime.now() + timedelta(minutes=5),
args=[instance],
id=name, replace_existing=True)
62 changes: 62 additions & 0 deletions systers_portal/meetup/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from django.contrib.auth.models import Group, Permission
from django.core.mail import send_mail
from django.db import transaction
from django.template.loader import render_to_string

from meetup.permissions import groups_templates, group_permissions

from meetup.models import Rsvp

from users.models import UserSetting

from systers_portal.settings.dev import FROM_EMAIL


@transaction.atomic
def create_groups(meetup):
Expand Down Expand Up @@ -48,3 +56,57 @@ def assign_permissions(meetup, groups):
for perm in group_permissions[key]:
group.permissions.add(Permission.objects.filter(codename=perm).first())
group.save()


def send_reminder(meetup):
rsvp_list = Rsvp.objects.filter(meetup=meetup)
subject = "Reminder for {0}".format(meetup)
for rsvp in rsvp_list:
setting = UserSetting.objects.get(user=rsvp.user)
if setting.reminder:
html_text = render_to_string("templates/meetup/reminder.html",
context={'meetup': meetup,
'user': rsvp.user})
send_mail(
subject,
'Reminder Mail',
FROM_EMAIL,
[rsvp.user.user.email],
html_message=html_text,
)


def notify_location(meetup):
rsvp_list = Rsvp.objects.filter(meetup=meetup)
subject = "Notification for change in location for {0}".format(meetup)
for rsvp in rsvp_list:
setting = UserSetting.objects.get(user=rsvp.user)
if setting.location_change:
html_text = render_to_string("templates/meetup/location_change_email.html",
context={'meetup': meetup,
'user': rsvp.user})
send_mail(
subject,
'Change in Location',
FROM_EMAIL,
[rsvp.user.user.email],
html_message=html_text,
)


def notify_time(meetup):
rsvp_list = Rsvp.objects.filter(meetup=meetup)
subject = "Notification for change in location for {0}".format(meetup)
for rsvp in rsvp_list:
setting = UserSetting.objects.get(user=rsvp.user)
if setting.time_change:
html_text = render_to_string("templates/meetup/time_change_email.html",
context={'meetup': meetup,
'user': rsvp.user})
send_mail(
subject,
'Time Changed',
FROM_EMAIL,
[rsvp.user.user.email],
html_message=html_text,
)
9 changes: 9 additions & 0 deletions systers_portal/systers_portal/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@
'users',
'rest_framework',
'pinax.notifications',
'django_apscheduler',
)

SCHEDULER_CONFIG = {
"apscheduler.jobstores.default": {
"class": "django_apscheduler.jobstores:DjangoJobStore"
},
'apscheduler.executors.processpool': {
"type": "threadpool"
},
}
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
Expand Down
2 changes: 2 additions & 0 deletions systers_portal/systers_portal/settings/dev.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .base import *

SCHEDULER_AUTOSTART = True
DEBUG = True
TEMPLATES[0]['OPTIONS']['debug'] = DEBUG

Expand All @@ -21,3 +22,4 @@
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
FROM_EMAIL = os.environ.get('FROM_EMAIL')
1 change: 1 addition & 0 deletions systers_portal/systers_portal/settings/testing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .base import *

SCHEDULER_AUTOSTART = False
DEBUG = True
TEMPLATES[0]['OPTIONS']['debug'] = DEBUG

Expand Down
16 changes: 16 additions & 0 deletions systers_portal/templates/community/weekly_digest_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
Hey {{user}}, We have details for {{community}} for you.
We have {{count}} members as of now!. Get the latest news and resources available at the community
by checking out the Systers Portal. <br>
Thank You, <br>
AnitaB.org
</body>
</html>
15 changes: 15 additions & 0 deletions systers_portal/templates/meetup/location_change_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
Hello {{user}},<br>
<p>This is to inform you that the location for {{meetup}} has been changed to {{meetup.meetup_location}}<br></p>
Thank You,<br>
AnitaB.org
</body>
</html>
15 changes: 15 additions & 0 deletions systers_portal/templates/meetup/reminder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
Hello {{user}},<br>
<p>This is a gentle reminder for {{meetup}} which will begin on {{meetup.date}} {%if meetup.time%} at {{meetup.time}} {% endif %} <br></p>
Thank You,<br>
AnitaB.org
</body>
</html>
15 changes: 15 additions & 0 deletions systers_portal/templates/meetup/time_change_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
Hello {{user}},<br>
<p>This is to inform you that the timing for {{meetup}} has been changed to {{meetup.date}} {%if meetup.time%} {{meetup.time}} {% endif %} <br></p>
Thank You,<br>
AnitaB.org
</body>
</html>
4 changes: 4 additions & 0 deletions systers_portal/users/apps.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.apps import AppConfig
from django.conf import settings


class UsersConfig(AppConfig):
name = 'users'

def ready(self):
import users.signals # noqa # pylint: disable=unused-variable
from . import scheduler
if settings.SCHEDULER_AUTOSTART:
scheduler.start()
53 changes: 53 additions & 0 deletions systers_portal/users/scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import datetime
import logging
from datetime import timedelta

from apscheduler.schedulers.background import BackgroundScheduler
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django_apscheduler.jobstores import register_events, register_job

from django.conf import settings

from meetup.utils import send_reminder
from meetup.models import Meetup

from community.models import Community

from systers_portal.settings.dev import FROM_EMAIL

scheduler = BackgroundScheduler(settings.SCHEDULER_CONFIG)


@register_job(scheduler, 'cron', day_of_week='mon', hour=5, minute=30, replace_existing=True)
def weekly_digest():
communities = Community.objects.all()
for community in communities:
subject = "Weekly update from {0}".format(community)
count = community.members.count()
for member in community.members:
html_text = \
render_to_string("templates/community/weekly_digest_email.html",
{'user': member,
'count': count,
'community': community})
send_mail(
subject,
'Weekly Digest',
FROM_EMAIL,
[member.user.email],
html_message=html_text,
)


def start():
if settings.DEBUG:
logging.basicConfig()
logging.getLogger('apscheduler').setLevel(logging.DEBUG)
register_events(scheduler)
scheduler.start()
meetup_list = Meetup.objects.filter(date__gte=datetime.date.today())
for meetup in meetup_list:
name = "Reminder for {0}".format(meetup.title)
scheduler.add_job(send_reminder, "date", run_date=meetup.date - timedelta(hours=1),
args=[meetup], id=name, replace_existing=True)
Binary file added workdir.0
Binary file not shown.

0 comments on commit 97a6d46

Please sign in to comment.