Skip to content
This repository has been archived by the owner on Aug 18, 2019. It is now read-only.

Commit

Permalink
None fields are ot saved
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsca committed Dec 16, 2014
1 parent 80ec0fe commit e4e4c04
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.py[cod]
.DS_Store
.idea

# C extensions
*.so
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ python:
- "2.6"
- "3.3"
# command to install dependencies
install: pip install pytest sqlalchemy_wrapper -r requirements.txt
install: pip install -r requirements-tests.txt
# command to run tests, e.g. python setup.py test
script: py.test tests
3 changes: 3 additions & 0 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r requirements.txt
pytest-cov
sqlalchemy_wrapper
10 changes: 6 additions & 4 deletions solution/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,11 @@ def _save_new_object(self, backref_obj=None):
db = self._model.db
data = dict([
(key, val) for key, val in self.cleaned_data.items()
if not isinstance(getattr(self, key), FormSet)
if (
(not isinstance(getattr(self, key), FormSet)) and
(getattr(self, key) is not None)
)
])

if self._backref and backref_obj:
data[self._backref] = backref_obj

Expand All @@ -319,8 +321,8 @@ def save_to(self, obj):
for key in self.changed_fields:
if key in self.cleaned_data:
val = self.cleaned_data.get(key)
set_obj_value(obj, key, val)

if val is not None:
set_obj_value(obj, key, val)
return obj

def __repr__(self):
Expand Down
98 changes: 91 additions & 7 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,91 @@ class MyContactForm(f.Form):
db.commit()


def init_subform_with_classes():
class FormA(f.Form):
a = f.Text()

class WrapForm(f.Form):
fa = FormA()

data = {
'fa.a': u'A',
}
form = WrapForm(data)
assert form.save() == data

user_data = {
'fa.a': u'AAA',
}
form = WrapForm(user_data, data)
assert form.save() == user_data


def test_dont_save_not_used_fields():

class MySubForm(f.Form):
a = f.Text()
b = f.Text()
c = f.Text()

class MyForm(f.Form):
subject = f.Text()
email = f.Text()
foobar = f.Boolean()
message1 = f.Text()
message2 = f.Text()
subform = MySubForm()
formset = f.FormSet(MySubForm)

user_data = {
'email': u'new@example.com',
'foobar': False,
'subform.c': 4,
'formset.1-c': 54,
'formset.2-c': 64,
'formset.3-c': 74,
}
original_data = {
'subject': u'foo',
'email': u'test@example.com',
'subform': {
'a': 1,
'c': 3,
},
'formset': [
{'a': 51, 'c': 52},
{'a': 61, 'c': 62},
{'a': 71, 'c': 72},
]
}

form = MyForm(user_data, original_data)
assert form.is_valid()
saved_data = form.save()

assert saved_data['subject'] == u'foo'
assert saved_data['email'] == u'new@example.com'
assert saved_data['foobar'] == False
assert 'message1' not in saved_data.keys()
assert 'message2' not in saved_data.keys()

assert saved_data['subform']['a'] == 1
assert 'b' not in saved_data['subform'].keys()
assert saved_data['subform']['c'] == 4

assert saved_data['formset'][0]['a'] == 51
assert 'b' not in saved_data['formset'][0].keys()
assert saved_data['formset'][0]['c'] == 54

assert saved_data['formset'][1]['a'] == 61
assert 'b' not in saved_data['formset'][1].keys()
assert saved_data['formset'][1]['c'] == 64

assert saved_data['formset'][2]['a'] == 71
assert 'b' not in saved_data['formset'][2].keys()
assert saved_data['formset'][2]['c'] == 74


def test_prefix_save():
db = SQLAlchemy()

Expand Down Expand Up @@ -369,7 +454,6 @@ class WrapForm(f.Form):
assert objb.b2 == data['fb.b2']



def test_formset_as_field():
class MyForm(f.Form):
a = f.Text(validate=[f.Required])
Expand Down Expand Up @@ -439,8 +523,9 @@ class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
user = db.relationship('User',
backref=db.backref('addresses', lazy='dynamic'))
user = db.relationship(
'User', backref=db.backref('addresses', lazy='dynamic')
)

def __repr__(self):
return '<Address %s>' % (self.email,)
Expand Down Expand Up @@ -512,8 +597,9 @@ class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
user = db.relationship('User',
backref=db.backref('addresses', lazy='dynamic'))
user = db.relationship(
'User', backref=db.backref('addresses', lazy='dynamic')
)

def __repr__(self):
return self.email
Expand Down Expand Up @@ -559,7 +645,6 @@ def test_formset_save_to_dict():
class FormAddress(f.Form):
email = f.Text(validate=[f.ValidEmail])


class FormUser(f.Form):
name = f.Text()
addresses = f.FormSet(FormAddress, parent='user')
Expand Down Expand Up @@ -589,7 +674,6 @@ def test_save_conflicting_field_names():
class FormValue(f.Form):
val = f.Text()


class FormUser(f.Form):
name = f.Text()
values = f.FormSet(FormValue, parent='user')
Expand Down
4 changes: 1 addition & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/solution
commands = py.test tests
deps =
pytest
sqlalchemy_wrapper
-r{toxinidir}/requirements.txt
-r{toxinidir}/requirements-tests.txt

0 comments on commit e4e4c04

Please sign in to comment.