def test_formset_valid(self): data = { 'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1, 'form-0-id': str(self.review.id), } formset = ReviewFormSet(data) self.assertTrue(formset.is_valid()) form = formset.forms[0] self.assertEqual(form.fields['decision'].initial, 'undecided')
def post(self, request): review_formset = ReviewFormSet(request.POST) view = request.GET.getlist('view', [''])[0] # if we are viewing approved or rejected, we can ignore the decision # unless it is the opposite change = False if review_formset.has_changed(): for form in review_formset: if form.is_valid(): if form.has_changed() and 'decision' in form.changed_data: review = form.save(commit=False) decision = form.cleaned_data.get('decision') if decision == 'approve' and view != 'approved': review.approve() messages.success( request, 'Review from user {} {} has been ' 'approved'.format(review.user.first_name, review.user.last_name)) change = True ActivityLog.objects.create( log="Testimonial{} (id {}) approved by {}". format(" update" if review.edited else "", review.id, request.user.username)) elif decision == 'reject' and view != 'rejected': review.reject() messages.success( request, 'Review from user {} {} has been ' 'rejected'.format(review.user.first_name, review.user.last_name)) change = True ActivityLog.objects.create( log="Testimonial{} (id {}) rejected " "by {}".format( " update" if review.edited else "", review.id, request.user.username)) review.save() review_formset.save() if not change: messages.info(request, 'No changes made') return HttpResponseRedirect(self.get_success_url())
def test_decision_initial_value(self): data = { 'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1, 'form-0-id': str(self.review.id), } formset = ReviewFormSet(data, previous='approved') self.assertTrue(formset.is_valid()) form = formset.forms[0] self.assertEqual(form.fields['decision'].initial, 'approve') formset = ReviewFormSet(data, previous='rejected') self.assertTrue(formset.is_valid()) form = formset.forms[0] self.assertEqual(form.fields['decision'].initial, 'reject')
def get_context_data(self, *args, **kwargs): context = super(StaffReviewListView, self).get_context_data() context['review_formset'] = ReviewFormSet(queryset=self.get_queryset(), previous=self.previous) context['showing_previous'] = self.previous return context