web/views/urlpublishing.py
changeset 2650 18aec79ec3a3
parent 2293 7ded2a1416e4
child 2657 de974465d381
equal deleted inserted replaced
2649:5d4a943695d1 2650:18aec79ec3a3
    54 
    54 
    55     def __init__(self, default_method='view'):
    55     def __init__(self, default_method='view'):
    56         super(URLPublisherComponent, self).__init__()
    56         super(URLPublisherComponent, self).__init__()
    57         self.default_method = default_method
    57         self.default_method = default_method
    58         evaluators = []
    58         evaluators = []
    59         for evaluatorcls in self.vreg.registry_objects('components',
    59         for evaluatorcls in self.vreg['components']['urlpathevaluator']:
    60                                                        'urlpathevaluator'):
       
    61             # instantiation needed
    60             # instantiation needed
    62             evaluator = evaluatorcls(self)
    61             evaluator = evaluatorcls(self)
    63             evaluators.append(evaluator)
    62             evaluators.append(evaluator)
    64         self.evaluators = sorted(evaluators, key=lambda x: x.priority)
    63         self.evaluators = sorted(evaluators, key=lambda x: x.priority)
    65 
    64 
    81         :raise NotFound: if no handler is able to decode the given path
    80         :raise NotFound: if no handler is able to decode the given path
    82         """
    81         """
    83         parts = [part for part in path.split('/')
    82         parts = [part for part in path.split('/')
    84                  if part != ''] or (self.default_method,)
    83                  if part != ''] or (self.default_method,)
    85         if req.form.get('rql'):
    84         if req.form.get('rql'):
    86             if parts[0] in self.vreg.registry('controllers'):
    85             if parts[0] in self.vreg['controllers']:
    87                 return parts[0], None
    86                 return parts[0], None
    88             return 'view', None
    87             return 'view', None
    89         for evaluator in self.evaluators:
    88         for evaluator in self.evaluators:
    90             try:
    89             try:
    91                 pmid, rset = evaluator.evaluate_path(req, parts[:])
    90                 pmid, rset = evaluator.evaluate_path(req, parts[:])
   112 
   111 
   113         <publishing_method>?parameters...
   112         <publishing_method>?parameters...
   114     """
   113     """
   115     priority = 0
   114     priority = 0
   116     def evaluate_path(self, req, parts):
   115     def evaluate_path(self, req, parts):
   117         if len(parts) == 1 and parts[0] in self.vreg.registry('controllers'):
   116         if len(parts) == 1 and parts[0] in self.vreg['controllers']:
   118             return parts[0], None
   117             return parts[0], None
   119         raise PathDontMatch()
   118         raise PathDontMatch()
   120 
   119 
   121 
   120 
   122 class EidPathEvaluator(URLPathEvaluator):
   121 class EidPathEvaluator(URLPathEvaluator):
   193     """
   192     """
   194     priority = 3
   193     priority = 3
   195     def evaluate_path(self, req, parts):
   194     def evaluate_path(self, req, parts):
   196         # uri <=> req._twreq.path or req._twreq.uri
   195         # uri <=> req._twreq.path or req._twreq.uri
   197         uri = req.url_unquote('/' + '/'.join(parts))
   196         uri = req.url_unquote('/' + '/'.join(parts))
   198         vobjects = sorted(self.vreg.registry_objects('urlrewriting'),
   197         vobjects = sorted(self.vreg['urlrewriting'].all_objects(),
   199                           key=lambda x: x.priority,
   198                           key=lambda x: x.priority, reverse=True)
   200                           reverse=True)
       
   201         for rewritercls in vobjects:
   199         for rewritercls in vobjects:
   202             rewriter = rewritercls()
   200             rewriter = rewritercls()
   203             try:
   201             try:
   204                 # XXX we might want to chain url rewrites
   202                 # XXX we might want to chain url rewrites
   205                 return rewriter.rewrite(req, uri)
   203                 return rewriter.rewrite(req, uri)
   218         if len(parts) < 2:
   216         if len(parts) < 2:
   219             raise PathDontMatch()
   217             raise PathDontMatch()
   220         # remove last part and see if this is something like an actions
   218         # remove last part and see if this is something like an actions
   221         # if so, call
   219         # if so, call
   222         try:
   220         try:
       
   221             actionsreg = self.vreg['actions']
   223             requested = parts.pop(-1)
   222             requested = parts.pop(-1)
   224             actions = self.vreg.registry_objects('actions', requested)
   223             actions = actionsreg[requested]
   225         except RegistryException:
   224         except RegistryException:
   226             raise PathDontMatch()
   225             raise PathDontMatch()
   227         for evaluator in self.urlpublisher.evaluators:
   226         for evaluator in self.urlpublisher.evaluators:
   228             if evaluator is self or evaluator.priority == 0:
   227             if evaluator is self or evaluator.priority == 0:
   229                 continue
   228                 continue
   231                 pmid, rset = evaluator.evaluate_path(req, parts[:])
   230                 pmid, rset = evaluator.evaluate_path(req, parts[:])
   232             except PathDontMatch:
   231             except PathDontMatch:
   233                 continue
   232                 continue
   234             else:
   233             else:
   235                 try:
   234                 try:
   236                     action = self.vreg.select_best(actions, req, rset=rset)
   235                     action = actionsreg.select_best(actions, req, rset=rset)
   237                 except RegistryException:
   236                 except RegistryException:
   238                     raise PathDontMatch()
   237                     continue
   239                 else:
   238                 else:
   240                     # XXX avoid redirect
   239                     # XXX avoid redirect
   241                     raise Redirect(action.url())
   240                     raise Redirect(action.url())
   242         raise PathDontMatch()
   241         raise PathDontMatch()