Passed
Push — master ( 7d61bb...58af22 )
by Peter
01:49
created

inform_student()   B

Complexity

Conditions 4

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
rs 8.5806
cc 4
1
from django.core.mail import EmailMessage
2
from django.core.urlresolvers import reverse
3
4
from opensubmit import settings
5
6
7
STUDENT_FAILED_SUB = 'Warning - Validation failed'
8
9
STUDENT_FAILED_MSG = '''
10
Hi,
11
12
this is a short notice that your submission for "%s" in "%s"
13
failed in the automated validation test.
14
15
Further information can be found at %s.'''
16
17
STUDENT_PASSED_SUB = 'Validation successful'
18
19
STUDENT_PASSED_MSG = '''
20
Hi,
21
22
this is a short notice that your submission for "%s" in "%s"
23
passed in the automated validation test.
24
25
Further information can be found at %s.'''
26
27
STUDENT_GRADED_SUB = 'Grading finished'
28
29
STUDENT_GRADED_MSG = '''
30
Hi,
31
32
this is a short notice that the of grading your submission
33
for "%s" in "%s" was finalized.
34
35
Further information can be found at %s.'''
36
37
38
def inform_student(submission, state):
39
    '''
40
    Create an email message for the student,
41
    based on the given submission state.
42
43
    Sending eMails on validation completion does
44
    not work, since this may have been triggered
45
    by the admin.
46
    '''
47
    details_url = settings.MAIN_URL + reverse('details', args=(submission.pk,))
48
49
    if state == submission.TEST_VALIDITY_FAILED:
50
        subject = STUDENT_FAILED_SUB
51
        message = STUDENT_FAILED_MSG
52
        message = message % (submission.assignment,
53
                             submission.assignment.course,
54
                             details_url)
55
56
    elif state == submission.CLOSED:
57
        if submission.assignment.is_graded():
58
            subject = STUDENT_GRADED_SUB
59
            message = STUDENT_GRADED_MSG
60
        else:
61
            subject = STUDENT_PASSED_SUB
62
            message = STUDENT_PASSED_MSG
63
        message = message % (submission.assignment,
64
                             submission.assignment.course,
65
                             details_url)
66
    else:
67
        return
68
69
    subject = "[%s] %s" % (submission.assignment.course, subject)
70
    from_email = submission.assignment.course.owner.email
71
    recipients = submission.authors.values_list(
72
        'email', flat=True).distinct().order_by('email')
73
    # send student email with BCC to course owner.
74
    # TODO: This might be configurable later
75
    # email = EmailMessage(subject, message, from_email, recipients,
76
    # [self.assignment.course.owner.email])
77
    email = EmailMessage(subject, message, from_email, recipients)
78
    email.send(fail_silently=True)
79