Bumped version to 0.0.5
Fixed problem where fields contained shared values by performing a shallow copy on all fields during Record instantiation. That way, each record has its own copy of the field instances, rather than the shared class-wide instance provided by the definition.
This commit is contained in:
parent
4023d46b4a
commit
7cb8bed61e
4 changed files with 28 additions and 23 deletions
|
@ -1,6 +1,6 @@
|
|||
from record import *
|
||||
|
||||
VERSION = (0, 0, 3)
|
||||
VERSION = (0, 0, 5)
|
||||
|
||||
RECORD_TYPES = [
|
||||
'SubmitterRecord',
|
||||
|
@ -146,3 +146,16 @@ def verify_records(records):
|
|||
verify_required_records(records)
|
||||
verify_record_order(records)
|
||||
|
||||
def test_unique_fields():
|
||||
r1 = EmployeeWageRecord()
|
||||
|
||||
r1.employee_first_name.value = "John Johnson"
|
||||
|
||||
r2 = EmployeeWageRecord()
|
||||
print 'r1:', r1.employee_first_name.value, r1.employee_first_name, r1.employee_first_name.creation_counter
|
||||
print 'r2:', r2.employee_first_name.value, r2.employee_first_name, r2.employee_first_name.creation_counter
|
||||
|
||||
if r1.employee_first_name.value == r2.employee_first_name.value:
|
||||
raise ValidationError("Horrible problem involving shared values across records")
|
||||
|
||||
|
||||
|
|
|
@ -50,12 +50,6 @@ 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:
|
||||
|
|
|
@ -1,33 +1,31 @@
|
|||
from fields import Field, FieldWrapper
|
||||
from fields import Field
|
||||
import copy
|
||||
import pdb
|
||||
|
||||
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)
|
||||
if not field.name:
|
||||
setattr(field, 'name', key)
|
||||
setattr(field, 'parent_name', self.__class__.__name__)
|
||||
# GRAB THE FIELD INSTANCE FROM THE CLASS DEFINITION
|
||||
# AND MAKE A LOCAL COPY FOR THIS RECORD'S INSTANCE,
|
||||
# OTHERWISE WE'LL END UP WITH VALUES BEING SHARED
|
||||
# ACROSS RECORDS.
|
||||
src_field = self.__class__.__dict__[key]
|
||||
if not src_field.name:
|
||||
setattr(src_field, 'name', key)
|
||||
setattr(src_field, 'parent_name', self.__class__.__name__)
|
||||
self.__dict__[key] = copy.copy(src_field)
|
||||
|
||||
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:
|
||||
# MAYBE THIS SHOULD RAISE A PROPERTY ERROR?
|
||||
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():
|
||||
|
|
2
setup.py
2
setup.py
|
@ -1,6 +1,6 @@
|
|||
from distutils.core import setup
|
||||
setup(name='pyaccuwage',
|
||||
version='0.0.4',
|
||||
version='0.0.5',
|
||||
packages=['pyaccuwage'],
|
||||
zip_safe=True,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue