Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor Collection.get_field to remove duplication.

  • Loading branch information...
commit f78281d87ca1289d8a1a7e2ed1139ed7b0cd5e77 1 parent daef745
@emgee emgee authored
Showing with 17 additions and 14 deletions.
  1. +17 −14 formish/forms.py
View
31 formish/forms.py
@@ -550,22 +550,25 @@ def widget(self):
def get_field(self, name):
""" recursively get dotted field names """
segments = name.split('.')
- # Bind '*' to a fake field.
if segments[0] == '*':
- b = self.bind('*', self.attr)
- if len(segments) == 1:
- return b
+ # Bind '*' to a fake field.
+ field = self.bind('*', self.attr)
+ else:
+ # Find named field.
+ for field in self.fields:
+ if field.name.split('.')[-1] == segments[0]:
+ break
else:
- return b.get_field('.'.join(segments[1:]))
- # Find the field in the segments.
- for field in self.fields:
- if field.name.split('.')[-1] == segments[0]:
- if len(segments) == 1:
- return field
- else:
- return field.get_field(''.join(segments[1:]))
- raise KeyError('%s %r has no field %r' % (self.__class__.__name__,
- self.name, segments[0]))
+ field = None
+ # Raise a nice error if the field was not found.
+ if not field:
+ raise KeyError('%s %r has no field %r' % (self.__class__.__name__,
+ self.name, segments[0]))
+ # Return field or recurse.
+ if len(segments) == 1:
+ return field
+ else:
+ return field.get_field('.'.join(segments[1:]))
def __getitem__(self, key):
Please sign in to comment.
Something went wrong with that request. Please try again.