comparison src/server/server.py @ 922:16d1084d1371

server (pages): added "None" access (page is not rendered at all) and some HTTP code constants + helper methods to get session data
author Goffi <goffi@goffi.org>
date Mon, 03 Apr 2017 01:00:26 +0200
parents cf527974a0fa
children edb322c87ea4
comparison
equal deleted inserted replaced
921:cf527974a0fa 922:16d1084d1371
1457 if name in self.named_pages: 1457 if name in self.named_pages:
1458 raise exceptions.ConflictError(_(u'a Libervia page named "{}" already exists'.format(name))) 1458 raise exceptions.ConflictError(_(u'a Libervia page named "{}" already exists'.format(name)))
1459 self.named_pages[name] = self 1459 self.named_pages[name] = self
1460 if access is None: 1460 if access is None:
1461 access = C.PAGES_ACCESS_PUBLIC 1461 access = C.PAGES_ACCESS_PUBLIC
1462 if access != C.PAGES_ACCESS_PUBLIC: 1462 if access not in (C.PAGES_ACCESS_PUBLIC, C.PAGES_ACCESS_PROFILE, C.PAGES_ACCESS_NONE):
1463 raise NotImplementedError(u"Non public access are not implemented yet") 1463 raise NotImplementedError(_(u"{} access is not implemented yet").format(access))
1464 self.access = access
1464 self.parse_url = parse_url 1465 self.parse_url = parse_url
1465 self.prepare_render = prepare_render 1466 self.prepare_render = prepare_render
1466 self.template = template 1467 self.template = template
1467 self.render_method = render 1468 self.render_method = render
1469 if access == C.PAGES_ACCESS_NONE:
1470 # none pages just return a 404, no further check is needed
1471 return
1468 if template is None: 1472 if template is None:
1469 if not callable(render): 1473 if not callable(render):
1470 log.error(_(u"render must be implemented and callable if template is not set")) 1474 log.error(_(u"render must be implemented and callable if template is not set"))
1471 else: 1475 else:
1472 if render is not None: 1476 if render is not None:
1523 """ 1527 """
1524 pathElement = request.postpath.pop(0) 1528 pathElement = request.postpath.pop(0)
1525 request.prepath.append(pathElement) 1529 request.prepath.append(pathElement)
1526 return urllib.unquote(pathElement).decode('utf-8') 1530 return urllib.unquote(pathElement).decode('utf-8')
1527 1531
1528 def pageError(self, request, code=404): 1532 def pageError(self, request, code=C.HTTP_NOT_FOUND):
1529 """generate an error page and terminate the request 1533 """generate an error page and terminate the request
1530 1534
1531 @param request(server.Request): HTTP request 1535 @param request(server.Request): HTTP request
1532 @param core(int): error code to use 1536 @param core(int): error code to use
1533 """ 1537 """
1582 1586
1583 def _renderEb(self, failure_, request): 1587 def _renderEb(self, failure_, request):
1584 """don't raise error on CancelError""" 1588 """don't raise error on CancelError"""
1585 failure_.trap(exceptions.CancelError) 1589 failure_.trap(exceptions.CancelError)
1586 1590
1591 def getProfile(self, request):
1592 """helper method to easily get current profile
1593
1594 @return (unicode, None): current profile
1595 None if no profile session is started
1596 """
1597 sat_session = self.host.getSessionData(request, session_iface.ISATSession)
1598 return sat_session.profile
1599
1600 def getRData(self, request):
1601 """helper method to get request data dict
1602
1603 this dictionnary if for the request only, it is not saved in session
1604 It is mainly used to pass data between pages/methods called during request workflow
1605 @return (dict): request data
1606 """
1607 try:
1608 return request.data
1609 except AttributeError:
1610 request.data = {}
1611 return request.data
1612
1613 def _checkAccess(self, data, request):
1614 """Check access according to self.access
1615
1616 if access is not granted, show a HTTP_UNAUTHORIZED pageError and stop request,
1617 else return data (so it can be inserted in deferred chain
1618 """
1619 if self.access == C.PAGES_ACCESS_PUBLIC:
1620 pass
1621 elif self.access == C.PAGES_ACCESS_PROFILE:
1622 profile = self.getProfile(request)
1623 if not profile:
1624 # no session started, access is not granted
1625 self.pageError(request, C.HTTP_UNAUTHORIZED)
1626
1627 return data
1628
1587 def render_GET(self, request): 1629 def render_GET(self, request):
1588 if not hasattr(request, 'template_data'): 1630 if not hasattr(request, 'template_data'):
1589 request.template_data = {} 1631 request.template_data = {}
1632
1633 # XXX: here is the code which need to be executed once
1634 # at the beginning of the request hanling
1590 if request.postpath and not request.postpath[-1]: 1635 if request.postpath and not request.postpath[-1]:
1591 # we don't differenciate URLs finishing with '/' or not 1636 # we don't differenciate URLs finishing with '/' or not
1592 del request.postpath[-1] 1637 del request.postpath[-1]
1638
1593 d = defer.Deferred() 1639 d = defer.Deferred()
1640 d.addCallback(self._checkAccess, request)
1641
1594 if self.parse_url is not None: 1642 if self.parse_url is not None:
1595 d.addCallback(self.parse_url, request) 1643 d.addCallback(self.parse_url, request)
1596 1644
1597 d.addCallback(self._subpagesHandler, request) 1645 d.addCallback(self._subpagesHandler, request)
1598 1646