<s> import json <EOL> import logging <EOL> from django . http import Http404 <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . http import require_POST <EOL> from desktop . lib . django_util import JsonResponse <EOL> from desktop . lib . i18n import force_unicode <EOL> from metadata . navigator_client import NavigatorApi , is_navigator_enabled <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class MetadataApiException ( Exception ) : <EOL> pass <EOL> def error_handler ( view_fn ) : <EOL> def decorator ( * args , ** kwargs ) : <EOL> try : <EOL> if is_navigator_enabled ( ) : <EOL> return view_fn ( * args , ** kwargs ) <EOL> else : <EOL> raise MetadataApiException ( '<STR_LIT>' ) <EOL> except Http404 , e : <EOL> raise e <EOL> except Exception , e : <EOL> status = <NUM_LIT> <EOL> message = str ( e ) <EOL> LOG . exception ( message ) <EOL> if '<STR_LIT>' in message : <EOL> status = <NUM_LIT:200> <EOL> response = { <EOL> '<STR_LIT:status>' : - <NUM_LIT:1> , <EOL> '<STR_LIT:message>' : force_unicode ( message ) <EOL> } <EOL> return JsonResponse ( response , status = status ) <EOL> return decorator <EOL> @ error_handler <EOL> def search_entities ( request ) : <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> api = NavigatorApi ( ) <EOL> query_s = request . GET . get ( '<STR_LIT>' ) <EOL> offset = request . GET . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> limit = request . GET . get ( '<STR_LIT>' , <NUM_LIT:100> ) <EOL> query_s = query_s . strip ( ) <EOL> entities = api . search_entities ( query_s ) <EOL> response = { <EOL> '<STR_LIT>' : entities , <EOL> '<STR_LIT:count>' : len ( entities ) , <EOL> '<STR_LIT>' : offset , <EOL> '<STR_LIT>' : limit , <EOL> '<STR_LIT>' : query_s , <EOL> '<STR_LIT:status>' : <NUM_LIT:0> <EOL> } <EOL> return JsonResponse ( response ) <EOL> @ error_handler <EOL> def find_entity ( request ) : <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> api = NavigatorApi ( ) <EOL> entity_type = request . GET . get ( '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> database = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> table = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> name = request . GET . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> path = request . GET . get ( '<STR_LIT:path>' , '<STR_LIT>' ) <EOL> if not entity_type : <EOL> raise MetadataApiException ( "<STR_LIT>" ) <EOL> if entity_type . lower ( ) == '<STR_LIT>' : <EOL> if not name : <EOL> raise MetadataApiException ( '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = api . get_database ( name ) <EOL> elif entity_type . lower ( ) == '<STR_LIT>' : <EOL> if not database or not name : <EOL> raise MetadataApiException ( '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = api . get_table ( database , name ) <EOL> elif entity_type . lower ( ) == '<STR_LIT>' : <EOL> if not database or not table or not name : <EOL> raise MetadataApiException ( '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = api . get_field ( database , table , name ) <EOL> elif entity_type . lower ( ) == '<STR_LIT>' : <EOL> if not path : <EOL> raise MetadataApiException ( '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = api . get_directory ( path ) <EOL> elif entity_type . lower ( ) == '<STR_LIT:file>' : <EOL> if not path : <EOL> raise MetadataApiException ( '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = api . get_file ( path ) <EOL> else : <EOL> raise MetadataApiException ( "<STR_LIT>" % entity_type ) <EOL> if '<STR_LIT>' in response [ '<STR_LIT>' ] and not response [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> response [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ ] <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> return JsonResponse ( response ) <EOL> @ error_handler <EOL> def get_entity ( request ) : <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> api = NavigatorApi ( ) <EOL> entity_id = request . REQUEST . get ( '<STR_LIT:id>' ) <EOL> if not entity_id : <EOL> raise MetadataApiException ( "<STR_LIT>" ) <EOL> entity = api . get_entity ( entity_id ) <EOL> response [ '<STR_LIT>' ] = entity <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> return JsonResponse ( response ) <EOL> @ require_POST <EOL> @ error_handler <EOL> def add_tags ( request ) : <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> api = NavigatorApi ( ) <EOL> entity_id = json . loads ( request . POST . get ( '<STR_LIT:id>' , '<STR_LIT>' ) ) <EOL> tags = json . loads ( request . POST . get ( '<STR_LIT>' , [ ] ) ) <EOL> if not entity_id or not tags or not isinstance ( tags , list ) : <EOL> response [ '<STR_LIT:error>' ] = _ ( "<STR_LIT>" ) <EOL> else : <EOL> response [ '<STR_LIT>' ] = api . add_tags ( entity_id , tags ) <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> return JsonResponse ( response ) <EOL> @ require_POST <EOL> @ error_handler <EOL> def delete_tags ( request ) : <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> api = NavigatorApi ( ) <EOL> entity_id = json . loads ( request . POST . get ( '<STR_LIT:id>' , '<STR_LIT>' ) ) <EOL> tags = json . loads ( request . POST . get ( '<STR_LIT>' , [ ] ) ) <EOL> if not entity_id or not tags or not isinstance ( tags , list ) : <EOL> response [ '<STR_LIT:error>' ] = _ ( "<STR_LIT>" ) <EOL> else : <EOL> response [ '<STR_LIT>' ] = api . delete_tags ( entity_id , tags ) <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> return JsonResponse ( response ) <EOL> @ require_POST <EOL> @ error_handler <EOL> def update_properties ( request ) : <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> api = NavigatorApi ( ) <EOL> entity_id = json . loads ( request . POST . get ( '<STR_LIT:id>' , '<STR_LIT>' ) ) <EOL> properties = json . loads ( request . POST . get ( '<STR_LIT>' , { } ) ) <EOL> if not entity_id or not properties or not isinstance ( properties , dict ) : <EOL> response [ '<STR_LIT:error>' ] = _ ( "<STR_LIT>" ) <EOL> else : <EOL> response [ '<STR_LIT>' ] = api . update_properties ( entity_id , properties ) <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> return JsonResponse ( response ) <EOL> @ require_POST <EOL> @ error_handler <EOL> def delete_properties ( request ) : <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> } <EOL> api = NavigatorApi ( ) <EOL> entity_id = json . loads ( request . POST . get ( '<STR_LIT:id>' , '<STR_LIT>' ) ) <EOL> keys = json . loads ( request . POST . get ( '<STR_LIT>' , [ ] ) ) <EOL> if not entity_id or not keys or not isinstance ( keys , list ) : <EOL> response [ '<STR_LIT:error>' ] = _ ( "<STR_LIT>" ) <EOL> else : <EOL> response [ '<STR_LIT>' ] = api . delete_properties ( entity_id , keys ) <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> return JsonResponse ( response ) <EOL> @ error_handler <EOL> def get_lineage ( request ) : <EOL> response = { '<STR_LIT:status>' : - <NUM_LIT:1> , '<STR_LIT>' : [ ] , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> api = NavigatorApi ( ) <EOL> entity_id = request . REQUEST . get ( '<STR_LIT:id>' ) <EOL> if not entity_id : <EOL> raise MetadataApiException ( "<STR_LIT>" ) <EOL> lineage = api . get_lineage ( entity_id ) <EOL> entity_name = api . get_entity ( entity_id ) [ '<STR_LIT>' ] . upper ( ) <EOL> response [ '<STR_LIT:id>' ] = entity_id <EOL> parent_operation = next ( ( entity for entity in lineage [ '<STR_LIT>' ] if entity . get ( '<STR_LIT>' , [ ] ) == [ entity_name ] ) , None ) <EOL> if parent_operation : <EOL> response [ '<STR_LIT>' ] = [ input . lower ( ) for input in parent_operation [ '<STR_LIT>' ] ] <EOL> response [ '<STR_LIT>' ] = parent_operation . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> children = [ entity for entity in lineage [ '<STR_LIT>' ] if entity . get ( '<STR_LIT>' ) is not None and entity_name in entity . get ( '<STR_LIT>' ) ] <EOL> if children is not None : <EOL> response [ '<STR_LIT>' ] = [ child [ '<STR_LIT>' ] for child in children if child . get ( '<STR_LIT>' ) is not None ] <EOL> outputs = [ child [ '<STR_LIT>' ] for child in children if child . get ( '<STR_LIT>' ) is not None ] <EOL> response [ '<STR_LIT>' ] = [ target . lower ( ) for output in outputs for target in output ] <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:0> <EOL> return JsonResponse ( response ) </s>
