Skip to content
This repository was archived by the owner on Jun 15, 2020. It is now read-only.
Andrew Chan edited this page Dec 27, 2017 · 10 revisions

PSA: The above image is slightly outdated since the introduction of application assignments.

Checkbox Model

create

Checkbox.create(question_text, options)

Convenience function for creating Checkbox objects.

  • question_text - (String) The text displayed by the Checkbox question.
  • options - (list) A list of string options.

Example Usage:

Checkbox.create("What classes have you taken?", ["CS61A", "CS61B", "CS70"])

Dropdown Model

create

Dropdown.create(question_text, options)

Convenience function for creating Dropdown objects.

  • question_text - (String) The text displayed by the Dropdown question.
  • options - (list) A list of string options.

Example Usage:

Dropdown.create("What year are you?", ["Freshman", "Sophomore", "Junior", "Senior"])

Paragraph Model

create

Paragraph.create(question_text)

Convenience function for creating Paragraph objects.

  • question_text - (String) The text displayed by the Paragraph question.

Example Usage:

Paragraph.create("What's your favorite part of Berkeley?")

Radiobutton Model

create

Radiobutton.create(question_text, options)

Convenience function for creating Radiobutton objects.

  • question_text - (String) The text displayed by the Radiobutton question.
  • options - (list) A list of string options.

Example Usage:

Radiobutton.create("How cool is Andrew Chan?", ["Cool", "Really cool", "Super cool"])

Question Model

get_options_list

Question.get_options_list()

Convenience function for getting the options property of a question as a list instead of a string. Use this function instead of looking at Question.options directly! Note that modifying the list you get from this function will not change the stringified list stored in the Question object.

Example Usage:

>>> q = Radiobutton.create("How cool is Andrew Chan?", ["Cool", "Really cool", "Super cool"])
>>> q.get_options_list()
["Cool", "Really cool", "Super cool"]

set_options_list

Question.set_options_list(options_list)

Convenience function for setting the options property of a question by passing in a list instead of a string. Use this function instead of setting Question.options directly.

  • options_list - (list) A list of string options.

Example Usage:

>>> q = Radiobutton.create("How cool is Andrew Chan?", ["Cool", "Really cool", "Super cool"])
>>> q.set_options_list(["Super uncool", "Very uncool", "Uncool"])
>>> q.get_options_list()
["Super uncool", "Very uncool", "Uncool"]

question_text

Question.question_text

The text that the question will display.

question_type

Question.question_type

String representation of the question type. Use the same string as the class name (i.e. the string type for Radio Buttons should be "Radiobutton", since the class name is Radiobutton, and Radiobutton.__name__ evaluates to "Radiobutton"). In the future, if we keep the specific question models, it may be a good idea to modify the constructors so they pass in class.__name__ to this value automatically.

options

Question.options

A string representation of a python list - The list of NJ, TX, FL should be stored as "['NJ', 'TX', 'FL']". For consistency, groups that need to store and read the options should use list.__str__ and ast.literal_eval to convert between the string representation and a python list. options will be None if there are no options for that type of question (i.e. Paragraph).

Remember that we opted not to make option its own model because it would complicate the code a bit, so we made it a simple string that can be turned into a python list quickly. It would be great if we could just store python lists in the database, but unfortunately this is not possible, hence the string.

Example conversion between python list and string:

from portal.models import Question, Checkbox
import ast

x = ["NY", "TX", "FL"]
q = Question(
    question_text="What state are you from?", 
    question_type=Checkbox.__name__,
    options=x.__str__(),
    order_number=1,
    )
# converting from a string to a python list
y = ast.literal_eval(q.options)

Note: This is just showing how to manipulate the underlying data types of a Question object. We have functions specifically made for modifying the options list of a Question object so that you don't have to deal with the string representation. Use those functions instead!

order_number

Question.order_number

Where on the application form this question should appear, in relation to other questions. Should start at 1 and all entries should be unique. The question that has number 1 would appear first, the one that has number 2 would be second and so on.

We can enforce uniqueness later.

Models that inherit from Question (i.e. Paragraph, Checkbox, etc.)

Querying

To select all of the questions use Question.objects.all(). You can also query specific question types by using Paragraph.objects.all(), Radiobutton.objects.all(), Checkbox.objects.all(), and Dropdown.objects.all().

Working with Query Results

The best way to determine the type of a question type is to use each specific question type's isinstance helper function. Example:

>>> from portal.models import Radiobutton, Checkbox
>>> q1 = Radiobutton.create("This is a radio button question", ["Yes", "No"])
>>> q2 = Dropdown.create("This is a dropdown question", ["Yeah", "Nah"])
>>> Radiobutton.isinstance(q1)
True
>>> Radiobutton.isinstance(q2)
False
>>> Dropdown.isinstance(q1)
False
>>> Dropdown.isinstance(q2)
True

Creation

Each specific type of question has a special function, <Question_type>.create (where instead of <Question_type> you would have Radiobutton, Checkbox, Dropdown, etc.). Use these! See each model's documentation for more.