From 4023d46b4a65aea5d16f41323c119cbba32a3e28 Mon Sep 17 00:00:00 2001 From: Binh Van Nguyen Date: Tue, 25 Oct 2011 14:54:22 -0500 Subject: [PATCH] Changed a few fields to be optional. Found a fairly difficult bug involved with Field instances being shared across Records. The issue is that Field instances are static. I either need to implement a way to instantiate copies of all the Fields per-record, or write a wrapping interface which provides a unique value store on a per-Record basis. --- pyaccuwage/fields.py | 7 +++++++ pyaccuwage/model.py | 12 +++++++++++- pyaccuwage/record.py | 32 ++++++++++++++++---------------- setup.py | 2 +- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/pyaccuwage/fields.py b/pyaccuwage/fields.py index 282376d..5839508 100644 --- a/pyaccuwage/fields.py +++ b/pyaccuwage/fields.py @@ -12,6 +12,7 @@ class ValidationError(Exception): return "(%s.%s) %s" % (self.field.parent_name, self.field.name, self.msg) else: return repr(self.msg) + class Field(object): creation_counter = 0 @@ -49,6 +50,12 @@ class Field(object): self.value = s.strip() +class FieldInterface(object): + def __init__(self, value_dict, field): + self.value_dict = value_dict + self.field = field + + class TextField(Field): def validate(self): if self.value == None and self.required: diff --git a/pyaccuwage/model.py b/pyaccuwage/model.py index f3b8658..65e8f6e 100644 --- a/pyaccuwage/model.py +++ b/pyaccuwage/model.py @@ -1,10 +1,12 @@ -from fields import Field +from fields import Field, FieldWrapper class Model(object): record_identifier = ' ' required = False def __init__(self): + self.value_dict = {} + for (key, value) in self.__class__.__dict__.items(): if isinstance(value, Field): field = getattr(self, key) @@ -13,11 +15,19 @@ class Model(object): setattr(field, 'parent_name', self.__class__.__name__) def __setattr__(self, key, value): + print "Model.__setattr__", self, key, value if hasattr(self, key) and isinstance(getattr(self, key), Field): getattr(self, key).value = value else: self.__dict__[key] = value + def __getattribute__(self, key): + result = object.__getattribute__(self, key) + if isinstance(result, Field): + return FieldWrapper(self.value_dict, result) + return result + + def get_fields(self): fields = [] for key in self.__class__.__dict__.keys(): diff --git a/pyaccuwage/record.py b/pyaccuwage/record.py index 30a61b8..6ab4226 100644 --- a/pyaccuwage/record.py +++ b/pyaccuwage/record.py @@ -163,23 +163,23 @@ class EmployeeWageRecord(model.Model): medicare_tax_withheld = MoneyField(max_length=11) social_security_tips = MoneyField(max_length=11) advance_eic = MoneyField(max_length=11) - dependent_care_benefits = MoneyField(max_length=11) - deferred_compensation_401k = MoneyField(max_length=11) - deferred_compensation_403b = MoneyField(max_length=11) - deferred_compensation_408k = MoneyField(max_length=11) - deferred_compensation_457b = MoneyField(max_length=11) - deferred_compensation_501c = MoneyField(max_length=11) - military_pay = MoneyField(max_length=11) - non_qualified_457 = MoneyField(max_length=11) - employer_contrib_to_hsa = MoneyField(max_length=11) - non_qualified_not_457 = MoneyField(max_length=11) - nontaxable_combat_pay = MoneyField(max_length=11) + dependent_care_benefits = MoneyField(max_length=11, required=False) + deferred_compensation_401k = MoneyField(max_length=11, required=False) + deferred_compensation_403b = MoneyField(max_length=11, required=False) + deferred_compensation_408k = MoneyField(max_length=11, required=False) + deferred_compensation_457b = MoneyField(max_length=11, required=False) + deferred_compensation_501c = MoneyField(max_length=11, required=False) + military_pay = MoneyField(max_length=11, required=False) + non_qualified_457 = MoneyField(max_length=11, required=False) + employer_contrib_to_hsa = MoneyField(max_length=11, required=False) + non_qualified_not_457 = MoneyField(max_length=11, required=False) + nontaxable_combat_pay = MoneyField(max_length=11, required=False) blank2 = BlankField(max_length=11) - cost_of_premiums_for_insurance = MoneyField(max_length=11) - income_nonstatutory_stock_opts = MoneyField(max_length=11) - deferred_compensation_409a = MoneyField(max_length=11) - designated_roth_contrib_401k = MoneyField(max_length=11) - designated_roth_contrib_403b = MoneyField(max_length=11) + cost_of_premiums_for_insurance = MoneyField(max_length=11, required=False) + income_nonstatutory_stock_opts = MoneyField(max_length=11, required=False) + deferred_compensation_409a = MoneyField(max_length=11, required=False) + designated_roth_contrib_401k = MoneyField(max_length=11, required=False) + designated_roth_contrib_403b = MoneyField(max_length=11, required=False) blank3 = BlankField(max_length=23) statutory_employee_indicator = BooleanField() blank4 = BlankField(max_length=1) diff --git a/setup.py b/setup.py index 14fba7a..f6d8825 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from distutils.core import setup setup(name='pyaccuwage', - version='0.0.3', + version='0.0.4', packages=['pyaccuwage'], zip_safe=True, )