Пример #1
0
class ModelFormTest(TestCase):
    F = model_form(test_models.User, exclude=['id'], field_args={
        'posts': {
            'validators': [validators.NumberRange(min=4, max=7)],
            'description': 'Test'
        }
    })
    form = F()
    form_with_pk = model_form(test_models.User)()
    form_with_only = model_form(test_models.User, only=['nullbool', 'birthday'])()

    def test_form_sanity(self):
        self.assertEqual(self.F.__name__, 'UserForm')
        self.assertEqual(len([x for x in self.form]), 14)
        self.assertEqual(len([x for x in self.form_with_pk]), 15)
        self.assertEqual(len([x for x in self.form_with_only]), 2)

    def test_label(self):
        self.assertEqual(self.form.reg_ip.label.text, 'IP Addy')
        self.assertEqual(self.form.posts.label.text, 'posts')

    def test_description(self):
        self.assertEqual(self.form.birthday.description, 'Teh Birthday')

    def test_max_length(self):
        self.assertTrue(contains_validator(self.form.username, validators.Length))
        self.assertFalse(contains_validator(self.form.posts, validators.Length))

    def test_optional(self):
        self.assertTrue(contains_validator(self.form.email, validators.Optional))

    def test_simple_fields(self):
        self.assertEqual(type(self.form.file), fields.FileField)
        self.assertEqual(type(self.form.file2), fields.FileField)
        self.assertEqual(type(self.form_with_pk.id), fields.IntegerField)
        self.assertEqual(type(self.form.slug), fields.TextField)
        self.assertEqual(type(self.form.birthday), fields.DateField)

    def test_custom_converters(self):
        self.assertEqual(type(self.form.email), fields.TextField)
        self.assertTrue(contains_validator(self.form.email, validators.Email))
        self.assertEqual(type(self.form.reg_ip), fields.TextField)
        self.assertTrue(contains_validator(self.form.reg_ip, validators.IPAddress))
        self.assertEqual(type(self.form.group_id), ModelSelectField)

    def test_us_states(self):
        self.assertTrue(len(self.form.state.choices) >= 50)

    def test_field_args(self):
        self.assertTrue(contains_validator(self.form.posts, validators.NumberRange))
        self.assertEqual(self.form.posts.description, 'Test')

    def test_nullbool(self):
        field = self.form.nullbool
        assert isinstance(field, fields.SelectField)
        self.assertEqual(len(field.choices), 3)
        pairs = (('True', True), ('False', False), ('None', None), ('2', True), ('0', False))
        for input_val, expected in pairs:
            form = self.F(DummyPostData(nullbool=[input_val]))
            assert form.nullbool.data is expected
Пример #2
0
class ModelFormTest(TestCase):
    F = model_form(test_models.User,
                   exclude=['id'],
                   field_args={
                       'posts': {
                           'validators':
                           [validators.NumberRange(min=4, max=7)],
                           'description': 'Test'
                       }
                   })
    form = F()
    form_with_pk = model_form(test_models.User)()

    def test_form_sanity(self):
        self.assertEqual(self.F.__name__, 'UserForm')
        self.assertEqual(len([x for x in self.form]), 13)
        self.assertEqual(len([x for x in self.form_with_pk]), 14)

    def test_label(self):
        self.assertEqual(self.form.reg_ip.label.text, 'IP Addy')
        self.assertEqual(self.form.posts.label.text, 'posts')

    def test_description(self):
        self.assertEqual(self.form.birthday.description, 'Teh Birthday')

    def test_max_length(self):
        self.assertTrue(
            contains_validator(self.form.username, validators.Length))
        self.assertFalse(contains_validator(self.form.posts,
                                            validators.Length))

    def test_optional(self):
        self.assertTrue(
            contains_validator(self.form.email, validators.Optional))

    def test_simple_fields(self):
        self.assertEqual(type(self.form.file), fields.FileField)
        self.assertEqual(type(self.form.file2), fields.FileField)
        self.assertEqual(type(self.form_with_pk.id), fields.IntegerField)
        self.assertEqual(type(self.form.slug), fields.TextField)
        self.assertEqual(type(self.form.birthday), fields.DateField)

    def test_custom_converters(self):
        self.assertEqual(type(self.form.email), fields.TextField)
        self.assertTrue(contains_validator(self.form.email, validators.Email))
        self.assertEqual(type(self.form.reg_ip), fields.TextField)
        self.assertTrue(
            contains_validator(self.form.reg_ip, validators.IPAddress))
        self.assertEqual(type(self.form.group_id), ModelSelectField)

    def test_us_states(self):
        self.assertTrue(len(self.form.state.choices) >= 50)

    def test_field_args(self):
        self.assertTrue(
            contains_validator(self.form.posts, validators.NumberRange))
        self.assertEqual(self.form.posts.description, 'Test')
Пример #3
0
# Create your views here.

from django.views.decorators.http import require_http_methods

from django.http import HttpResponse
from django.http import HttpResponseRedirect

from django.core.context_processors import csrf

from django.shortcuts import render_to_response, redirect

from wtforms.ext.django.orm import model_form
from alaskahackathon.hackathon.models import Subscriber

SubscriberForm = model_form(Subscriber, exclude=['date'])

def comingsoon(request):
    return render_to_response('comingsoon.html')

@require_http_methods(["GET", "POST"])
def main(request):
    subscriber_form = SubscriberForm()
    subscribed = False

    if request.method == 'POST':
        if request.POST['submit'] == 'Subscribe':
            request.session['subscriber_form'] = request.POST
        else:
            pass

        return redirect('index')
Пример #4
0
__author__ = 'Konstantin Oficerov'
__email__ = '*****@*****.**'

import wtforms
from calendar_scheduler.core.models import CalendarEvent
from wtforms.ext.django.orm import model_form


BaseCalendarForm = model_form(CalendarEvent)


class CalendarForm(BaseCalendarForm):
    pass
Пример #5
0
from django.views.decorators.http import require_http_methods

from django.http import HttpResponse
from django.http import HttpResponseRedirect

from django.core.context_processors import csrf

from django.core.mail import send_mail

from django.shortcuts import render_to_response, redirect

from wtforms.ext.django.orm import model_form
from alaskahackathon.nonprofits.models import Application

ApplicationForm = model_form(Application, exclude=['id','date'])

@require_http_methods(["GET", "POST"])
def application(request):
    application_form = ApplicationForm()
    entered = False

    if request.method == 'POST':
        if request.POST['submit'] == 'Enter':
            request.session['np_application_form'] = request.POST
        else:
            pass

        return redirect('nonprofit-application')

    else: