示例#1
0
文件: fields.py 项目: pombredanne/lux
 def __init__(self,
              required=None,
              default=None,
              initial=None,
              validation_error=None,
              help_text=None,
              label=None,
              widget=None,
              widget_attrs=None,
              disabled=None,
              attrname=None,
              wrong_value_message=None,
              **kwargs):
     self.name = attrname
     self.default = default if default is not None else self.default
     self.initial = initial
     self.required = required if required is not None else self.required
     self.validation_error = (validation_error or self.validation_error
                              or standard_validation_error)
     if wrong_value_message:
         self.wrong_value_message = wrong_value_message
     self.help_text = escape(help_text)
     self.label = label
     if widget:
         self.widget = lambda *args, **kwargs: widget(self, *args, **kwargs)
     self.widget_attrs = widget_attrs or {}
     if disabled:
         self.widget_attrs['disabled'] = 'disabled'
     self.handle_params(**kwargs)
     # Increase the creation counter, and save our local copy.
     self.creation_counter = Field.creation_counter
     Field.creation_counter += 1
示例#2
0
def attr_iter(attrs):
    for k in sorted(attrs):
        v = attrs[k]
        if v is True:
            yield " %s" % k
        elif v is not None:
            yield " %s='%s'" % (k, escape(v, force=True))
示例#3
0
文件: fields.py 项目: pombredanne/lux
 def __init__(self,
              required=None,
              default=None,
              initial=None,
              validation_error=None,
              help_text=None,
              label=None,
              widget=None,
              widget_attrs=None,
              disabled=None,
              attrname=None,
              wrong_value_message=None,
              **kwargs):
     self.name = attrname
     self.default = default if default is not None else self.default
     self.initial = initial
     self.required = required if required is not None else self.required
     self.validation_error = (validation_error or self.validation_error or
                              standard_validation_error)
     if wrong_value_message:
         self.wrong_value_message = wrong_value_message
     self.help_text = escape(help_text)
     self.label = label
     if widget:
         self.widget = lambda *args, **kwargs: widget(self, *args, **kwargs)
     self.widget_attrs = widget_attrs or {}
     if disabled:
         self.widget_attrs['disabled'] = 'disabled'
     self.handle_params(**kwargs)
     # Increase the creation counter, and save our local copy.
     self.creation_counter = Field.creation_counter
     Field.creation_counter += 1
示例#4
0
 def __init__(self,
              name=None,
              required=None,
              default=None,
              validation_error=None,
              help_text=None,
              label=None,
              attrs=None,
              validator=None,
              required_error=None,
              **kwargs):
     self.name = name
     self.default = default if default is not None else self.default
     self.required = required if required is not None else self.required
     self.validation_error = validation_error or self.validation_error
     self.required_error = required_error or self.required_error
     self.help_text = escape(help_text)
     self.label = label
     self.validator = validator
     self.attrs = dict(self.attrs or ())
     self.attrs.update(attrs or ())
     self.attrs['required'] = self.required
     self.handle_params(**kwargs)
     # Increase the creation counter, and save our local copy.
     self.creation_counter = Field.creation_counter
     Field.creation_counter += 1
示例#5
0
 def __init__(self, name=None, required=None, default=None,
              validation_error=None, help_text=None,
              validator=None, required_error=None, **kwargs):
     super().__init__(name, **kwargs)
     self.default = default if default is not None else self.default
     self.required = required if required is not None else self.required
     self.validation_error = validation_error or self.validation_error
     self.required_error = required_error or self.required_error
     self.help_text = escape(help_text)
     self.validator = validator
     self.attrs['required'] = self.required
示例#6
0
 def get(self, request):
     """The home page of this router"""
     ul = Html("ul")
     for router in sorted(self.routes, key=lambda r: r.creation_count):
         a = router.link(escape(router.route.path))
         li = Html("li", a, " %s" % router.parameters.get("title", ""))
         ul.append(li)
     title = "Pulsar HttpBin"
     html = request.html_document
     html.head.title = title
     html.head.links.append("/media/httpbin.css")
     html.head.scripts.append("//code.jquery.com/jquery.min.js")
     html.head.scripts.append("/media/httpbin.js")
     ul = ul.render(request)
     body = template() % (title, version, ul, pyversion, JAPANESE)
     html.body.append(body)
     return html.http_response(request)
示例#7
0
 def get(self, request):
     '''The home page of this router'''
     ul = Html('ul')
     for router in sorted(self.routes, key=lambda r: r.creation_count):
         a = router.link(escape(router.route.path))
         li = Html('li', a, ' %s' % router.parameters.get('title', ''))
         ul.append(li)
     title = 'Pulsar HttpBin'
     html = request.html_document
     html.head.title = title
     html.head.links.append('/media/httpbin.css')
     html.head.scripts.append('//code.jquery.com/jquery.min.js')
     html.head.scripts.append('/media/httpbin.js')
     ul = ul.render(request)
     body = template() % (title, version, ul, pyversion, JAPANESE)
     html.body.append(body)
     return html.http_response(request)
