pedagogy: add script to remove all previous doubled comments

This commit is contained in:
2019-09-04 20:49:17 +02:00
parent ca042fe75e
commit a69f7b12b1
7 changed files with 215 additions and 124 deletions

View File

@ -1,7 +1,7 @@
# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
# Copyright 2019
# - Sli <antoine@bartuccio.fr>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
@ -116,12 +116,28 @@ class UVCommentForm(forms.ModelForm):
"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)
self.fields["author"].queryset = User.objects.filter(id=author_id).all()
self.fields["author"].initial = author_id
self.fields["uv"].queryset = UV.objects.filter(id=uv_id).all()
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):

View File

@ -164,6 +164,17 @@ class UV(models.Model):
def get_absolute_url(self):
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
def grade_global_average(self):
return self.__grade_average_generic("grade_global")

View File

@ -81,6 +81,11 @@
</div>
<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">
<h2>{% trans %}Leave comment{% endtrans %}</h2>
<div>
@ -134,6 +139,7 @@
</form>
</div>
</div>
{% endif %}
<br>
{% if object.comments.exists() %}

View File

@ -25,6 +25,7 @@
from django.conf import settings
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.core.management import call_command
from core.models import User, Notification
@ -421,6 +422,50 @@ class UVCommentCreationAndDisplay(TestCase):
)
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):
"""

View File

@ -1,6 +1,6 @@
# -*- coding:utf-8 -*
#
# Copyright 2017
# Copyright 2019
# - Sli <antoine@bartuccio.fr>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
@ -99,6 +99,7 @@ class UVDetailFormView(CanViewMixin, CanCreateUVFunctionMixin, DetailFormView):
kwargs = super(UVDetailFormView, self).get_form_kwargs()
kwargs["author_id"] = self.request.user.id
kwargs["uv_id"] = self.get_object().id
kwargs["is_creation"] = True
return kwargs
def form_valid(self, form):
@ -126,6 +127,7 @@ class UVCommentUpdateView(CanEditPropMixin, UpdateView):
obj = self.get_object()
kwargs["author_id"] = obj.author.id
kwargs["uv_id"] = obj.uv.id
kwargs["is_creation"] = False
return kwargs