def test_survey_instances_from_survey(self): o=Organization.objects.get(id=1) o.save() i=Instrument.objects.get(id=1) i.save() survey_settings = survey_logic.configure_survey_setting(o, i, is_active=True) survey = survey_logic.create_survey(o, [i, ]) #pre-check survey_inst_list = SurveyInstance.objects.all() self.assertEqual(len(survey_inst_list), 0) self.assertEqual(survey.n_invited, 0) sii_list = SurveyInstanceItem.objects.all() self.assertEqual(len(sii_list), 0) #Make surveyinstances survey_logic.survey_instances_from_survey(survey) #Check that it worked as expected survey_inst_list = SurveyInstance.objects.all() self.assertEqual(len(survey_inst_list), 1) self.assertEqual(survey.n_invited, 1) sii_list = SurveyInstanceItem.objects.all() self.assertEqual(len(sii_list), 6) #Make surveyinstances AGAIN survey_logic.survey_instances_from_survey(survey) #Check that it worked as expected survey_inst_list = SurveyInstance.objects.all() self.assertEqual(len(survey_inst_list), 1) sii_list = SurveyInstanceItem.objects.all() self.assertEqual(len(sii_list), 6) #add another employee r2 = Respondent( organization=o, first_name=None, last_name=None, email="*****@*****.**", receives_surveys=True ) r2.save() survey_logic.survey_instances_from_survey(survey) #Check that it worked as expected survey_inst_list = SurveyInstance.objects.all() self.assertEqual(len(survey_inst_list), 2) self.assertEqual(survey.n_invited, 2) sii_list = SurveyInstanceItem.objects.all() self.assertEqual(len(sii_list), 12) instrument_item_formulations = [item.formulation for item in i.get_items()] for sii in SurveyInstance.objects.get(id=1).get_items(): self.assertIn(sii.survey_item.item_formulation, instrument_item_formulations)
def test_configure_survey_setting(self): o=Organization.objects.get(id=1) o.save() i=Instrument.objects.get(id=1) i.save() #Test: try to configure instrument for organization ss = survey_logic.configure_survey_setting(o, i) ss2 = SurveySetting.objects.get(id=1) #check that it worked self.assertEqual(ss, ss2) self.assertEqual(ss.instrument, i) self.assertEqual(ss.organization, o) self.assertEqual(ss.is_active, False) self.assertEqual(ss.survey_interval, 90) self.assertEqual(ss.surveys_remain_open_days, 7) self.assertEqual(ss.last_survey_open, None) self.assertEqual(ss.last_survey_close, None) #Do it again with **kwargs ss3 = survey_logic.configure_survey_setting( o, i, is_active=True, survey_interval=180, surveys_remain_open_days=15 ) #check that it worked ss4 = SurveySetting.objects.get(id=1) self.assertEqual(ss3, ss4) self.assertEqual(ss3.instrument, i) self.assertEqual(ss3.organization, o) self.assertEqual(ss3.is_active, True) self.assertEqual(ss3.survey_interval, 180) self.assertEqual(ss3.surveys_remain_open_days, 15) #check that we cannot make a new SS for the same org and i def create_bad_ss(): bad_ss = SurveySetting(organization=o, instrument=i) bad_ss.save() self.assertRaises(IntegrityError, create_bad_ss)
def test_create_survey(self): o=Organization.objects.get(id=1) o.save() i=Instrument.objects.get(id=1) i.save() ss = survey_logic.configure_survey_setting(o, i, is_active=True) #pre-check self.assertEqual(ss.last_survey_open, None) self.assertEqual(ss.last_survey_close, None) self.assertEqual(len(ss.surveys.all()), 0) #Make a survey survey = survey_logic.create_survey(o, [i, ]) #Check that it worked as expected surveys = Survey.objects.all() self.assertEqual(len(surveys), 1) self.assertEqual(survey.owner, o) self.assertEqual(survey.date_open, datetime.date.today()) self.assertEqual(survey.date_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) self.assertEqual(survey.n_invited, 0) self.assertEqual(survey.n_completed, 0) self.assertEqual(survey.n_incomplete, 0) self.assertEqual(survey.is_closed, False) surveyitems = SurveyItem.objects.all() self.assertEqual(len(surveyitems), 6) si_formulation_list = [] for si in surveyitems: si_formulation_list.append(si.item_formulation) for item in i.get_items(): self.assertIn(item.formulation, si_formulation_list) ss = SurveySetting.objects.get(id=1) self.assertEqual(ss.last_survey_open, datetime.date.today()) self.assertEqual(ss.last_survey_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) self.assertEqual(len(ss.surveys.all()), 1) rsdr_list = RatioScaleDimensionResult.objects.all() dimension_list = Dimension.objects.all() for rsdr in rsdr_list: self.assertEqual(rsdr.survey, survey) self.assertIsInstance(rsdr.dimension, Dimension) self.assertEqual(len(dimension_list), len(rsdr_list))
def setUp(self): rti = create_test_data(1) setup_instrument.setup_instrument(rti) i=Instrument.objects.get(id=1) u = User (username="******", email="*****@*****.**", password="******") u.save() o = Organization( owner=u, name="TestOrg", phone=None, #active_products = models.ManyToManyField(Product, blank=True, help_text='Products this organization is currently using') address_line_1="Test st. 77", address_line_2=None, zip_code="7777", city="Test Town", country='NO' ) o.save() ss = survey_logic.configure_survey_setting(organization=o, instrument=i, is_active=True) ss.save()
def setUp(self): u = User(username="******", email="*****@*****.**", password="******") u.save() o = Organization(owner=u, name="TestOrg", phone=None, address_line_1="Test st. 77", address_line_2=None, zip_code="7777", city="Test Town", country='NO') o.save() r = Respondent(organization=o, first_name=None, last_name=None, email="*****@*****.**", receives_surveys=True) r.save() rti = create_test_data(1) setup_instrument.setup_instrument(rti) i = Instrument.objects.get(id=1) #configure surveys survey_setting = survey_logic.configure_survey_setting(organization=o, instrument=i, is_active=True) #create a survey and instance s = survey_logic.create_survey(owner=o, instrument_list=[ i, ]) survey_instance_list = survey_logic.survey_instances_from_survey(s) for survey_instance in survey_instance_list: survey_instance.consent_was_given = True survey_instance.save()
def test_answer_item(self): o=Organization.objects.get(id=1) o.save() i=Instrument.objects.get(id=1) i.save() survey_settings = survey_logic.configure_survey_setting(o, i, is_active=True) survey = survey_logic.create_survey(o, [i, ]) r2 = Respondent( organization=o, first_name=None, last_name=None, email="*****@*****.**", receives_surveys=True ) r2.save() #Make surveyinstances survey_logic.survey_instances_from_survey(survey) #Answer items survey_instances = SurveyInstance.objects.all() for si in survey_instances: items = si.get_items() for i in items: self.assertEqual(i.answer, None) self.assertEqual(i.answered, False) i.answer_item(4) #check that it worked for si in survey_instances: items = si.get_items() for i in items: self.assertEqual(i.answer, 4) self.assertEqual(i.answered, True)
def test_dashboard_view(self): user = User.objects.get(id=1) organization = Organization.objects.get(id=1) employee = Respondent.objects.get(id=1) instrument = Instrument.objects.get(id=1) survey = Survey.objects.get(id=1) #test that login is required self.assertEqual(len(Respondent.objects.all()), 1) response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertRedirects( response, reverse('loginc') + "?next=" + reverse('surveys-dashboard'), 302, 200) #test that we can see the dashboard self.client.force_login(user=user) response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'dashboard.html') #test if we get the correct context self.assertEqual(response.context['todays_date'], datetime.date.today()) self.assertEqual(response.context['employee_count'], 1) self.assertEqual(len(response.context['employee_list']), 1) self.assertEqual(response.context['employee_list'][0], employee) self.assertEqual(response.context['subscription_paid'], False) self.assertEqual(response.context['inactive_instrument_list'], []) self.assertEqual( response.context['active_instrument_data'][0]['instrument'], instrument) self.assertEqual( response.context['active_instrument_data'][0]['closed_surveys'], None) self.assertEqual( response.context['active_instrument_data'][0]['open_survey'], survey) #Test if we can get some content in inactive instrument_list survey_setting = survey_logic.configure_survey_setting( organization=organization, instrument=instrument, is_active=False) response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertEqual(response.context['inactive_instrument_list'], [instrument]) self.assertEqual(response.context['active_instrument_data'], None) #Test if we can get some data from previous surveys survey_setting = survey_logic.configure_survey_setting( organization=organization, instrument=instrument, is_active=True) #Move date_open and date_close back in time, close the survey survey.date_open = datetime.date.today() + datetime.timedelta( days=-100) survey.date_close = datetime.date.today() + datetime.timedelta( days=-93) survey.save() survey_setting.last_survey_open = datetime.date.today( ) + datetime.timedelta(days=-100) survey_setting.last_survey_close = datetime.date.today( ) + datetime.timedelta(days=-93) survey_setting.save() survey_logic.close_survey(survey) response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'dashboard.html') #test that we get the expected data self.assertEqual( response.context['active_instrument_data'][0]['instrument'], instrument) self.assertEqual( response.context['active_instrument_data'][0]['open_survey'], None) self.assertEqual( len(response.context['active_instrument_data'][0] ['closed_surveys']), 1) self.assertIn( 'instrument', response.context['active_instrument_data'][0]['closed_surveys'][0]) self.assertIn( 'survey', response.context['active_instrument_data'][0]['closed_surveys'][0]) self.assertIn( 'dimension_results', response.context['active_instrument_data'][0]['closed_surveys'][0]) self.assertIn( 'item_results', response.context['active_instrument_data'][0]['closed_surveys'][0]) self.assertEqual( response.context['active_instrument_data'][0]['closed_surveys'][0] ['instrument'], instrument) self.assertEqual( response.context['active_instrument_data'][0]['closed_surveys'][0] ['survey'], survey) self.assertEqual( len(response.context['active_instrument_data'][0]['closed_surveys'] [0]['dimension_results']), 3) self.assertEqual( len(response.context['active_instrument_data'][0]['closed_surveys'] [0]['item_results']), 6) #test that we are stopped due to not paid subscription self.assertIn( "To get started with our employee engagement software, you first need to pick a plan", response.content.decode()) self.assertNotIn("Latest measurements", response.content.decode()) self.assertNotIn( "<a href=\"/surveys/survey-details/MQ/employee_engagement/#vigor\">Vigor</a>\n", response.content.decode()) self.assertNotIn("<div>Employee Engagement tracking:</div>", response.content.decode()) self.assertNotIn("<h3 class=\"card-title\">Co-workers at TestOrg</h3>", response.content.decode()) #test that we move past block if organization has_free_access organization.has_free_access = True organization.save() response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'dashboard.html') self.assertNotIn( "To get started with our employee engagement software, you first need to pick a plan", response.content.decode()) self.assertIn("Latest measurements", response.content.decode()) self.assertIn( "/surveys/survey-details/MQ/employee_engagement/#vigor\">Vigor</a>\n", response.content.decode()) self.assertIn("<div>Employee Engagement tracking:</div>", response.content.decode()) self.assertIn("<h3 class=\"card-title\">Co-workers at TestOrg</h3>", response.content.decode())
def test_dailysurveytasks(self): logging.disable(logging.CRITICAL) #some setup call_command('setupinstruments') instrument = Instrument.objects.get(id=1) user_list = User.objects.all() organization_list = Organization.objects.all() for organization in organization_list: survey_setting = survey_logic.configure_survey_setting( organization=organization, instrument=instrument, is_active=False) survey_setting_list = SurveySetting.objects.all() respondent_list = Respondent.objects.all() self.assertEqual(len(user_list), 15) self.assertEqual(len(organization_list), 15) self.assertEqual(len(respondent_list), 150) self.assertEqual(len(survey_setting_list), 15) #test that no surveys are sent when no organization has set an active product survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 0) si_list = SurveyInstance.objects.all() self.assertEqual(len(si_list), 0) call_command('dailysurveytasks') survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 0) si_list = SurveyInstance.objects.all() self.assertEqual(len(si_list), 0) self.assertEqual(len(RespondentEmail.objects.all()), 0) self.assertEqual(len(mail.outbox), 0) #test that a single survey is create with 10 instances when we change the setting of one org, and that 10 emails are sent active_org = Organization.objects.get(id=1) survey_setting = survey_logic.configure_survey_setting( organization=active_org, instrument=instrument, is_active=True) call_command('dailysurveytasks') survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 1) si_list = SurveyInstance.objects.all() self.assertEqual(len(si_list), 10) self.assertEqual(len(RespondentEmail.objects.all()), 10) self.assertEqual(len(mail.outbox), 10) #Test that it works when we activate two more orgs and check active_org = Organization.objects.get(id=2) survey_setting = survey_logic.configure_survey_setting( organization=active_org, instrument=instrument, is_active=True) active_org = Organization.objects.get(id=3) survey_setting = survey_logic.configure_survey_setting( organization=active_org, instrument=instrument, is_active=True) call_command('dailysurveytasks') survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 3) si_list = SurveyInstance.objects.all() self.assertEqual(len(si_list), 30) self.assertEqual(len(RespondentEmail.objects.all()), 30) self.assertEqual(len(mail.outbox), 30) #test that due surveys are closed due_organization = Organization.objects.get(id=1) due_survey = Survey.objects.get(owner=due_organization) due_survey.date_close += datetime.timedelta(days=-360) due_survey.date_open += datetime.timedelta(days=-360) due_survey.save() due_survey_setting = SurveySetting.objects.get( organization=due_organization) due_survey_setting = survey_logic.configure_survey_setting( organization=due_organization, instrument=instrument, is_active=True) self.assertEqual(due_survey_setting.last_survey_close, due_survey.date_close) self.assertEqual(due_survey_setting.last_survey_open, due_survey.date_open) self.assertEqual(len(Survey.objects.filter(is_closed=True)), 0) call_command('dailysurveytasks') self.assertEqual(len(Survey.objects.filter(is_closed=True)), 1)
def test_create_survey_if_due(self): o=Organization.objects.get(id=1) i=Instrument.objects.get(id=1) ss=SurveySetting.objects.get(id=1) #test all is calm to set things off survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 0) self.assertEqual(ss.last_survey_open, None) self.assertEqual(ss.last_survey_close, None) self.assertEqual(len(ss.surveys.all()), 0) #run it once, creating a survey survey = survey_logic.create_survey_if_due(organization=o) #Test that it worked, creating 1 survey survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 1) self.assertIsInstance(survey, Survey) self.assertEqual(survey.date_open, datetime.date.today()) self.assertEqual(survey.date_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) ss=SurveySetting.objects.get(id=1) self.assertEqual(ss.last_survey_open, datetime.date.today()) self.assertEqual(ss.last_survey_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) self.assertEqual(len(ss.surveys.all()), 1) #run it again, NOT creating a survey survey2 = survey_logic.create_survey_if_due(organization=o) survey3 = survey_logic.create_survey_if_due(organization=o) survey4 = survey_logic.create_survey_if_due(organization=o) survey5 = survey_logic.create_survey_if_due(organization=o) #Test that it didnt create more surveys self.assertIsInstance(survey, Survey) self.assertEqual(survey2, None) self.assertEqual(survey3, None) self.assertEqual(survey4, None) self.assertEqual(survey5, None) survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 1) #Move date_open and _close back in time so it a survey becomes due again survey.date_open = datetime.date.today()+datetime.timedelta(days=-100) survey.date_close = datetime.date.today()+datetime.timedelta(days=-93) survey.save() ss.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss.save() survey_logic.close_survey(survey) #run it once, creating a new survey survey6 = survey_logic.create_survey_if_due(organization=o) #Test that it worked, creating 1 additional survey self.assertIsInstance(survey6, Survey) survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 2) #add another instrument rti = create_test_data(2) rti['instrument']['name'] = "Another instrument" rti['instrument']['slug_name'] = "another_instrument" rti['instrument']['name_nb'] = "Another instrument" rti['instrument']['slug_name_nb'] = "another_instrument" rti['scales'][0]['name'] = "Another Scale" setup_instrument.setup_instrument(rti) i2=Instrument.objects.get(id=2) #run it once, ensure no new Surveys are made survey7 = survey_logic.create_survey_if_due(organization=o) survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 2) #Move date_open and _close back in time so it a survey becomes due again survey6.date_open = datetime.date.today()+datetime.timedelta(days=-100) survey6.date_close = datetime.date.today()+datetime.timedelta(days=-93) survey6.save() ss.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss.save() survey_logic.close_survey(survey6) #configure new instrument ss2 = survey_logic.configure_survey_setting(organization=o, instrument=i2, is_active=True) ss2.save() #run it once, 1 new survey should be made survey8 = survey_logic.create_survey_if_due(organization=o) #check that 1 was made self.assertIsInstance(survey8, Survey) survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 3) #check that both instruments were included dr_list8 = survey8.dimensionresult_set.all() self.assertEqual(len(dr_list8), 6) #while in the old one, only 1 instrument dr_list6 = survey6.dimensionresult_set.all() self.assertEqual(len(dr_list6), 3) #Move date_open and _close back in time so it a survey becomes due again survey8.date_open = datetime.date.today()+datetime.timedelta(days=-100) survey8.date_close = datetime.date.today()+datetime.timedelta(days=-93) survey8.save() ss.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss.save() ss2.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss2.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss2.save() survey_logic.close_survey(survey8) #deactive the first product ss = survey_logic.configure_survey_setting(organization=o, instrument=i, is_active=False) ss.save() #surveys are due again survey9 = survey_logic.create_survey_if_due(organization=o) #only one instrument should be included dr_list9 = survey9.dimensionresult_set.all() self.assertEqual(len(dr_list9), 3) #Move date_open and _close back in time so it becomes not due, but still in the timewindow to be included with anopther instrument survey9.date_open = datetime.date.today()+datetime.timedelta(days=-75) survey9.date_close = datetime.date.today()+datetime.timedelta(days=-70) survey9.save() ss2.last_survey_open = datetime.date.today()+datetime.timedelta(days=-75) ss2.last_survey_close = datetime.date.today()+datetime.timedelta(days=-70) ss2.save() survey_logic.close_survey(survey9) #Run it again, nothing happens... it not due... survey10 = survey_logic.create_survey_if_due(organization=o) self.assertEqual(survey10, None) #Activate first product again ss = survey_logic.configure_survey_setting(organization=o, instrument=i, is_active=True) ss.save() #check that everything is in order befoe the big finale self.assertEqual(ss.last_survey_open, datetime.date.today()+datetime.timedelta(days=-100)) #is due self.assertEqual(ss.last_survey_close, datetime.date.today()+datetime.timedelta(days=-93)) self.assertEqual(ss2.last_survey_open, datetime.date.today()+datetime.timedelta(days=-75)) #is not due, but close enough self.assertEqual(ss2.last_survey_close, datetime.date.today()+datetime.timedelta(days=-70)) #Run it again, both should be included, product 1 because it's so lojng ago, and product 2 it get's dragged in because it's due within 60 days and 1/3 of 90 days survey11 = survey_logic.create_survey_if_due(organization=o) #Both should be included dr_list11 = survey11.dimensionresult_set.all() self.assertEqual(len(dr_list11), 6) #and dates are now aligned so that in the future, they fire at the same time ss = survey_logic.configure_survey_setting(organization=o, instrument=i) self.assertEqual(ss.last_survey_open, datetime.date.today()) self.assertEqual(ss.last_survey_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) ss2 = survey_logic.configure_survey_setting(organization=o, instrument=i2) self.assertEqual(ss2.last_survey_open, datetime.date.today()) self.assertEqual(ss2.last_survey_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) #Move date_open and _close back in time so it a survey becomes due again survey11.date_open = datetime.date.today()+datetime.timedelta(days=-100) survey11.date_close = datetime.date.today()+datetime.timedelta(days=-93) survey11.save() ss.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss.save() ss2.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss2.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss2.save() survey_logic.close_survey(survey11)
def test_close_survey_w_all_filled(self): o=Organization.objects.get(id=1) o.save() i=Instrument.objects.get(id=1) i.save() survey_settings = survey_logic.configure_survey_setting(o, i, is_active=True) survey = survey_logic.create_survey(o, [i, ]) r2 = Respondent( organization=o, first_name=None, last_name=None, email="*****@*****.**", receives_surveys=True ) r2.save() #answer survey_logic.survey_instances_from_survey(survey) survey_instances = SurveyInstance.objects.all() for si in survey_instances: self.assertEqual(si.started, False) self.assertEqual(si.completed, False) items = si.get_items() for i in items: self.assertEqual(i.answer, None) self.assertEqual(i.answered, False) i.answer_item(3) #check that it worked survey_instances = SurveyInstance.objects.all() for si in survey_instances: #self.assertEqual(si.started, True) # -> removed the code that marks survey as started while answering questions, this is now handled by view self.assertEqual(si.completed, False) items = si.get_items() for i in items: self.assertEqual(i.answer, 3) self.assertEqual(i.answered, True) #close survey survey = survey_logic.close_survey(survey) #check that the survey was closed self.assertEqual(survey.date_open, datetime.date.today()) self.assertEqual(survey.date_close, datetime.date.today()+datetime.timedelta(days=-1)) self.assertEqual(survey.n_invited, 2) self.assertEqual(survey.n_completed, 2) self.assertEqual(survey.n_incomplete, 0) self.assertEqual(survey.n_not_started, 0) self.assertEqual(survey.is_closed, True) #check thaty the dimensions were closed i = Instrument.objects.get(id=1) dimension_list=[d for d in i.dimension_set.all()] dr_list = survey.dimensionresult_set.all() for dr in dr_list: self.assertIsInstance(dr, RatioScaleDimensionResult) self.assertEqual(dr.survey, survey) self.assertIn(dr.dimension, dimension_list) self.assertEqual(dr.n_completed, 2) self.assertEqual(dr.average, 3) #check that the items were closed si_list = survey.surveyitem_set.all() for si in si_list: self.assertEqual(si.n_answered, 2) self.assertEqual(si.average, 3)
def setup_instrument_view(request, **kwargs): #get the Instrument to be configured instrument_slug_name = kwargs.get('instrument', None) instrument = get_object_or_404(Instrument, slug_name=instrument_slug_name) #get the SurveySetting to be configured try: survey_setting = SurveySetting.objects.get( organization=request.user.organization, instrument=instrument) except SurveySetting.DoesNotExist as err: survey_setting = survey_logic.configure_survey_setting( organization=request.user.organization, instrument=instrument) survey_setting.save() #prepare a form in case it's a get request data = { 'is_active': survey_setting.is_active, 'survey_interval': survey_setting.survey_interval, 'surveys_remain_open_days': survey_setting.surveys_remain_open_days, 'survey_language_preference': request.user.organization.survey_language_preference, } form = EditSurveySettingsForm(initial=data) #if post, validate form and save, redirect to dashboard if request.method == 'POST': form = EditSurveySettingsForm(request.POST) if form.is_valid(): #update settings survey_setting.is_active = form.cleaned_data['is_active'] survey_setting.survey_interval = form.cleaned_data[ 'survey_interval'] survey_setting.surveys_remain_open_days = form.cleaned_data[ 'surveys_remain_open_days'] survey_setting.save() request.user.organization.survey_language_preference = form.cleaned_data[ 'survey_language_preference'] request.user.organization.save() #report back if survey_setting.is_active == True: success_string = _("Your settings were updated successfully, %(instrument_name)s tracking is ACTIVE!"\ %{'instrument_name': survey_setting.instrument.name}) messages.success(request, success_string, extra_tags='alert alert-success') #mark an event event = Event(category='started_tracking_with_an_instrument', user=request.user, comment=None) event.save() else: success_string = _("Your settings were updated successfully, %(instrument_name)s tracking is INACTIVE!"\ %{'instrument_name': survey_setting.instrument.name}) messages.success(request, success_string, extra_tags='alert alert-warning') #mark an event event = Event(category='stopped_tracking_with_an_instrument', user=request.user, comment=None) event.save() #redirect to dashboard return HttpResponseRedirect(reverse('surveys-dashboard')) #if get, make form, return form, context = { 'form': form, 'instrument': instrument, 'submit_button_text': _("Update settings") } return render(request, 'setup_instrument.html', context)
def handle(*args, **kwargs): logger.warning("\nCreating someone to test on...") #create a test organization (after running this command, you can log in with these credentials) tusername = "******" tuser = None try: tuser = User.objects.get(username=tusername) except User.DoesNotExist as err: tuser = User.objects.create_user( username=tusername, email=tusername, password="******", ) tuser.save() logger.warning("...created user: %s." % (tuser)) assert tuser is not None, "tuser was None" torganization = None try: torganization = Organization.objects.get(owner=tuser) except Organization.DoesNotExist as err: torganization = Organization(owner=tuser, name="Test Organization #4", phone=None, address_line_1="Test street", address_line_2="", zip_code="9999", city="Testcity", country="NO", accepted_terms_and_conditions=True, has_free_access=True) torganization.save() logger.warning("...created organization: %s." % (torganization)) assert torganization is not None, "torganization was None" #create little employees employee_target = 7 existing_employees = Respondent.objects.filter( organization=torganization) if len(existing_employees) < employee_target: for e in range(employee_target - len(existing_employees)): email = "testemployee" + str( e + 1 + len(existing_employees)) + "@aa.aa" temployee = Respondent(organization=torganization, email=email) temployee.save() logger.warning("...created employee: %s." % (temployee)) #create an instrument logger.warning("Ensuring there is an instrument in place...") if len(Instrument.objects.all()) > 0: tinstrument = Instrument.objects.get(id=1) logger.warning("...there is already one: %s" % (tinstrument)) else: ri = employee_engagement_instrument.employee_engagement_instrument setup_instrument.setup_instrument(ri) tinstrument = Instrument.objects.get(id=1) logger.warning("...there wasn't already one, so I made one: %s" % (tinstrument)) #configure surveys tsurvey_setting = survey_logic.configure_survey_setting( organization=torganization, instrument=tinstrument, is_active=True) logger.warning("...created survey settings: %s" % (tsurvey_setting)) #create surveys logger.warning("Closing existing surveys....") open_surveys = Survey.objects.filter(owner=torganization, is_closed=False) for survey in open_surveys: survey_logic.close_survey(survey) ("... closed a survey with id %s!" % (survey.id)) logger.warning("... done!") surveys_target = 5 survey_list = [] current_date = date.today() for i in range(surveys_target): logger.warning( "Creating a survey (number %s) for the test-organization..." % (i)) tsurvey = survey_logic.create_survey(owner=torganization, instrument_list=[ tinstrument, ]) survey_list.append(tsurvey) logger.warning("...created a survey: %s" % (tsurvey)) tsurvey_instance_list = survey_logic.survey_instances_from_survey( tsurvey) logger.warning("...created %s survey instances." % (len(tsurvey_instance_list))) #send emails: #for survey_instance in tsurvey_instance_list: # survey_logic.send_email_for_survey_instance(survey_instance) # logger.warning("... sent an email to %s"%(survey_instance.respondent)) #answer survey logger.warning("... answering this survey...") for tsinstance in tsurvey_instance_list: logger.warning("... ... %s is answering..." % (tsinstance.respondent)) for titem in tsinstance.get_items(): answer_value = random.randint( titem.survey_item.item_dimension.scale.min_value, titem.survey_item.item_dimension.scale.max_value) titem.answer_item(answer_value) tsinstance.check_completed() #close survey logger.warning("...moving survey back in time") current_date += timedelta(days=-90) tsurvey.date_close = current_date tsurvey.date_open = current_date + timedelta(days=-10) tsurvey.save() logger.warning("...done.") logger.warning("...closing survey with id %s" % (tsurvey.id)) survey_logic.close_survey_if_date_close_has_passed(tsurvey) logger.warning("...done.") #update survey setting tsurvey_setting.check_last_survey_dates() logger.warning("All done!")