<s> import os , sys , logging <EOL> sys . path . insert ( <NUM_LIT:1> , os . path . join ( os . path . abspath ( '<STR_LIT:.>' ) , '<STR_LIT>' ) ) <EOL> import webapp2 , jinja2 <EOL> from decimal import Decimal <EOL> from base64 import b32encode <EOL> from cStringIO import StringIO <EOL> from jinja2 import Markup <EOL> from urllib import urlencode <EOL> from urllib import quote as url_quote <EOL> from google . appengine . api import memcache <EOL> import qrcode <EOL> from qrcode . image . svg import SvgPathImage , SvgFragmentImage <EOL> from qrcode . image . pil import PilImage <EOL> from ckapi import CKRequestor , CKReqReceive , CKObject <EOL> from models import MyInvoice , MAX_PAY_TIME <EOL> logger = logging . getLogger ( __name__ ) <EOL> JINJA_ENV = jinja2 . Environment ( <EOL> loader = jinja2 . FileSystemLoader ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) , <EOL> extensions = [ '<STR_LIT>' ] , <EOL> autoescape = True ) <EOL> try : <EOL> from settings import * <EOL> except NotImplementedError : <EOL> from settings_dev import * <EOL> def get_pubnub_auth ( ) : <EOL> d = memcache . get ( '<STR_LIT>' ) <EOL> if d : <EOL> return d <EOL> d = CK_API . pubnub_enable ( ) <EOL> memcache . add ( '<STR_LIT>' , d ) <EOL> return d <EOL> def get_ck_detail ( refnum , cache_ttl = <NUM_LIT:15> ) : <EOL> rk = '<STR_LIT>' % refnum <EOL> d = memcache . get ( rk ) <EOL> if d : return d <EOL> d = CK_API . get_detail ( refnum ) <EOL> memcache . add ( rk , d , time = cache_ttl ) <EOL> return d <EOL> class MainHandler ( webapp2 . RequestHandler ) : <EOL> def get ( self ) : <EOL> template_values = { } <EOL> template = JINJA_ENV . get_template ( '<STR_LIT>' ) <EOL> self . response . write ( template . render ( template_values ) ) <EOL> class InvoiceHandler ( webapp2 . RequestHandler ) : <EOL> def get ( self , token , example = '<STR_LIT>' ) : <EOL> ctx = CKObject ( max_time = MAX_PAY_TIME ) <EOL> if token == '<STR_LIT>' : <EOL> cct = example . upper ( ) <EOL> ctx . cct = cct <EOL> ctx . payable = Decimal ( '<STR_LIT>' ) <EOL> ctx . payable_cct = cct <EOL> ctx . amount_cct = '<STR_LIT>' <EOL> ctx . amount = Decimal ( '<STR_LIT>' ) <EOL> ctx . show_fiat = True <EOL> ctx . label = '<STR_LIT>' <EOL> ctx . pubkey = '<STR_LIT>' <EOL> ctx . time_left = <NUM_LIT:10> * <NUM_LIT> <EOL> ctx . is_paid = False <EOL> else : <EOL> inv = MyInvoice . get_by_token ( token ) <EOL> if not inv : <EOL> ctx . token = token <EOL> template = JINJA_ENV . get_template ( '<STR_LIT>' ) <EOL> self . response . write ( template . render ( ctx ) ) <EOL> return <EOL> ctx . update ( inv . to_dict ( ) ) <EOL> cct = inv . payable_cct <EOL> ctx . label = ctx . label or '<STR_LIT>' <EOL> ctx . time_left = inv . get_time_left ( ) <EOL> d = get_ck_detail ( inv . ck_refnum ) <EOL> print "<STR_LIT>" % d <EOL> ctx . is_paid = d . is_completed or ( d . amount_so_far . decimal >= ctx . payable ) <EOL> assert ctx . pubkey == d . coin . address <EOL> if ctx . show_fiat : <EOL> ctx . exchange_rates = [ <EOL> ( '<STR_LIT>' % cct , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' % ( ctx . amount , cct ) , '<STR_LIT>' ) , <EOL> ] <EOL> ctx . pubnub_auth = get_pubnub_auth ( ) <EOL> ctx . bitcoin_link = ( '<STR_LIT>' % ctx . pubkey ) + urlencode ( dict ( <EOL> amount = ctx . amount , message = ctx . label [ <NUM_LIT:0> : <NUM_LIT> ] ) ) <EOL> template = JINJA_ENV . get_template ( '<STR_LIT>' ) <EOL> self . response . write ( template . render ( ctx ) ) <EOL> class QRHandler ( webapp2 . RequestHandler ) : <EOL> def get ( self , nonce , extension ) : <EOL> qr = qrcode . QRCode ( error_correction = qrcode . ERROR_CORRECT_H ) <EOL> qr . add_data ( "<STR_LIT>" ) <EOL> mime , factory = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , SvgPathImage ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , PilImage ) , <EOL> } [ extension ] <EOL> qr_img = qr . make_image ( image_factory = factory ) <EOL> self . response . content_type = mime <EOL> self . response . content_type_params = { } <EOL> body = StringIO ( ) <EOL> qr_img . save ( body ) <EOL> self . response . write ( body . getvalue ( ) ) <EOL> class TestHandler ( webapp2 . RequestHandler ) : <EOL> def get ( self , token , test_case ) : <EOL> inv = MyInvoice . get_by_token ( token ) <EOL> if test_case == '<STR_LIT:0>' : <EOL> msg = dict ( event_code = '<STR_LIT>' , <EOL> activity = '<STR_LIT>' , <EOL> desc = "<STR_LIT>" , <EOL> request = inv . ck_refnum ) <EOL> ret = CK_API . pubnub_send ( msg ) <EOL> else : <EOL> self . error ( <NUM_LIT> ) <EOL> self . response . content_type = '<STR_LIT>' <EOL> self . response . write ( repr ( ret ) ) <EOL> class MakeHandler ( webapp2 . RequestHandler ) : <EOL> def get ( self ) : <EOL> template = JINJA_ENV . get_template ( '<STR_LIT>' ) <EOL> ctx = CKObject ( ) <EOL> ctx . valid_coins = ACCOUNT_MAP . keys ( ) <EOL> recent = MyInvoice . recent_invoices ( ) . fetch ( <NUM_LIT> ) <EOL> ctx . recent_invoices = recent <EOL> for inv in recent : <EOL> if inv . paid_at != None : <EOL> inv . text_status = '<STR_LIT>' <EOL> continue <EOL> try : <EOL> d = get_ck_detail ( inv . ck_refnum ) <EOL> except : <EOL> logger . error ( "<STR_LIT>" % inv . ck_refnum , exc_info = <NUM_LIT:1> ) <EOL> continue <EOL> inv . details = d <EOL> if d . is_completed or d . amount_so_far . decimal : <EOL> conf = d . events [ <NUM_LIT:0> ] . confirmed_at <EOL> if conf != None : <EOL> inv . paid_at = conf <EOL> inv . text_status = '<STR_LIT>' <EOL> inv . put_async ( ) <EOL> else : <EOL> inv . text_status = '<STR_LIT>' <EOL> else : <EOL> inv . text_status = '<STR_LIT>' <EOL> self . response . write ( template . render ( ctx ) ) <EOL> def post ( self ) : <EOL> amount = Decimal ( self . request . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> amount_cct = self . request . get ( '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:0> : <NUM_LIT:3> ] . upper ( ) <EOL> payable_cct = self . request . get ( '<STR_LIT>' , '<STR_LIT>' ) [ <NUM_LIT:0> : <NUM_LIT:3> ] . upper ( ) <EOL> label = self . request . get ( '<STR_LIT:label>' , None ) <EOL> show_fiat = self . request . get ( '<STR_LIT>' , False ) <EOL> n = MyInvoice ( ) <EOL> n . amount_cct = amount_cct <EOL> n . payable_cct = payable_cct <EOL> if amount_cct == payable_cct : <EOL> n . payable = amount <EOL> n . amount = amount <EOL> else : <EOL> q = CK_API . get ( '<STR_LIT>' % ( payable_cct , amount_cct , amount ) ) <EOL> n . payable = q . result . decimal . quantize ( Decimal ( '<STR_LIT>' ) ) <EOL> n . amount = amount <EOL> n . label = label [ <NUM_LIT:0> : <NUM_LIT> ] if label else None <EOL> n . show_fiat = bool ( show_fiat ) <EOL> n . token = b32encode ( os . urandom ( <NUM_LIT:10> ) ) <EOL> acct = ACCOUNT_MAP [ payable_cct ] <EOL> r = CK_API . put ( '<STR_LIT>' , account = acct , <EOL> memo = '<STR_LIT>' % n . get_url ( ) , amount = n . payable ) <EOL> req = r . result <EOL> n . pubkey = req . coin . address <EOL> n . ck_refnum = req . ref_number <EOL> key = n . put ( ) <EOL> from google . appengine . ext import ndb <EOL> ndb . get_context ( ) . clear_cache ( ) <EOL> self . redirect ( '<STR_LIT>' ) <EOL> def inline_svg_qr ( contents , boxsize = <NUM_LIT:10> ) : <EOL> qr = qrcode . QRCode ( error_correction = qrcode . ERROR_CORRECT_H , box_size = boxsize ) <EOL> qr . add_data ( contents ) <EOL> qr_img = qr . make_image ( image_factory = SvgPathImage ) <EOL> body = StringIO ( ) <EOL> qr_img . save ( body ) <EOL> return Markup ( body . getvalue ( ) ) <EOL> JINJA_ENV . filters . update ( { <EOL> '<STR_LIT>' : inline_svg_qr , <EOL> '<STR_LIT>' : url_quote , <EOL> } ) <EOL> app = webapp2 . WSGIApplication ( [ <EOL> ( '<STR_LIT:/>' , MainHandler ) , <EOL> ( '<STR_LIT>' , MakeHandler ) , <EOL> ( '<STR_LIT>' , InvoiceHandler ) , <EOL> ( '<STR_LIT>' , TestHandler ) , <EOL> ( '<STR_LIT>' , InvoiceHandler ) , <EOL> ( '<STR_LIT>' , QRHandler ) , <EOL> ] , debug = True ) </s>
