예제 #1
0
class OverrideController(object):

  '''
  classdocs
  '''

  def __init__(self, request):
    self.request = request
    self.dbSession = DBSession()


  @view_config(route_name='resource.any', request_method='GET', http_cache=0)
  def OverrideResourceHandler(self):
    
    log.debug("ANY override handler")

    authorName = self.request.matchdict['authorname']
    resource = self.request.matchdict['resource']
    ext = self.request.matchdict['ext']

    base = os.path.dirname(os.path.dirname(__file__))

    # check for a custom asset
    candidate = '%s/assets/custom/%s/%s.%s' % (base,authorName,resource,ext)
    if os.path.exists(candidate):
      asset = open(candidate)
      contentType=mimetypes.guess_type(candidate)[0]
      return Response(content_type=contentType,app_iter=asset)
    else:
      # check for a stock asset based on the author's configured template
      try:
        template, = self.dbSession.query(Author.template).filter_by(author_name=authorName).one()
      except NoResultFound:
        return HTTPNotFound(self.request.path)
      candidate = '%s/assets/stock/%s/%s.%s' % (base,template,resource,ext) if template else None 
      if candidate and os.path.exists(candidate):
        asset = open(candidate)
        contentType=mimetypes.guess_type(candidate)[0]
        return Response(content_type=contentType,app_iter=asset)
      else:
        # check for a default asset
        candidate = '%s/assets/default/%s.%s' % (base,resource,ext)
        if os.path.exists(candidate):
          asset = open(candidate)
          contentType=mimetypes.guess_type(candidate)[0]
          return Response(content_type=contentType,app_iter=asset)
        else:
          return HTTPNotFound(self.request.path)
예제 #2
0
 def __init__(self, request):
   self.request = request
   self.dbSession = DBSession()