<s> import urllib2 <EOL> import json <EOL> from celery . result import AsyncResult <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . views . decorators . http import require_POST , require_safe <EOL> from ide . models . project import Project <EOL> __author__ = '<STR_LIT>' <EOL> def json_response ( response = None ) : <EOL> if response is None : <EOL> response = { } <EOL> response [ "<STR_LIT:success>" ] = True <EOL> return HttpResponse ( json . dumps ( response ) , content_type = "<STR_LIT:application/json>" ) <EOL> def json_failure ( error ) : <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT:error>" : error } ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ login_required <EOL> @ require_POST <EOL> def proxy_keen ( request , project_id ) : <EOL> from utils . td_helper import send_td_event <EOL> project = get_object_or_404 ( Project , pk = project_id ) <EOL> acceptable_events = { <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> } <EOL> event = request . POST [ '<STR_LIT>' ] <EOL> if event not in acceptable_events : <EOL> return json_failure ( "<STR_LIT>" ) <EOL> data = { } <EOL> if '<STR_LIT:data>' in request . POST : <EOL> data [ '<STR_LIT:data>' ] = json . loads ( request . POST [ '<STR_LIT:data>' ] ) <EOL> if '<STR_LIT>' in request . POST : <EOL> data [ '<STR_LIT>' ] = json . loads ( request . POST [ '<STR_LIT>' ] ) <EOL> collections = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if '<STR_LIT>' in request . POST : <EOL> collections = list ( set ( collections ) & set ( json . loads ( request . POST [ '<STR_LIT>' ] ) ) ) <EOL> if len ( data . items ( ) ) == <NUM_LIT:0> : <EOL> data = None <EOL> send_td_event ( event , data = data , request = request , project = project ) <EOL> return json_response ( { } ) <EOL> @ require_safe <EOL> def check_task ( request , task_id ) : <EOL> result = AsyncResult ( task_id ) <EOL> return json_response ( { <EOL> '<STR_LIT:state>' : { <EOL> '<STR_LIT:status>' : result . status , <EOL> '<STR_LIT:result>' : result . result if result . status == '<STR_LIT>' else str ( result . result ) <EOL> } <EOL> } ) <EOL> @ require_POST <EOL> def get_shortlink ( request ) : <EOL> from utils . td_helper import send_td_event <EOL> url = request . POST [ '<STR_LIT:url>' ] <EOL> try : <EOL> r = urllib2 . Request ( '<STR_LIT>' , json . dumps ( { '<STR_LIT:value>' : url , '<STR_LIT>' : <NUM_LIT> } ) , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> response = json . loads ( urllib2 . urlopen ( r ) . read ( ) ) <EOL> except urllib2 . URLError as e : <EOL> return json_failure ( str ( e ) ) <EOL> else : <EOL> send_td_event ( '<STR_LIT>' , data = { <EOL> '<STR_LIT:data>' : { '<STR_LIT>' : response [ '<STR_LIT:url>' ] } <EOL> } , request = request ) <EOL> return json_response ( { '<STR_LIT:url>' : response [ '<STR_LIT:url>' ] } ) <EOL> def heartbeat ( request ) : <EOL> return json_response ( { } ) </s>
