forked from python/pythondotorg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisteners.py
122 lines (102 loc) · 4.26 KB
/
listeners.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from django.conf import settings
from django.core.mail import send_mail
from django.db import models
from django.dispatch import receiver
from django.contrib.sites.models import Site
from django.template import loader
from django.utils.translation import ugettext_lazy as _
from .models import Job
from .signals import (
job_was_submitted, job_was_approved, job_was_rejected, comment_was_posted,
)
# Python job board team email address
EMAIL_JOBS_BOARD = '[email protected]'
@receiver(comment_was_posted)
def on_comment_was_posted(sender, comment, **kwargs):
"""
Notify the author of the post when the first comment has been posted.
"""
if not comment.comment:
return False
job = comment.job
if job.creator is None:
name = job.contact or 'Job Submitter'
else:
name = (
job.creator.get_full_name() or job.creator.get_username() or
job.contact or 'Job Submitter'
)
send_to = [EMAIL_JOBS_BOARD]
reviewer_name = (
comment.creator.get_full_name() or comment.creator.get_username() or
'Community Reviewer'
)
is_job_board_admin = job.creator.email != comment.creator.email
context = {
'comment': comment.comment.raw,
'content_object': job,
'site': Site.objects.get_current(),
}
if is_job_board_admin:
# Send email to both [email protected] and creator's email when
# job board admins left a comment.
send_to.append(job.email)
context['user_name'] = name
context['reviewer_name'] = reviewer_name
template_name = 'comment_was_posted'
else:
context['submitter_name'] = name
template_name = 'comment_was_posted_admin'
subject = _("Python Job Board: Review comment for: {}").format(
job.display_name)
text_message_template = loader.get_template('jobs/email/{}.txt'.format(template_name))
text_message = text_message_template.render(context)
send_mail(subject, text_message, settings.JOB_FROM_EMAIL, send_to)
def send_job_review_message(job, user, subject_template_path,
message_template_path):
"""Helper function wrapping logic of sending the review message concerning
a job.
`user` param holds user that performed the review action.
"""
subject_template = loader.get_template(subject_template_path)
message_template = loader.get_template(message_template_path)
reviewer_name = user.get_full_name() or user.username or 'Community Reviewer'
context = {
'user_name': job.contact or 'Job Submitter',
'reviewer_name': reviewer_name,
'content_object': job,
'site': Site.objects.get_current(),
}
# subject can't contain newlines, thus strip() call
subject = subject_template.render(context).strip()
message = message_template.render(context)
send_mail(subject, message, settings.JOB_FROM_EMAIL,
[job.email, EMAIL_JOBS_BOARD])
@receiver(job_was_approved)
def on_job_was_approved(sender, job, approving_user, **kwargs):
"""Handle approving job offer. Currently an email should be sent to the
person that sent the offer.
"""
send_job_review_message(job, approving_user,
'jobs/email/job_was_approved_subject.txt',
'jobs/email/job_was_approved.txt')
@receiver(job_was_rejected)
def on_job_was_rejected(sender, job, rejecting_user, **kwargs):
"""Handle rejecting job offer. Currently an email should be sent to the
person that sent the offer.
"""
send_job_review_message(job, rejecting_user,
'jobs/email/job_was_rejected_subject.txt',
'jobs/email/job_was_rejected.txt')
@receiver(job_was_submitted)
def on_job_was_submitted(sender, job, **kwargs):
"""
Notify the jobs board when a new job has been submitted for approval
"""
subject_template = loader.get_template('jobs/email/job_was_submitted_subject.txt')
message_template = loader.get_template('jobs/email/job_was_submitted.txt')
context = {'content_object': job, 'site': Site.objects.get_current()}
subject = subject_template.render(context)
message = message_template.render(context)
send_mail(subject, message, settings.JOB_FROM_EMAIL,
[EMAIL_JOBS_BOARD])