示例#8
0
 def __init__(self,
              unique=False,
              primary_key=None,
              required=None,
              index=False,
              default=None,
              validation_error=None,
              help_text=None,
              label=None,
              widget=None,
              attrs=None,
              attrname=None,
              wrong_value_message=None,
              **kwargs):
     self.name = attrname
     self.foreign_keys = ()
     self.primary_key = (self.primary_key
                         if primary_key is None else primary_key)
     self.default = default if default is not None else self.default
     self.required = required if required is not None else self.required
     index = index if index is not None else self.index
     if self.primary_key:
         self.unique = True
         self.required = True
         self.index = True
     else:
         self.unique = unique
         self.required = required
         self.index = True if unique else index
     self.validation_error = (validation_error or self.validation_error
                              or standard_validation_error)
     if wrong_value_message:
         self.wrong_value_message = wrong_value_message
     self.help_text = escape(help_text)
     self.label = label
     if widget:
         self.widget = lambda *args, **kwargs: widget(self, *args, **kwargs)
     self.attrs = dict(self.attrs or ())
     self.attrs.update(attrs or ())
     self.attrs['required'] = self.required
     if label:
         self.attrs['label'] = label
     self.handle_params(**kwargs)
     # Increase the creation counter, and save our local copy.
     self.creation_counter = Field.creation_counter
     Field.creation_counter += 1
示例#9
0
 def get(self, request):
     '''The home page of this router'''
     ul = Html('ul')
     for router in sorted(self.routes, key=lambda r: r.creation_count):
         a = router.link(escape(router.route.path))
         li = Html('li', a, ' %s' % router.parameters.get('title', ''))
         ul.append(li)
     title = 'Pulsar HttpBin'
     html = request.html_document
     html.head.title = title
     html.head.links.append('/media/httpbin.css')
     html.head.scripts.append('//code.jquery.com/jquery.min.js')
     html.head.scripts.append('/media/httpbin.js')
     ul = ul.render(request)
     body = template() % (title, version, ul, pyversion, JAPANESE)
     html.body.append(body)
     #html.meta.append()
     return html.http_response(request)
示例#10
0
def render_error_debug(request, exc, content_type):
    '''Render the traceback into the content type in *response*.'''
    response = request.response
    is_html = content_type == 'text/html'
    error = Html('div', cn='section traceback error') if is_html else []
    trace = format_traceback(exc)
    for trace in format_traceback(exc):
        counter = 0
        for line in trace.split('\n'):
            if line.startswith('  '):
                counter += 1
                line = line[2:]
            if line:
                if is_html:
                    line = Html('p', escape(line))
                    if counter:
                        line.css({'margin-left': '%spx' % (20*counter)})
                error.append(line)
    return error
示例#11
0
def render_error_debug(request, exception, is_html):
    '''Render the ``exception`` traceback
    '''
    error = Html('div', cn='well well-lg') if is_html else []
    for trace in format_traceback(exception):
        counter = 0
        for line in trace.split('\n'):
            if line.startswith('  '):
                counter += 1
                line = line[2:]
            if line:
                if is_html:
                    line = Html('p', escape(line), cn='text-danger')
                    if counter:
                        line.css({'margin-left': '%spx' % (20 * counter)})
                error.append(line)
    if is_html:
        error = Html('div', Html('h1', request.response.status), error)
    return error
示例#12
0
def render_error_debug(request, exception, is_html):
    '''Render the ``exception`` traceback
    '''
    error = Html('div', cn='well well-lg') if is_html else []
    for trace in format_traceback(exception):
        counter = 0
        for line in trace.split('\n'):
            if line.startswith('  '):
                counter += 1
                line = line[2:]
            if line:
                if is_html:
                    line = Html('p', escape(line), cn='text-danger')
                    if counter:
                        line.css({'margin-left': '%spx' % (20*counter)})
                error.append(line)
    if is_html:
        error = Html('div', Html('h1', request.response.status), error)
    return error
示例#13
0
 def __init__(self, name=None, required=None, default=None,
              validation_error=None, help_text=None,
              label=None, attrs=None, validator=None,
              required_error=None,
              **kwargs):
     self.name = name
     self.default = default if default is not None else self.default
     self.required = required if required is not None else self.required
     self.validation_error = validation_error or self.validation_error
     self.required_error = required_error or self.required_error
     self.help_text = escape(help_text)
     self.label = label
     self.validator = validator
     self.attrs = dict(self.attrs or ())
     self.attrs.update(attrs or ())
     self.attrs['required'] = self.required
     self.handle_params(**kwargs)
     # Increase the creation counter, and save our local copy.
     self.creation_counter = Field.creation_counter
     Field.creation_counter += 1
示例#14
0
def render_error_debug(request, failure):
    '''Render the traceback into the content type in *response*.'''
    response = request.response
    is_html = response.content_type == 'text/html'
    error = Html('div', cn='section traceback error') if is_html else []
    for traces in failure.exc_info[2]:
        counter = 0
        for trace in traces.split('\n'):
            if trace.startswith('  '):
                counter += 1
                trace = trace[2:]
            if trace:
                if is_html:
                    trace = Html('p', escape(trace))
                    if counter:
                        trace.css({'margin-left': '%spx' % (20 * counter)})
                error.append(trace)
    if not is_html:
        error = '\n'.join(error)
    return error
示例#15
0
def render_error_debug(request, failure):
    '''Render the traceback into the content type in *response*.'''
    response = request.response
    is_html = response.content_type == 'text/html'
    error = Html('div', cn='section traceback error') if is_html else []
    for traces in failure.exc_info[2]:
        counter = 0
        for trace in traces.split('\n'):
            if trace.startswith('  '):
                counter += 1
                trace = trace[2:]
            if trace:
                if is_html:
                    trace = Html('p', escape(trace))
                    if counter:
                        trace.css({'margin-left': '%spx' % (20*counter)})
                error.append(trace)
    if not is_html:
        error = '\n'.join(error)
    return error
示例#16
0
文件: manage.py 项目: kwsy/pulsar
 def get(self, request):
     '''The home page of this router'''
     ul = Html('ul')
     for router in sorted(self.routes, key=lambda r: r.creation_count):
         a = router.link(escape(router.route.path))
         a.addClass(router.name)
         for method in METHODS:
             if router.getparam(method):
                 a.addClass(method)
         li = Html('li', a, ' %s' % router.getparam('title', ''))
         ul.append(li)
     title = 'Pulsar'
     html = request.html_document
     html.head.title = title
     html.head.links.append('httpbin.css')
     html.head.links.append('favicon.ico', rel="icon", type='image/x-icon')
     html.head.scripts.append('httpbin.js')
     ul = ul.render(request)
     templ, _, _ = asset('template.html')
     body = templ % (title, JAPANESE, CHINESE, version, pyversion, ul)
     html.body.append(body)
     return html.http_response(request)
示例#17
0
 def get(self, request):
     """The home page of this router"""
     ul = Html("ul")
     for router in sorted(self.routes, key=lambda r: r.creation_count):
         a = router.link(escape(router.route.path))
         a.addClass(router.name)
         for method in METHODS:
             if router.getparam(method):
                 a.addClass(method)
         li = Html("li", a, " %s" % router.getparam("title", ""))
         ul.append(li)
     title = "Pulsar"
     html = request.html_document
     html.head.title = title
     html.head.links.append("httpbin.css")
     html.head.links.append("favicon.ico", rel="icon", type="image/x-icon")
     html.head.scripts.append("httpbin.js")
     ul = ul.render(request)
     templ = asset("template.html")
     body = templ % (title, JAPANESE, CHINESE, version, pyversion, ul)
     html.body.append(body)
     return html.http_response(request)
示例#18
0
 def get(self, request):
     '''The home page of this router'''
     ul = Html('ul')
     for router in sorted(self.routes, key=lambda r: r.creation_count):
         a = router.link(escape(router.route.path))
         a.addClass(router.name)
         for method in METHODS:
             if router.getparam(method):
                 a.addClass(method)
         li = Html('li', a, ' %s' % router.getparam('title', ''))
         ul.append(li)
     title = 'Pulsar'
     html = request.html_document
     html.head.title = title
     html.head.links.append('httpbin.css')
     html.head.links.append('favicon.ico', rel="icon", type='image/x-icon')
     html.head.scripts.append('httpbin.js')
     ul = ul.render(request)
     templ, _, _ = asset('template.html')
     body = templ % (title, JAPANESE, CHINESE, version, pyversion, ul)
     html.body.append(body)
     return html.http_response(request)
示例#19
0
文件: fields.py 项目: tourist/lux
 def __init__(self, unique=False, primary_key=None, required=None,
              index=False, default=None,
              validation_error=None, help_text=None,
              label=None, widget=None, attrs=None,
              attrname=None, wrong_value_message=None,
              **kwargs):
     self.name = attrname
     self.foreign_keys = ()
     self.primary_key = (self.primary_key if primary_key is None else
                         primary_key)
     self.default = default if default is not None else self.default
     self.required = required if required is not None else self.required
     index = index if index is not None else self.index
     if self.primary_key:
         self.unique = True
         self.required = True
         self.index = True
     else:
         self.unique = unique
         self.required = required
         self.index = True if unique else index
     self.validation_error = (validation_error or self.validation_error or
                              standard_validation_error)
     if wrong_value_message:
         self.wrong_value_message = wrong_value_message
     self.help_text = escape(help_text)
     self.label = label
     if widget:
         self.widget = lambda *args, **kwargs: widget(self, *args, **kwargs)
     self.attrs = dict(self.attrs or ())
     self.attrs.update(attrs or ())
     self.attrs['required'] = self.required
     if label:
         self.attrs['label'] = label
     self.handle_params(**kwargs)
     # Increase the creation counter, and save our local copy.
     self.creation_counter = Field.creation_counter
     Field.creation_counter += 1
示例#20
0
def render_error(request, exc):
    '''Default renderer for errors.'''
    cfg = request.get('pulsar.cfg')
    debug = cfg.debug if cfg else False
    response = request.response
    if not response.content_type:
        content_type = request.get('default.content_type')
        response.content_type = request.content_types.best_match(
            content_type or DEFAULT_RESPONSE_CONTENT_TYPES)
    content_type = None
    if response.content_type:
        content_type = response.content_type.split(';')[0]
    is_html = content_type == 'text/html'

    if debug:
        msg = render_error_debug(request, exc, is_html)
    else:
        msg = escape(error_messages.get(response.status_code) or exc)
        if is_html:
            msg = textwrap.dedent("""
                <h1>{0[reason]}</h1>
                {0[msg]}
                <h3>{0[version]}</h3>
            """).format({
                "reason": response.status,
                "msg": msg,
                "version": request.environ['SERVER_SOFTWARE']
            })
    #
    if content_type == 'text/html':
        doc = HtmlDocument(title=response.status)
        doc.head.embedded_css.append(error_css)
        doc.body.append(Html('div', msg, cn='pulsar-error'))
        return doc.render(request)
    elif content_type in JSON_CONTENT_TYPES:
        return json.dumps({'status': response.status_code, 'message': msg})
    else:
        return '\n'.join(msg) if isinstance(msg, (list, tuple)) else msg
示例#21
0
文件: utils.py 项目: quantmind/pulsar
def render_error(request, exc):
    '''Default renderer for errors.'''
    cfg = request.get('pulsar.cfg')
    debug = cfg.debug if cfg else False
    response = request.response
    if not response.content_type:
        content_type = request.get('default.content_type')
        response.content_type = request.content_types.best_match(
            as_tuple(content_type or DEFAULT_RESPONSE_CONTENT_TYPES)
        )
    content_type = None
    if response.content_type:
        content_type = response.content_type.split(';')[0]
    is_html = content_type == 'text/html'

    if debug:
        msg = render_error_debug(request, exc, is_html)
    else:
        msg = escape(error_messages.get(response.status_code) or exc)
        if is_html:
            msg = textwrap.dedent("""
                <h1>{0[reason]}</h1>
                {0[msg]}
                <h3>{0[version]}</h3>
            """).format({"reason": response.status, "msg": msg,
                         "version": request.environ['SERVER_SOFTWARE']})
    #
    if content_type == 'text/html':
        doc = HtmlDocument(title=response.status)
        doc.head.embedded_css.append(error_css)
        doc.body.append(Html('div', msg, cn='pulsar-error'))
        return doc.to_bytes(request)
    elif content_type in JSON_CONTENT_TYPES:
        return json.dumps({'status': response.status_code,
                           'message': msg})
    else:
        return '\n'.join(msg) if isinstance(msg, (list, tuple)) else msg
示例#22
0
文件: fields.py 项目: SirZazu/lux
 def __init__(self, name=None, required=None, default=None,
              validation_error=None, help_text=None,
              label=None, widget=None, attrs=None,
              wrong_value_message=None, **kwargs):
     self.name = name
     self.default = default if default is not None else self.default
     self.required = required if required is not None else self.required
     self.validation_error = (validation_error or self.validation_error or
                              standard_validation_error)
     if wrong_value_message:
         self.wrong_value_message = wrong_value_message
     self.help_text = escape(help_text)
     self.label = label
     if widget:
         self.widget = lambda *args, **kwargs: widget(self, *args, **kwargs)
     self.attrs = dict(self.attrs or ())
     self.attrs.update(attrs or ())
     self.attrs['required'] = self.required
     if label:
         self.attrs['label'] = label
     self.handle_params(**kwargs)
     # Increase the creation counter, and save our local copy.
     self.creation_counter = Field.creation_counter
     Field.creation_counter += 1