def test_save_batch_updates_order(self): survey = mommy.make(Survey) batch1 = mommy.make(Batch, survey=survey, order=1) batch2 = Batch(survey=survey, name='Test batch', description='test desc') batch2.save() self.assertTrue(batch2.order > batch1.order)
def delete(request, survey_id, batch_id): try: batch = Batch.get(id=batch_id) QuestionSetView(model_class=Batch).delete(request, batch) except Exception as e: messages.warning(request, str(e)) return HttpResponseRedirect( reverse('batch_index_page', args=(batch.survey.id, )))
def update_parameter_list(batch_question): # check if this group has been previously assigned to this Question set. from survey.models import Batch if batch_question.group and RespondentGroup.objects.filter( questions__qset__id=batch_question.qset.id, id=batch_question.group.id).exists(): SurveyParameterList.update_parameter_list( Batch.get(pk=batch_question.qset.pk))
def list_all_questions(request): batch_id = request.GET.get('id', None) batch = Batch.get(pk=batch_id) # if request.is_ajax(): json_dump = json.dumps( [{'id': q.id, 'identifier': q.identifier} for q in batch.all_questions], cls=DjangoJSONEncoder) return HttpResponse(json_dump, content_type='application/json')
def edit(request, batch_id): batch = Batch.get(pk=batch_id) survey = batch.survey qset_view = QuestionSetView(model_class=Batch) qset_view.questionSetForm = BatchForm breadcrumbs = Batch.edit_breadcrumbs(survey=survey) cancel_url = '../' if breadcrumbs: request.breadcrumbs(breadcrumbs) cancel_url = breadcrumbs[-1][1] response = qset_view.edit( request, batch, extra_context={ 'cancel_url': cancel_url}, initial={ 'survey': survey.pk}) if response.status_code == 302: response = HttpResponseRedirect( reverse('batch_index_page', args=(survey.pk, ))) return response
def test_should_be_unique_together_batch_name_and_survey_id(self): survey = Survey.objects.create(name="very fast") batch_a = Batch.objects.create(survey=survey, name='Batch A', description='description') batch = Batch(survey=survey, name=batch_a.name, description='something else') self.assertRaises(IntegrityError, batch.save)
def edit(request, batch_id): batch = Batch.get(pk=batch_id) survey = batch.survey qset_view = QuestionSetView(model_class=Batch) qset_view.questionSetForm = BatchForm breadcrumbs = Batch.edit_breadcrumbs(survey=survey) cancel_url = '../' if breadcrumbs: request.breadcrumbs(breadcrumbs) cancel_url = breadcrumbs[-1][1] response = qset_view.edit(request, batch, extra_context={'cancel_url': cancel_url}, initial={'survey': survey.pk}) if response.status_code == 302: response = HttpResponseRedirect( reverse('batch_index_page', args=(survey.pk, ))) return response
def test_fields(self): batch = Batch() fields = [str(item.attname) for item in batch._meta.fields] self.assertEqual(8, len(fields)) for field in [ 'id', 'created', 'modified', 'order', 'name', 'description', 'survey_id', 'start_question_id' ]: self.assertIn(field, fields)
def list_all_questions(request): batch_id = request.GET.get('id', None) batch = Batch.get(pk=batch_id) # if request.is_ajax(): json_dump = json.dumps([{ 'id': q.id, 'identifier': q.identifier } for q in batch.all_questions], cls=DjangoJSONEncoder) return HttpResponse(json_dump, content_type='application/json')
def list_batch_questions(request): batch_id = request.GET.get('id', None) batch = Batch.get(pk=batch_id) if request.is_ajax(): json_dump = json.dumps([{ 'id': q.id, 'identifier': q.identifier } for q in batch.flow_questions], cls=DjangoJSONEncoder) return HttpResponse(json_dump, content_type='application/json') return HttpResponseRedirect( reverse('batch_index_page', args=(batch.survey.pk, )))
def new(request, survey_id): survey = Survey.objects.get(id=survey_id) qset_view = QuestionSetView(model_class=Batch) qset_view.questionSetForm = BatchForm request.breadcrumbs(Batch.new_breadcrumbs(survey=survey)) response = qset_view.new(request, extra_context={'survey': survey}, initial={'survey': survey.pk}) if response.status_code == 302: response = HttpResponseRedirect( reverse('batch_index_page', args=(survey.pk, ))) return response
def delete(request, survey_id, batch_id): try: batch = Batch.get(id=batch_id) QuestionSetView(model_class=Batch).delete(request, batch) except Exception as e: messages.warning(request, str(e)) return HttpResponseRedirect( reverse( 'batch_index_page', args=( batch.survey.id, )))
def _get_name_references(qset): name_references = {} for question in qset.questions.all(): name_references[question.identifier] = mark_safe('<output value="%s"/>' % get_node_path(question)) try: qset = Batch.get(pk=qset.pk) if hasattr(qset, 'parameter_list'): for question in qset.parameter_list.questions.all(): name_references[question.identifier] = \ mark_safe('<output value="%s"/>' % get_group_question_path(qset, question)) except Batch.DoesNotExist: pass return name_references
def new(request, survey_id): survey = Survey.objects.get(id=survey_id) qset_view = QuestionSetView(model_class=Batch) qset_view.questionSetForm = BatchForm request.breadcrumbs(Batch.new_breadcrumbs(survey=survey)) response = qset_view.new( request, extra_context={ 'survey': survey}, initial={ 'survey': survey.pk}) if response.status_code == 302: response = HttpResponseRedirect( reverse('batch_index_page', args=(survey.pk, ))) return response
def list_batch_questions(request): batch_id = request.GET.get('id', None) batch = Batch.get(pk=batch_id) if request.is_ajax(): json_dump = json.dumps( [{'id': q.id, 'identifier': q.identifier} for q in batch.flow_questions], cls=DjangoJSONEncoder) return HttpResponse(json_dump, content_type='application/json') return HttpResponseRedirect( reverse( 'batch_index_page', args=( batch.survey.pk, )))
def index(request, survey_id=None): if survey_id: survey = Survey.get(pk=survey_id) request.breadcrumbs(Batch.index_breadcrumbs(survey=survey)) if survey_id is None: batches = Batch.objects.all() else: batches = Batch.objects.filter(survey__id=survey_id) qset_view = QuestionSetView(model_class=Batch) qset_view.questionSetForm = BatchForm return qset_view.index(request, batches, extra_context={'survey': survey}, initial={'survey': survey.pk})
def index(request, survey_id=None): if survey_id: survey = Survey.get(pk=survey_id) request.breadcrumbs(Batch.index_breadcrumbs(survey=survey)) if survey_id is None: batches = Batch.objects.all() else: batches = Batch.objects.filter(survey__id=survey_id) qset_view = QuestionSetView(model_class=Batch) qset_view.questionSetForm = BatchForm return qset_view.index( request, batches, extra_context={ 'survey': survey}, initial={ 'survey': survey.pk})
def show(request, survey_id, batch_id): batch = Batch.objects.get(id=batch_id) open_status_filter = BatchOpenStatusFilterForm(batch, request.GET) batch_location_ids = batch.open_locations.values_list( 'location_id', flat=True) locations = open_status_filter.get_locations() search_fields = ['name', ] if 'q' in request.GET: locations = get_filterset(locations, request.GET['q'], search_fields) request.breadcrumbs(Batch.edit_breadcrumbs(survey=batch.survey)) open_locations = locations.filter(id__in=batch_location_ids) non_response_active_locations = batch.get_non_response_active_locations() context = { 'batch': batch, 'locations': locations, 'open_locations': open_locations, 'open_status_filter': open_status_filter, 'non_response_active_locations': non_response_active_locations } return render(request, 'batches/show.html', context)
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') request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), ]) can_delete = survey.survey_house_listings.exists( ) is False #if there has been no household listing yet context = { 'batches': batches, 'survey': survey, 'request': request, 'batchform': BatchForm(instance=Batch(survey=survey)), 'action': '/surveys/%s/batches/new/' % survey_id, 'can_delete': can_delete } return render(request, 'batches/index.html', context)
def show(request, survey_id, batch_id): batch = Batch.objects.get(id=batch_id) open_status_filter = BatchOpenStatusFilterForm(batch, request.GET) batch_location_ids = batch.open_locations.values_list('location_id', flat=True) locations = open_status_filter.get_locations() search_fields = [ 'name', ] if 'q' in request.GET: locations = get_filterset(locations, request.GET['q'], search_fields) request.breadcrumbs(Batch.edit_breadcrumbs(survey=batch.survey)) open_locations = locations.filter(id__in=batch_location_ids) non_response_active_locations = batch.get_non_response_active_locations() context = { 'batch': batch, 'locations': locations, 'open_locations': open_locations, 'open_status_filter': open_status_filter, 'non_response_active_locations': non_response_active_locations } return render(request, 'batches/show.html', context)
url( r'^surveys/(?P<survey_id>\d+)/survey_batches/$', 'survey.views.batch.batches', name='survey_batches_page'), url( r'^surveys/(?P<survey_id>\d+)/batches/new/$', 'survey.views.batch.new', name='new_batch_page'), url( r'^surveys/(?P<survey_id>\d+)/batches/(?P<batch_id>\d+)/$', 'survey.views.batch.show', name='batch_show_page'), url( r'^surveys/batches/$', 'survey.views.batch.index', name='%s_home' % Batch.resolve_tag()), url( r'^surveys/batches/(?P<batch_id>\d+)/edit/$', 'survey.views.batch.edit', name='edit_%s_page' % Batch.resolve_tag()), url( r'^surveys/interviewers_completion/$', 'survey.views.survey_completion.show_interviewer_completion_summary', name='show_interviewer_completion_summary'), url( r'^survey/(?P<survey_id>\d+)/completion/json/$', 'survey.views.survey_completion.completion_json', name='survey_completion_json'), url( r'^survey/completion/json/$', 'survey.views.survey_completion.json_summary',
name='delete_survey'), url(r'^surveys/(?P<survey_id>\d+)/batches/$', 'survey.views.batch.index', name='batch_index_page'), url(r'^surveys/(?P<survey_id>\d+)/survey_batches/$', 'survey.views.batch.batches', name='survey_batches_page'), url(r'^surveys/(?P<survey_id>\d+)/batches/new/$', 'survey.views.batch.new', name='new_batch_page'), url(r'^surveys/(?P<survey_id>\d+)/batches/(?P<batch_id>\d+)/$', 'survey.views.batch.show', name='batch_show_page'), url(r'^surveys/batches/$', 'survey.views.batch.index', name='%s_home' % Batch.resolve_tag()), url(r'^surveys/batches/(?P<batch_id>\d+)/edit/$', 'survey.views.batch.edit', name='edit_%s_page' % Batch.resolve_tag()), url(r'^surveys/interviewers_completion/$', 'survey.views.survey_completion.show_interviewer_completion_summary', name='show_interviewer_completion_summary'), url(r'^survey/(?P<survey_id>\d+)/completion/json/$', 'survey.views.survey_completion.completion_json', name='survey_completion_json'), url(r'^survey/completion/json/$', 'survey.views.survey_completion.json_summary', name='survey_json_summary'), url(r'^survey/survey_indicators/json/$', 'survey.views.survey_completion.indicators_json', name='survey_indicators'),
def set_currently_open_batch(self): if self.household: self.currently_open_batch = Batch.currently_open_for( self.household.location)
def set_currently_open_batch(self): if self.household: self.currently_open_batch = Batch.currently_open_for(self.household.location)