Skip to content

Commit

Permalink
Merge pull request #3 from IIIT-Delhi/v2-e
Browse files Browse the repository at this point in the history
Add Async Call to Email Function
  • Loading branch information
chirag-jn authored Dec 27, 2020
2 parents b87f0a6 + 79df587 commit bd0c013
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
14 changes: 14 additions & 0 deletions studentportal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
diff_type, add_diff, Flag

from django.core.mail import send_mail
from supervisor.async_helper import AsyncMethod

import credentials

Expand All @@ -29,6 +30,16 @@
diff_worker = diff_match_patch.diff_match_patch()


def Async(fnc=None, callback=None):
if fnc is None:
def AddAsyncCallback(fnc):
return AsyncMethod(fnc, callback)

return AddAsyncCallback
else:
return AsyncMethod(fnc, callback)


def index(request):
if request.user.is_authenticated:
if request.user.email in access_cache.get_TA():
Expand All @@ -54,6 +65,8 @@ def home(request):
return render(request, 'studenthome.html',
{'news': access_cache.get_news(), 'stages': project_stage})


@Async
@login_required
def send_cw_sg_email(request, subject, text, recipients, project_id=None):
ta_email = None
Expand All @@ -74,6 +87,7 @@ def send_cw_sg_email(request, subject, text, recipients, project_id=None):

send_mail(subject, text, ta_email, recipients, auth_user=ta_email, auth_password=ta_pass)


@login_required
def addproject(request):
allow_project_flag = Flag.objects.get(key='add_project')
Expand Down
34 changes: 34 additions & 0 deletions supervisor/async_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import threading

class TimeoutError(RuntimeError):
pass

class AsyncCall(object):
def __init__(self, fnc, callback = None):
self.Callable = fnc
self.Callback = callback

def __call__(self, *args, **kwargs):
self.Thread = threading.Thread(target = self.run, name = self.Callable.__name__, args = args, kwargs = kwargs)
self.Thread.start()
return self

def wait(self, timeout = None):
self.Thread.join(timeout)
if self.Thread.isAlive():
raise TimeoutError()
else:
return self.Result

def run(self, *args, **kwargs):
self.Result = self.Callable(*args, **kwargs)
if self.Callback:
self.Callback(self.Result)

class AsyncMethod(object):
def __init__(self, fnc, callback=None):
self.Callable = fnc
self.Callback = callback

def __call__(self, *args, **kwargs):
return AsyncCall(self.Callable, self.Callback)(*args, **kwargs)
14 changes: 13 additions & 1 deletion supervisor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@
from django.core.mail import send_mail

import credentials
from supervisor.async_helper import AsyncMethod


def Async(fnc=None, callback=None):
if fnc is None:
def AddAsyncCallback(fnc):
return AsyncMethod(fnc, callback)

return AddAsyncCallback
else:
return AsyncMethod(fnc, callback)


@supervisor_logged_in
Expand Down Expand Up @@ -350,7 +361,7 @@ def accept_NGO(request, noti_id):
"Org Name: " + str(noti.NGO_name) + "\n" +
"Org Details: " + str(noti.NGO_details) + "\n" +
"Org Link: " + str(noti.NGO_link) + "\n"
"Suggested By: " + str(noti.NGO_sugg_by) + "\n",
"Suggested By: " + str(noti.NGO_sugg_by) + "\n",
recipients=[str(noti.NGO_sugg_by.email)], notif_id=noti_id)

messages.success(request, "%s is now a trusted Organization." % noti.NGO_name)
Expand Down Expand Up @@ -712,6 +723,7 @@ def get_TA_logs(request, ta_id):
'ta': ta, 'diffs': diffs})


@Async
@supervisor_logged_in
def send_cw_sg_email(request, subject, text, recipients, project_id=None, notif_id=None):
ta_email = None
Expand Down

0 comments on commit bd0c013

Please sign in to comment.