Esempio n. 1
0
def new(request, survey_id):
    batch = Batch(survey=Survey.objects.get(id=survey_id))
    response, batchform = _process_form(request=request, batchform=(BatchForm(instance=batch)), action_str='added')

    context = {'batchform': batchform, 'button_label': "Create", 'id': 'add-batch-form', 'title': 'New Batch',
               'action': '/surveys/%s/batches/new/' % survey_id, 'cancel_url': '/surveys/'}
    return response or render(request, 'batches/new.html', context)
Esempio n. 2
0
    def test_form_should_be_invalid_if_name_already_exists_on_the_same_survey(self):
        survey = Survey.objects.create(name="very fast")
        Batch.objects.create(survey=survey, name='Batch A', description='description')

        form_data = {
            'name': 'Batch A',
            'description': 'description goes here',
        }
        batch_form = BatchForm(data=form_data, instance=Batch(survey=survey))
        self.assertFalse(batch_form.is_valid())
Esempio n. 3
0
def index(request, survey_id):
    survey = Survey.objects.get(id=survey_id)
    batches = Batch.objects.filter(survey__id=survey_id)
    if request.is_ajax():
        batches = batches.values('id', 'name').order_by('name')
        json_dump = json.dumps(list(batches), cls=DjangoJSONEncoder)
        return HttpResponse(json_dump, mimetype='application/json')

    context = {'batches': batches, 'survey': survey,
               'request': request, 'batchform': BatchForm(instance=Batch(survey=survey)),
               'action': '/surveys/%s/batches/new/' % survey_id, }
    return render(request, 'batches/index.html',
                  context)
Esempio n. 4
0
    def test_form_should_be_valid_if_name_already_exists_on_a_different_survey(
            self):
        survey = Survey.objects.create(name="very fast")
        form_data = {
            'name': 'Batch A',
            'description': 'description goes here',
        }

        Batch.objects.create(survey=survey,
                             name=form_data['name'],
                             description='description')
        different_survey = Survey.objects.create(name="different")
        batch_form = BatchForm(data=form_data,
                               instance=Batch(survey=different_survey))
        self.assertTrue(batch_form.is_valid())