Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor form import structure #244

Merged
merged 4 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .error_messages import error_messages

__all__ = ["error_messages"]
2 changes: 1 addition & 1 deletion app/forms/duration_form.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from wtforms import Form

from app.forms.fields.integer_field_with_separator import IntegerFieldWithSeparator
from app.forms.fields import IntegerFieldWithSeparator


class DurationForm(Form):
Expand Down
47 changes: 0 additions & 47 deletions app/forms/field_factory.py

This file was deleted.

44 changes: 44 additions & 0 deletions app/forms/field_handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from .date_handlers import DateHandler, MonthYearDateHandler, YearDateHandler
from .dropdown_handler import DropdownHandler
from .duration_handler import DurationHandler
from .number_handler import NumberHandler
from .select_handlers import SelectHandler, SelectMultipleHandler
from .string_handler import StringHandler
from .text_area_handler import TextAreaHandler

FIELD_HANDLER_MAPPINGS = {
"Checkbox": SelectMultipleHandler,
"Radio": SelectHandler,
"Relationship": SelectHandler,
"TextArea": TextAreaHandler,
"TextField": StringHandler,
"Dropdown": DropdownHandler,
"Number": NumberHandler,
"Currency": NumberHandler,
"Unit": NumberHandler,
"Percentage": NumberHandler,
"Date": DateHandler,
"MonthYearDate": MonthYearDateHandler,
"YearDate": YearDateHandler,
"Duration": DurationHandler,
}


