Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change when the review_update webhook sends #56

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions home/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from datetime import datetime

from django.urls import reverse
from django.template.defaultfilters import pluralize

from discord_webhook import DiscordWebhook
from discord_webhook.webhook import DiscordEmbed
Expand Down Expand Up @@ -204,30 +205,27 @@ def recompute_ttl_cache():
value = (0, *values)
_ttl_cache[key] = value

def send_updates_webhook(request, *, include_professors=True, include_reviews=True):
def send_updates_webhook(request):
# avoid circular imports
from home.models import Professor, Review
if not WEBHOOK_URL_UPDATE:
return

title = ""
num_professors = Professor.pending.count()
num_reviews = Review.pending.count()

if include_professors:
title += f"{num_professors} unverified professor(s)"
if include_professors and include_reviews:
title += " and "
if include_reviews:
title += f"{num_reviews} unverified review(s)"
title = f"{num_reviews} unverified review{pluralize(num_reviews)}"

if num_professors:
title += f" and {num_professors} unverified professor{pluralize(num_professors)}"

webhook = DiscordWebhook(url=WEBHOOK_URL_UPDATE)
embed = DiscordEmbed(title=title, description="\n",
url=request.build_absolute_uri(reverse("admin")))

webhook.add_embed(embed)

if (num_reviews + num_professors) % WEBHOOK_FREQUENCY != 0:
if num_reviews % WEBHOOK_FREQUENCY != 0:
return

webhook.execute()
Expand Down
2 changes: 1 addition & 1 deletion home/views/professor.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def post(self, request, slug):
new_review = Review(**review_data)
new_review.save()

send_updates_webhook(request, include_professors=False)
send_updates_webhook(request)

form = ProfessorFormReview(user, professor)
context = {
Expand Down