<s> from calendar import timegm <EOL> from hashlib import md5 <EOL> from json import dumps , JSONEncoder , loads <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import Count <EOL> from django . db . models . query import QuerySet <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import render <EOL> from django . utils . encoding import smart_bytes , smart_text <EOL> from twobuntu . accounts . models import Profile <EOL> from twobuntu . articles . models import Article <EOL> from twobuntu . categories . models import Category <EOL> from twobuntu . utils import timestamp_to_datetime <EOL> def index ( request ) : <EOL> """<STR_LIT>""" <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> } ) <EOL> class APIException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class ObjectEncoder ( JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _request = request <EOL> super ( ObjectEncoder , self ) . __init__ ( ) <EOL> def default ( self , o ) : <EOL> """<STR_LIT>""" <EOL> if type ( o ) is QuerySet : <EOL> return list ( o ) <EOL> elif type ( o ) is Article : <EOL> return self . _encode_article ( o ) <EOL> elif type ( o ) is Category : <EOL> return self . _encode_category ( o ) <EOL> elif type ( o ) is Profile : <EOL> return self . _encode_profile ( o ) <EOL> else : <EOL> return JSONEncoder . default ( self , o ) <EOL> def _encode_article ( self , article ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:id>' : article . id , <EOL> '<STR_LIT:title>' : smart_text ( article ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : article . author . id , <EOL> '<STR_LIT:name>' : smart_text ( article . author . profile ) , <EOL> '<STR_LIT>' : md5 ( smart_bytes ( article . author . email ) ) . hexdigest ( ) , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : article . category . id , <EOL> '<STR_LIT:name>' : article . category . name , <EOL> } , <EOL> '<STR_LIT:body>' : article . render ( ) , <EOL> '<STR_LIT>' : article . cc_license , <EOL> '<STR_LIT:date>' : timegm ( article . date . utctimetuple ( ) ) , <EOL> '<STR_LIT:url>' : self . _request . build_absolute_uri ( article . get_absolute_url ( ) ) , <EOL> } <EOL> def _encode_category ( self , category ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:id>' : category . id , <EOL> '<STR_LIT:name>' : category . name , <EOL> '<STR_LIT>' : category . num_articles , <EOL> '<STR_LIT:url>' : self . _request . build_absolute_uri ( category . get_absolute_url ( ) ) , <EOL> } <EOL> def _encode_profile ( self , profile ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:id>' : profile . user . id , <EOL> '<STR_LIT:name>' : smart_text ( profile ) , <EOL> '<STR_LIT>' : md5 ( smart_bytes ( profile . user . email ) ) . hexdigest ( ) , <EOL> '<STR_LIT>' : profile . age ( ) , <EOL> '<STR_LIT:location>' : profile . location , <EOL> '<STR_LIT>' : profile . website , <EOL> '<STR_LIT>' : profile . bio , <EOL> '<STR_LIT>' : timegm ( profile . user . last_login . utctimetuple ( ) ) if profile . user . last_login else <NUM_LIT:0> , <EOL> '<STR_LIT:url>' : self . _request . build_absolute_uri ( profile . get_absolute_url ( ) ) , <EOL> } <EOL> def endpoint ( fn ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( request , ** kwargs ) : <EOL> try : <EOL> json = dumps ( <EOL> fn ( request , ** kwargs ) , <EOL> cls = ObjectEncoder , <EOL> request = request , <EOL> ) <EOL> except APIException as e : <EOL> json = dumps ( { <EOL> '<STR_LIT:error>' : smart_text ( e ) , <EOL> } ) <EOL> if '<STR_LIT>' in request . GET : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : reverse ( '<STR_LIT>' ) , <EOL> } , <EOL> '<STR_LIT>' : dumps ( loads ( json ) , indent = <NUM_LIT:4> ) , <EOL> } ) <EOL> elif '<STR_LIT>' in request . GET : <EOL> return HttpResponse ( <EOL> '<STR_LIT>' % ( request . GET [ '<STR_LIT>' ] , json ) , <EOL> content_type = '<STR_LIT>' , <EOL> ) <EOL> else : <EOL> return HttpResponse ( json , content_type = '<STR_LIT:application/json>' ) <EOL> return wrap <EOL> def paginate ( fn ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( request , ** kwargs ) : <EOL> try : <EOL> page = max ( int ( request . GET [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) if '<STR_LIT>' in request . GET else <NUM_LIT:1> <EOL> size = min ( max ( int ( request . GET [ '<STR_LIT:size>' ] ) , <NUM_LIT:0> ) , <NUM_LIT:20> ) if '<STR_LIT:size>' in request . GET else <NUM_LIT:20> <EOL> except ValueError : <EOL> raise APIException ( "<STR_LIT>" ) <EOL> return fn ( request , ** kwargs ) [ ( page - <NUM_LIT:1> ) * size : page * size ] <EOL> return wrap <EOL> def minmax ( field ) : <EOL> """<STR_LIT>""" <EOL> def outer ( fn ) : <EOL> def inner ( request , ** kwargs ) : <EOL> filters = { } <EOL> try : <EOL> if '<STR_LIT>' in request . GET : <EOL> filters [ '<STR_LIT>' % field ] = timestamp_to_datetime ( int ( request . GET [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in request . GET : <EOL> filters [ '<STR_LIT>' % field ] = timestamp_to_datetime ( int ( request . GET [ '<STR_LIT>' ] ) ) <EOL> except ValueError : <EOL> raise APIException ( "<STR_LIT>" ) <EOL> return fn ( request , ** kwargs ) . filter ( ** filters ) <EOL> return inner <EOL> return outer <EOL> @ endpoint <EOL> @ paginate <EOL> @ minmax ( '<STR_LIT:date>' ) <EOL> def articles ( request ) : <EOL> """<STR_LIT>""" <EOL> return Article . objects . select_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) . filter ( status = Article . PUBLISHED ) <EOL> @ endpoint <EOL> def article_by_id ( request , id ) : <EOL> """<STR_LIT>""" <EOL> return Article . objects . select_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) . filter ( pk = id , status = Article . PUBLISHED ) <EOL> @ endpoint <EOL> @ paginate <EOL> @ minmax ( '<STR_LIT>' ) <EOL> def authors ( request ) : <EOL> """<STR_LIT>""" <EOL> return Profile . objects . select_related ( '<STR_LIT:user>' ) . all ( ) <EOL> @ endpoint <EOL> def author_by_id ( request , id ) : <EOL> """<STR_LIT>""" <EOL> return Profile . objects . select_related ( '<STR_LIT:user>' ) . filter ( pk = id ) <EOL> @ endpoint <EOL> @ paginate <EOL> @ minmax ( '<STR_LIT:date>' ) <EOL> def articles_by_author ( request , id ) : <EOL> """<STR_LIT>""" <EOL> return Article . objects . select_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) . filter ( author = id , status = Article . PUBLISHED ) <EOL> @ endpoint <EOL> @ paginate <EOL> def categories ( request ) : <EOL> """<STR_LIT>""" <EOL> return Category . objects . all ( ) . annotate ( num_articles = Count ( '<STR_LIT>' ) ) <EOL> @ endpoint <EOL> @ paginate <EOL> @ minmax ( '<STR_LIT:date>' ) <EOL> def articles_by_category ( request , id ) : <EOL> """<STR_LIT>""" <EOL> return Article . objects . select_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) . filter ( category = id , status = Article . PUBLISHED ) </s>
