def test_invalid_bulk_form(self): invalid_bulks = ['1:user:test.cpp', '1:usertestcpp 1:user:test.cpp'] mock_request = lambda: None mock_request.contest = None for bulk in invalid_bulks: form_data = {'similar_groups': bulk} form = BulkAddSubmissionsSimilarityForm(mock_request, data=form_data) self.assertFalse(form.is_valid())
def test_invalid_bulk_form(self): invalid_bulks = ['1:user:test.cpp', '1:usertestcpp 1:user:test.cpp'] mock_request = lambda: None mock_request.contest = None for bulk in invalid_bulks: form_data = {'similar_groups': bulk} form = BulkAddSubmissionsSimilarityForm( mock_request, data=form_data) self.assertFalse(form.is_valid())
def bulk_add_similarities_view(request): if request.method == 'POST': form = BulkAddSubmissionsSimilarityForm(request, request.POST) if form.is_valid(): groups = form.cleaned_data['similar_groups'] for group in groups: entries = SubmissionsSimilarityEntry.objects.filter( submission__in=group ).select_related('group') if entries.exists(): groups = set([entry.group for entry in entries]) group_model = groups.pop() # If group contains submissions from multiple existing # groups then merge all of them. for g in groups: for e in g.submissions.all(): e.group = group_model e.save() g.delete() else: group_model = SubmissionsSimilarityGroup(contest=request.contest) group_model.save() for submission in group: SubmissionsSimilarityEntry.objects.get_or_create( group=group_model, submission=submission ) messages.success( request, ungettext_lazy( "Created one group", "Created %(groups_count)d groups", len(groups) ) % {'groups_count': len(groups)}, ) return redirect( 'oioioiadmin:similarsubmits_' 'submissionssimilaritygroup_changelist' ) else: form = BulkAddSubmissionsSimilarityForm(request) return TemplateResponse(request, 'similarsubmits/bulk_add.html', {'form': form})
def bulk_add_similarities_view(request): if request.method == 'POST': form = BulkAddSubmissionsSimilarityForm(request, request.POST) if form.is_valid(): groups = form.cleaned_data['similar_groups'] for group in groups: entries = SubmissionsSimilarityEntry.objects.filter( submission__in=group).select_related('group') if entries.exists(): groups = set([entry.group for entry in entries]) group_model = next(iter(groups)) groups -= group_model # If group contains submissions from multiple existing # groups then merge all of them. for g in groups: for e in g.submissions: e.group = group_model g.delete() else: group_model = SubmissionsSimilarityGroup( contest=request.contest) group_model.save() for submission in group: SubmissionsSimilarityEntry.objects.get_or_create( group=group_model, submission=submission).save() messages.success(request, ungettext_lazy("Created one group", "Created %(groups_count)d groups", len(groups)) % {'groups_count': len(groups)}) return redirect( 'oioioiadmin:similarsubmits_' 'submissionssimilaritygroup_changelist') else: form = BulkAddSubmissionsSimilarityForm(request) return TemplateResponse(request, 'similarsubmits/bulk_add.html', {'form': form})