Skip to content

Commit

Permalink
Send notifications for new corrections or feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
danielzeljko committed Aug 18, 2023
1 parent 9dbaf18 commit 54e9eed
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions langcorrect/corrections/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.utils.translation import gettext_lazy as translate
from notifications.signals import notify

from langcorrect.corrections.helpers import check_can_make_corrections
from langcorrect.corrections.models import CorrectedRow, OverallFeedback, PerfectRow
Expand All @@ -22,6 +24,10 @@ def make_corrections(request, slug):
corrections_data = request.POST.get("corrections_data")
overall_feedback = request.POST.get("overall_feedback", None)

previous_correctors = post.get_correctors
new_correction_made = False
new_feedback_given = False

if corrections_data:
corrections = json.loads(corrections_data)

Expand All @@ -34,18 +40,21 @@ def make_corrections(request, slug):
post_row_instance = PostRow.objects.get(id=sentence_id)

if action == "perfect":
PerfectRow.available_objects.get_or_create(
_, perfect_created = PerfectRow.available_objects.get_or_create(
post=post, post_row=post_row_instance, user=current_user
)
new_correction_made |= perfect_created

if action == "corrected":
corrected_row, _ = CorrectedRow.available_objects.get_or_create(
corrected_row, correctedrow_created = CorrectedRow.available_objects.get_or_create(
post=post, post_row=post_row_instance, user=current_user
)
corrected_row.correction = corrected_text
corrected_row.note = feedback
corrected_row.save()

new_correction_made |= correctedrow_created

if action == "delete":
# Note: a sentence cannot be both marked as perfect or corrected
PerfectRow.available_objects.filter(
Expand All @@ -56,13 +65,33 @@ def make_corrections(request, slug):
).delete()

if overall_feedback:
feedback_row, _ = OverallFeedback.available_objects.get_or_create(
feedback_row, feedback_created = OverallFeedback.available_objects.get_or_create(
post=post,
user=current_user,
)
feedback_row.comment = overall_feedback
feedback_row.save()

new_feedback_given |= feedback_created

if current_user not in previous_correctors:
if new_correction_made:
notify.send(
sender=current_user,
recipient=post.user,
verb=translate("corrected"),
action_object=post,
notification_type="new_correction",
)
elif new_feedback_given:
notify.send(
sender=current_user,
recipient=post.user,
verb=translate("commented on"),
action_object=post,
notification_type="new_reply",
)

return redirect(reverse("posts:detail", kwargs={"slug": post.slug}))
else:
all_post_rows = PostRow.available_objects.filter(post=post, is_actual=True).order_by("order")
Expand Down

0 comments on commit 54e9eed

Please sign in to comment.