Commit defb7fb3 authored by Sli's avatar Sli

Merge branch 'pedagogy_v2' into 'master'

Disable having two comments from the same user for the same uv in uv guide

See merge request !229
parents ca042fe7 f30bea3d
Pipeline #2003 passed with stage
in 36 minutes and 19 seconds
...@@ -1867,6 +1867,13 @@ $pedagogy-white-text: #f0f0f0; ...@@ -1867,6 +1867,13 @@ $pedagogy-white-text: #f0f0f0;
grid-area: hours-the; grid-area: hours-the;
} }
#leave_comment_not_allowed {
p {
text-align: center;
color: red;
}
}
#leave_comment { #leave_comment {
.leave-comment-grid-container { .leave-comment-grid-container {
display: grid; display: grid;
......
This diff is collapsed.
# -*- coding:utf-8 -* # -*- coding:utf-8 -*
# #
# Copyright 2016,2017 # Copyright 2019
# - Skia <skia@libskia.so> # - Sli <antoine@bartuccio.fr>
# #
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, # Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr. # http://ae.utbm.fr.
...@@ -116,12 +116,28 @@ class UVCommentForm(forms.ModelForm): ...@@ -116,12 +116,28 @@ class UVCommentForm(forms.ModelForm):
"grade_work_load": StarList(5), "grade_work_load": StarList(5),
} }
def __init__(self, author_id, uv_id, *args, **kwargs): def __init__(self, author_id, uv_id, is_creation, *args, **kwargs):
super(UVCommentForm, self).__init__(*args, **kwargs) super(UVCommentForm, self).__init__(*args, **kwargs)
self.fields["author"].queryset = User.objects.filter(id=author_id).all() self.fields["author"].queryset = User.objects.filter(id=author_id).all()
self.fields["author"].initial = author_id self.fields["author"].initial = author_id
self.fields["uv"].queryset = UV.objects.filter(id=uv_id).all() self.fields["uv"].queryset = UV.objects.filter(id=uv_id).all()
self.fields["uv"].initial = uv_id self.fields["uv"].initial = uv_id
self.is_creation = is_creation
def clean(self):
self.cleaned_data = super(UVCommentForm, self).clean()
uv = self.cleaned_data.get("uv")
author = self.cleaned_data.get("author")
if self.is_creation and uv and author and uv.has_user_already_commented(author):
self.add_error(
None,
forms.ValidationError(
_("This user has already commented on this UV"), code="invalid"
),
)
return self.cleaned_data
class UVCommentReportForm(forms.ModelForm): class UVCommentReportForm(forms.ModelForm):
......
# -*- coding:utf-8 -*
#
# Copyright 2019
# - Sli <antoine@bartuccio.fr>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
from __future__ import unicode_literals
from django.db import migrations
from core.models import User
def remove_multiples_comments_from_same_user(apps, schema_editor):
for user in User.objects.exclude(uv_comments=None).prefetch_related("uv_comments"):
for uv in user.uv_comments.values("uv").distinct():
last = (
user.uv_comments.filter(uv__id=uv["uv"])
.order_by("-publish_date")
.first()
)
for comment in (
user.uv_comments.filter(uv__id=uv["uv"]).exclude(pk=last.pk).all()
):
print("removing : %s" % (comment,))
comment.delete()
class Migration(migrations.Migration):
dependencies = [("pedagogy", "0001_initial")]
operations = [
migrations.RunPython(
remove_multiples_comments_from_same_user,
reverse_code=migrations.RunPython.noop,
)
]
...@@ -164,6 +164,17 @@ class UV(models.Model): ...@@ -164,6 +164,17 @@ class UV(models.Model):
def get_absolute_url(self): def get_absolute_url(self):
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.id}) return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.id})
def has_user_already_commented(self, user):
"""
Help prevent multiples comments from the same user
This function checks that no other comment has been posted by a specified user
:param user: core.models.User
:return: if the user has already posted a comment on this UV
:rtype: bool
"""
return self.comments.filter(author=user).exists()
@cached_property @cached_property
def grade_global_average(self): def grade_global_average(self):
return self.__grade_average_generic("grade_global") return self.__grade_average_generic("grade_global")
......
...@@ -81,6 +81,11 @@ ...@@ -81,6 +81,11 @@
</div> </div>
<br> <br>
{% if object.has_user_already_commented(user) %}
<div id="leave_comment_not_allowed">
<p>{% trans %}You already posted a comment on this UV. If you want to comment again, please modify or delete your previous comment.{% endtrans %}</p>
</div>
{% else %}
<div id="leave_comment"> <div id="leave_comment">
<h2>{% trans %}Leave comment{% endtrans %}</h2> <h2>{% trans %}Leave comment{% endtrans %}</h2>
<div> <div>
...@@ -134,6 +139,7 @@ ...@@ -134,6 +139,7 @@
</form> </form>
</div> </div>
</div> </div>
{% endif %}
<br> <br>
{% if object.comments.exists() %} {% if object.comments.exists() %}
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.core.management import call_command from django.core.management import call_command
from core.models import User, Notification from core.models import User, Notification
...@@ -421,6 +422,50 @@ class UVCommentCreationAndDisplay(TestCase): ...@@ -421,6 +422,50 @@ class UVCommentCreationAndDisplay(TestCase):
) )
self.assertNotContains(response, text="Superbe UV") self.assertNotContains(response, text="Superbe UV")
def test_create_uv_comment_twice_fail(self):
# Checks that the has_user_already_commented method works proprely
self.assertFalse(self.uv.has_user_already_commented(self.bibou))
# Create a first comment
self.client.login(username="root", password="plop")
self.client.post(
reverse("pedagogy:uv_detail", kwargs={"uv_id": self.uv.id}),
create_uv_comment_template(self.bibou.id),
)
# Checks that the has_user_already_commented method works proprely
self.assertTrue(self.uv.has_user_already_commented(self.bibou))
# Create the second comment
comment = create_uv_comment_template(self.bibou.id)
comment["comment"] = "Twice"
response = self.client.post(
reverse("pedagogy:uv_detail", kwargs={"uv_id": self.uv.id}), comment
)
self.assertEquals(response.status_code, 200)
self.assertTrue(
UVComment.objects.filter(comment__contains="Superbe UV").exists()
)
self.assertFalse(UVComment.objects.filter(comment__contains="Twice").exists())
self.assertContains(
response,
_(
"You already posted a comment on this UV. If you want to comment again, please modify or delete your previous comment."
),
)
# Ensure that there is no crash when no uv or no author is given
self.client.post(
reverse("pedagogy:uv_detail", kwargs={"uv_id": self.uv.id}),
create_uv_comment_template(self.bibou.id, exclude_list=["uv"]),
)
self.assertEquals(response.status_code, 200)
self.client.post(
reverse("pedagogy:uv_detail", kwargs={"uv_id": self.uv.id}),
create_uv_comment_template(self.bibou.id, exclude_list=["author"]),
)
self.assertEquals(response.status_code, 200)
class UVCommentDeleteTest(TestCase): class UVCommentDeleteTest(TestCase):
""" """
......
# -*- coding:utf-8 -* # -*- coding:utf-8 -*
# #
# Copyright 2017 # Copyright 2019
# - Sli <antoine@bartuccio.fr> # - Sli <antoine@bartuccio.fr>
# #
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, # Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
...@@ -99,6 +99,7 @@ class UVDetailFormView(CanViewMixin, CanCreateUVFunctionMixin, DetailFormView): ...@@ -99,6 +99,7 @@ class UVDetailFormView(CanViewMixin, CanCreateUVFunctionMixin, DetailFormView):
kwargs = super(UVDetailFormView, self).get_form_kwargs() kwargs = super(UVDetailFormView, self).get_form_kwargs()
kwargs["author_id"] = self.request.user.id kwargs["author_id"] = self.request.user.id
kwargs["uv_id"] = self.get_object().id kwargs["uv_id"] = self.get_object().id
kwargs["is_creation"] = True
return kwargs return kwargs
def form_valid(self, form): def form_valid(self, form):
...@@ -126,6 +127,7 @@ class UVCommentUpdateView(CanEditPropMixin, UpdateView): ...@@ -126,6 +127,7 @@ class UVCommentUpdateView(CanEditPropMixin, UpdateView):
obj = self.get_object() obj = self.get_object()
kwargs["author_id"] = obj.author.id kwargs["author_id"] = obj.author.id
kwargs["uv_id"] = obj.uv.id kwargs["uv_id"] = obj.uv.id
kwargs["is_creation"] = False
return kwargs return kwargs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment