Commits

James Crasta committed 7a1d354 Merge

Merged in hvdklauw/wtforms (pull request #30)

  • Participants
  • Parent commits a64990f, 53579d9

Comments (0)

Files changed (2)

File tests/ext_sqlalchemy.py

 
 from sqlalchemy import create_engine, ForeignKey
 from sqlalchemy.schema import MetaData, Table, Column
-from sqlalchemy.types import String, Integer, Date
+from sqlalchemy.types import String, Integer, Date, Enum
 from sqlalchemy.orm import sessionmaker, relationship, backref
 from sqlalchemy.ext.declarative import declarative_base
 
 from wtforms.compat import text_type, iteritems
 from wtforms.ext.sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField
 from wtforms.form import Form
-from wtforms.fields import TextField
+from wtforms.fields import TextField, SelectField
 from wtforms.ext.sqlalchemy.orm import model_form
 from wtforms.validators import Optional, Required, Length
 from wtforms.ext.sqlalchemy.validators import Unique
             __tablename__ = "student"
             id = Column(Integer, primary_key=True)
             full_name = Column(String(255), nullable=False, unique=True)
+            gender = Column(Enum('male', 'female'), nullable=False)
             dob = Column(Date(), nullable=True)
             current_school_id = Column(Integer, ForeignKey(School.id),
                 nullable=False)
         self.assertTrue(issubclass(QuerySelectMultipleField,
             student_form._fields['courses'].__class__))
 
+    def test_dropdown_for_enum(self):
+        student_form = model_form(self.Student, self.sess)()
+        self.assertTrue(issubclass(SelectField,
+            student_form._fields['gender'].__class__))
+
+    def test_options_for_enum(self):
+        student_form = model_form(self.Student, self.sess)()
+        self.assertEqual([('male', 'male'), ('female', 'female')],
+            student_form._fields['gender'].choices)
+
 
 class UniqueValidatorTest(TestCase):
     def setUp(self):

File wtforms/ext/sqlalchemy/orm.py

     def conv_DateTime(self, field_args, **extra):
         return f.DateTimeField(**field_args)
 
+    @converts('Enum')
+    def conv_Enum(self, column, field_args, **extra):
+        field_args['choices'] = [(e, e) for e in column.type.enums]
+        return f.SelectField(**field_args)
+
     @converts('Integer', 'SmallInteger')
     def handle_integer_types(self, column, field_args, **extra):
         unsigned = getattr(column.type, 'unsigned', False)