Changeset 48

Show
Ignore:
Timestamp:
05/31/05 20:02:55 (4 years ago)
Author:
zool
Message:

altering getitems support in the UI

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • bbox/bbox/ui/__init__.py

    Revision 47 Revision 48
    1from Cheetah.Template import Template 1from Cheetah.Template import Template 
    2from bbox import BBox 2from bbox import BBox 
    3#from spatialStore import Store 3#from spatialStore import Store 
    4import quixote 4import quixote 
    5import os  5import os  
    6import RDF 6import RDF 
    7import rdfobj 7import rdfobj 
    8 8 
    9_q_exports = ['_q_index','subs','_q_lookup'9_q_exports = ['_q_index','subs','_q_lookup','getitems'
    10 10 
    11def _q_index(r): 11def _q_index(r): 
    12    return 'bbox' 12    return 'bbox' 
    13 13 
    14def subs(r): 14def subs(r): 
    15    """List current subscriptions.""" 15    """List current subscriptions.""" 
    16    bb = BBoxUI() 16    bb = BBoxUI() 
    17    return bb.subs(r) 17    return bb.subs(r) 
    18 18 
    19def read_subs(r): 19def read_subs(r): 
    20    """Trigger a read of updates to subscriptions, if found.""" 20    """Trigger a read of updates to subscriptions, if found.""" 
    21    bb = BBoxUI() 21    bb = BBoxUI() 
    22    return bb.read_subs(r) 22    return bb.read_subs(r) 
    23 23 
    24def getitems(r):   
    25    """Provides a bloglines-API style interface."""   
    26    bb = BBoxUI()   
    27    return bb.getitems(r)   
    28       
    29def _q_lookup(r,n): 24def _q_lookup(r,n): 
    30    pass 25    pass 
    31 26 
    32#def _q_access(request): 27#def _q_access(request): 
    33#    raise UnauthorizedError(realm='Foo Realm') 28#    raise UnauthorizedError(realm='Foo Realm') 
    34 29 
    35class BBoxUI: 30class BBoxUI: 
    36    def __init__(self): 31    def __init__(self): 
    37        self.bbox = BBox('bbox.db') 32        self.bbox = BBox('bbox.db') 
    38 33 
    39    def subscribe(self,r): 34    def subscribe(self,r): 
    40        uri = r.get_form_var('uri') 35        uri = r.get_form_var('uri') 
    41        self.bbox.subscribe(uri=uri) 36        self.bbox.subscribe(uri=uri) 
    42        context = self.bbox.visit(uri) 37        context = self.bbox.visit(uri) 
    43        self.bbox.read(uri,context=context) 38        self.bbox.read(uri,context=context) 
    44 39 
    45    def read_subs(self,r): 40    def read_subs(self,r): 
    46        self.bbox.read_subscriptions() 41        self.bbox.read_subscriptions() 
    47 42 
    48    def subs(self,r): 43    def subs(self,r): 
    49        subs = self.bbox.subscriptions() 44        subs = self.bbox.subscriptions() 
    50        xml = self.serialise(subs) 45        xml = self.serialise(subs) 
    51        return xml 46        return xml 
    52 47 
    53    def getitems(self,r): 48    def getitems(self,r): 
    54        """This is the bloglines-style interface""" 49        """This is the bloglines-style interface""" 
    55        """See http://www.bloglines.com/services/api/getitems """     50        """See http://www.bloglines.com/services/api/getitems """     
    56         51         
    57        read = r.get_form_var('n') 52        read = r.get_form_var('n') 
    58        """ 'n' parameter - 1 marks downloaded items as read, 0 does not.""" 53        """ 'n' parameter - 1 marks downloaded items as read, 0 does not.""" 
    59 54 
    60        date = r.get_form_var('d') 55        date = r.get_form_var('d') 
    61        """ 'd' parameter - date to get new entries since. """ 56        """ 'd' parameter - date to get new entries since. """ 
    62 57 
    63        sub = r.get_form_var('s') 58        s = r.get_form_var('s') 
    64         59         
    65        """ 'subid'. we will accept an integer id which is a unique key in the node table, but this parameter is optional instead of compulsory; you can otherwise supply a URI-encoded url...""" 60        """ 'subid'. we will accept an integer id which is a unique key in the node table, but this parameter is optional instead of compulsory; you can otherwise supply a URI-encoded url...""" 
    66          61         #sub = bbox.subscription(s) 
       62         items = bbox.items(s) 
       63         out = '' 
       64         for i in items: 
       65             out = out + str(i) + "\n" 
       66  
       67         if d: 
       68             pass  
       69              
    67        uri = r.get_form_var('uri') 70        uri = r.get_form_var('uri') 
    68        if uri is None: uri = r.get_form_var('u') 71        if uri is None: uri = r.get_form_var('u') 
    69           
    70          72          
    71        b_user = self.bbox.root_user()   
    72        self.bbox.user(b_user)   
    73   
    74        # this now looks for ocntents of self.user()   
    75        self.bbox.read_subscriptions()   
    76 73 
    77 74 
    78    """HTTP Status Response 75    """HTTP Status Response 
    79 76 
    80    * 200 - normal 77    * 200 - normal 
    81    * 304 - the request produced no entries (either there were no unread entries, or no entries after the given date, depending on the parameters passed in) 78    * 304 - the request produced no entries (either there were no unread entries, or no entries after the given date, depending on the parameters passed in) 
    82    * 401 - incorrect email address or password 79    * 401 - incorrect email address or password 
    83    * 403 - invalid or missing BloglinesSubId 80    * 403 - invalid or missing BloglinesSubId 
    84    * 410 - subscription has been deleted 81    * 410 - subscription has been deleted 
    85    """ 82    """ 
    86 83 
    87    def serialise(self,objs): 84    def serialise(self,objs): 
    88        """Serialises a list of rdf objects, supplied, as RDF/XML."""  85        """Serialises a list of rdf objects, supplied, as RDF/XML."""  
    89        from rdfobj import dc 86        from rdfobj import dc 
    90        dc = rdfobj.dc 87        dc = rdfobj.dc 
    91        out = '' 88        out = '' 
    92        # proper serialisation 89        # proper serialisation 
    93        tmpstore = RDF.MemoryStorage() 90        tmpstore = RDF.MemoryStorage() 
    94        tmpmodel = RDF.Model(tmpstore) 91        tmpmodel = RDF.Model(tmpstore) 
    95        for o in objs: 92        for o in objs: 
    96            statement = RDF.Statement(RDF.Uri(o.uri()),None,None) 93            statement = RDF.Statement(RDF.Uri(o.uri()),None,None) 
    97            statements = self.bbox.model.model.find_statements(statement) 94            statements = self.bbox.model.model.find_statements(statement) 
    98            for s in statements: 95            for s in statements: 
    99                tmpmodel.add_statement(s) 96                tmpmodel.add_statement(s) 
    100        ser = RDF.Serializer() 97        ser = RDF.Serializer() 
    101        out = ser.serialize_model_to_string(tmpmodel) 98        out = ser.serialize_model_to_string(tmpmodel) 
    102        return out 99        return out 
    103 100 
    104from quixote.errors import AccessError 101from quixote.errors import AccessError 
    105status_code = 401 102status_code = 401 
    106title = "Unauthorized" 103title = "Unauthorized" 
    107description = "You are not authorized to access this resource." 104description = "You are not authorized to access this resource." 
    108 105 
    109class UnauthorizedError(AccessError): 106class UnauthorizedError(AccessError): 
    110    """The request requires user authentication. 107    """The request requires user authentication. 
    111   108   
    112    This subclass of AccessError sends a 401 instead of a 403, 109    This subclass of AccessError sends a 401 instead of a 403, 
    113    hinting that the client should try again with authentication.""" 110    hinting that the client should try again with authentication.""" 
    114 111 
    115    def __init__(self, realm='Protected', public_msg=None, private_msg=None): 112    def __init__(self, realm='Protected', public_msg=None, private_msg=None): 
    116        self.realm = realm 113        self.realm = realm 
    117        AccessError.__init__(self, public_msg, private_msg) 114        AccessError.__init__(self, public_msg, private_msg) 
    118 115 
    119    def format(self, request): 116    def format(self, request): 
    120        request.response.set_header('WWW-Authenticate', 'Basic realm="%s"' % self.realm) 117        request.response.set_header('WWW-Authenticate', 'Basic realm="%s"' % self.realm) 
    121        return AccessError.format(self, request) 118        return AccessError.format(self, request)