Marat Khabibullin avatar Marat Khabibullin committed 3890188

some refactorings

Comments (0)

Files changed (5)

 ^docs/html
 ^dist/
 ^MANIFEST$
+^.idea/

wtforms/ext/mongoengine/document_update.py

+#coding=utf-8
+#Author: Marat Khabibullin <maratfm@gmail.com>
+from mongoengine.document import Document
+
+def _get_list(f):
+    def _wrapper(o, n):
+        if o and not isinstance(o, (list, tuple)):
+            raise TypeError('Must be list or tuple')
+        return f(o, n) or o
+    return _wrapper
+
+def update(self, **kwargs):
+    '''
+    Update method that updates current instance in memory and make
+    query to MongoDb.
+    '''
+    transforms = {
+        'set': lambda o, n: n,
+        'unset': lambda o, n: None,
+        'inc': lambda o, n: o + n,
+        'dec': lambda o, n: o - n,
+        'push': _get_list(lambda o, n: not o and [n] or o.appned(n)),
+        'push_all': _get_list(lambda o, n: not o and n or o.extend(n)),
+        'pull': _get_list(lambda o, n: n in o and o.remove(n)),
+        'pull_all': _get_list(lambda o, n: reduce(lambda x, y: y in o and o.remove(y), n, False)),
+        }
+
+    self.commit = kwargs.pop('commit', True)
+
+    for key, value in kwargs.iteritems():
+        parts = key.split('__')
+        old = getattr(self, parts[1])
+        value = transforms[parts[0]](old, value)
+        setattr(self, parts[1], value)
+
+    if self.commit:
+        return self.__class__.objects(id=self.id).update_one(**kwargs)
+
+Document.update = update
+

wtforms/ext/mongoengine/fields.py

+#coding=utf-8
+#Author: Marat Khabibullin <maratfm@gmail.com>
 """
 Useful form fields for use with the mongoengine.
 """
 from wtforms.fields import SelectFieldBase
 from wtforms.validators import ValidationError
 
-
-__all__ = (
-    'ModelSelectField', 'QuerySetSelectField',
-)
-
+__all__ = ('ModelSelectField', 'QuerySetSelectField',)
 
 class QuerySetSelectField(SelectFieldBase):
     """
     Like a QuerySetSelectField, except takes a model class instead of a
     queryset and lists everything in it.
     """
+
     def __init__(self, label=u'', validators=None, model=None, **kwargs):
         super(ModelSelectField, self).__init__(label, validators, queryset=model.objects, **kwargs)

wtforms/ext/mongoengine/models.py

+#coding=utf-8
+#Author: Marat Khabibullin <maratfm@gmail.com>
 from wtforms.form import Form
-
-def update_path(self, **kwargs):
-
-    def _get_list(f):
-        def _wrapper(o, n):
-            if o and not isinstance(o, (list, tuple)):
-                raise Exception('must be list')
-            return f(o, n) or o
-        return _wrapper
-
-    transforms = {
-        'set': lambda o, n: n,
-        'unset': lambda o, n: None,
-        'inc': lambda o, n: o + n ,
-        'dec': lambda o, n: o - n,
-        'push': _get_list(lambda o, n: not o and [n] or o.appned(n)),
-        'push_all': _get_list(lambda o, n: not o and n or o.extend(n)),
-        'pull': _get_list(lambda o, n: n in o and o.remove(n)),
-        'pull_all': _get_list(lambda o, n: reduce(lambda x, y: y in o and o.remove(y), n, False)),
-    }
-
-    self.commit = kwargs.pop('commit', True)
-
-    for key, value in kwargs.iteritems():
-        parts = key.split('__')
-        old = getattr(self, parts[1])
-        value = transforms[parts[0]](old, value)
-        setattr(self, parts[1], value)
-    if not self.commit:
-        return
-    return self.__class__.objects(id=self.id).update_one(**kwargs)
-
-# Path instance
-from mongoengine.document import Document
-Document.update = update_path
-
+from document_update import Document
 
 class ModelForm(Form):
+    model_class = None
     def __init__(self, *args, **kwargs):
-        #self.model_class = kwargs.pop('model_class', None)
         self.instance = kwargs['obj'] = kwargs.pop('instance', None) or \
                                         kwargs.get('obj', None)
+        self.model_class = kwargs.pop('model_class', None) or \
+                           self.model_class or \
+                           self.instance and self.instance.__class__
+
+        assert issubclass(self.model_class, Document), 'ModelForm model_class attribute must be subclass of Document'
+        assert self.instance is None or isinstance(self.instance, self.model_class), 'ModelForm instance attribute must be instance of model_class'
+
         super(ModelForm, self).__init__(*args, **kwargs)
 
     def save(self, commit=True):

wtforms/ext/mongoengine/orm.py

+#coding=utf-8
+#Author: Marat Khabibullin <maratfm@gmail.com>
 """
 Tools for generating forms based on mongoengine Document schemas.
 """
 from fields import ModelSelectField
 from models import ModelForm
 from wtforms import fields as f, validators
-from wtforms.form import Form
 import inspect
 
 __all__ = (
 
 def model_fields(model, only=None, exclude=None, field_args=None, converter=None):
     """
-    Generate a dictionary of fields for a given Django model.
-
-    See `model_form` docstring for description of parameters.
+    Generate a dictionary of fields for a given mongoengine.Document.
     """
     from mongoengine.base import BaseDocument
     if BaseDocument not in inspect.getmro(model):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.