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 5 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
11 changes: 9 additions & 2 deletions lms/lmsdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,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 @@ -400,9 +400,16 @@ def comments(self):
SolutionFile,
).where(SolutionFile.solution == self)

@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
52 changes: 29 additions & 23 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-09-28 15:12+0300\n"
"POT-Creation-Date: 2020-10-03 17:26+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:627
msgid "כישלון חמור"
msgstr "Fatal error"

Expand All @@ -32,24 +32,34 @@ msgstr "Your solution for the %(subject)s exercise has been checked."
msgid "הבודק האוטומטי נתן %(errors_num)d הערות על תרגילך %(name)s."
msgstr "The automatic checker gave you %(errors_num)d for your %(name)s solution."

#: lmstests/public/unittests/services.py:83
#: lmstests/public/unittests/services.py:96
#, python-format
msgid "הבודק האוטומטי נכשל ב־ %(number)d דוגמאות בתרגיל \"%(subject)s\"."
msgstr "The automatic checker failed in %(number)d examples in your \"%(subject)s\" solution."

#: lmstests/public/unittests/services.py:112
msgid "הבודק האוטומטי לא הצליח להריץ את הקוד שלך."
msgstr "The automatic checker couldn't run your code."

#: lmstests/public/unittests/services.py:90
#: lmstests/public/unittests/services.py:119
msgid "אחי, בדקת את הקוד שלך?"
msgstr "Bro, did you check your code?"

#: lmstests/public/unittests/services.py:125
#, python-format
msgid "הבודק האוטומטי נכשל ב־ %(number)d דוגמאות בתרגיל \"%(subject)s\"."
msgstr "The automatic checker failed in %(number)d examples in your \"%(subject)s\" solution."

#: lmsweb/tools/registration.py:104
msgid "מערכת הגשת התרגילים"
msgstr "Exercuse submission system"

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

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

#: models/solutions.py:55
#, 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 @@ -266,38 +276,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 @@ -206,6 +206,8 @@ def _create_comment(
# should never happend, kind was checked before
return fail(400, 'Invalid kind.')

solutions.send_notification_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
39 changes: 38 additions & 1 deletion lms/models/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,50 @@

from flask_babel import gettext as _

from lms.lmsdb.models import Solution, SolutionFile
from lms.lmsdb.models import Comment, 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 send_notification_after_check(user: User, solution: Solution) -> bool:
is_checked = solution.is_checked
if is_checked:
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
if (
not solution.comments
or (
solution.comments
and not solution.comments.order_by(
Comment.timestamp.desc(),
)[0].commenter == user
)
):
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 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
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