def my_generate_likert_table(labels, questions, form_name=None, help_texts=None, widget=None, use_likert_scale=True, make_label_tag=False, **kwargs): """ Generate a table with Likert scales between 1 and `len(labels)` in each row for questions supplied with `questions` as list of tuples (field name, field label). Optionally provide `help_texts` which is a list of help texts for each question (hence must be of same length as `questions`. If `make_label_tag` is True, then each label is surrounded by a <label>...</label> tag, otherwise it's not. Optionally set `widget` (default is `RadioSelect`). """ if not help_texts: help_texts = [''] * len(questions) if not widget: widget = widgets.RadioSelect if len(help_texts) != len(questions): raise ValueError( 'Number of questions must be equal to number of help texts.') if use_likert_scale: field_generator = generate_likert_field(labels, widget=widget) header_labels = labels else: field_generator = partial(models.StringField, choices=labels, widget=widget or widgets.RadioSelectHorizontal) header_labels = [t[1] for t in labels] fields = [] for (field_name, field_label), help_text in zip(questions, help_texts): fields.append((field_name, { 'help_text': help_text, 'label': field_label, 'make_label_tag': make_label_tag, 'field': field_generator(blank=True), })) form_def = { 'form_name': form_name, 'fields': fields, 'render_type': 'table', 'header_labels': header_labels } form_def.update(dict(**kwargs)) return form_def
) YESNO_CHOICES = ( ('yes', 'Yes'), ('no', 'No'), ) # define a Likert 5-point scale with its labels likert_5_labels = ('Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree') likert_7_labels = ('Strongly Disagree', 'Moderately Disagree', 'Slightly Disagree', 'Neutral', 'Slightly Agree', 'Moderately Agree', 'Strongly Agree') likert_5point_field = generate_likert_field(likert_5_labels) likert_7point_field = generate_likert_field(likert_7_labels) # define survey questions per page # for each page define a page title and a list of questions # the questions have a field name, a question text (input label), and a field type (model field class) SURVEY_DEFINITIONS = ( { 'page_title': '', 'survey_fields': [ # create a table of Likert scale choices # we use the same 5-point scale a before and specify four rows for the table, # each with a tuple (field name, label) ## Big-five (OCEAN: Openness, Conscientiousness, Extraversion, Agreeableness, Neuroticism) generate_likert_table(likert_7_labels, [
'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree' ) likert_5_labels_html = ( '<b>Strongly</b> disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', '<b>Strongly</b> agree' ) likert_5point_field = generate_likert_field(likert_5_labels) likert_5point_field_html = generate_likert_field(likert_5_labels_html, html_labels=True) likert_5point_field_centered = generate_likert_field(likert_5_labels, choices_values=-2) likert_5point_values = ['strong_dis', 'dis', 'neutral', 'agr', 'strong_agr'] likert_5point_field_labeled_values = generate_likert_field(likert_5_labels, choices_values=likert_5point_values, widget=Select) # also use Select widget from Django for dropdown menu # define survey questions per page # for each page define a page title and a list of questions # the questions have a field name, a question text (input label), and a field type (model field class) SURVEY_DEFINITIONS = { 'SurveyPage1': { 'page_title': 'Survey Questions - Page 1 - Simple questions and inputs', 'survey_fields': [
'6', '7', '8', '9', '10 - Very Willing To Take Risks', ) AGREE_DISAGREE_5_CHOICES = ( ('1_strongly_agree', 'Strongly agree'), ('2_agree', 'Agree'), ('3_neutral', 'Neutral'), ('4_disagree', 'Disagree'), ('5_strongly_disagree', 'Strongly disagree'), ) likert_11point_field = generate_likert_field(likert_11_labels) COVID_CAUSE_CHOICES = ( ('bacteria', 'Bacterial infection'), ('insect', 'Insect bite'), ('virus', 'Viral infection'), ('animal', 'Animals'), ('do_not_know', "I don't know"), ) COVID_SPREAD_CHOICES = ( ('cough', 'Infected persons coughing or sneezing'), ('gathering', 'By being in a public gathering where there is an infected person'), ('surface', 'Virus-contaminated surfaces'), ('face', 'Touching your face after you have been in contact with an infected person'), ('do_not_know', "I don't know"),
'6', '7', '8', '9', '10 - Very Willing To Take Risks', ) AGREE_DISAGREE_5_CHOICES = ( ('1_strongly_agree', 'Strongly agree'), ('2_agree', 'Agree'), ('3_neutral', 'Neutral'), ('4_disagree', 'Disagree'), ('5_strongly_disagree', 'Strongly disagree'), ) likert_11point_field = generate_likert_field(likert_11_labels) USA_RELIGION_CHOICES = ( ('atheism', 'Atheism'), ('buddhism', 'Buddhism'), ('christ-bapt', 'Christianity - Baptist'), ('christ-cath', 'Christianity - Catholic'), ('christ-luth', 'Christianity - Lutheran'), ('christ-meth', 'Christianity - Methodist'), ('christ-other', 'Christianity - Other'), ('hinduism', 'Hinduism'), ('islam', 'Islam'), ('judaism', 'Judaism'), ('agnostic', 'Nonreligious or Agnostic'), ('blank', 'Prefer to not answer'), ('other', 'Other'),