示例#1
0
    def test_passwordfield(self):
        class User(Document):
            password = StringField()

        UserForm = model_form(User, field_args={'password': {'password': True}})
        form = UserForm(password='******')
        self.assertEqual(wtforms.widgets.PasswordInput, type(form.password.widget))
示例#2
0
    def test_binaryfield(self):
        class Binary(Document):
            binary = BinaryField()

        BinaryForm = model_form(Binary)
        form = BinaryForm(MultiDict({'binary': '1'}))
        self.assertTrue(form.validate())
        form.save()
示例#3
0
    def test_model_form_only(self):
        class BlogPost(Document):
            title = StringField(required=True, max_length=200)
            posted = DateTimeField(default=datetime.datetime.now)
            tags = ListField(StringField())

        BlogPost.drop_collection()

        BlogPostForm = model_form(BlogPost, only=['tags'])
        form = BlogPostForm()
        self.assertTrue(hasattr(form, 'tags'))
        self.assertFalse(hasattr(form, 'posted'))

        BlogPostForm = model_form(BlogPost, exclude=['posted'])
        form = BlogPostForm()
        self.assertTrue(hasattr(form, 'tags'))
        self.assertFalse(hasattr(form, 'posted'))
示例#4
0
    def test_model_form(self):
        class BlogPost(Document):
            meta = {'allow_inheritance': True}
            title = StringField(required=True, max_length=200)
            posted = DateTimeField(default=datetime.datetime.now)
            tags = ListField(StringField())

        class TextPost(BlogPost):
            email = EmailField(required=False)
            content = StringField(required=True)

        class LinkPost(BlogPost):
            url = StringField(required=True)

        # Create a text-based post
        TextPostForm = model_form(TextPost)

        form = TextPostForm(**{
            'title': 'Using MongoEngine',
            'tags': ['mongodb', 'mongoengine']})

        self.assertFalse(form.validate())

        form = TextPostForm(**{
            'title': 'Using MongoEngine',
            'content': 'See the tutorial',
            'tags': ['mongodb', 'mongoengine']})

        self.assertTrue(form.validate())
        form.save()

        self.assertEquals(BlogPost.objects.first().title, 'Using MongoEngine')
        self.assertEquals(BlogPost.objects.count(), 1)

        form = TextPostForm(**{
            'title': 'Using Flask-MongoEngine',
            'content': 'See the tutorial',
            'tags': ['flask', 'mongodb', 'mongoengine']})

        self.assertTrue(form.validate())
        form.save()
        self.assertEquals(BlogPost.objects.count(), 2)

        post = BlogPost.objects(title="Using Flask-MongoEngine").get()

        form = TextPostForm(MultiDict({
            'title': 'Using Flask-MongoEngine',
            'content': 'See the tutorial',
            'tags-0': 'flask',
            'tags-1': 'mongodb',
            'tags-2': 'mongoengine',
            'tags-3': 'flask-mongoengine',
        }), instance=post)
        self.assertTrue(form.validate())
        form.save()
        post = post.reload()

        self.assertEqual(post.tags, ['flask', 'mongodb', 'mongoengine', 'flask-mongoengine'])
示例#5
0
    def test_model_form_help_text(self):
        class BlogPost(Document):
            title = StringField(required=True, help_text="Some imaginative title to set the world on fire")

        post = BlogPost(title="hello world").save()

        BlogPostForm = model_form(BlogPost)
        form = BlogPostForm(instance=post)

        self.assertEqual(form.title.description, "Some imaginative title to set the world on fire")
示例#6
0
    def test_list_choices_coerce(self):
        CHOICES = ((1, "blue"), (2, "red"))

        class MyChoices(Document):
            pill = ListField(IntField(choices=CHOICES))

        MyChoicesForm = model_form(MyChoices)
        form = MyChoicesForm(MultiDict({"pill": "1"}))
        self.assertTrue(form.validate())
        form.save()
        self.assertEqual(MyChoices.objects.first().pill[0], 1)
示例#7
0
    def test_emailfield(self):
        class Email(Document):
            email = EmailField(required=False)

        EmailForm = model_form(Email)
        form = EmailForm(instance=Email())
        self.assertFalse("None" in "%s" % form.email)
        self.assertTrue(form.validate())

        form = EmailForm(MultiDict({"email": ""}))
        self.assertFalse("None" in "%s" % form.email)
        self.assertTrue(form.validate())

        # Ensure required works

        class Email(Document):
            email = EmailField(required=True)

        EmailForm = model_form(Email)
        form = EmailForm(MultiDict({"email": ""}))
        self.assertFalse("None" in "%s" % form.email)
        self.assertFalse(form.validate())
示例#8
0
    def test_embedded_model_form(self):
        class Content(EmbeddedDocument):
            text = StringField()
            lang = StringField(max_length=3)

        class Post(Document):
            title = StringField(max_length=120, required=True)
            tags = ListField(StringField(max_length=30))
            content = EmbeddedDocumentField("Content")

        PostForm = model_form(Post)
        form = PostForm()
        self.assertTrue("content-text" in "%s" % form.content.text)
示例#9
0
    def test_sub_field_args(self):
        class TestModel(Document):
            lst = ListField(StringField())

        field_args = {'lst': {'label': 'Custom Label',
                              'field_args': {'widget': wtforms.widgets.HiddenInput(),
                                             'label': "Hidden Input"}}}
        CustomForm = model_form(TestModel, field_args=field_args)

        custom_form = CustomForm(obj=TestModel(lst=["Foo"]))
        list_label = str(custom_form.lst.label)
        self.assertTrue("Custom Label" in list_label)
        self.assertTrue("Hidden Input" not in list_label)

        sub_label = str(custom_form.lst)
        self.assertTrue("Hidden Input" in sub_label)
示例#10
0
    def test_modelselectfield(self):
        class Dog(Document):
            name = StringField()

        class DogOwner(Document):
            dog = ReferenceField(Dog)

        DogOwnerForm = model_form(DogOwner)

        dog = Dog(name="fido")
        dog.save()

        form = DogOwnerForm(dog=dog)
        self.assertTrue(form.validate())

        self.assertEqual(wtforms.widgets.Select, type(form.dog.widget))
        self.assertEqual(False, form.dog.widget.multiple)
示例#11
0
    def test_model_form_with_custom_query_set(self):
        class Dog(Document):
            breed = StringField()

            @queryset_manager
            def large_objects(cls, queryset):
                return queryset(breed__in=['german sheppard', 'wolfhound'])

        class DogOwner(Document):
            dog = ReferenceField(Dog)

        big_dogs = [Dog(breed="german sheppard"), Dog(breed="wolfhound")]
        dogs = [Dog(breed="poodle")] + big_dogs
        for dog in dogs:
            dog.save()

        BigDogForm = model_form(DogOwner, field_args={'dog': {'queryset': Dog.large_objects}})

        form = BigDogForm(dog=big_dogs[0])
        self.assertTrue(form.validate())
        self.assertEqual(big_dogs, [d[1] for d in form.dog.iter_choices()])
示例#12
0
    def test_modelselectfield_multiple_selected_elements_must_be_retained(self):
        class Dog(Document):
            name = StringField()

            def __unicode__(self):
                return self.name

        class DogOwner(Document):
            dogs = ListField(ReferenceField(Dog))

        DogOwnerForm = model_form(DogOwner)

        fido = Dog(name="fido").save()
        Dog(name="rex").save()

        dogOwner = DogOwner(dogs=[fido])
        form = DogOwnerForm(obj=dogOwner)
        html = form.dogs()

        m = re.search("<option selected .+?>(.*?)</option>", html)
        self.assertTrue(m is not None, "Should have one selected option")
        self.assertEqual("fido", m.group(1))
示例#13
0
    def test_modelselectfield_multiple(self):
        class Dog(Document):
            name = StringField()

        class DogOwner(Document):
            dogs = ListField(ReferenceField(Dog))

        DogOwnerForm = model_form(DogOwner)

        dogs = [Dog(name="fido"), Dog(name="rex")]
        for dog in dogs:
            dog.save()

        form = DogOwnerForm(dogs=dogs)
        self.assertTrue(form.validate())

        self.assertEqual(wtforms.widgets.Select, type(form.dogs.widget))
        self.assertEqual(True, form.dogs.widget.multiple)

        # Validate if both dogs are selected
        choices = list(form.dogs)
        self.assertEqual(len(choices), 2)
        self.assertTrue(choices[0].checked)
        self.assertTrue(choices[1].checked)