Skip to content

feat: Allow users to comment #193

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 9 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions lms/lmsdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ def _by_file(cls, file_id: int):
CommentText.id.alias('comment_id'), CommentText.text,
SolutionFile.id.alias('file_id'),
User.fullname.alias('author_name'),
User.role.alias('author_role'),
]
return (
cls
Expand Down
16 changes: 10 additions & 6 deletions lms/lmsweb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from werkzeug.utils import redirect

from lms.lmsdb.models import (
ALL_MODELS, Comment, CommentText, Exercise, RoleOptions,
ALL_MODELS, Comment, CommentText, Exercise, Role, RoleOptions,
SharedSolution, Solution, SolutionFile, User, database,
)
from lms.lmsweb import babel, routes, webapp
Expand Down Expand Up @@ -208,8 +208,8 @@ def _create_comment(

return jsonify({
'success': 'true', 'text': comment_.comment.text,
'author_name': user.fullname, 'is_auto': False, 'id': comment_.id,
'line_number': line_number,
'author_name': user.fullname, 'author_role': user.role.id,
'is_auto': False, 'id': comment_.id, 'line_number': line_number,
})


Expand Down Expand Up @@ -501,9 +501,13 @@ def _common_comments(exercise_id=None, user_id=None):
Most common comments throughout all exercises.
Filter by exercise id when specified.
"""
query = CommentText.filter(**{
CommentText.flake8_key.name: None,
}).select(CommentText.id, CommentText.text).join(Comment)
query = (
CommentText.select(CommentText.id, CommentText.text).join(Comment)
.join(User).join(Role).where(
CommentText.flake8_key.name is not None,
Comment.commenter.role > Role.get_student_role().id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

role doesn't have total ordering, and we don't wanna take the comments of banned users.

Check if role == administrator or role == staff

).switch(Comment)
)

if exercise_id is not None:
query = (
Expand Down
7 changes: 6 additions & 1 deletion lms/static/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ function isUserGrader() {
return ['staff', 'administrator'].includes(sessionStorage.getItem('role'));
}

function isSolverComment(commentData) {
return commentData.author_name === sessionStorage.getItem('solver');
}

function formatCommentData(commentData) {
let changedCommentText = `<span class="comment-author">${commentData.author_name}:</span> ${commentData.text}`;
if (isUserGrader()) {
if (isUserGrader() || isSolverComment(commentData)) {
const deleteButton = `<i class="fa fa-trash grader-delete" aria-hidden="true" data-commentid="${commentData.id}" onclick="deleteComment(${window.fileId}, ${commentData.id});"></i>`;
changedCommentText = `${deleteButton} ${changedCommentText}`;
}
Expand Down Expand Up @@ -139,6 +143,7 @@ window.addEventListener('load', () => {
window.solutionId = codeElement.id;
window.fileId = codeElement.file;
sessionStorage.setItem('role', codeElement.role);
sessionStorage.setItem('solver', codeElement.solver);
addLineSpansToPre(document.getElementsByTagName('code'));
pullComments(window.fileId, treatComments);
});
4 changes: 2 additions & 2 deletions lms/templates/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h1>{{ _('תצוגת תרגיל') }} {{ solution['exercise']['id'] }}: {{ soluti
</div>
<div id="view-body">
<div class="code-view-container">
<div id="code-view" data-id="{{ solution['id'] }}" data-file="{{ current_file.id }}" data-exercise="{{ solution['exercise']['id'] }}" data-role="{{ role }}">
<div id="code-view" data-id="{{ solution['id'] }}" data-file="{{ current_file.id }}" data-exercise="{{ solution['exercise']['id'] }}" data-role="{{ role }}" data-solver="{{ solution['solver']['fullname'] }}">
<pre><code id="user-code" class="language-{{ current_file | language_name }} line-numbers highlight">{{- current_file.code | trim(chars=' ') | e -}}</code></pre>
</div>
<div id="copy-code">
Expand Down Expand Up @@ -131,8 +131,8 @@ <h2>{{ _('הערות בודק') }}</h2>
<script src="{{ url_for('static', filename='prism.js') }}"></script>
{% if not shared_url %}
<script src="{{ url_for('static', filename='comments.js') }}"></script>
{%- if is_manager %}
<script src="{{ url_for('static', filename='grader.js') }}"></script>
{%- if is_manager %}
<script src="{{ url_for('static', filename='keyboard.js') }}"></script>
{% endif -%}
{% endif %}
Expand Down