Skip to content

Commit 2aabe8e

Browse files
authored
Add slack integration for job board (chicagopython#419)
* adds slack integration for job board * adds logging for failed job posting * removes print statment * updates template and adds weekdays parameter to command * updates to django 2.2.24 for security issue * adds @ray's feedback Co-authored-by: Aaron Elmquist <[email protected]>
1 parent ad10d9f commit 2aabe8e

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

chipy_org/apps/job_board/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Meta:
2929
]
3030

3131
help_texts = {
32-
"affiliation": ("Is this posting affiliated with a 3rd party? Please select:"), # pylint: disable=line-too-long
32+
"affiliation": ("Is this posting affiliated with a 3rd party? Please select:"),
3333
}
3434

3535
widgets = {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import datetime
2+
import json
3+
import logging
4+
5+
import requests
6+
from django.conf import settings
7+
from django.core.management.base import BaseCommand, CommandError
8+
from django.template import loader
9+
10+
from chipy_org.apps.job_board.models import JobPost
11+
12+
logger = logging.getLogger(__name__)
13+
14+
15+
class Command(BaseCommand):
16+
help = """
17+
This command retrieves all approved and active job posts from the database
18+
and posts them to the slack jobs channel.
19+
20+
Integrate this command with a scheduler to automatically post jobs to the
21+
slack jobs channel.
22+
"""
23+
24+
def add_arguments(self, parser):
25+
parser.add_argument(
26+
"weekdays",
27+
nargs="+",
28+
type=int,
29+
help="""
30+
The desired weekdays to post represented an integer where Monday=0, Tuesday=1, ... , Saturday=5, and Sunday=6.
31+
Posts to the slack channel will only occur on the selected weekdays.
32+
""",
33+
)
34+
35+
def handle(self, *args, **options):
36+
posts = JobPost.approved_and_active.all()
37+
38+
if posts.count() and datetime.date.today().weekday() in options["weekdays"]:
39+
context = {"posts": posts}
40+
template = loader.get_template("job_board/slack_template.txt")
41+
msg = template.render(context)
42+
43+
job_post_key = settings.JOB_POST_KEY
44+
webhook_url = f"https://hooks.slack.com/services/{job_post_key}"
45+
slack_data = {"text": msg}
46+
47+
response = requests.post(
48+
webhook_url,
49+
data=json.dumps(slack_data),
50+
headers={"Content-Type": "application/json"},
51+
)
52+
53+
if response.status_code != 200:
54+
raise CommandError(
55+
f"Failed to post to slack job channel - status code: {response.status_code}."
56+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
There {{ posts.count|pluralize:"is,are" }} currently *{{ posts.count }}* job{{ posts.count|pluralize }} on the Chipy job board.
2+
3+
{% for post in posts %}
4+
*{{ post.company_name }}* posted a *{{ post.position }}* opportunity.
5+
6+
*Description*:
7+
{{ post.description }}
8+
9+
{% endfor %}
10+
11+
See all the open job posts on https://www.chipy.org/job-board/

chipy_org/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ def env_list(key, defaults=None, delimiter=","):
111111
else:
112112
MEDIA_ROOT = os.path.abspath(os.path.join(PROJECT_ROOT, "mediafiles"))
113113

114+
115+
JOB_POST_KEY = env_var("JOB_POST_KEY", "")
116+
117+
114118
STATIC_ROOT = os.path.abspath(os.path.join(PROJECT_ROOT, "..", "staticfiles"))
115119

116120
STATIC_URL = "/static/"
@@ -124,7 +128,6 @@ def env_list(key, defaults=None, delimiter=","):
124128
# Make this unique, and don't share it with anybody.
125129
SECRET_KEY = env_var("SECRET_KEY")
126130

127-
128131
ROOT_URLCONF = "chipy_org.urls"
129132

130133

docker/docker.env.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend'
8383
#AWS_STORAGE_BUCKET_NAME=""
8484
#
8585
################################################
86+
87+
# Slack Webhooks ##########################################
88+
JOB_POST_KEY=""
89+
###########################################################

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ django-ical==1.7.0
1414
django-nocaptcha-recaptcha==0.0.20
1515
django-storages==1.9.1
1616
django-tinymce==2.8.0
17-
Django==2.2.22
17+
Django==2.2.24
1818
djangorestframework==3.11.2
1919
feedparser==5.2.1
2020
gunicorn==20.0.4

0 commit comments

Comments
 (0)