<s> import re , datetime <EOL> from functools import wraps <EOL> import pytz <EOL> from django . http import HttpResponse <EOL> try : <EOL> from django . utils . timezone import get_default_timezone <EOL> except ImportError : <EOL> from django . conf import settings <EOL> _default_timezone = pytz . timezone ( settings . TIME_ZONE ) <EOL> get_default_timezone = lambda : _default_timezone <EOL> from . decorators import render_to_json <EOL> class AjaxException ( Exception ) : <EOL> def __init__ ( self , error = '<STR_LIT>' , ** kwargs ) : <EOL> defaults = { '<STR_LIT:success>' : False , '<STR_LIT:error>' : error , '<STR_LIT:data>' : { } } <EOL> self . params = dict ( defaults , ** kwargs ) <EOL> class Ajax ( object ) : <EOL> error = AjaxException <EOL> def __call__ ( self , func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( request , * args , ** kwargs ) : <EOL> try : <EOL> data = func ( request , * args , ** kwargs ) <EOL> if isinstance ( data , HttpResponse ) : <EOL> return data <EOL> return { '<STR_LIT:success>' : True , '<STR_LIT:data>' : data } <EOL> except self . error as e : <EOL> return e . params <EOL> return render_to_json ( default = encode_object ) ( wrapper ) <EOL> def catch ( self , * exceptions ) : <EOL> assert exceptions , '<STR_LIT>' <EOL> def decorator ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except self . error : <EOL> raise <EOL> except exceptions as e : <EOL> error_name = camel_to_underscores ( e . __class__ . __name__ ) <EOL> error_text = e . args [ <NUM_LIT:0> ] if e . args else '<STR_LIT>' <EOL> raise self . error ( error_name , error_text = error_text ) <EOL> return wrapper <EOL> return decorator <EOL> def login_required ( self , func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( request , * args , ** kwargs ) : <EOL> if request . user . is_anonymous ( ) : <EOL> raise self . error ( '<STR_LIT>' ) <EOL> else : <EOL> return func ( request , * args , ** kwargs ) <EOL> return wrapper <EOL> ajax = Ajax ( ) <EOL> def camel_to_underscores ( name ) : <EOL> words = re . findall ( r'<STR_LIT>' , name ) <EOL> return '<STR_LIT:_>' . join ( w . lower ( ) for w in words ) <EOL> from pytz . tzinfo import DstTzInfo <EOL> DstTzInfo . __json__ = lambda self : self . zone <EOL> UTC = pytz . timezone ( '<STR_LIT>' ) <EOL> EPOCH = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , tzinfo = UTC ) <EOL> def encode_object ( obj ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . __json__ ( ) <EOL> elif isinstance ( obj , datetime . datetime ) : <EOL> delta = to_default ( obj ) - EPOCH <EOL> return { '<STR_LIT>' : '<STR_LIT:date>' , '<STR_LIT>' : int ( delta . total_seconds ( ) * <NUM_LIT:1000> ) } <EOL> elif isinstance ( obj , datetime . date ) : <EOL> return datetime . datetime ( * obj . timetuple ( ) [ : <NUM_LIT:6> ] ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % obj . __class__ . __name__ ) <EOL> def to_default ( dt ) : <EOL> if dt . tzinfo is None : <EOL> return get_default_timezone ( ) . localize ( dt ) <EOL> else : <EOL> return dt . astimezone ( get_default_timezone ( ) ) </s>
