Skip to content

Commit f4be512

Browse files
committed
group...feedbackfeed: Bugfix to handle case with uploading file, and removing it to add empty comment.
1 parent 0ba0b82 commit f4be512

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

devilry/devilry_group/tests/test_feedbackfeed/student/test_feedbackfeed_student.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.utils import timezone
1010
from cradmin_legacy import cradmin_testhelpers
1111
from model_bakery import baker
12+
from cradmin_legacy.apps.cradmin_temporaryfileuploadstore.models import TemporaryFileCollection
1213

1314
from devilry.apps.core import models as core_models
1415
from devilry.devilry_account.models import PeriodUserGuidelineAcceptance
@@ -1175,8 +1176,35 @@ def test_upload_single_file(self):
11751176
}
11761177
})
11771178
self.assertEqual(1, group_models.GroupComment.objects.count())
1179+
self.assertEqual(1, TemporaryFileCollection.objects.count())
11781180
self.assertEqual(1, comment_models.CommentFile.objects.count())
11791181

1182+
def test_upload_has_temporary_file_collection_id_but_empty_collection(self):
1183+
# Test that a CommentFile is created on upload.
1184+
feedbackset = group_baker.feedbackset_first_attempt_unpublished(
1185+
group__parentnode__parentnode=baker.make_recipe('devilry.apps.core.period_active'))
1186+
candidate = baker.make('core.Candidate', assignment_group=feedbackset.group)
1187+
temporary_filecollection = baker.make(
1188+
'cradmin_temporaryfileuploadstore.TemporaryFileCollection',
1189+
user=candidate.relatedstudent.user)
1190+
mockresponse = self.mock_http200_postrequest_htmls(
1191+
cradmin_role=candidate.assignment_group,
1192+
requestuser=candidate.relatedstudent.user,
1193+
viewkwargs={'pk': feedbackset.group.id},
1194+
requestkwargs={
1195+
'data': {
1196+
'text': '',
1197+
'student_add_comment': 'unused value',
1198+
'temporary_file_collection_id': temporary_filecollection.id
1199+
}
1200+
})
1201+
self.assertEqual(
1202+
'A comment must have either text or a file attached, or both. An empty comment is not allowed.',
1203+
mockresponse.selector.one('#error_1_id_text').alltext_normalized)
1204+
self.assertEqual(0, group_models.GroupComment.objects.count())
1205+
self.assertEqual(1, TemporaryFileCollection.objects.count())
1206+
self.assertEqual(1, group_models.FeedbackSet.objects.count())
1207+
11801208
def test_upload_single_file_content(self):
11811209
# Test the content of a CommentFile after upload.
11821210
feedbackset = group_baker.feedbackset_first_attempt_unpublished(

devilry/devilry_group/views/cradmin_feedbackfeed_base.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Meta:
5959
def __init__(self, *args, **kwargs):
6060
self.group = kwargs.pop('group')
6161
self.feedback_set = kwargs.pop('feedback_set')
62+
self.user = kwargs.pop('user')
6263
super(GroupCommentForm, self).__init__(*args, **kwargs)
6364
self.instance.feedback_set = self.feedback_set
6465

@@ -75,7 +76,16 @@ class StandardGroupCommentForm(GroupCommentForm):
7576
"""
7677
def clean(self):
7778
super(GroupCommentForm, self).clean()
78-
if len(self.cleaned_data['text']) == 0 and self.cleaned_data['temporary_file_collection_id'] is None:
79+
temporary_file_collection_id = self.cleaned_data['temporary_file_collection_id']
80+
temporary_file_collection = None
81+
if temporary_file_collection_id is not None:
82+
temporary_file_collection = TemporaryFileCollection.objects \
83+
.filter_for_user(self.user) \
84+
.get(id=temporary_file_collection_id)
85+
if temporary_file_collection.files.count() == 0:
86+
temporary_file_collection = None
87+
88+
if len(self.cleaned_data['text']) == 0 and temporary_file_collection is None:
7989
raise ValidationError({
8090
'text': gettext_lazy('A comment must have either text or a file attached, or both.'
8191
' An empty comment is not allowed.')
@@ -183,6 +193,7 @@ def get_form_kwargs(self):
183193
kwargs = super(FeedbackFeedBaseView, self).get_form_kwargs()
184194
group = self.assignment_group
185195
kwargs['group'] = group
196+
kwargs['user'] = self.request.user
186197
kwargs['feedback_set'] = group.cached_data.last_feedbackset
187198
return kwargs
188199

0 commit comments

Comments
 (0)