<s> import json <EOL> import logging <EOL> import socket <EOL> from django . utils . encoding import smart_str <EOL> from django . utils . translation import ugettext as _ <EOL> from sqoop import client , conf <EOL> from sqoop . client . exception import SqoopException <EOL> from decorators import get_link_or_exception <EOL> from desktop . lib . django_util import JsonResponse <EOL> from desktop . lib . exceptions import StructuredException <EOL> from desktop . lib . rest . http_client import RestException <EOL> from exception import handle_rest_exception <EOL> from utils import list_to_dict <EOL> from django . views . decorators . cache import never_cache <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> LOG = logging . getLogger ( __name__ ) <EOL> @ never_cache <EOL> def get_links ( request ) : <EOL> response = { <EOL> '<STR_LIT:status>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> try : <EOL> c = client . SqoopClient ( conf . SERVER_URL . get ( ) , request . user . username , request . LANGUAGE_CODE ) <EOL> response [ '<STR_LIT>' ] = list_to_dict ( c . get_links ( ) ) <EOL> except RestException , e : <EOL> response . update ( handle_rest_exception ( e , _ ( '<STR_LIT>' ) ) ) <EOL> return JsonResponse ( response ) <EOL> @ never_cache <EOL> def create_link ( request ) : <EOL> response = { <EOL> '<STR_LIT:status>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } <EOL> if '<STR_LIT>' not in request . POST : <EOL> raise StructuredException ( code = "<STR_LIT>" , message = _ ( '<STR_LIT>' ) , data = { '<STR_LIT>' : '<STR_LIT>' } , error_code = <NUM_LIT> ) <EOL> d = json . loads ( smart_str ( request . POST [ '<STR_LIT>' ] ) ) <EOL> link = client . Link . from_dict ( d ) <EOL> try : <EOL> c = client . SqoopClient ( conf . SERVER_URL . get ( ) , request . user . username , request . LANGUAGE_CODE ) <EOL> response [ '<STR_LIT>' ] = c . create_link ( link ) . to_dict ( ) <EOL> except RestException , e : <EOL> response . update ( handle_rest_exception ( e , _ ( '<STR_LIT>' ) ) ) <EOL> except SqoopException , e : <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:100> <EOL> response [ '<STR_LIT>' ] = e . to_dict ( ) <EOL> return JsonResponse ( response ) <EOL> @ never_cache <EOL> def update_link ( request , link ) : <EOL> response = { <EOL> '<STR_LIT:status>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } <EOL> if '<STR_LIT>' not in request . POST : <EOL> raise StructuredException ( code = "<STR_LIT>" , message = _ ( '<STR_LIT>' ) , data = { '<STR_LIT>' : '<STR_LIT>' } , error_code = <NUM_LIT> ) <EOL> link . update_from_dict ( json . loads ( smart_str ( request . POST [ '<STR_LIT>' ] ) ) ) <EOL> try : <EOL> c = client . SqoopClient ( conf . SERVER_URL . get ( ) , request . user . username , request . LANGUAGE_CODE ) <EOL> response [ '<STR_LIT>' ] = c . update_link ( link ) . to_dict ( ) <EOL> except RestException , e : <EOL> response . update ( handle_rest_exception ( e , _ ( '<STR_LIT>' ) ) ) <EOL> except SqoopException , e : <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:100> <EOL> response [ '<STR_LIT>' ] = e . to_dict ( ) <EOL> return JsonResponse ( response ) <EOL> @ never_cache <EOL> def links ( request ) : <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return get_links ( request ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> return create_link ( request ) <EOL> else : <EOL> raise StructuredException ( code = "<STR_LIT>" , message = _ ( '<STR_LIT>' ) , error_code = <NUM_LIT> ) <EOL> @ never_cache <EOL> @ get_link_or_exception ( ) <EOL> def link ( request , link ) : <EOL> response = { <EOL> '<STR_LIT:status>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } <EOL> if request . method == '<STR_LIT:GET>' : <EOL> response [ '<STR_LIT>' ] = link . to_dict ( ) <EOL> return JsonResponse ( response ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> return update_link ( request , link ) <EOL> else : <EOL> raise StructuredException ( code = "<STR_LIT>" , message = _ ( '<STR_LIT>' ) , error_code = <NUM_LIT> ) <EOL> @ never_cache <EOL> @ get_link_or_exception ( ) <EOL> def link_clone ( request , link ) : <EOL> if request . method != '<STR_LIT:POST>' : <EOL> raise StructuredException ( code = "<STR_LIT>" , message = _ ( '<STR_LIT>' ) , error_code = <NUM_LIT> ) <EOL> response = { <EOL> '<STR_LIT:status>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None <EOL> } <EOL> link . id = - <NUM_LIT:1> <EOL> link . name = '<STR_LIT>' % link . name <EOL> try : <EOL> c = client . SqoopClient ( conf . SERVER_URL . get ( ) , request . user . username , request . LANGUAGE_CODE ) <EOL> response [ '<STR_LIT>' ] = c . create_link ( link ) . to_dict ( ) <EOL> except RestException , e : <EOL> response . update ( handle_rest_exception ( e , _ ( '<STR_LIT>' ) ) ) <EOL> except SqoopException , e : <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:100> <EOL> response [ '<STR_LIT>' ] = e . to_dict ( ) <EOL> return JsonResponse ( response ) <EOL> @ never_cache <EOL> @ get_link_or_exception ( ) <EOL> def link_delete ( request , link ) : <EOL> if request . method != '<STR_LIT:POST>' : <EOL> raise StructuredException ( code = "<STR_LIT>" , message = _ ( '<STR_LIT>' ) , error_code = <NUM_LIT> ) <EOL> response = { <EOL> '<STR_LIT:status>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : None <EOL> } <EOL> try : <EOL> c = client . SqoopClient ( conf . SERVER_URL . get ( ) , request . user . username , request . LANGUAGE_CODE ) <EOL> c . delete_link ( link ) <EOL> except RestException , e : <EOL> response . update ( handle_rest_exception ( e , _ ( '<STR_LIT>' ) ) ) <EOL> except SqoopException , e : <EOL> response [ '<STR_LIT:status>' ] = <NUM_LIT:100> <EOL> response [ '<STR_LIT>' ] = e . to_dict ( ) <EOL> return JsonResponse ( response ) </s>
