def _create_surveys(libraries, sample_year, ignore_missing_variables=False): template_cells = survey_template(sample_year).cells variables = {} # Fetch variables once for IO-performance for variable in Variable.objects.all(): variables[variable.key] = variable created = 0 for library in libraries: existing_surveys = Survey.objects.filter(sample_year=sample_year, library__sigel=library.sigel).only("library") if existing_surveys.count() != 0: survey = existing_surveys[0] survey.library = library else: survey = Survey( library=library, sample_year=sample_year, observations=[]) for cell in template_cells: variable_key = cell.variable_key if variable_key not in variables: if ignore_missing_variables: continue raise Exception("Can't find variable with key '{}'".format(variable_key)) survey.observations.append(SurveyObservation(variable=variables[variable_key])) created += 1 survey.save() return created
def _create_surveys(libraries, sample_year, ignore_missing_variables=False): template_cells = survey_template(sample_year).cells variables = {} # Fetch variables once for IO-performance for variable in Variable.objects.all(): variables[variable.key] = variable created = 0 for library in libraries: existing_surveys = Survey.objects.filter( sample_year=sample_year, library__sigel=library.sigel).only("library") if existing_surveys.count() != 0: survey = existing_surveys[0] survey.library = library else: survey = Survey(library=library, sample_year=sample_year, observations=[]) for cell in template_cells: variable_key = cell.variable_key if variable_key not in variables: if ignore_missing_variables: continue raise Exception("Can't find variable with key '{}'".format( variable_key)) survey.observations.append( SurveyObservation(variable=variables[variable_key])) created += 1 survey.save() return created
def test_creates_new_surveys_from_2014_template(self): library = self._dummy_library(sigel="sigel1") self._dummy_variable(key=survey_template(2014).cells[0].variable_key) _create_surveys([library], 2014, ignore_missing_variables=True) self.assertEquals(len(Survey.objects.all()[0].observations), 1)
def test_can_create_surveys_for_multiple_years(self): library = self._dummy_library(sigel="sigel1") self._dummy_variable(key=survey_template(2014).cells[0].variable_key) _create_surveys([library], 2014, ignore_missing_variables=True) _create_surveys([library], 2015, ignore_missing_variables=True) self.assertEquals(Survey.objects.count(), 2)
def test_returns_default_template_for_2013(self): survey = self._dummy_survey(observations=[ self._dummy_observation(), self._dummy_observation(), self._dummy_observation(), ]) template = survey_template(2013, survey) self.assertEquals(len(template.cells), 3)
def example_survey(request): sample_year = 2014 context = { "hide_navbar": True, "hide_bottom_bar": True, "hide_admin_panel": True, "form": SurveyForm(survey=Survey( sample_year=sample_year, library=Library( name=u"Exempelbiblioteket", sigel=u"exempel_sigel", email=u"*****@*****.**", city=u"Exempelstaden", municipality_code=180, address=u"Exempelgatan 14B", library_type=u"folkbib" ), observations=[SurveyObservation(variable=Variable.objects.get(key=cell.variable_key)) for cell in survey_template(sample_year).cells])), } return render(request, 'libstat/survey.html', context)
def test_returns_empty_template_for_2013_without_survey(self): template = survey_template(2013) self.assertEquals(len(template.cells), 0)
def test_returns_base_template_for_2015(self): template = survey_template(2015) self.assertEquals(template, _survey_template_base())
def __init__(self, *args, **kwargs): survey = kwargs.pop('survey', None) authenticated = kwargs.pop('authenticated', False) super(SurveyForm, self).__init__(*args, **kwargs) # Cache variables for performance variables = {} for variable in Variable.objects.all(): variables[variable.key] = variable template = survey_template(survey.sample_year, survey) self.fields["disabled_inputs"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "disabled_inputs"})) #TODO: remove? self.fields["unknown_inputs"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "unknown_inputs"})) self.fields["altered_fields"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "altered_fields"})) self.fields["selected_libraries"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "selected_libraries"})) self.fields["scroll_position"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "scroll_position"})) self.fields["submit_action"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "submit_action"})) self.fields["read_only"] = forms.CharField(required=False, widget=forms.HiddenInput(attrs={"id": "read_only"})) self.fields["key"] = forms.CharField(required=False, widget=forms.HiddenInput(), initial=survey.pk) self.fields["selected_status"] = forms.CharField( required=False, widget=forms.HiddenInput(), initial=survey.status) intro_text = variables[template.intro_text_variable_key].description if template.intro_text_variable_key in variables else "" self.intro_text = intro_text self.library_name = survey.library.name self.library_sigel = survey.library.sigel self.city = survey.library.city self.municipality_code = survey.library.municipality_code self.sample_year = survey.sample_year self.is_user_read_only = not survey.status in (u"not_viewed", u"initiated") self.is_read_only = not authenticated and self.is_user_read_only self.can_submit = not authenticated and survey.status in ("not_viewed", "initiated") self.password = survey.password self.status = self._status_label(survey.status) self.notes = survey.notes if survey.notes else "" self.notes_rows = min(max(5, survey.notes.count('\n') if survey.notes else 0) + 1, 10) self.statuses = Survey.STATUSES self.is_published = survey.status == "published" self.latest_version_published = survey.latest_version_published self.url = settings.API_BASE_URL + reverse('survey', args=(survey.pk,)) self.url_with_password = "******".format(self.url, self.password) self.email = survey.library.email self.mailto = self._mailto_link() self._set_libraries(survey, survey.selected_libraries, authenticated) if hasattr(self, 'library_selection_conflict') and self.library_selection_conflict: self.conflicting_surveys = survey.get_conflicting_surveys() for conflicting_survey in self.conflicting_surveys: conflicting_survey.url = settings.API_BASE_URL + reverse('survey', args=(conflicting_survey.pk,)) conflicting_survey.conflicting_libraries = self._conflicting_libraries( survey.selected_libraries + [survey.library.sigel], conflicting_survey.selected_libraries) self.can_submit = False previous_survey = survey.previous_years_survey() for cell in template.cells: variable_key = cell.variable_key if not variable_key in variables: raise Exception("Can't find variable with key '{}'".format(variable_key)) variable_type = variables[variable_key].type cell.types.append(variable_type) #cell is given same type as variable observation = survey.get_observation(variable_key) if observation: cell.disabled = observation.disabled #TODO: remove? cell.value_unknown = observation.value_unknown if previous_survey: cell.previous_value = survey.previous_years_value(observation.variable, previous_survey) if not observation: observation = SurveyObservation(variable=variables[variable_key]) survey.observations.append(observation) self.fields[variable_key] = self._cell_to_input_field(cell, observation, authenticated, variable_type) self.sections = template.sections if self.is_read_only: self.fields["read_only"].initial = "true" for key, input in self.fields.iteritems(): input.widget.attrs["readonly"] = ""
def __init__(self, *args, **kwargs): survey = kwargs.pop('survey', None) authenticated = kwargs.pop('authenticated', False) super(SurveyForm, self).__init__(*args, **kwargs) # Cache variables for performance variables = {} for variable in Variable.objects.all(): variables[variable.key] = variable template = survey_template(survey.sample_year, survey) self.fields["disabled_inputs"] = forms.CharField( required=False, widget=forms.HiddenInput( attrs={"id": "disabled_inputs"})) #TODO: remove? self.fields["unknown_inputs"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "unknown_inputs"})) self.fields["altered_fields"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "altered_fields"})) self.fields["selected_libraries"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "selected_libraries"})) self.fields["scroll_position"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "scroll_position"})) self.fields["submit_action"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "submit_action"})) self.fields["read_only"] = forms.CharField( required=False, widget=forms.HiddenInput(attrs={"id": "read_only"})) self.fields["key"] = forms.CharField(required=False, widget=forms.HiddenInput(), initial=survey.pk) self.fields["selected_status"] = forms.CharField( required=False, widget=forms.HiddenInput(), initial=survey.status) intro_text = variables[ template. intro_text_variable_key].description if template.intro_text_variable_key in variables else "" self.intro_text = intro_text self.library_name = survey.library.name self.library_sigel = survey.library.sigel self.city = survey.library.city self.municipality_code = survey.library.municipality_code self.sample_year = survey.sample_year self.is_user_read_only = not survey.status in (u"not_viewed", u"initiated") self.is_read_only = not authenticated and self.is_user_read_only self.can_submit = not authenticated and survey.status in ("not_viewed", "initiated") self.password = survey.password self.status = self._status_label(survey.status) self.notes = survey.notes if survey.notes else "" self.notes_rows = min( max(5, survey.notes.count('\n') if survey.notes else 0) + 1, 10) self.statuses = Survey.STATUSES self.is_published = survey.status == "published" self.latest_version_published = survey.latest_version_published self.url = settings.API_BASE_URL + reverse('survey', args=(survey.pk, )) self.url_with_password = "******".format(self.url, self.password) self.email = survey.library.email self.mailto = self._mailto_link() self._set_libraries(survey, survey.selected_libraries, authenticated) if hasattr(self, 'library_selection_conflict' ) and self.library_selection_conflict: self.conflicting_surveys = survey.get_conflicting_surveys() for conflicting_survey in self.conflicting_surveys: conflicting_survey.url = settings.API_BASE_URL + reverse( 'survey', args=(conflicting_survey.pk, )) conflicting_survey.conflicting_libraries = self._conflicting_libraries( survey.selected_libraries + [survey.library.sigel], conflicting_survey.selected_libraries) self.can_submit = False previous_survey = survey.previous_years_survey() for cell in template.cells: variable_key = cell.variable_key if not variable_key in variables: raise Exception( "Can't find variable with key '{}'".format(variable_key)) variable_type = variables[variable_key].type cell.types.append( variable_type) #cell is given same type as variable observation = survey.get_observation(variable_key) if observation: cell.disabled = observation.disabled #TODO: remove? cell.value_unknown = observation.value_unknown if previous_survey: cell.previous_value = survey.previous_years_value( observation.variable, previous_survey) if not observation: observation = SurveyObservation( variable=variables[variable_key]) survey.observations.append(observation) self.fields[variable_key] = self._cell_to_input_field( cell, observation, authenticated, variable_type) self.sections = template.sections if self.is_read_only: self.fields["read_only"].initial = "true" for key, input in self.fields.iteritems(): input.widget.attrs["readonly"] = ""