def get_field_handler(
answer,
error_messages,
answer_store,
metadata=None,
location=None,
disable_validation=False,
question_title=None,
):
return FIELD_HANDLER_MAPPINGS[answer.get("type")](
answer,
error_messages=error_messages,
answer_store=answer_store,
metadata=metadata,
location=location,
disable_validation=disable_validation,
question_title=question_title,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dateutil.relativedelta import relativedelta

from app.forms.field_handlers.field_handler import FieldHandler
from app.forms.fields.date_field import DateField
from app.forms.fields import DateField, MonthYearDateField, YearDateField
from app.questionnaire.rules import convert_to_datetime
from app.forms.validators import (
SingleDatePeriodCheck,
Expand Down Expand Up @@ -110,3 +110,57 @@ def get_date_value(self, key):
date_value = self.transform_date_by_offset(date_value, offset)

return date_value


class MonthYearDateHandler(DateHandler):
DATE_FORMAT = "yyyy-mm"
DISPLAY_FORMAT = "MMMM yyyy"

def get_field(self) -> MonthYearDateField:
return MonthYearDateField(
self.validators, label=self.label, description=self.guidance
)

def get_min_max_validator(self, minimum_date, maximum_date):
messages = self.answer_schema.get("validation", {}).get("messages")

minimum_date = (
minimum_date.replace(day=1) if minimum_date else None
) # First day of Month
maximum_date = (
maximum_date + relativedelta(day=31) if maximum_date else None
) # Last day of month

return SingleDatePeriodCheck(
messages=messages,
date_format=self.DISPLAY_FORMAT,
minimum_date=minimum_date,
maximum_date=maximum_date,
)


class YearDateHandler(DateHandler):
DATE_FORMAT = "yyyy"
DISPLAY_FORMAT = "yyyy"

def get_field(self) -> YearDateField:
return YearDateField(
self.validators, label=self.label, description=self.guidance
)

def get_min_max_validator(self, minimum_date, maximum_date):
messages = self.answer_schema.get("validation", {}).get("messages")

minimum_date = (
minimum_date.replace(month=1, day=1) if minimum_date else None
) # January 1st
maximum_date = (
maximum_date.replace(month=12, day=31) if maximum_date else None
) # Last day of december

return SingleDatePeriodCheck(
messages=messages,
date_format=self.DISPLAY_FORMAT,
minimum_date=minimum_date,
maximum_date=maximum_date,
)
32 changes: 0 additions & 32 deletions app/forms/field_handlers/month_year_date_handler.py

This file was deleted.

5 changes: 2 additions & 3 deletions app/forms/field_handlers/number_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
from wtforms import IntegerField, DecimalField

from app.data_model.answer_store import AnswerStore
from app.forms.fields.decimal_field_with_separator import DecimalFieldWithSeparator
from app.forms.fields.integer_field_with_separator import IntegerFieldWithSeparator
from app.forms.fields import DecimalFieldWithSeparator, IntegerFieldWithSeparator
from app.forms.field_handlers.field_handler import FieldHandler
from app.forms.validators import NumberCheck, NumberRange, DecimalPlaces
from app.questionnaire.location import Location
from app.questionnaire import Location
from app.settings import MAX_NUMBER


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from app.forms.fields.select_field_with_detail_answer import SelectFieldWithDetailAnswer
from app.forms.fields import (
MultipleSelectFieldWithDetailAnswer,
SelectFieldWithDetailAnswer,
)
from app.forms.field_handlers.field_handler import FieldHandler


Expand Down Expand Up @@ -40,3 +43,17 @@ def get_field(self) -> SelectFieldWithDetailAnswer:
validators=self.validators,
coerce=self.coerce_str_unless_none,
)


class SelectMultipleHandler(SelectHandler):
MANDATORY_MESSAGE_KEY = "MANDATORY_CHECKBOX"

def get_field(self) -> MultipleSelectFieldWithDetailAnswer:
return MultipleSelectFieldWithDetailAnswer(
label=self.label,
description=self.guidance,
choices=self.build_choices_with_detail_answer_ids(
self.answer_schema["options"]
),
validators=self.validators,
)
18 changes: 0 additions & 18 deletions app/forms/field_handlers/select_multiple_handler.py

This file was deleted.

2 changes: 1 addition & 1 deletion app/forms/field_handlers/text_area_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from wtforms import validators

from app.forms.fields.max_text_area_field import MaxTextAreaField
from app.forms.fields import MaxTextAreaField
from app.forms.field_handlers.field_handler import FieldHandler


Expand Down
30 changes: 0 additions & 30 deletions app/forms/field_handlers/year_date_handler.py

This file was deleted.

31 changes: 31 additions & 0 deletions app/forms/fields/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from app.forms.fields.date_field import DateField
from app.forms.fields.decimal_field_with_separator import (
DecimalField,
DecimalFieldWithSeparator,
)
from app.forms.fields.integer_field_with_separator import (
IntegerField,
IntegerFieldWithSeparator,
)
from app.forms.fields.max_text_area_field import MaxTextAreaField
from app.forms.fields.month_year_date_field import MonthYearDateField
from app.forms.fields.multiple_select_field_with_detail_answer import (
MultipleSelectFieldWithDetailAnswer,
)
from .select_field_with_detail_answer import SelectField, SelectFieldWithDetailAnswer
from .year_date_field import YearDateField


__all__ = [
"DateField",
"DecimalField",
"DecimalFieldWithSeparator",
"IntegerField",
"IntegerFieldWithSeparator",
"MaxTextAreaField",
"MonthYearDateField",
"MultipleSelectFieldWithDetailAnswer",
"SelectField",
"SelectFieldWithDetailAnswer",
"YearDateField",
]
3 changes: 1 addition & 2 deletions app/forms/questionnaire_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from werkzeug.datastructures import MultiDict, ImmutableMultiDict
from wtforms import validators

from app.forms.field_factory import get_field_handler
from app.forms.field_handlers.date_handler import DateHandler
from app.forms.field_handlers import get_field_handler, DateHandler
from app.forms.validators import DateRangeCheck, SumCheck, MutuallyExclusiveCheck

logger = logging.getLogger(__name__)
Expand Down
4 changes: 2 additions & 2 deletions app/forms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from app.helpers.template_helper import safe_content
from app.jinja_filters import format_number, get_formatted_currency
from app.questionnaire.rules import convert_to_datetime
from app.forms.error_messages import error_messages
from app.forms import error_messages

logger = get_logger()

Expand Down Expand Up @@ -407,7 +407,7 @@ def format_playback_value(value, currency=None):


def format_message_with_title(error_message, question_title):
return error_message % dict(question_title=safe_content(question_title))
return error_message % {"question_title": safe_content(question_title)}


class MutuallyExclusiveCheck:
Expand Down
2 changes: 1 addition & 1 deletion app/questionnaire/questionnaire_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from werkzeug.datastructures import ImmutableDict

from app.data_model.answer import Answer
from app.forms.error_messages import error_messages
from app.forms import error_messages
from app.questionnaire.schema_utils import get_values_for_key

DEFAULT_LANGUAGE_CODE = "en"
Expand Down
2 changes: 1 addition & 1 deletion tests/app/forms/field_handlers/test_date_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from app.data_model.answer import Answer
from app.data_model.answer_store import AnswerStore
from app.forms.field_handlers.date_handler import DateHandler
from app.forms.field_handlers import DateHandler
from app.forms.fields import date_field
from app.questionnaire.location import Location
from app.questionnaire.questionnaire_schema import QuestionnaireSchema
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from wtforms import Form

from app.forms.field_handlers.month_year_date_handler import MonthYearDateHandler
from app.forms.fields.month_year_date_field import MonthYearDateField
from app.forms.field_handlers import MonthYearDateHandler
from app.forms.fields import MonthYearDateField


def test_month_year_date_field_created_with_guidance():
Expand Down
Loading