<s> import hmac <EOL> import json <EOL> import datetime <EOL> import logging <EOL> import mimetypes <EOL> import urllib <EOL> import cgi <EOL> import random <EOL> import string <EOL> from hashlib import sha1 <EOL> from operator import itemgetter <EOL> from tornado import gen <EOL> from tornado import httpclient <EOL> from boto . s3 . connection import S3Connection <EOL> from boto . s3 . key import Key <EOL> from main . helper import * <EOL> from main . db import * <EOL> from main . db2 import * <EOL> class API2TagCloud ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> def get ( self ) : <EOL> db_result = self . get_tag_cloud ( <EOL> definition = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> limit = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> ) <EOL> if len ( db_result ) == <NUM_LIT:0> : <EOL> self . json ( { <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> '<STR_LIT:count>' : <NUM_LIT:0> , <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } , ] , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> } <EOL> } ) <EOL> return <EOL> seq_count = [ x [ '<STR_LIT>' ] for x in db_result ] <EOL> seq_log = [ x [ '<STR_LIT>' ] for x in db_result ] <EOL> min_cnt = min ( seq_count ) <EOL> max_cnt = max ( seq_count ) <EOL> min_log = min ( seq_log ) <EOL> max_log = max ( seq_log ) <EOL> for row in db_result : <EOL> row [ '<STR_LIT>' ] = <NUM_LIT:1.0> * ( row [ '<STR_LIT>' ] - min_cnt ) / ( max_cnt - min_cnt ) if ( max_cnt > min_cnt ) else <NUM_LIT:1> <EOL> row [ '<STR_LIT>' ] = <NUM_LIT:1.0> * ( row [ '<STR_LIT>' ] - min_log ) / ( max_log - min_log ) if ( max_log > min_log ) else <NUM_LIT:1> <EOL> self . json ( { <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> '<STR_LIT:count>' : len ( db_result ) , <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT>' : db_result , <EOL> '<STR_LIT>' : { '<STR_LIT>' : min_cnt , '<STR_LIT>' : max_cnt } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : min_log , '<STR_LIT>' : max_log } <EOL> } <EOL> } ) <EOL> class API2EntityList ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> def get ( self ) : <EOL> db_result = self . get_entities_info ( <EOL> definition = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> query = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> limit = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> page = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> ) <EOL> result = { } <EOL> for e in db_result . get ( '<STR_LIT>' , [ ] ) : <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) [ '<STR_LIT:label>' ] = e . get ( '<STR_LIT:label>' ) if e . get ( '<STR_LIT:label>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:id>' ] = e . get ( '<STR_LIT:id>' ) if e . get ( '<STR_LIT:id>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:name>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:info>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) if e . get ( '<STR_LIT>' ) else None <EOL> self . json ( { <EOL> '<STR_LIT:result>' : sorted ( result . values ( ) , key = itemgetter ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> '<STR_LIT:count>' : db_result . get ( '<STR_LIT:count>' , <NUM_LIT:0> ) , <EOL> } ) <EOL> class API2CmdiXml ( myRequestHandler , Entity ) : <EOL> @ web . removeslash <EOL> def get ( self , entity_id = None ) : <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> entity = self . get_entities ( entity_id = entity_id , limit = <NUM_LIT:1> ) <EOL> if not entity : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> entity [ '<STR_LIT>' ] = { '<STR_LIT>' : entity [ '<STR_LIT>' ] } <EOL> xml = """<STR_LIT>""" % { <EOL> '<STR_LIT>' : entity [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ { '<STR_LIT:value>' : '<STR_LIT>' } ] ) [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , <EOL> '<STR_LIT>' : entity [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ { '<STR_LIT:value>' : '<STR_LIT>' } ] ) [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , <EOL> '<STR_LIT>' : entity [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ { '<STR_LIT:value>' : '<STR_LIT>' } ] ) [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , <EOL> '<STR_LIT>' : entity [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ { '<STR_LIT:value>' : '<STR_LIT>' } ] ) [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , <EOL> '<STR_LIT>' : entity [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ { '<STR_LIT:value>' : '<STR_LIT>' } ] ) [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , <EOL> '<STR_LIT>' : entity [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' , [ { '<STR_LIT:value>' : '<STR_LIT>' } ] ) [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] , <EOL> } <EOL> mimetypes . init ( ) <EOL> mime = mimetypes . types_map . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> filename = '<STR_LIT>' % entity_id <EOL> self . add_header ( '<STR_LIT:Content-Type>' , mime ) <EOL> self . write ( xml ) <EOL> class API2Entity ( myRequestHandler , Entity ) : <EOL> @ web . removeslash <EOL> def get ( self , entity_id = None ) : <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> try : <EOL> entity = self . get_entities ( entity_id = entity_id , limit = <NUM_LIT:1> ) <EOL> except Exception , e : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if not entity : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> entity [ '<STR_LIT>' ] = { '<STR_LIT>' : entity [ '<STR_LIT>' ] } <EOL> self . json ( { <EOL> '<STR_LIT:result>' : entity , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> @ web . removeslash <EOL> def put ( self , entity_id = None ) : <EOL> if not self . current_user : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> new_properties = { } <EOL> for dataproperty , value in self . request . arguments . iteritems ( ) : <EOL> if dataproperty in [ '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> continue <EOL> if '<STR_LIT:.>' in dataproperty : <EOL> property_definition_keyname = dataproperty . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> old_property_id = dataproperty . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> ] <EOL> else : <EOL> property_definition_keyname = dataproperty <EOL> old_property_id = None <EOL> if type ( value ) is not list : <EOL> value = [ value ] <EOL> for v in value : <EOL> new_property_id = self . set_property ( entity_id = entity_id , old_property_id = old_property_id , property_definition_keyname = property_definition_keyname , value = v ) <EOL> if new_property_id : <EOL> new_properties . setdefault ( property_definition_keyname , [ ] ) . append ( { '<STR_LIT:id>' : new_property_id , '<STR_LIT:value>' : v } ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:id>' : entity_id , <EOL> '<STR_LIT>' : new_properties <EOL> } , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> @ web . removeslash <EOL> def post ( self , entity_id = None ) : <EOL> if not self . current_user : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> entity = self . get_entities ( entity_id = entity_id , limit = <NUM_LIT:1> ) <EOL> if not entity : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> definition = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not definition : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> entity_id = self . create_entity ( entity_definition_keyname = definition , parent_entity_id = entity_id ) <EOL> for dataproperty , value in self . request . arguments . iteritems ( ) : <EOL> if dataproperty in [ '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> continue <EOL> if type ( value ) is not list : <EOL> value = [ value ] <EOL> for v in value : <EOL> new_property_id = self . set_property ( entity_id = entity_id , property_definition_keyname = dataproperty , value = v ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:id>' : entity_id , <EOL> } , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> @ web . removeslash <EOL> def delete ( self , entity_id = None ) : <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> result = self . delete_entity ( entity_id = entity_id ) <EOL> if not result : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:id>' : entity_id , <EOL> } , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2EntityChilds ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> def get ( self , entity_id = None ) : <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> db_result = self . get_entities_info ( <EOL> parent_entity_id = entity_id , <EOL> definition = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> query = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> limit = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> page = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> ) <EOL> result = { } <EOL> for e in db_result . get ( '<STR_LIT>' , [ ] ) : <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) [ '<STR_LIT:label>' ] = e . get ( '<STR_LIT:label>' ) if e . get ( '<STR_LIT:label>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:id>' ] = e . get ( '<STR_LIT:id>' ) if e . get ( '<STR_LIT:id>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:name>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:info>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) if e . get ( '<STR_LIT>' ) else None <EOL> for r in result . values ( ) : <EOL> r [ '<STR_LIT>' ] = sorted ( r . get ( '<STR_LIT>' , { } ) . values ( ) , key = itemgetter ( '<STR_LIT>' ) ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : result , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> '<STR_LIT:count>' : db_result . get ( '<STR_LIT:count>' , <NUM_LIT:0> ) , <EOL> } ) <EOL> class API2EntityReferrals ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> def get ( self , entity_id = None ) : <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> db_result = self . get_entities_info ( <EOL> referred_to_entity_id = entity_id , <EOL> definition = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> query = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> limit = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) , <EOL> page = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> ) <EOL> result = { } <EOL> for e in db_result . get ( '<STR_LIT>' , [ ] ) : <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) [ '<STR_LIT:label>' ] = e . get ( '<STR_LIT:label>' ) if e . get ( '<STR_LIT:label>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:id>' ] = e . get ( '<STR_LIT:id>' ) if e . get ( '<STR_LIT:id>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:name>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT:info>' ] = e . get ( '<STR_LIT>' ) if e . get ( '<STR_LIT>' ) else None <EOL> result . setdefault ( e . get ( '<STR_LIT>' ) , { } ) . setdefault ( '<STR_LIT>' , { } ) . setdefault ( e . get ( '<STR_LIT:id>' ) , { } ) [ '<STR_LIT>' ] = e . get ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) if e . get ( '<STR_LIT>' ) else None <EOL> for r in result . values ( ) : <EOL> r [ '<STR_LIT>' ] = sorted ( r . get ( '<STR_LIT>' , { } ) . values ( ) , key = itemgetter ( '<STR_LIT>' ) ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : result , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> '<STR_LIT:count>' : db_result . get ( '<STR_LIT:count>' , <NUM_LIT:0> ) , <EOL> } ) <EOL> class API2File ( myRequestHandler , Entity ) : <EOL> @ web . removeslash <EOL> def get ( self , file_id = None ) : <EOL> if not file_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> files = self . get_file ( file_id = file_id , sharing_key = self . get_argument ( '<STR_LIT:key>' , default = None , strip = True ) ) <EOL> if not files : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if files [ <NUM_LIT:0> ] . get ( '<STR_LIT:url>' ) : <EOL> return self . redirect ( files [ <NUM_LIT:0> ] . get ( '<STR_LIT:url>' ) ) <EOL> if files [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) : <EOL> try : <EOL> AWS_BUCKET = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> AWS_ACCESS_KEY = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> ] <EOL> AWS_SECRET_KEY = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:2> ] <EOL> except Exception , e : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> s3_conn = S3Connection ( AWS_ACCESS_KEY , AWS_SECRET_KEY ) <EOL> s3_bucket = s3_conn . get_bucket ( AWS_BUCKET , validate = False ) <EOL> s3_key = s3_bucket . get_key ( files [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) ) <EOL> if not s3_key : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> s3_url = s3_key . generate_url ( expires_in = <NUM_LIT> , query_auth = True ) <EOL> return self . redirect ( s3_url ) <EOL> if not files [ <NUM_LIT:0> ] . get ( '<STR_LIT:file>' ) : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> mimetypes . init ( ) <EOL> mime = mimetypes . types_map . get ( '<STR_LIT>' % files [ <NUM_LIT:0> ] . get ( '<STR_LIT:filename>' ) . lower ( ) . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> filename = files [ <NUM_LIT:0> ] . get ( '<STR_LIT:filename>' ) <EOL> self . add_header ( '<STR_LIT:Content-Type>' , mime ) <EOL> self . add_header ( '<STR_LIT>' , '<STR_LIT>' % filename ) <EOL> self . write ( files [ <NUM_LIT:0> ] . get ( '<STR_LIT:file>' ) ) <EOL> @ web . removeslash <EOL> def delete ( self , file_id = None ) : <EOL> pass <EOL> class API2FileUpload ( myRequestHandler , Entity ) : <EOL> @ web . removeslash <EOL> def post ( self ) : <EOL> if not self . current_user : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if self . request . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) . startswith ( '<STR_LIT>' ) : <EOL> uploaded_file = self . request . files . get ( '<STR_LIT:file>' , [ { } ] ) [ <NUM_LIT:0> ] . body <EOL> else : <EOL> uploaded_file = self . request . body <EOL> if not uploaded_file : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if not self . request . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) . startswith ( '<STR_LIT>' ) : <EOL> try : <EOL> file_size = int ( self . request . headers . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> except Exception : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if file_size != len ( uploaded_file ) : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> entity_id = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> property_definition_keyname = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not property_definition_keyname : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> filename = self . get_argument ( '<STR_LIT:filename>' , default = None , strip = True ) <EOL> if not filename : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> new_property_id = self . set_property ( entity_id = entity_id , property_definition_keyname = property_definition_keyname , value = { '<STR_LIT:filename>' : filename , '<STR_LIT:body>' : uploaded_file } ) <EOL> if new_property_id : <EOL> properties = { property_definition_keyname : [ { '<STR_LIT:id>' : new_property_id , '<STR_LIT:value>' : filename } ] } <EOL> else : <EOL> properties = None <EOL> self . json ( { <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:id>' : entity_id , <EOL> '<STR_LIT>' : properties <EOL> } , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2AmazonFileUpload ( myRequestHandler , Entity ) : <EOL> def post ( self ) : <EOL> try : <EOL> AWS_BUCKET = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> AWS_ACCESS_KEY = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> ] <EOL> AWS_SECRET_KEY = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:2> ] <EOL> except Exception , e : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if not self . current_user : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> entity_id = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> property_definition_keyname = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not property_definition_keyname : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> filename = self . get_argument ( '<STR_LIT:filename>' , default = None , strip = True ) <EOL> if not filename : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> filetype = self . get_argument ( '<STR_LIT>' , None , True ) <EOL> if not filetype : <EOL> filetype = '<STR_LIT>' <EOL> filesize = self . get_argument ( '<STR_LIT>' , <NUM_LIT:0> , True ) <EOL> if int ( filesize ) > <NUM_LIT> : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> key = '<STR_LIT>' % ( self . app_settings ( '<STR_LIT>' ) , entity_id ) <EOL> new_property_id = self . set_property ( entity_id = entity_id , property_definition_keyname = property_definition_keyname , value = { '<STR_LIT:filename>' : filename , '<STR_LIT>' : key , '<STR_LIT>' : filesize } ) <EOL> if new_property_id : <EOL> properties = { property_definition_keyname : [ { '<STR_LIT:id>' : new_property_id , '<STR_LIT:value>' : filename } ] } <EOL> else : <EOL> properties = None <EOL> key = '<STR_LIT>' % ( key , new_property_id ) <EOL> policy = { <EOL> '<STR_LIT>' : ( datetime . datetime . utcnow ( ) + datetime . timedelta ( hours = <NUM_LIT:1> ) ) . strftime ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : AWS_BUCKET } , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , key ] , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:Content-Type>' : filetype } , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> ] <EOL> } <EOL> encoded_policy = json . dumps ( policy ) . encode ( '<STR_LIT:utf-8>' ) . encode ( '<STR_LIT>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> signature = hmac . new ( str ( AWS_SECRET_KEY ) , str ( encoded_policy ) , sha1 ) . digest ( ) . encode ( '<STR_LIT>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:id>' : entity_id , <EOL> '<STR_LIT>' : properties , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:url>' : '<STR_LIT>' % AWS_BUCKET , <EOL> '<STR_LIT:data>' : { <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : AWS_ACCESS_KEY , <EOL> '<STR_LIT>' : encoded_policy , <EOL> '<STR_LIT>' : signature , <EOL> '<STR_LIT:Content-Type>' : filetype , <EOL> '<STR_LIT>' : '<STR_LIT>' % urllib . quote ( filename . encode ( '<STR_LIT:utf-8>' ) ) , <EOL> } <EOL> } <EOL> } , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2UrlFileUpload ( myRequestHandler , Entity ) : <EOL> @ gen . coroutine <EOL> def post ( self ) : <EOL> try : <EOL> AWS_BUCKET = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> AWS_ACCESS_KEY = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:1> ] <EOL> AWS_SECRET_KEY = self . app_settings ( '<STR_LIT>' , '<STR_LIT:\n>' , True ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:2> ] <EOL> except Exception , e : <EOL> self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> return <EOL> if not self . current_user : <EOL> self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> return <EOL> entity_id = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not entity_id : <EOL> self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> return <EOL> property_definition_keyname = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not property_definition_keyname : <EOL> self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> return <EOL> url = self . get_argument ( '<STR_LIT:url>' , default = None , strip = True ) <EOL> if not url : <EOL> self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> return <EOL> filename = self . get_argument ( '<STR_LIT:filename>' , default = None , strip = True ) <EOL> download = self . get_argument ( '<STR_LIT>' , default = '<STR_LIT:false>' , strip = True ) <EOL> if download . lower ( ) != '<STR_LIT:true>' : <EOL> new_property_id = self . set_property ( entity_id = entity_id , property_definition_keyname = property_definition_keyname , value = { '<STR_LIT:filename>' : filename , '<STR_LIT:url>' : url } ) <EOL> if new_property_id : <EOL> properties = { property_definition_keyname : [ { '<STR_LIT:id>' : new_property_id , '<STR_LIT:value>' : filename } ] } <EOL> else : <EOL> properties = None <EOL> self . json ( { <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:id>' : entity_id , <EOL> '<STR_LIT>' : properties <EOL> } , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> return <EOL> http_client = httpclient . AsyncHTTPClient ( ) <EOL> response = yield http_client . fetch ( url ) <EOL> if response . headers . get ( '<STR_LIT:Content-Type>' , None ) : <EOL> filetype = response . headers . get ( '<STR_LIT:Content-Type>' ) <EOL> else : <EOL> filetype = '<STR_LIT>' <EOL> if not filename and response . headers . get ( '<STR_LIT>' , None ) : <EOL> filename_value , filename_params = cgi . parse_header ( response . headers . get ( '<STR_LIT>' ) ) <EOL> if filename_params . get ( '<STR_LIT:filename>' , None ) : <EOL> filename = filename_params . get ( '<STR_LIT:filename>' ) . decode ( '<STR_LIT:utf-8>' ) <EOL> elif filename_params . get ( '<STR_LIT>' , None ) : <EOL> filename = urllib . unquote ( filename_params . get ( '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if not filename : <EOL> filename = url . split ( '<STR_LIT:#>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> filesize = len ( response . body ) <EOL> if int ( filesize ) > <NUM_LIT> : <EOL> self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> return <EOL> key = '<STR_LIT>' % ( self . app_settings ( '<STR_LIT>' ) , entity_id ) <EOL> new_property_id = self . set_property ( entity_id = entity_id , property_definition_keyname = property_definition_keyname , value = { '<STR_LIT:filename>' : filename , '<STR_LIT>' : key , '<STR_LIT>' : filesize } ) <EOL> if new_property_id : <EOL> properties = { property_definition_keyname : [ { '<STR_LIT:id>' : new_property_id , '<STR_LIT:value>' : filename } ] } <EOL> else : <EOL> properties = None <EOL> key = '<STR_LIT>' % ( key , new_property_id ) <EOL> s3_conn = S3Connection ( AWS_ACCESS_KEY , AWS_SECRET_KEY ) <EOL> s3_bucket = s3_conn . get_bucket ( AWS_BUCKET , validate = False ) <EOL> s3_file = Key ( s3_bucket ) <EOL> s3_file . key = key <EOL> s3_file . content_type = filetype <EOL> s3_file . set_metadata ( '<STR_LIT>' , '<STR_LIT>' % urllib . quote ( filename . encode ( '<STR_LIT:utf-8>' ) ) ) <EOL> s3_file . set_contents_from_string ( response . body , encrypt_key = True ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:id>' : entity_id , <EOL> '<STR_LIT>' : properties , <EOL> } , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2EntityPicture ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> def get ( self , entity_id = None ) : <EOL> picture = self . get_entity_picture ( entity_id ) <EOL> if not picture : <EOL> return self . redirect ( '<STR_LIT>' % ( hashlib . md5 ( str ( entity_id ) ) . hexdigest ( ) ) ) <EOL> if picture . get ( '<STR_LIT>' ) : <EOL> self . add_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> return self . write ( picture . get ( '<STR_LIT>' ) ) <EOL> elif picture . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> return self . redirect ( '<STR_LIT>' % ( hashlib . md5 ( str ( entity_id ) ) . hexdigest ( ) ) ) <EOL> elif picture . get ( '<STR_LIT>' , '<STR_LIT>' ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return self . redirect ( '<STR_LIT>' % entity_id ) <EOL> else : <EOL> return self . redirect ( '<STR_LIT>' % ( hashlib . md5 ( str ( entity_id ) ) . hexdigest ( ) ) ) <EOL> class API2EntityRights ( myRequestHandler , Entity2 ) : <EOL> def post ( self , entity_id ) : <EOL> if not self . current_user : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> right = self . get_argument ( '<STR_LIT:right>' , default = None , strip = True ) <EOL> if right and right not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> related_entity_id = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not related_entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> response = self . set_entity_right ( entity_id = entity_id , related_entity_id = related_entity_id , right = right ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : response , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2EntityShare ( myRequestHandler , Entity ) : <EOL> def post ( self , entity_id ) : <EOL> if not self . current_user : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> to = StrToList ( self . get_argument ( '<STR_LIT:to>' , default = None , strip = True ) ) <EOL> if not to : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> entity = self . get_entities ( entity_id = entity_id , limit = <NUM_LIT:1> ) <EOL> if not entity : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> url = '<STR_LIT>' % ( self . request . headers . get ( '<STR_LIT>' ) , entity . get ( '<STR_LIT>' , '<STR_LIT>' ) , entity . get ( '<STR_LIT:id>' ) ) <EOL> message = self . get_argument ( '<STR_LIT:message>' , default = '<STR_LIT>' , strip = True ) <EOL> response = self . mail_send ( <EOL> to = to , <EOL> subject = entity . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> message = '<STR_LIT>' % ( message , url , self . current_user . get ( '<STR_LIT:name>' , '<STR_LIT>' ) , self . current_user . get ( '<STR_LIT:email>' , '<STR_LIT>' ) ) , <EOL> html = False , <EOL> tag = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : response , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2Email ( myRequestHandler , Entity2 ) : <EOL> def get ( self ) : <EOL> pass <EOL> def post ( self ) : <EOL> if not self . current_user : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> to = self . get_argument ( '<STR_LIT:to>' , default = None , strip = True ) <EOL> if not to : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> subject = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if not subject : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> message = self . get_argument ( '<STR_LIT:message>' , default = None , strip = True ) <EOL> if not message : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> campaign = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> tag = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> response = self . mail_send ( <EOL> to = to , <EOL> subject = subject , <EOL> message = message , <EOL> html = True , <EOL> campaign = campaign , <EOL> tag = tag <EOL> ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : response , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2DefinitionList ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> def get ( self ) : <EOL> menu = self . get_menu ( ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : menu , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2Definition ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> def get ( self , definition_id = None ) : <EOL> if not definition_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> definitions = self . get_entity_definitions ( definition_id = definition_id ) <EOL> if not definitions : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : definitions , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2User ( myRequestHandler , Entity ) : <EOL> @ web . removeslash <EOL> def get ( self ) : <EOL> if not self . current_user : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> person = self . get_entities ( entity_id = self . current_user . get ( '<STR_LIT:id>' ) , limit = <NUM_LIT:1> ) <EOL> result = self . current_user <EOL> result [ '<STR_LIT>' ] = person <EOL> del result [ '<STR_LIT>' ] <EOL> del result [ '<STR_LIT>' ] <EOL> self . json ( { <EOL> '<STR_LIT:result>' : result , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2UserAuth ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> @ web . authenticated <EOL> def get ( self ) : <EOL> self . redirect ( '<STR_LIT>' ) <EOL> @ web . removeslash <EOL> def post ( self ) : <EOL> state = self . get_argument ( '<STR_LIT:state>' , default = None , strip = True ) <EOL> if not state : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> redirect_url = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> provider = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> token = hashlib . md5 ( '<STR_LIT>' % ( state , redirect_url , '<STR_LIT>' . join ( random . choice ( string . ascii_letters + string . digits ) for x in range ( <NUM_LIT:16> ) ) ) ) . hexdigest ( ) <EOL> tmp_filename = '<STR_LIT>' % token <EOL> tmp_file = json . dumps ( { <EOL> '<STR_LIT:state>' : state , <EOL> '<STR_LIT>' : redirect_url , <EOL> '<STR_LIT>' : provider <EOL> } ) <EOL> self . set_tmp_file ( filename = tmp_filename , content = tmp_file ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : { <EOL> '<STR_LIT:state>' : state , <EOL> '<STR_LIT>' : '<STR_LIT>' % ( self . request . full_url ( ) , token ) <EOL> } , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2UserAuthTokenProvider ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> def get ( self , token , provider ) : <EOL> self . set_cookie ( name = '<STR_LIT>' , value = provider , expires_days = <NUM_LIT:1> , domain = self . settings [ '<STR_LIT>' ] ) <EOL> self . redirect ( '<STR_LIT>' % token ) <EOL> class API2UserAuthToken ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> @ web . authenticated <EOL> def get ( self , token ) : <EOL> self . clear_cookie ( '<STR_LIT>' , domain = self . settings [ '<STR_LIT>' ] ) <EOL> if not token : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> filename = '<STR_LIT>' % token <EOL> tmp_file = self . get_tmp_file ( filename = filename ) <EOL> if not tmp_file : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> tmp_file_json = json . loads ( tmp_file . get ( '<STR_LIT:file>' , '<STR_LIT>' ) ) <EOL> tmp_file_json [ '<STR_LIT:user>' ] = self . current_user <EOL> del tmp_file_json [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] <EOL> del tmp_file_json [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] <EOL> tmp_filename = '<STR_LIT>' % token <EOL> tmp_file = json . dumps ( tmp_file_json ) <EOL> self . set_tmp_file ( filename = tmp_filename , content = tmp_file ) <EOL> if tmp_file_json . get ( '<STR_LIT>' , None ) : <EOL> return self . redirect ( tmp_file_json . get ( '<STR_LIT>' ) ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : tmp_file_json , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> @ web . removeslash <EOL> def post ( self , token ) : <EOL> if not token : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> filename = '<STR_LIT>' % token <EOL> tmp_file = self . get_tmp_file ( filename = filename ) <EOL> if not tmp_file : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> tmp_file_json = json . loads ( tmp_file . get ( '<STR_LIT:file>' , '<STR_LIT>' ) ) <EOL> state = self . get_argument ( '<STR_LIT:state>' , default = None , strip = True ) <EOL> if not state : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if state != tmp_file_json . get ( '<STR_LIT:state>' , None ) : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : tmp_file_json , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2History ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> @ web . authenticated <EOL> def get ( self ) : <EOL> limit = self . get_argument ( '<STR_LIT>' , default = <NUM_LIT:10> , strip = True ) <EOL> if limit > <NUM_LIT:50> : <EOL> limit = <NUM_LIT:50> <EOL> timestamp = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> if limit == '<STR_LIT>' : <EOL> limit = <NUM_LIT:10> <EOL> timeframe = self . get_history_timeframe ( limit = limit , timestamp = timestamp ) <EOL> events = self . get_history_events ( timeframe ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : events , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> '<STR_LIT:count>' : len ( events [ '<STR_LIT>' ] ) , <EOL> } ) <EOL> class API2Changed ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> @ web . authenticated <EOL> def get ( self ) : <EOL> timestamp = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> definition = self . get_argument ( '<STR_LIT>' , default = None , strip = True ) <EOL> try : <EOL> limit = int ( self . get_argument ( '<STR_LIT>' , default = <NUM_LIT:50> , strip = True ) ) <EOL> except Exception , e : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> if limit > <NUM_LIT> : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> changed = self . get_recently_changed ( timestamp = timestamp , definition = definition , limit = limit ) <EOL> self . json ( { <EOL> '<STR_LIT:result>' : changed , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> '<STR_LIT:count>' : len ( changed ) , <EOL> } ) <EOL> class API2EntityParents ( myRequestHandler , Entity2 ) : <EOL> @ web . removeslash <EOL> @ web . authenticated <EOL> def get ( self , entity_id = None ) : <EOL> if not entity_id : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> try : <EOL> id = int ( entity_id ) <EOL> except Exception , e : <EOL> return self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> parents = self . get_parents ( id = id ) <EOL> return self . json ( { <EOL> '<STR_LIT:result>' : parents , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2Ping ( myRequestHandler ) : <EOL> def get ( self ) : <EOL> self . json ( { <EOL> '<STR_LIT:result>' : '<STR_LIT>' , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } ) <EOL> class API2NotFound ( myRequestHandler , Entity2 ) : <EOL> def get ( self , url ) : <EOL> self . json ( { <EOL> '<STR_LIT:error>' : '<STR_LIT>' % url , <EOL> '<STR_LIT:time>' : round ( self . request . request_time ( ) , <NUM_LIT:3> ) , <EOL> } , <NUM_LIT> ) <EOL> def put ( self , url ) : <EOL> self . get ( url ) <EOL> def post ( self , url ) : <EOL> self . get ( url ) <EOL> def delete ( self , url ) : <EOL> self . get ( url ) <EOL> handlers = [ <EOL> ( r'<STR_LIT>' , API2EntityList ) , <EOL> ( r'<STR_LIT>' , API2EntityChilds ) , <EOL> ( r'<STR_LIT>' , API2EntityReferrals ) , <EOL> ( r'<STR_LIT>' , API2EntityPicture ) , <EOL> ( r'<STR_LIT>' , API2EntityRights ) , <EOL> ( r'<STR_LIT>' , API2EntityShare ) , <EOL> ( r'<STR_LIT>' , API2EntityParents ) , <EOL> ( r'<STR_LIT>' , API2Entity ) , <EOL> ( r'<STR_LIT>' , API2FileUpload ) , <EOL> ( r'<STR_LIT>' , API2AmazonFileUpload ) , <EOL> ( r'<STR_LIT>' , API2UrlFileUpload ) , <EOL> ( r'<STR_LIT>' , API2File ) , <EOL> ( r'<STR_LIT>' , API2DefinitionList ) , <EOL> ( r'<STR_LIT>' , API2Definition ) , <EOL> ( r'<STR_LIT>' , API2CmdiXml ) , <EOL> ( r'<STR_LIT>' , API2Email ) , <EOL> ( r'<STR_LIT>' , API2UserAuth ) , <EOL> ( r'<STR_LIT>' , API2UserAuthTokenProvider ) , <EOL> ( r'<STR_LIT>' , API2UserAuthToken ) , <EOL> ( r'<STR_LIT>' , API2History ) , <EOL> ( r'<STR_LIT>' , API2Changed ) , <EOL> ( r'<STR_LIT>' , API2TagCloud ) , <EOL> ( r'<STR_LIT>' , API2User ) , <EOL> ( r'<STR_LIT>' , API2Ping ) , <EOL> ( r'<STR_LIT>' , API2NotFound ) , <EOL> ] </s>
