Skip to content

feat: Send notification for user comment #213

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

Merged
merged 17 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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
15 changes: 13 additions & 2 deletions lms/lmsdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def of_user(
if exercise.get('solution_id') is None:
exercise['solution_id'] = solution.id
exercise['is_checked'] = solution.is_checked
exercise['comments_num'] = len(solution.comments)
exercise['comments_num'] = len(solution.staff_comments)
if solution.is_checked and solution.checker:
exercise['checker'] = solution.checker.fullname
return tuple(exercises.values())
Expand All @@ -401,9 +401,20 @@ def comments(self):
SolutionFile,
).where(SolutionFile.solution == self)

@property
def ordered_comments(self):
return self.comments.order_by(Comment.timestamp.desc())

@property
def staff_comments(self):
return self.comments.switch(Comment).join(User).join(Role).where(
(Comment.commenter.role == Role.get_staff_role().id)
| (Comment.commenter.role == Role.get_admin_role().id),
)

@property
def comments_per_file(self):
return Counter(c.file.id for c in self.comments)
return Counter(c.file.id for c in self.staff_comments)

@classmethod
def create_solution(
Expand Down
38 changes: 22 additions & 16 deletions lms/lmsweb/translations/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: lmsweb-1.0\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2020-10-02 17:31+0300\n"
"POT-Creation-Date: 2020-10-04 10:37+0300\n"
"PO-Revision-Date: 2020-09-16 18:29+0300\n"
"Last-Translator: Or Ronai\n"
"Language: en\n"
Expand All @@ -18,7 +18,7 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.8.0\n"

#: lmsdb/models.py:616
#: lmsdb/models.py:631
msgid "כישלון חמור"
msgstr "Fatal error"

Expand Down Expand Up @@ -49,7 +49,17 @@ msgstr "Bro, did you check your code?"
msgid "מערכת הגשת התרגילים"
msgstr "Exercuse submission system"

#: models/solutions.py:19
#: models/solutions.py:46
#, python-format
msgid "%(solver)s הגיב לך על בדיקת תרגיל \"%(subject)s\"."
msgstr "%(solver)s has replied for your \"%(subject)s\" check."

#: models/solutions.py:53
#, python-format
msgid "%(checker)s הגיב לך על תרגיל \"%(subject)s\"."
msgstr "%(checker)s replied for \"%(subject)s\"."

#: models/solutions.py:65
#, python-format
msgid "הפתרון שלך לתרגיל \"%(subject)s\" נבדק."
msgstr "Your solution for the \"%(subject)s\" exercise has been checked."
Expand Down Expand Up @@ -110,7 +120,7 @@ msgstr "Username"
msgid "סיסמה"
msgstr "Password"

#: templates/login.html:23
#: templates/login.html:26
msgid "התחבר"
msgstr "Login"

Expand Down Expand Up @@ -274,38 +284,34 @@ msgstr "Presenter"
msgid "ניווט בגרסאות ההגשה"
msgstr "Navigate in solution versions"

#: templates/view.html:48
#: templates/view.html:35
msgid "לסיום בדיקה"
msgstr "Finish Checking"

#: templates/view.html:64
#: templates/view.html:75
msgid "בדיקות אוטומטיות"
msgstr "Automatic Checking"

#: templates/view.html:71
#: templates/view.html:82
msgid "שגיאה:"
msgstr "Error:"

#: templates/view.html:76
#: templates/view.html:87
msgid "שגיאת סגל:"
msgstr "Staff Error:"

#: templates/view.html:90
#: templates/view.html:101
msgid "הערות על התרגיל"
msgstr "Comments for the exercise"

#: templates/view.html:98
#: templates/view.html:109
msgid "הערות כלליות"
msgstr "General comments"

#: templates/view.html:106
#: templates/view.html:117
msgid "הערות בודק"
msgstr "Checker comments"

#: templates/view.html:116
#: templates/view.html:127
msgid "סיימתי לבדוק!"
msgstr "Done Checking!"

#: templates/view.html:127
msgid "הורד פתרון"
msgstr "Download Solution"
2 changes: 2 additions & 0 deletions lms/lmsweb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ def _create_comment(
# should never happend, kind was checked before
return fail(400, 'Invalid kind.')

solutions.notify_comment_after_check(user, file.solution)

comment_ = Comment.create(
commenter=user,
line_number=line_number,
Expand Down
1 change: 1 addition & 0 deletions lms/models/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class NotificationKind(enum.Enum):
CHECKED = 1
FLAKE8_ERROR = 2
UNITTEST_ERROR = 3
USER_RESPONSE = 4


def get(user: User) -> Iterable[Notification]:
Expand Down
48 changes: 47 additions & 1 deletion lms/models/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,59 @@

from flask_babel import gettext as _

from lms.lmsdb.models import Solution, SolutionFile
from lms.lmsdb.models import Solution, SolutionFile, User
from lms.lmstests.public.general import tasks as general_tasks
from lms.lmstests.public.identical_tests import tasks as identical_tests_tasks
from lms.lmsweb import config, routes
from lms.models import notifications


def notify_comment_after_check(user: User, solution: Solution) -> bool:
is_checked = solution.is_checked
if is_checked:
msg, addressee = get_message_and_addressee(user, solution)
if is_last_to_reply(user, solution):
notifications.send(
kind=notifications.NotificationKind.USER_RESPONSE,
user=addressee,
related_id=solution.id,
message=msg,
action_url=f'{routes.SOLUTIONS}/{solution.id}',
)
return True
return False


def is_last_to_reply(user: User, solution: Solution) -> bool:
return (
not solution.comments
or (
solution.comments
and solution.ordered_comments[0].commenter != user
)
)


def get_message_and_addressee(
user: User, solution: Solution,
) -> Tuple[str, User]:
if solution.solver == user:
msg = _(
'%(solver)s הגיב לך על בדיקת תרגיל "%(subject)s".',
solver=solution.solver.fullname,
subject=solution.exercise.subject,
)
addressee = solution.checker
else: # solution.checker == user
msg = _(
'%(checker)s הגיב לך על תרגיל "%(subject)s".',
checker=solution.checker.fullname,
subject=solution.exercise.subject,
)
addressee = solution.solver
return msg, addressee


def mark_as_checked(solution_id: int, checker_id: int) -> bool:
checked_solution: Solution = Solution.get_by_id(solution_id)
is_updated = checked_solution.mark_as_checked(by=checker_id)
Expand Down
1 change: 0 additions & 1 deletion lms/tests/test_html_escaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class TestHtmlEscaping:
def test_comment_text_escaping(student_user: User, solution: Solution):
client = conftest.get_logged_user(student_user.username)

# Creating a comment
comment_response = client.post('/comments', data=json.dumps(dict(
fileId=solution.files[0].id, act='create', kind='text',
comment=USER_COMMENT_BEFORE_ESCAPING, line=1,
Expand Down
54 changes: 53 additions & 1 deletion lms/tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import random
import string

from flask import json
import pytest # type: ignore

from lms.lmsdb.models import Exercise, Notification, Solution, User
from lms.models import notifications
from lms.models import notifications, solutions
from lms.tests import conftest


Expand Down Expand Up @@ -164,3 +165,54 @@ def test_auto_deletion(self, student_user: User):
actual = Notification.select().order_by(
Notification.created.desc()).get().id
assert expected == actual

@staticmethod
def test_user_commented_after_check(
solution: Solution,
student_user: User,
staff_user: User,
):
client = conftest.get_logged_user(student_user.username)

# Marking the solution as checked
solutions.mark_as_checked(solution.id, staff_user.id)
solution = Solution.get_by_id(solution.id)

# Sending comments after solution checked
comment_response = client.post('/comments', data=json.dumps(dict(
fileId=solution.files[0].id, act='create', kind='text',
comment='new one', line=1,
)), content_type='application/json')
new_comment_response = client.post('/comments', data=json.dumps(dict(
fileId=solution.files[0].id, act='create', kind='text',
comment='another one', line=2,
)), content_type='application/json')
assert comment_response.status_code == 200
assert new_comment_response.status_code == 200
assert len(list(notifications.get(staff_user))) == 1

conftest.logout_user(client)
client2 = conftest.get_logged_user(staff_user.username)

# Sending a comment after student user commented
staff_comment_response = client2.post(
'/comments', data=json.dumps(dict(
fileId=solution.files[0].id, act='create', kind='text',
comment='FINE', line=1,
)), content_type='application/json',
)
assert staff_comment_response.status_code == 200
assert len(list(notifications.get(student_user))) == 2

conftest.logout_user(client2)
client = conftest.get_logged_user(student_user.username)

# User student comments again
another_comment_response = client.post(
'/comments', data=json.dumps(dict(
fileId=solution.files[0].id, act='create', kind='text',
comment='OK', line=3,
)), content_type='application/json',
)
assert another_comment_response.status_code == 200
assert len(list(notifications.get(staff_user))) == 2