<s> '''<STR_LIT>''' <EOL> import logging <EOL> from datetime import datetime <EOL> from webapp2_extras import json <EOL> import settings <EOL> import utils <EOL> from google . appengine . ext import db <EOL> class UnsupportedFieldTypeError ( Exception ) : pass <EOL> class InvalidFieldValueError ( Exception ) : pass <EOL> class RESTModel ( db . Model ) : <EOL> @ classmethod <EOL> def validate ( cls , kvs ) : <EOL> pass <EOL> @ classmethod <EOL> def get ( cls , i ) : <EOL> return cls . get_by_id ( i ) <EOL> @ classmethod <EOL> def put ( cls , kvs , entity = None ) : <EOL> for k in kvs . keys ( ) : <EOL> if not k in cls . properties ( ) : <EOL> del kvs [ k ] <EOL> continue <EOL> v = cls . __dict__ [ k ] <EOL> if isinstance ( v , db . IntegerProperty ) : <EOL> kvs [ k ] = int ( kvs [ k ] ) <EOL> elif isinstance ( v , db . FloatProperty ) : <EOL> kvs [ k ] = float ( kvs [ k ] ) <EOL> elif isinstance ( v , db . BooleanProperty ) : <EOL> kvs [ k ] = True if kvs [ k ] == '<STR_LIT:True>' else False <EOL> elif isinstance ( v , db . StringProperty ) or isinstance ( v , db . TextProperty ) : <EOL> kvs [ k ] = kvs [ k ] <EOL> elif isinstance ( v , db . DateProperty ) : <EOL> kvs [ k ] = datetime . strptime ( kvs [ k ] , settings . DATE_FORMAT ) . date ( ) <EOL> elif isinstance ( v , db . LinkProperty ) : <EOL> kvs [ k ] = kvs [ k ] <EOL> elif isinstance ( v , db . EmailProperty ) : <EOL> kvs [ k ] = kvs [ k ] <EOL> else : <EOL> raise UnsupportedFieldTypeError ( v ) <EOL> cls . validate ( kvs ) <EOL> try : <EOL> if not entity : <EOL> entity = cls ( ** kvs ) <EOL> else : <EOL> for k in kvs . keys ( ) : <EOL> setattr ( entity , k , kvs [ k ] ) <EOL> db . put ( entity ) <EOL> except Exception as e : <EOL> logging . error ( "<STR_LIT>" % kvs ) <EOL> logging . error ( e ) <EOL> return None <EOL> return entity <EOL> @ classmethod <EOL> def post ( cls , i , kvs ) : <EOL> entity = cls . get_by_id ( i ) <EOL> return cls . put ( kvs , entity = entity ) <EOL> @ classmethod <EOL> def delete1 ( cls , i ) : <EOL> item = cls . get_by_id ( i ) <EOL> if item : <EOL> item . delete ( ) <EOL> return True <EOL> return False <EOL> @ property <EOL> def str ( self ) : <EOL> return str ( json . encode ( utils . to_dict ( self ) ) ) <EOL> def generate_model_form ( cls , with_key = False ) : <EOL> fields = filter ( lambda x : issubclass ( type ( x [ <NUM_LIT:1> ] ) , db . Property ) , cls . __dict__ . iteritems ( ) ) <EOL> form = [ { '<STR_LIT>' : '<STR_LIT:text>' , '<STR_LIT:label>' : '<STR_LIT:id>' , '<STR_LIT:id>' : '<STR_LIT:id>' } ] if with_key else [ ] <EOL> for ( k , t ) in fields : <EOL> d = { '<STR_LIT:label>' : k , '<STR_LIT:id>' : k } <EOL> if isinstance ( t , db . IntegerProperty ) or isinstance ( t , db . FloatProperty ) : <EOL> d [ '<STR_LIT>' ] = '<STR_LIT:text>' <EOL> d [ '<STR_LIT:class>' ] = '<STR_LIT>' <EOL> elif isinstance ( t , db . StringProperty ) : <EOL> d [ '<STR_LIT>' ] = '<STR_LIT:text>' <EOL> elif isinstance ( t , db . TextProperty ) : <EOL> d [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> d [ '<STR_LIT:class>' ] = '<STR_LIT>' <EOL> elif isinstance ( t , db . DateProperty ) : <EOL> d [ '<STR_LIT>' ] = '<STR_LIT:text>' <EOL> d [ '<STR_LIT:class>' ] = '<STR_LIT:date>' <EOL> d [ '<STR_LIT>' ] = settings . DATE_FORMAT_HTML <EOL> elif isinstance ( t , db . BooleanProperty ) : <EOL> d [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> d [ '<STR_LIT:class>' ] = '<STR_LIT>' <EOL> elif isinstance ( t , db . EmailProperty ) : <EOL> d [ '<STR_LIT>' ] = '<STR_LIT:text>' <EOL> d [ '<STR_LIT:class>' ] = '<STR_LIT:email>' <EOL> elif isinstance ( t , db . LinkProperty ) : <EOL> d [ '<STR_LIT>' ] = '<STR_LIT:text>' <EOL> d [ '<STR_LIT:class>' ] = '<STR_LIT:url>' <EOL> else : <EOL> raise UnsupportedFieldTypeError ( k ) <EOL> form . append ( d ) <EOL> return form <EOL> class Widget ( RESTModel ) : <EOL> int_field = db . IntegerProperty ( required = True ) <EOL> boolean_field = db . BooleanProperty ( required = True ) <EOL> string_field = db . StringProperty ( required = True ) <EOL> text_field = db . TextProperty ( required = True ) <EOL> email_field = db . EmailProperty ( required = True ) <EOL> link_field = db . LinkProperty ( required = True ) <EOL> date_field = db . DateProperty ( required = True ) <EOL> class EmailAddr ( db . Model ) : <EOL> email = db . EmailProperty ( required = True ) </s>
