Changeset 326

Show
Ignore:
Timestamp:
06/13/09 13:01:53 (3 years ago)
Author:
ocjhb
Message:

cleaning up a bit

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/sandbox/jhb/webwallet/cgi/wallet.cgi

    r325 r326  
    66################################## 
    77 
     8import sys, os 
     9sys.path.append(libdir) 
    810import cgitb; cgitb.enable() 
    911import cgi 
    10 import sys, os 
    11 sys.path.append(libdir) 
    1212import oc2 
    1313from oc2 import storage as oc2storage 
    1414from oc2 import wallet, transports 
    15  
    16  
    17 def out(text): 
    18     if type(text) != type(''): 
    19         text = str(text) 
    20     output.append(text+'<br/>') 
    21  
    22 def printout(): 
    23     print """Content-type:text/html""" 
    24     print 
    25     print '\n'.join(output) 
    2615 
    2716 
     
    3221        self.storage = storage 
    3322        self.wallet = wallet.Wallet(storage) 
     23        self.output = [] 
     24 
     25    def out(self,text): 
     26        if type(text) != type(''): 
     27            text = str(text) 
     28        self.output.append(text+'<br/>') 
     29         
     30    def printout(self): 
     31        print """Content-type:text/html""" 
     32        print 
     33        print '\n'.join(self.output) 
    3434 
    3535    def getCurrency(self,currencyId): 
     
    3939        return [(cdd.currencyId,(cdd,amount)) for cdd,amount in w.wallet.listCurrencies()] 
    4040 
     41    def dispatchRequest(self): 
     42        self.env = os.environ 
     43        self.form = cgi.FieldStorage(keep_blank_values=1) 
     44        self.action = action = self.form.getfirst('action','') 
     45        self.method = method = self.env['REQUEST_METHOD'].lower() 
     46         
    4147 
     48        if method == 'post': 
     49            if action == 'addcurrency': 
     50                self.addCurrency() 
     51            elif action == 'mint': 
     52                self.mintCoins() 
     53            elif action == '': 
     54                pass  
     55 
     56        elif method == 'get': 
     57            if action == '': 
     58                #basically nothing happened 
     59                self.displayMain() 
     60            elif action == 'addcurrency': 
     61                self.displayAddCurrency() 
     62            elif action == 'mint': 
     63                self.displayMint() 
     64 
     65 
     66    def addCurrency(self): 
     67        url = self.form.getfirst('url','') 
     68        if url: 
     69            transport = transports.HTTPTransport(url) 
     70            w.wallet.addCurrency(transport) 
     71            storage.save() 
     72        self.displayMain()         
     73 
     74 
     75    def mintCoins(self): 
     76        amount = int(self.form.getfirst('amount',1)) 
     77        reference = self.form.getfirst('reference') 
     78        cdd,wehave = self.getCurrency(self.form.getfirst('currencyId')) 
     79        transport = transports.HTTPTransport(cdd.issuerServiceLocation) 
     80        self.wallet.mintCoins(transport,amount,reference) 
     81        self.storage.save() 
     82        self.displayMain() 
     83 
     84 
     85    def displayMain(self): 
     86        tmp = [(cdd.currencyId,cdd,amount) for cdd,amount in self.wallet.listCurrencies()] 
     87        tmp.sort() 
     88        currencies = [(t[1],t[2]) for t in tmp] 
     89        items = [] 
     90        for cdd,amount in currencies: 
     91            entry = """ 
     92            <p>%s <b href='%s'>%ss</b><br/> 
     93                <a href='%s?action=mint&currencyId=%s'>Withdraw</a> 
     94            </p> 
     95            """ % (amount,cdd.issuerServiceLocation,cdd.currencyId,baseurl,cdd.currencyId) 
     96            items.append(entry) 
     97        items = '\n'.join(items) 
     98        html = """ 
     99        <html><body> 
     100        <h2>Wallet content</h2> 
     101        %s 
     102        ----<br/> 
     103        <a href='%s?action=addcurrency'>Add a currency</a> 
     104        </body></html> 
     105        """ % (items,baseurl) 
     106        self.out(html) 
     107 
     108    def displayAddCurrency(self): 
     109        html=""" 
     110        <html><body> 
     111        <h2>Add a currency</h2> 
     112        <form action='%s' method='post'> 
     113            The url of the issuer:<br> 
     114            <input type='text' name='url' value='http://baach.de:9090' /><br> 
     115            <input type='submit' /> 
     116            <input type='hidden' name='action' value='addcurrency'/> 
     117        </form> 
     118        </body></html> 
     119        """ % baseurl 
     120        self.out(html) 
     121 
     122    def displayMint(self): 
     123        currencyId = self.form.getfirst('currencyId','coin') 
     124        html=""" 
     125        <html><body> 
     126        <h2>Get new coins</h2> 
     127        <form action='%s' method='post'> 
     128            How many <b>%ss</b><br> 
     129            <input type='text' name='amount' value='1' /><br> 
     130            Reference<br> 
     131            <input type='text' name='reference' value='secret' /><br> 
     132            <input type='submit' /> 
     133            <input type='hidden' name='action' value='mint'/> 
     134            <input type='hidden' name='currencyId' value='%s'/> 
     135        </form> 
     136        </body></html> 
     137 
     138        """ % (baseurl,currencyId,currencyId) 
     139        self.out(html) 
     140 
     141         
    42142baseserver = "http://%s:%s" % (os.environ['SERVER_NAME'],os.environ['SERVER_PORT']) 
    43143baseurl = os.environ['SCRIPT_NAME'] 
    44144 
    45 env = os.environ 
    46 form = cgi.FieldStorage(keep_blank_values=1) 
    47  
    48 subpath = env['PATH_INFO'] 
    49  
    50 if subpath.startswith('/'): 
    51     subpath=subpath[1:] 
    52 method = env['REQUEST_METHOD'].lower() 
    53  
    54 action = form.getfirst('action','') 
    55  
    56 output = [] 
    57145 
    58146 
     
    61149storage.restore() 
    62150w = CGIWallet(storage) 
    63  
    64 # 
    65  
    66 if method=='post' and action == 'addcurrency': 
    67     url = form.getfirst('url','') 
    68     if url: 
    69         transport = transports.HTTPTransport(url) 
    70         w.wallet.addCurrency(transport) 
    71         storage.save() 
    72         action = '' 
    73 elif method=='post' and action == 'mint': 
    74     amount = int(form.getfirst('amount',1)) 
    75     reference = form.getfirst('reference') 
    76     cdd,wehave = w.getCurrency(form.getfirst('currencyId')) 
    77     transport = transports.HTTPTransport(cdd.issuerServiceLocation) 
    78     w.wallet.mintCoins(transport,amount,reference) 
    79     action = '' 
    80  
    81 elif method == 'post' and action == '': 
    82     #we got stuff posted from a wallet 
    83     pass 
    84  
    85  
    86 if action == '': 
    87     tmp = [(cdd.currencyId,cdd,amount) for cdd,amount in w.wallet.listCurrencies()] 
    88     tmp.sort() 
    89     currencies = [(t[1],t[2]) for t in tmp] 
    90     items = [] 
    91     for cdd,amount in currencies: 
    92         entry = """ 
    93         <li>%s %s<br/> 
    94             %s<br> 
    95             <a href='%s?action=mint&currencyId=%s'>Withdraw</a> 
    96         </li> 
    97         """ % (amount,cdd.currencyId,cdd.issuerServiceLocation,baseurl,cdd.currencyId) 
    98         items.append(entry) 
    99     items = '\n'.join(items) 
    100     html = """ 
    101     <html><body> 
    102     <b>Wallet content</b> 
    103     <ul> 
    104     %s 
    105     </ul> 
    106     ----<br/> 
    107     <a href='%s?action=addcurrency'>Add a currency</a> 
    108     </body></html> 
    109     """ % (items,baseurl) 
    110     out(html) 
    111  
    112 elif action == 'addcurrency': 
    113     html=""" 
    114     <html><body> 
    115     <b>Add a currency</b> 
    116     <form action='%s' method='post'> 
    117         The url of the issuer:<br> 
    118         <input type='text' name='url' value='http://baach.de:9090' /><br> 
    119         <input type='submit' /> 
    120         <input type='hidden' name='action' value='addcurrency'/> 
    121     </form> 
    122     </body></html> 
    123     """ % baseurl 
    124     out(html) 
    125  
    126 elif action == 'mint': 
    127     currencyId = form.getfirst('currencyId','coin') 
    128     html=""" 
    129     <html><body> 
    130     <b>Get new coins</b> 
    131     <form action='%s' method='post'> 
    132         How many %ss<br> 
    133         <input type='text' name='amount' value='1' /><br> 
    134         Reference<br> 
    135         <input type='text' name='reference' value='secret' /><br> 
    136         <input type='submit' /> 
    137         <input type='hidden' name='action' value='mint'/> 
    138         <input type='hidden' name='currencyId' value='%s'/> 
    139     </form> 
    140     </body></html> 
    141  
    142     """ % (baseurl,currencyId,currencyId) 
    143     out(html) 
     151w.dispatchRequest() 
    144152 
    145153#out('=' * 80) 
    146 for key,value in sorted(env.items()): 
    147     #out("%50s: %s" % (key,value)) 
    148     pass 
     154#for key,value in sorted(env.items()): 
     155#    #out("%50s: %s" % (key,value)) 
     156#    pass 
    149157#out(form.list) 
    150 printout() 
     158w.printout()