<s> import wtforms <EOL> from flask . ext import wtf <EOL> from wtforms import validators <EOL> from collection . documents import Book <EOL> class BookForm ( wtf . Form ) : <EOL> document_class = Book <EOL> title = wtforms . TextField ( validators = [ validators . Required ( ) ] ) <EOL> year = wtforms . IntegerField ( validators = [ validators . Required ( ) ] ) <EOL> instance = None <EOL> def __init__ ( self , document = None , * args , ** kwargs ) : <EOL> super ( BookForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if document is not None : <EOL> self . instance = document <EOL> self . _copy_data_to_form ( ) <EOL> def _copy_data_to_form ( self ) : <EOL> self . title . data = self . instance . title <EOL> self . year . data = self . instance . year <EOL> def save ( self ) : <EOL> if self . instance is None : <EOL> self . instance = self . document_class ( ) <EOL> self . instance . title = self . title . data <EOL> self . instance . year = self . year . data <EOL> self . instance . save ( ) <EOL> return self . instance </s>
