Skip to content

Commit

Permalink
Progress on adding the object field types
Browse files Browse the repository at this point in the history
  • Loading branch information
nonprofittechy committed Dec 21, 2023
1 parent 6017370 commit 33a83cb
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docassemble/ALWeaver/data/questions/assembly_line.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ code: |
interview.auto_assign_attributes(
screens=draft_screens
)
if hasattr(interview, "start_with_json") and interview.start_with_json:
elif hasattr(interview, "start_with_json") and interview.start_with_json:
#try: # Don't know yet why this always raises an exception. A name error that DA silently fixes?
interview.parsed_json = json.loads(interview.uploaded_json.slurp())
#except:
Expand Down
25 changes: 22 additions & 3 deletions docassemble/ALWeaver/data/templates/output_defs.mako
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@
from more_itertools import unique_everseen
%>\
<%def name="field_entry_yaml(field)">\
% if hasattr(field, "field_type") and field.field_type in ("name_person", "name_any", "name_business", "address", "gender", "pronouns", "language"):
- code: |
% if field.field_type == "name_person":
${ field.get_parent_variable_with_index() }.name_fields(person_or_business="person")
% elif field.field_type == "name_any":
${ field.get_parent_variable_with_index() }.name_fields(person_or_business="unknown")
% elif field.field_type == "name_business":
${ field.get_parent_variable_with_index() }.name_fields(person_or_business="business")
% elif field.field_type == "address":
${ field.get_parent_variable_with_index() }.address_fields(country_code=AL_DEFAULT_COUNTRY, default_state=AL_DEFAULT_STATE)
% elif field.field_type == "gender":
${ field.get_parent_variable_with_index() }.gender_fields()
% elif field.field_type == "pronouns":
${ field.get_parent_variable_with_index() }.pronoun_fields(show_help=True, required=False)
% elif field.field_type == "language":
${ field.get_parent_variable_with_index() }.language_fields(choices=al_language_user_choices)
% endif
% else:
- "${ escape_double_quoted_yaml(field.label) if field.has_label else "no label" }": ${ field.get_settable_var() }
% endif
% if hasattr(field, "field_type"):
% if field.field_type in ["yesno", "yesnomaybe","file","yesnoradio","noyes","noyesradio", "integer","currency","email","range","number","date"]:
% if field.field_type in ["yesno", "yesnomaybe","file","yesnoradio","noyes","noyesradio", "integer","currency","email","range","number","date", "BirthDate"]:
datatype: ${ field.field_type }
% elif field.field_type == "multiple choice radio":
input type: radio
Expand All @@ -23,9 +42,9 @@
datatype: multiselect
% elif field.field_type == "area":
input type: area
% if field.need_maxlength():
% if field.need_maxlength():
maxlength: ${ field.maxlength }
% endif
% endif
% endif
% if field.field_type in ["integer", "currency"]:
min: 0
Expand Down
1 change: 1 addition & 0 deletions docassemble/ALWeaver/generator_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class GeneratorConstantObject(object):
"_gender_nonbinary": ".gender_nonbinary",
"_gender_undisclosed": ".gender_undisclosed",
"_gender_self_described": ".gender_self_described",
"_pronouns": ".pronouns",
"_birthdate": ".birthdate.format()",
"_age": ".age_in_years()",
"_email": ".email",
Expand Down
101 changes: 91 additions & 10 deletions docassemble/ALWeaver/interview_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,28 +249,44 @@ class DAFieldGroup(Enum):
CUSTOM = "custom"


def field_type_options() -> List[Dict[str, str]]:
return [
{"text": "Text"},
{"area": "Area"},
def field_type_options(include_object_options:bool=False) -> List[Dict[str, str]]:
options = [
{"text": "Short answer"},
{"area": "Long answer"},
{"yesno": "Yes/no checkbox"},
{"noyes": "No/yes checkbox"},
{"yesnoradio": "Yes/no radio"},
{"noyesradio": "No/yes radio"},
{"integer": "Whole number"},
{"number": "Number"},
{"range", "Number range"}
{"currency": "Currency"},
{"date": "Date"},
{"email": "Email"},
{"multiple choice dropdown": "Drop-down"},
{"multiple choice combobox": "Combobox"},
{"multiple choice radio": "Radio buttons"},
{"multiple choice checkboxes": "Checkboxes"},
{"multiselect": "Multi-select"},
{"multiple choice dropdown": "Drop-down (multiple choice)"},
{"multiple choice combobox": "Combobox (multiple choice)"},
{"multiple choice radio": "Radio buttons (multiple choice)"},
{"multiple choice checkboxes": "Checkboxes (multiple choice)"},
{"multiselect": "Multi-select (multiple choice)"},
{"file": "Uploaded file"},
]
object_options = [
{"name_any": "Name (person or business)"},
{"name_person": "Name (individual)"},
{"name_business": "Name (business)"},
{"address": "Address"},
{"BirthDate": "Birthdate"},
{"gender": "Gender"},
{"pronouns": "Pronouns"},
{"language": "Language"},
]
extras = [
{"code": "Python code"},
{"skip this field": "[Skip this field]"},
]
if include_object_options:
return options + object_options + extras
return options + extras


class DAField(DAObject):
Expand Down Expand Up @@ -324,12 +340,20 @@ def fill_in_docx_attributes(
if self.variable.endswith("_date"):
self.field_type_guess = "date"
self.variable_name_guess = f"Date of {variable_name_guess[:-5]}"
elif self.variable.endswith("_range"):
self.field_type_guess = "range"
elif self.variable.endswith("_amount"):
self.field_type_guess = "currency"
elif self.variable.endswith("_value"):
self.field_type_guess = "currency"
elif self.variable.endswith(".signature"):
self.field_type_guess = "signature"
elif self.variable.endswith(".gender"):
self.field_type_guess = "gender"
elif self.variable.endswith(".pronouns"):
self.field_type_guess = "pronouns"
elif self.variable.endswith(".language"):
self.field_type_guess = "language"
else:
self.field_type_guess = "text"

Expand Down Expand Up @@ -488,7 +512,11 @@ def user_ask_about_field(self):
"field": self.attr_name("field_type"),
"label above field": True,
"grid": 3,
"code": "field_type_options()",
"code": (
"field_type_options(include_object_options=True)"
if self.is_object_or_list()
else "field_type_options()"
),
"default": self.field_type_guess
if hasattr(self, "field_type_guess")
else None,
Expand Down Expand Up @@ -531,6 +559,33 @@ def user_ask_about_field(self):
"hint": "Descriptive name: key_name",
}
)
field_questions.append(
"label": "Min",
"datatype": "number",
"label above field": True,
"field": self.attr_name("range_min"),
"show if": {"variable": self.attr_name("field_type"), "is": "range"}
"grid": 4,
"default": 0,
)
field_questions.append(
"label": "Max",
"datatype": "number",
"label above field": True,
"field": self.attr_name("range_max"),
"show if": {"variable": self.attr_name("field_type"), "is": "range"}
"grid": 4,
)
field_questions.append(
"label": "Step",
"datatype": "number",
"label above field": True,
"field": self.attr_name("range_step"),
"show if": {"variable": self.attr_name("field_type"), "is": "range"}
"grid": 4,
"help": "Amount that dragging the slider will increase or decrease by (defaults to 1)"
"default": 1
)
if hasattr(self, "maxlength"):
field_questions.append(
{
Expand Down Expand Up @@ -696,11 +751,37 @@ def _get_parent_variable(
return prefix, "object"
return var_with_attribute, "primitive"

def get_parent_variable_with_index(
self,
) -> str:
"""If the variable has an index variable (like users[0].name.first),
this acts like _get_parent_variable except that it keeps the [index]
part of the variable name.
It always returns the variable "furthest to the left" (e.g., it ignores
anything coming after a .)
"""
# Parse the original variable name. it could look like
# users[0].name.first or users[0].name.full()
# we want to return just users[0].
# For now, we won't deal with nested lists, like
# users[0].attorneys[0]
var_parts = re.findall(r"([^.]+)(\.[^.]*)?", self.variable)
if not var_parts:
return self.variable
return var_parts[0][0]

def is_object_or_list(self) -> bool:
"""Returns true only if the variable represents an
object in a list"""
return self._get_parent_variable(self.variable)[1] in ("object", "list")

def __str__(self) -> str:
return self.variable

def __repr__(self) -> str:
return f"{self.variable} ({self.raw_field_names}, {self.final_display_var})"



class ParentCollection(object):
Expand Down

0 comments on commit 33a83cb

Please sign in to comment.