Skip to content

Instantly share code, notes, and snippets.

@ihfazhillah
Created February 19, 2020 15:14
Show Gist options
  • Save ihfazhillah/c4757dd01cb278b7d3cbc351c2e5b528 to your computer and use it in GitHub Desktop.
Save ihfazhillah/c4757dd01cb278b7d3cbc351c2e5b528 to your computer and use it in GitHub Desktop.
from django.contrib import messages
from django.core.mail import EmailMultiAlternatives
from django.shortcuts import redirect
from django.template.loader import render_to_string
from gradebook import celery_app
from mygrades.models import Student, StudentAssignment
@celery_app.task
def send_weekly_assigments_task(email):
student = Student.objects.filter(teacher_email=email)
for student in student:
assignments = StudentAssignment.objects.filter(student=student, status='Assigned') #, shown_in_weekly=True)
data = []
for asm in assignments:
standards = ",".join(asm.assignment.standard.all().values_list("standard_code",flat=True))
if standards == "":
standards = "-"
data.append({'title':asm.assignment.name, 'detail':asm.assignment.description, 'curriculum':asm.assignment.curriculum.name, 'cur_pk':asm.assignment.curriculum.pk, 'standards': standards})
subject, from_email, to = "%s's Assignments for This Week" % student.get_full_name(), '[email protected]', [
email]
text_content = 'A weekly assignment list is included. You may need to open this in a different browser if you do not see it here. '
html_content = render_to_string('mail_weekly_assignments.html', context={'assignments':data, 'student':student})
msg = EmailMultiAlternatives(subject, text_content, from_email, to)
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_all_students_weekly_assignments(request):
send_weekly_assigments_task.delay(request.user.email)
messages.success(request, "Weekly assignments for all of your students were sent to your email!")
return redirect("/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment