Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
melhorias na pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Apr 11, 2014
1 parent 8fe48d6 commit 06bfe62
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
1 change: 0 additions & 1 deletion examples/blogprj/mongotools

This file was deleted.

Binary file added examples/blogprj/mongotools
Binary file not shown.
32 changes: 20 additions & 12 deletions mongotools/forms/__init__.py
Expand Up @@ -11,14 +11,17 @@

__all__ = ('MongoForm',)


class MongoFormMetaClass(type):

"""Metaclass to create a new MongoForm."""

def __new__(cls, name, bases, attrs):
# get all valid existing Fields and sort them
fields = [(field_name, attrs.pop(field_name)) for field_name, obj in \
attrs.items() if isinstance(obj, forms.Field)]
fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
fields = [(field_name, attrs.pop(field_name)) for field_name, obj in
attrs.items() if isinstance(obj, forms.Field)]
fields.sort(
lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))

# get all Fields from base classes
for base in bases[::-1]:
Expand All @@ -27,20 +30,21 @@ def __new__(cls, name, bases, attrs):

# add the fields as "our" base fields
attrs['base_fields'] = SortedDict(fields)

# Meta class available?
if 'Meta' in attrs and hasattr(attrs['Meta'], 'document') and \
issubclass(attrs['Meta'].document, BaseDocument):
doc_fields = SortedDict()

formfield_generator = getattr(attrs['Meta'], 'formfield_generator', \
MongoFormFieldGenerator)()
formfield_generator = getattr(attrs['Meta'], 'formfield_generator',
MongoFormFieldGenerator)()

widgets = getattr(attrs["Meta"], "widgets", {})

# walk through the document fields
for field_name, field in iter_valid_fields(attrs['Meta']):
# add field and override clean method to respect mongoengine-validator
# add field and override clean method to respect
# mongoengine-validator

# use to get a custom widget
if hasattr(field, 'get_custom_widget'):
Expand All @@ -67,14 +71,17 @@ def __new__(cls, name, bases, attrs):
# maybe we need the Meta class later
attrs['_meta'] = attrs.get('Meta', object())

new_class = super(MongoFormMetaClass, cls).__new__(cls, name, bases, attrs)
new_class = super(MongoFormMetaClass, cls).__new__(
cls, name, bases, attrs)

if 'media' not in attrs:
new_class.media = media_property(new_class)

return new_class


class MongoForm(forms.BaseForm):

"""Base MongoForm class. Used to create new MongoForms"""
__metaclass__ = MongoFormMetaClass

Expand All @@ -85,7 +92,7 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=

assert isinstance(instance, (types.NoneType, BaseDocument)), \
'instance must be a mongoengine document, not %s' % \
type(instance).__name__
type(instance).__name__

assert hasattr(self, 'Meta'), 'Meta class is needed to use MongoForm'
# new instance or updating an existing one?
Expand Down Expand Up @@ -114,8 +121,8 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=
object_data.update(initial)

self._validate_unique = False
super(MongoForm, self).__init__(data, files, auto_id, prefix, object_data, \
error_class, label_suffix, empty_permitted)
super(MongoForm, self).__init__(data, files, auto_id, prefix, object_data,
error_class, label_suffix, empty_permitted)

def save(self, commit=True):
"""save the instance or create a new one.."""
Expand All @@ -131,7 +138,8 @@ def save(self, commit=True):

continue

setattr(self.instance, field_name, self.cleaned_data.get(field_name))
setattr(
self.instance, field_name, self.cleaned_data.get(field_name))

if commit:
self.instance.save()
Expand Down

0 comments on commit 06bfe62

Please sign in to comment.