<s> """<STR_LIT>""" <EOL> import cgi <EOL> import datetime <EOL> import math <EOL> import time <EOL> import types <EOL> import urllib <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import datastore <EOL> from google . appengine . api import datastore_types <EOL> from google . appengine . api import memcache <EOL> from google . appengine . api import users <EOL> from google . appengine . ext import db <EOL> from google . appengine . ext . db import metadata <EOL> from google . appengine . tools . devappserver2 . admin import admin_request_handler <EOL> def _format_datastore_key ( key ) : <EOL> """<STR_LIT>""" <EOL> path = key . to_path ( ) <EOL> parts = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( path ) // <NUM_LIT:2> ) : <EOL> kind = path [ i * <NUM_LIT:2> ] <EOL> value = path [ i * <NUM_LIT:2> + <NUM_LIT:1> ] <EOL> if isinstance ( value , ( int , long ) ) : <EOL> parts . append ( '<STR_LIT>' % ( kind , value ) ) <EOL> else : <EOL> parts . append ( '<STR_LIT>' % ( kind , value ) ) <EOL> return '<STR_LIT>' . join ( parts ) <EOL> def _property_name_to_values ( entities ) : <EOL> """<STR_LIT>""" <EOL> property_name_to_values = { } <EOL> for entity in entities : <EOL> for property_name , value in entity . iteritems ( ) : <EOL> property_name_to_values . setdefault ( property_name , [ ] ) . append ( value ) <EOL> return property_name_to_values <EOL> def _property_name_to_value ( entities ) : <EOL> """<STR_LIT>""" <EOL> return { key : values [ <NUM_LIT:0> ] <EOL> for ( key , values ) in _property_name_to_values ( entities ) . iteritems ( ) } <EOL> def _get_entities ( kind , namespace , order , start , count ) : <EOL> """<STR_LIT>""" <EOL> query = datastore . Query ( kind , _namespace = namespace ) <EOL> if order : <EOL> if order . startswith ( '<STR_LIT:->' ) : <EOL> direction = datastore . Query . DESCENDING <EOL> order = order [ <NUM_LIT:1> : ] <EOL> else : <EOL> direction = datastore . Query . ASCENDING <EOL> query . Order ( ( order , direction ) ) <EOL> total = query . Count ( ) <EOL> entities = query . Get ( count , start ) <EOL> return entities , total <EOL> class DataType ( object ) : <EOL> """<STR_LIT>""" <EOL> _MAX_SHORT_LENGTH = <NUM_LIT:30> <EOL> PLACEHOLDER = None <EOL> @ staticmethod <EOL> def get ( value ) : <EOL> return _DATA_TYPES [ value . __class__ ] <EOL> @ staticmethod <EOL> def get_by_name ( name ) : <EOL> return _NAMED_DATA_TYPES [ name ] <EOL> @ classmethod <EOL> def get_placholder_attribute ( cls ) : <EOL> if cls . PLACEHOLDER : <EOL> return '<STR_LIT>' % cgi . escape ( cls . PLACEHOLDER ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def format ( self , value ) : <EOL> if isinstance ( value , types . StringTypes ) : <EOL> return value <EOL> else : <EOL> return str ( value ) <EOL> def short_format ( self , value ) : <EOL> format = self . format ( value ) <EOL> if len ( format ) > self . _MAX_SHORT_LENGTH : <EOL> return format [ : self . _MAX_SHORT_LENGTH - <NUM_LIT:3> ] + '<STR_LIT>' <EOL> else : <EOL> return format <EOL> def input_field ( self , name , value , sample_values , back_uri ) : <EOL> string_value = self . format ( value ) if value else '<STR_LIT>' <EOL> return ( <EOL> '<STR_LIT>' % ( <EOL> cgi . escape ( self . name ( ) ) , <EOL> cgi . escape ( name ) , <EOL> self . input_field_size ( ) , <EOL> cgi . escape ( string_value , True ) , <EOL> self . get_placholder_attribute ( ) ) ) <EOL> def input_field_size ( self ) : <EOL> return <NUM_LIT:30> <EOL> class StringType ( DataType ) : <EOL> def input_field ( self , name , value , sample_values , back_uri ) : <EOL> string_value = self . format ( value ) if value else '<STR_LIT>' <EOL> sample_values = [ self . format ( s ) for s in sample_values ] <EOL> multiline = False <EOL> if value : <EOL> multiline = len ( string_value ) > <NUM_LIT:255> or string_value . find ( '<STR_LIT:\n>' ) >= <NUM_LIT:0> <EOL> if not multiline : <EOL> for sample_value in sample_values : <EOL> if sample_value and ( len ( sample_value ) > <NUM_LIT:255> or <EOL> sample_value . find ( '<STR_LIT:\n>' ) >= <NUM_LIT:0> ) : <EOL> multiline = True <EOL> break <EOL> if multiline : <EOL> return '<STR_LIT>' % ( <EOL> cgi . escape ( name ) , <EOL> self . get_placholder_attribute ( ) , <EOL> cgi . escape ( string_value ) ) <EOL> else : <EOL> return DataType . input_field ( self , name , value , sample_values , back_uri ) <EOL> def name ( self ) : <EOL> return '<STR_LIT:string>' <EOL> def parse ( self , value ) : <EOL> return value <EOL> def python_type ( self ) : <EOL> return str <EOL> def input_field_size ( self ) : <EOL> return <NUM_LIT:50> <EOL> class TextType ( StringType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def input_field ( self , name , value , sample_values , back_uri ) : <EOL> string_value = self . format ( value ) if value else '<STR_LIT>' <EOL> return '<STR_LIT>' % ( <EOL> cgi . escape ( name ) , <EOL> self . get_placholder_attribute ( ) , <EOL> cgi . escape ( string_value ) ) <EOL> def parse ( self , value ) : <EOL> return datastore_types . Text ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . Text <EOL> class ByteStringType ( StringType ) : <EOL> def format ( self , value ) : <EOL> if value is None : <EOL> return '<STR_LIT:None>' <EOL> r = value . encode ( '<STR_LIT>' ) <EOL> return r <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> bytestring = value . encode ( '<STR_LIT:ascii>' ) . decode ( '<STR_LIT>' ) <EOL> return datastore_types . ByteString ( bytestring ) <EOL> def python_type ( self ) : <EOL> return datastore_types . ByteString <EOL> class BlobType ( StringType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def input_field ( self , name , value , sample_values , back_uri ) : <EOL> return '<STR_LIT>' <EOL> def format ( self , value ) : <EOL> return '<STR_LIT>' <EOL> def python_type ( self ) : <EOL> return datastore_types . Blob <EOL> class EmbeddedEntityType ( BlobType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def python_type ( self ) : <EOL> return datastore_types . EmbeddedEntity <EOL> class TimeType ( DataType ) : <EOL> _FORMAT = '<STR_LIT>' <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def format ( self , value ) : <EOL> return value . isoformat ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:0> : <NUM_LIT> ] <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datetime . datetime ( * ( time . strptime ( value , <EOL> TimeType . _FORMAT ) [ <NUM_LIT:0> : <NUM_LIT:6> ] ) ) <EOL> def python_type ( self ) : <EOL> return datetime . datetime <EOL> class ListType ( DataType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT:list>' <EOL> def input_field ( self , name , value , sample_values , back_uri ) : <EOL> string_value = self . format ( value ) if value else '<STR_LIT>' <EOL> return cgi . escape ( string_value ) <EOL> def python_type ( self ) : <EOL> return list <EOL> class BoolType ( DataType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT:bool>' <EOL> def input_field ( self , name , value , sample_values , back_uri ) : <EOL> selected = { None : '<STR_LIT>' , False : '<STR_LIT>' , True : '<STR_LIT>' } <EOL> selected [ value ] = '<STR_LIT>' <EOL> return """<STR_LIT>""" % ( <EOL> cgi . escape ( self . name ( ) ) , cgi . escape ( name ) , selected [ None ] , <EOL> selected [ False ] , selected [ True ] ) <EOL> def parse ( self , value ) : <EOL> if value . lower ( ) == '<STR_LIT:true>' : <EOL> return True <EOL> if value . lower ( ) == '<STR_LIT:false>' : <EOL> return False <EOL> return bool ( int ( value ) ) <EOL> def python_type ( self ) : <EOL> return bool <EOL> class IntType ( DataType ) : <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def input_field_size ( self ) : <EOL> return <NUM_LIT:10> <EOL> def name ( self ) : <EOL> return '<STR_LIT:int>' <EOL> def parse ( self , value ) : <EOL> return int ( value ) <EOL> def python_type ( self ) : <EOL> return int <EOL> class FloatType ( DataType ) : <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def name ( self ) : <EOL> return '<STR_LIT:float>' <EOL> def parse ( self , value ) : <EOL> return float ( value ) <EOL> def python_type ( self ) : <EOL> return float <EOL> class UserType ( DataType ) : <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return users . User ( value ) <EOL> def python_type ( self ) : <EOL> return users . User <EOL> def input_field_size ( self ) : <EOL> return <NUM_LIT:15> <EOL> class ReferenceType ( DataType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def short_format ( self , value ) : <EOL> return str ( value ) [ : <NUM_LIT:8> ] + '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . Key ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . Key <EOL> def input_field ( self , name , value , sample_values , back_uri ) : <EOL> string_value = self . format ( value ) if value else '<STR_LIT>' <EOL> html = '<STR_LIT>' % ( <EOL> cgi . escape ( self . name ( ) ) , cgi . escape ( name ) , self . input_field_size ( ) , <EOL> cgi . escape ( string_value , True ) ) <EOL> if value : <EOL> html += '<STR_LIT>' % ( <EOL> cgi . escape ( string_value , True ) , <EOL> urllib . quote_plus ( back_uri ) , <EOL> cgi . escape ( _format_datastore_key ( value ) , True ) ) <EOL> return html <EOL> def input_field_size ( self ) : <EOL> return <NUM_LIT> <EOL> class EmailType ( StringType ) : <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . Email ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . Email <EOL> class CategoryType ( StringType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . Category ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . Category <EOL> class LinkType ( StringType ) : <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . Link ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . Link <EOL> class GeoPtType ( DataType ) : <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . GeoPt ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . GeoPt <EOL> class ImType ( DataType ) : <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . IM ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . IM <EOL> class PhoneNumberType ( StringType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . PhoneNumber ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . PhoneNumber <EOL> class PostalAddressType ( StringType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . PostalAddress ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . PostalAddress <EOL> class RatingType ( DataType ) : <EOL> PLACEHOLDER = '<STR_LIT>' <EOL> def input_field_size ( self ) : <EOL> return <NUM_LIT:5> <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . Rating ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . Rating <EOL> class NoneType ( DataType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT:None>' <EOL> def parse ( self , value ) : <EOL> return None <EOL> def python_type ( self ) : <EOL> return None <EOL> def format ( self , value ) : <EOL> return '<STR_LIT:None>' <EOL> class BlobKeyType ( StringType ) : <EOL> def name ( self ) : <EOL> return '<STR_LIT>' <EOL> def parse ( self , value ) : <EOL> return datastore_types . BlobKey ( value ) <EOL> def python_type ( self ) : <EOL> return datastore_types . BlobKey <EOL> _DATA_TYPES = { <EOL> types . NoneType : NoneType ( ) , <EOL> types . StringType : StringType ( ) , <EOL> types . UnicodeType : StringType ( ) , <EOL> datastore_types . Text : TextType ( ) , <EOL> datastore_types . Blob : BlobType ( ) , <EOL> datastore_types . EmbeddedEntity : EmbeddedEntityType ( ) , <EOL> types . BooleanType : BoolType ( ) , <EOL> types . IntType : IntType ( ) , <EOL> types . LongType : IntType ( ) , <EOL> types . FloatType : FloatType ( ) , <EOL> datetime . datetime : TimeType ( ) , <EOL> users . User : UserType ( ) , <EOL> datastore_types . Key : ReferenceType ( ) , <EOL> types . ListType : ListType ( ) , <EOL> datastore_types . Email : EmailType ( ) , <EOL> datastore_types . Category : CategoryType ( ) , <EOL> datastore_types . Link : LinkType ( ) , <EOL> datastore_types . GeoPt : GeoPtType ( ) , <EOL> datastore_types . IM : ImType ( ) , <EOL> datastore_types . PhoneNumber : PhoneNumberType ( ) , <EOL> datastore_types . PostalAddress : PostalAddressType ( ) , <EOL> datastore_types . Rating : RatingType ( ) , <EOL> datastore_types . BlobKey : BlobKeyType ( ) , <EOL> datastore_types . ByteString : ByteStringType ( ) , <EOL> } <EOL> _NAMED_DATA_TYPES = { } <EOL> for _data_type in _DATA_TYPES . values ( ) : <EOL> _NAMED_DATA_TYPES [ _data_type . name ( ) ] = _data_type <EOL> class DatastoreRequestHandler ( admin_request_handler . AdminRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> NUM_ENTITIES_PER_PAGE = <NUM_LIT:20> <EOL> @ staticmethod <EOL> def _calculate_writes_for_built_in_indices ( entity ) : <EOL> writes = <NUM_LIT:0> <EOL> for prop_name in entity . keys ( ) : <EOL> if not prop_name in entity . unindexed_properties ( ) : <EOL> prop_vals = entity [ prop_name ] <EOL> if isinstance ( prop_vals , ( list ) ) : <EOL> num_prop_vals = len ( prop_vals ) <EOL> else : <EOL> num_prop_vals = <NUM_LIT:1> <EOL> writes += <NUM_LIT:2> * num_prop_vals <EOL> return writes <EOL> @ staticmethod <EOL> def _calculate_writes_for_composite_index ( entity , index ) : <EOL> composite_index_value_count = <NUM_LIT:1> <EOL> for prop_name , _ in index . Properties ( ) : <EOL> if not prop_name in entity . keys ( ) or ( <EOL> prop_name in entity . unindexed_properties ( ) ) : <EOL> return <NUM_LIT:0> <EOL> prop_vals = entity [ prop_name ] <EOL> if isinstance ( prop_vals , ( list ) ) : <EOL> composite_index_value_count *= len ( prop_vals ) <EOL> ancestor_count = <NUM_LIT:1> <EOL> if index . HasAncestor ( ) : <EOL> key = entity . key ( ) . parent ( ) <EOL> while key is not None : <EOL> ancestor_count += <NUM_LIT:1> <EOL> key = key . parent ( ) <EOL> return composite_index_value_count * ancestor_count <EOL> @ classmethod <EOL> def _get_write_ops ( cls , entity ) : <EOL> writes = <NUM_LIT:2> + cls . _calculate_writes_for_built_in_indices ( entity ) <EOL> for index , _ in datastore . GetIndexes ( ) : <EOL> if index . Kind ( ) != entity . kind ( ) : <EOL> continue <EOL> writes += cls . _calculate_writes_for_composite_index ( entity , index ) <EOL> return writes <EOL> @ staticmethod <EOL> def _get_kinds ( namespace ) : <EOL> """<STR_LIT>""" <EOL> assert namespace is not None <EOL> q = metadata . Kind . all ( namespace = namespace ) <EOL> return sorted ( [ x . kind_name for x in q . run ( ) ] ) <EOL> @ classmethod <EOL> def _get_entity_template_data ( cls , <EOL> request_uri , <EOL> kind , <EOL> namespace , <EOL> order , <EOL> start ) : <EOL> entities , total_entities = _get_entities ( kind , <EOL> namespace , <EOL> order , <EOL> start , <EOL> cls . NUM_ENTITIES_PER_PAGE ) <EOL> property_name_to_value = _property_name_to_value ( entities ) <EOL> headers = [ { '<STR_LIT:name>' : property_name } <EOL> for property_name in sorted ( property_name_to_value ) ] <EOL> template_entities = [ ] <EOL> for entity in entities : <EOL> attributes = [ ] <EOL> for property_name in sorted ( property_name_to_value ) : <EOL> if property_name in entity : <EOL> raw_value = entity [ property_name ] <EOL> data_type = DataType . get ( raw_value ) <EOL> value = data_type . format ( raw_value ) <EOL> short_value = data_type . short_format ( raw_value ) <EOL> else : <EOL> value = '<STR_LIT>' <EOL> short_value = '<STR_LIT>' <EOL> attributes . append ( { '<STR_LIT:name>' : property_name , <EOL> '<STR_LIT:value>' : value , <EOL> '<STR_LIT>' : short_value , <EOL> } ) <EOL> edit_uri = '<STR_LIT>' % ( <EOL> entity . key ( ) , urllib . quote ( request_uri ) ) <EOL> template_entities . append ( <EOL> { '<STR_LIT>' : attributes , <EOL> '<STR_LIT>' : edit_uri , <EOL> '<STR_LIT:key>' : entity . key ( ) , <EOL> '<STR_LIT>' : entity . key ( ) . id ( ) , <EOL> '<STR_LIT>' : entity . key ( ) . name ( ) , <EOL> '<STR_LIT>' : str ( entity . key ( ) ) [ : <NUM_LIT:8> ] + '<STR_LIT>' , <EOL> '<STR_LIT>' : cls . _get_write_ops ( entity ) } ) <EOL> return headers , template_entities , total_entities <EOL> def get ( self ) : <EOL> datastore_stub = apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) <EOL> datastore_stub . Flush ( ) <EOL> kind = self . request . get ( '<STR_LIT>' , None ) <EOL> namespace = self . request . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> order = self . request . get ( '<STR_LIT>' , None ) <EOL> message = self . request . get ( '<STR_LIT:message>' , None ) <EOL> try : <EOL> page = int ( self . request . get ( '<STR_LIT>' , '<STR_LIT:1>' ) ) <EOL> except ValueError : <EOL> page = <NUM_LIT:1> <EOL> kinds = self . _get_kinds ( namespace ) <EOL> if not kind and kinds : <EOL> self . redirect ( self . _construct_url ( add = { '<STR_LIT>' : kinds [ <NUM_LIT:0> ] } ) ) <EOL> return <EOL> if kind : <EOL> start = ( page - <NUM_LIT:1> ) * self . NUM_ENTITIES_PER_PAGE <EOL> headers , template_entities , total_entities = ( <EOL> self . _get_entity_template_data ( <EOL> self . request . uri , <EOL> kind , <EOL> namespace , <EOL> order , <EOL> start ) ) <EOL> num_pages = int ( math . ceil ( float ( total_entities ) / <EOL> self . NUM_ENTITIES_PER_PAGE ) ) <EOL> else : <EOL> start = <NUM_LIT:0> <EOL> headers = [ ] <EOL> template_entities = [ ] <EOL> total_entities = <NUM_LIT:0> <EOL> num_pages = <NUM_LIT:0> <EOL> select_namespace_url = self . _construct_url ( <EOL> remove = [ '<STR_LIT:message>' ] , <EOL> add = { '<STR_LIT>' : self . request . get ( '<STR_LIT>' ) } ) <EOL> self . response . write ( self . render ( <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : template_entities , <EOL> '<STR_LIT>' : headers , <EOL> '<STR_LIT>' : kind , <EOL> '<STR_LIT>' : kinds , <EOL> '<STR_LIT:message>' : message , <EOL> '<STR_LIT>' : namespace , <EOL> '<STR_LIT>' : num_pages , <EOL> '<STR_LIT>' : order , <EOL> '<STR_LIT>' : self . _construct_url ( remove = [ '<STR_LIT:message>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : self . _construct_url ( remove = [ '<STR_LIT:message>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : select_namespace_url , <EOL> '<STR_LIT>' : self . request . get ( '<STR_LIT>' , None ) is not None , <EOL> '<STR_LIT:start>' : start , <EOL> '<STR_LIT>' : total_entities } ) ) <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . request . get ( '<STR_LIT>' ) : <EOL> if memcache . flush_all ( ) : <EOL> message = '<STR_LIT>' <EOL> else : <EOL> message = '<STR_LIT>' <EOL> self . redirect ( self . _construct_url ( remove = [ '<STR_LIT>' ] , <EOL> add = { '<STR_LIT:message>' : message } ) ) <EOL> elif self . request . get ( '<STR_LIT>' ) : <EOL> entity_keys = self . request . params . getall ( '<STR_LIT>' ) <EOL> db . delete ( entity_keys ) <EOL> self . redirect ( self . _construct_url ( <EOL> remove = [ '<STR_LIT>' ] , <EOL> add = { '<STR_LIT:message>' : '<STR_LIT>' % len ( entity_keys ) } ) ) <EOL> else : <EOL> self . error ( <NUM_LIT> ) <EOL> class DatastoreEditRequestHandler ( admin_request_handler . AdminRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , entity_key_string = None ) : <EOL> if entity_key_string : <EOL> entity_key = datastore . Key ( entity_key_string ) <EOL> entity_key_name = entity_key . name ( ) <EOL> entity_key_id = entity_key . id ( ) <EOL> namespace = entity_key . namespace ( ) <EOL> kind = entity_key . kind ( ) <EOL> entities = [ datastore . Get ( entity_key ) ] <EOL> parent_key = entity_key . parent ( ) <EOL> if parent_key : <EOL> parent_key_string = _format_datastore_key ( parent_key ) <EOL> else : <EOL> parent_key_string = None <EOL> else : <EOL> entity_key = None <EOL> entity_key_string = None <EOL> entity_key_name = None <EOL> entity_key_id = None <EOL> namespace = self . request . get ( '<STR_LIT>' ) <EOL> kind = self . request . get ( '<STR_LIT>' ) <EOL> entities , _ = _get_entities ( kind , <EOL> namespace , <EOL> order = None , <EOL> start = <NUM_LIT:0> , <EOL> count = <NUM_LIT:20> ) <EOL> parent_key = None <EOL> parent_key_string = None <EOL> if not entities : <EOL> self . redirect ( '<STR_LIT>' % ( <EOL> urllib . urlencode ( <EOL> [ ( '<STR_LIT>' , kind ) , <EOL> ( '<STR_LIT:message>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( kind , namespace ) ) , <EOL> ( '<STR_LIT>' , namespace ) ] ) ) ) <EOL> return <EOL> property_name_to_values = _property_name_to_values ( entities ) <EOL> fields = [ ] <EOL> for property_name , values in sorted ( property_name_to_values . iteritems ( ) ) : <EOL> data_type = DataType . get ( values [ <NUM_LIT:0> ] ) <EOL> field = data_type . input_field ( '<STR_LIT>' % ( data_type . name ( ) , property_name ) , <EOL> values [ <NUM_LIT:0> ] if entity_key else None , <EOL> values , <EOL> self . request . uri ) <EOL> fields . append ( ( property_name , data_type . name ( ) , field ) ) <EOL> self . response . write ( self . render ( <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : fields , <EOL> '<STR_LIT:key>' : entity_key_string , <EOL> '<STR_LIT>' : entity_key_id , <EOL> '<STR_LIT>' : entity_key_name , <EOL> '<STR_LIT>' : kind , <EOL> '<STR_LIT>' : namespace , <EOL> '<STR_LIT>' : self . request . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : parent_key , <EOL> '<STR_LIT>' : parent_key_string } ) ) <EOL> def post ( self , entity_key_string = None ) : <EOL> if self . request . get ( '<STR_LIT>' ) : <EOL> if entity_key_string : <EOL> datastore . Delete ( datastore . Key ( entity_key_string ) ) <EOL> self . redirect ( str ( self . request . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> else : <EOL> self . response . set_status ( <NUM_LIT> ) <EOL> return <EOL> if entity_key_string : <EOL> entity = datastore . Get ( datastore . Key ( entity_key_string ) ) <EOL> else : <EOL> kind = self . request . get ( '<STR_LIT>' ) <EOL> namespace = self . request . get ( '<STR_LIT>' , None ) <EOL> entity = datastore . Entity ( kind , _namespace = namespace ) <EOL> for arg_name in self . request . arguments ( ) : <EOL> if '<STR_LIT:|>' not in arg_name : <EOL> continue <EOL> data_type_name , property_name = arg_name . split ( '<STR_LIT:|>' ) <EOL> form_value = self . request . get ( arg_name ) <EOL> data_type = DataType . get_by_name ( data_type_name ) <EOL> if ( entity and <EOL> property_name in entity and <EOL> data_type . format ( entity [ property_name ] ) == form_value ) : <EOL> continue <EOL> if form_value : <EOL> entity [ property_name ] = data_type . parse ( form_value ) <EOL> elif property_name in entity : <EOL> del entity [ property_name ] <EOL> datastore . Put ( entity ) <EOL> self . redirect ( str ( self . request . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) </s>
