示例#1
0
def parse_path_info(path_info, av=False):
    """
    Parses path info formatted like a/c/f where c and f are optional
    and a leading `/` is accepted.
    Return tuple (a, c, f). If invalid path_info a is set to None.
    If c or f are omitted they are set to None.
    If av=True, parse args and vars
    """
    if av:
        vars = None
        if '?' in path_info:
            path_info, query = path_info.split('?', 2)
            vars = Storage()
            for var in query.split('&'):
                (var, val) = var.split('=', 2) if '=' in var else (var, None)
                vars[var] = val
        items = List(path_info.split('/'))
        args = List(items[3:]) if len(items) > 3 else None
        return (items(0), items(1), items(2), args, vars)

    mo = re.match(r'^/?(?P<a>\w+)(/(?P<c>\w+)(/(?P<f>\w+))?)?$', path_info)
    if mo:
        return (mo.group('a'), mo.group('c'), mo.group('f'))
    else:
        return (None, None, None)
示例#2
0
 def test_insert_submit(self):
     request = self.env['request']
     request.args = List(['db', 'auth_user'])
     form = self.run_function()['form']
     hidden_fields = form.hidden_fields()
     data = {}
     data['_formkey'] = hidden_fields.element('input',
                                              _name='_formkey')['_value']
     data['_formname'] = hidden_fields.element('input',
                                               _name='_formname')['_value']
     data['first_name'] = 'Lisa'
     data['last_name'] = 'Simpson'
     data['username'] = '******'
     data['password'] = '******'
     data['email'] = '*****@*****.**'
     request._vars = data
     result = self.run_function()
     self.env.update(result)
     try:
         self.run_view()
     except Exception as e:
         print(e.message)
         self.fail('Could not make the view')
     db = self.env['db']
     lisa_record = db(
         db.auth_user.username == 'lisasimpson').select().first()
     self.assertIsNotNone(lisa_record)
     del data['_formkey']
     del data['_formname']
     del data['password']
     for key in data:
         self.assertEqual(data[key], lisa_record[key])
示例#3
0
def rest():
    """
        Vanilla RESTful CRUD controller
    """

    # Restore controller/function
    c, f = request.args[:2]
    request.controller, request.function = c, f

    # Restore arguments List
    from gluon.storage import List
    request.args = List(request.args[2:])

    # Check module is enabled
    if not settings.has_module(c):
        raise HTTP(404, body="Module disabled: %s" % c)

    # Lookup prefix/name for REST
    rest_controllers = settings.get_base_rest_controllers()
    resource = rest_controllers.get((c, f))
    if isinstance(resource, tuple) and len(resource) == 2:
        prefix, name = resource
    else:
        prefix, name = c, f

    # Run REST controller
    return s3_rest_controller(prefix, name)
示例#4
0
def package_config():
    rargs = List(request.raw_args.split('/'))
    execution = rargs(2) if rargs(1) == 'execution' else -1
    res = read_and_exec('package-details-execution-values.sql',
                        placeholders=(execution, ),
                        as_dict=True,
                        time_expire=180)
    return json(res)
示例#5
0
 def test_insert(self):
     request = self.env['request']
     request.args = List(['db', 'auth_user'])
     result = self.run_function()
     self.assertTrue('table' in result)
     self.assertTrue('form' in result)
     self.assertTrue(str(result['table']) is 'auth_user')
     self.env.update(result)
     try:
         self.run_view()
     except Exception as e:
         print(e.message)
         self.fail('Could not make the view')
 def test_listcall(self):
     a = List((1, 2, 3))
     self.assertEqual(a(1), 2)
     self.assertEqual(a(-1), 3)
     self.assertEqual(a(-5), None)
     self.assertEqual(a(-5, default='x'), 'x')
     self.assertEqual(a(-3, cast=str), '1')
     a.append('1234')
     self.assertEqual(a(3), '1234')
     self.assertEqual(a(3, cast=int), 1234)
     a.append('x')
     self.assertRaises(HTTP, a, 4, cast=int)
     b = List()
     # default is always returned when especified
     self.assertEqual(b(0, cast=int, default=None), None)
     self.assertEqual(b(0, cast=int, default=None, otherwise='teste'), None)
     self.assertEqual(b(0, cast=int, default='a', otherwise='teste'), 'a')
     # if don't have value and otherwise is especified it will called
     self.assertEqual(b(0, otherwise=lambda: 'something'), 'something')
     self.assertEqual(b(0, cast=int, otherwise=lambda: 'something'),
                      'something')
     # except if default is especified
     self.assertEqual(b(0, default=0, otherwise=lambda: 'something'), 0)
示例#7
0
 def test_select(self):
     request = self.env['request']
     request.args = List(['db'])
     request.env.query_string = 'query=db.auth_user.id>0'
     result = self.run_function()
     self.assertTrue('table' in result and 'query' in result)
     self.assertTrue(result['table'] == 'auth_user')
     self.assertTrue(result['query'] == 'db.auth_user.id>0')
     self.env.update(result)
     try:
         self.run_view()
     except Exception as e:
         print(e.message)
         self.fail('Could not make the view')
示例#8
0
 def test_insert(self):
     request = self.env['request']
     request.args = List(['db', 'auth_user'])
     result = self.run_function()
     self.assertTrue('table' in result)
     self.assertTrue('form' in result)
     self.assertTrue(str(result['table']) == 'auth_user')
     self.env.update(result)
     try:
         self.run_view()
     except Exception as e:
         import traceback
         print(traceback.format_exc())
         self.fail('Could not make the view')
 def test_update_submit(self):
     request = self.env['request']
     request.args = List(['db', 'auth_user', '1'])
     form = self.run_function()['form']
     hidden_fields = form.hidden_fields()
     data = {}
     data['_formkey'] = hidden_fields.element('input', _name='_formkey')['_value']
     data['_formname'] = hidden_fields.element('input', _name='_formname')['_value']
     for element in form.elements('input'):
         data[element['_name']] = element['_value']
     data['email'] = '*****@*****.**'
     data['id'] = '1'
     request._vars = data
     self.assertRaises(HTTP, self.run_function)
示例#10
0
def package_info():
    rargs = List(request.raw_args.split('/'))
    execution = rargs(2) if rargs(1) == 'execution' else 0
    res = read_and_exec('package-info.sql',
                        placeholders=(execution, ),
                        as_dict=True,
                        time_expire=180)
    res = massage_resultset(res)
    rtn = {}
    res = res[0]
    rtn['folder'] = res.folder_name
    rtn['project'] = res.project_name
    rtn['package_name'] = res.package_name
    rtn['execution'] = execution
    return json(rtn)
示例#11
0
def package_history():
    rargs = List(request.raw_args.split('/'))
    folder = rargs(2) if rargs(1) == 'folder' else 'all'
    project = rargs(4) if rargs(3) == 'project' else 'all'
    package = rargs(6) if rargs(5) == 'package' else 'all'

    folder_pattern = folder != 'all' and fixup_like_param(folder) or '%'
    project_name = project != 'all' and fixup_like_param(project) or '%'
    package_pattern = package != 'all' and fixup_like_param(package) or '%'
    res = read_and_exec('package-history.sql',
                        placeholders=(folder_pattern, project_name,
                                      package_pattern),
                        as_dict=True,
                        time_expire=60)

    return json(res)
示例#12
0
def engine_kpi():

    rargs = List(request.raw_args.split('/'))

    folder = rargs(2) if rargs(1) == 'folder' else 'all'
    project = rargs(4) if rargs(3) == 'project' else 'all'
    package = rargs(6) if rargs(5) == 'package' else 'all'
    folder_pattern = folder != 'all' and fixup_like_param(folder) or '%'
    project_name = project != 'all' and fixup_like_param(project) or '%'
    package_pattern = package != 'all' and fixup_like_param(package) or '%'

    res = read_and_exec('engine-kpi.sql',
                        placeholders=(HOURSPAN, folder_pattern, project_name,
                                      package_pattern),
                        as_dict=True,
                        time_expire=60)
    rtn = {}
    for row in massage_resultset(res):
        rtn[STATUS_CODES[row.status_code]] = row.status_count
    return json(rtn)
示例#13
0
def package_kpi():
    rargs = List(request.raw_args.split('/'))
    folder = rargs(2) if rargs(1) == 'folder' else 'all'
    project = rargs(4) if rargs(3) == 'project' else 'all'
    package = rargs(6) if rargs(5) == 'package' else 'all'
    execution = rargs(8) if rargs(7) == 'execution' else -1
    folder_pattern = folder != 'all' and fixup_like_param(folder) or '%'
    project_name = project != 'all' and fixup_like_param(project) or '%'
    package_pattern = package != 'all' and fixup_like_param(package) or '%'
    execution = execution != 'all' and execution or -1
    time_expire = 60
    if rargs(2) == 'details':
        execution = rargs(1)
        time_expire = 180
    res = read_and_exec('package-kpi.sql',
                        placeholders=(HOURSPAN, folder_pattern, project_name,
                                      package_pattern, execution),
                        as_dict=True,
                        time_expire=time_expire)
    return json(res[0])
示例#14
0
 def __init__(self, env):
     Storage.__init__(self)
     self.env = Storage(env)
     self.env.web2py_path = global_settings.applications_parent
     self.cookies = Cookie.SimpleCookie()
     self._get_vars = None
     self._post_vars = None
     self._vars = None
     self._body = None
     self.folder = None
     self.application = None
     self.function = None
     self.args = List()
     self.extension = 'html'
     self.now = datetime.datetime.now()
     self.utcnow = datetime.datetime.utcnow()
     self.is_restful = False
     self.is_https = False
     self.is_local = False
     self._uuid = None
示例#15
0
def package_list():
    rargs = List(request.raw_args.split('/'))
    folder = rargs(2) if rargs(1) == 'folder' else 'all'
    project = rargs(4) if rargs(3) == 'project' else 'all'
    status = rargs(6) if rargs(5) == 'status' else 'all'
    package = rargs(8) if rargs(7) == 'package' else 'all'
    folder_pattern = folder != 'all' and fixup_like_param(folder) or '%'
    project_name = project != 'all' and fixup_like_param(project) or '%'
    package_pattern = package != 'all' and fixup_like_param(package) or '%'
    status_ = status != 'all' and status or 'all'
    status = 0
    for k, v in STATUS_CODES.iteritems():
        if status_ == v:
            status = k
            break
    res = read_and_exec('package-list.sql',
                        placeholders=(HOURSPAN, folder_pattern, project_name,
                                      package_pattern, status),
                        as_dict=True,
                        time_expire=60)
    return json(res)
示例#16
0
 def args(self):
     return List(self.items[3:])
示例#17
0
def LOAD(c=None,
         f='index',
         args=None,
         vars=None,
         extension=None,
         target=None,
         ajax=False,
         ajax_trap=False,
         url=None,
         user_signature=False,
         timeout=None,
         times=1,
         content='loading...',
         **attr):
    """  LOAD a component into the action's document

    Timing options:
    -times: An integer or string ("infinity"/"continuous")
    specifies how many times the component is requested
    -timeout (milliseconds): specifies the time to wait before
    starting the request or the frequency if times is greater than
    1 or "infinity".
    Timing options default to the normal behavior. The component
    is added on page loading without delay.
    """
    from html import TAG, DIV, URL, SCRIPT, XML
    if args is None:
        args = []
    vars = Storage(vars or {})
    target = target or 'c' + str(random.random())[2:]
    attr['_id'] = target
    request = current.request
    if '.' in f:
        f, extension = f.rsplit('.', 1)
    if url or ajax:
        url = url or URL(request.application,
                         c,
                         f,
                         r=request,
                         args=args,
                         vars=vars,
                         extension=extension,
                         user_signature=user_signature)
        # timing options
        if isinstance(times, basestring):
            if times.upper() in ("INFINITY", "CONTINUOUS"):
                times = "Infinity"
            else:
                raise TypeError("Unsupported times argument %s" % times)
        elif isinstance(times, int):
            if times <= 0:
                raise ValueError(
                    "Times argument must be greater than zero, 'Infinity' or None"
                )
        else:
            raise TypeError("Unsupported times argument type %s" % type(times))
        if timeout is not None:
            if not isinstance(timeout, (int, long)):
                raise ValueError("Timeout argument must be an integer or None")
            elif timeout <= 0:
                raise ValueError(
                    "Timeout argument must be greater than zero or None")
            statement = "$.web2py.component('%s','%s', %s, %s);" \
                % (url, target, timeout, times)
            attr['_data-w2p_timeout'] = timeout
            attr['_data-w2p_times'] = times
        else:
            statement = "$.web2py.component('%s','%s');" % (url, target)
        attr['_data-w2p_remote'] = url
        if not target is None:
            return DIV(content, **attr)

    else:
        if not isinstance(args, (list, tuple)):
            args = [args]
        c = c or request.controller
        other_request = Storage(request)
        other_request['env'] = Storage(request.env)
        other_request.controller = c
        other_request.function = f
        other_request.extension = extension or request.extension
        other_request.args = List(args)
        other_request.vars = vars
        other_request.get_vars = vars
        other_request.post_vars = Storage()
        other_response = Response()
        other_request.env.path_info = '/' + \
            '/'.join([request.application, c, f] +
                     map(str, other_request.args))
        other_request.env.query_string = \
            vars and URL(vars=vars).split('?')[1] or ''
        other_request.env.http_web2py_component_location = \
            request.env.path_info
        other_request.cid = target
        other_request.env.http_web2py_component_element = target
        other_response.view = '%s/%s.%s' % (c, f, other_request.extension)

        other_environment = copy.copy(current.globalenv)  # NASTY

        other_response._view_environment = other_environment
        other_response.generic_patterns = \
            copy.copy(current.response.generic_patterns)
        other_environment['request'] = other_request
        other_environment['response'] = other_response

        ## some magic here because current are thread-locals

        original_request, current.request = current.request, other_request
        original_response, current.response = current.response, other_response
        page = run_controller_in(c, f, other_environment)
        if isinstance(page, dict):
            other_response._vars = page
            other_response._view_environment.update(page)
            run_view_in(other_response._view_environment)
            page = other_response.body.getvalue()
        current.request, current.response = original_request, original_response
        js = None
        if ajax_trap:
            link = URL(request.application,
                       c,
                       f,
                       r=request,
                       args=args,
                       vars=vars,
                       extension=extension,
                       user_signature=user_signature)
            js = "$.web2py.trap_form('%s','%s');" % (link, target)
        script = js and SCRIPT(js, _type="text/javascript") or ''
        return TAG[''](DIV(XML(page), **attr), script)
 def test_listgetitem(self):
     '''Mantains list behaviour.'''
     a = List((1, 2, 3))
     self.assertEqual(a[0], 1)
     self.assertEqual(a[::-1], [3, 2, 1])
示例#19
0
def LOAD(c=None,
         f='index',
         args=None,
         vars=None,
         extension=None,
         target=None,
         ajax=False,
         ajax_trap=False,
         url=None,
         user_signature=False,
         timeout=None,
         times=1,
         content='loading...',
         post_vars=Storage(),
         **attr):
    """  LOADs a component into the action's document

    Args:
        c(str): controller
        f(str): function
        args(tuple or list): arguments
        vars(dict): vars
        extension(str): extension
        target(str): id of the target
        ajax(bool): True to enable AJAX behaviour
        ajax_trap(bool): True if `ajax` is set to `True`, traps
            both links and forms "inside" the target
        url(str): overrides `c`, `f`, `args` and `vars`
        user_signature(bool): adds hmac signature to all links
            with a key that is different for every user
        timeout(int): in milliseconds, specifies the time to wait before
            starting the request or the frequency if times is greater than
            1 or "infinity"
        times(integer or str): how many times the component will be requested
            "infinity" or "continuous" are accepted to reload indefinitely the
            component
    """
    if args is None:
        args = []
    vars = Storage(vars or {})
    target = target or 'c' + str(random.random())[2:]
    attr['_id'] = target
    request = current.request
    if '.' in f:
        f, extension = f.rsplit('.', 1)
    if url or ajax:
        url = url or html.URL(request.application,
                              c,
                              f,
                              r=request,
                              args=args,
                              vars=vars,
                              extension=extension,
                              user_signature=user_signature)
        # timing options
        if isinstance(times, basestring):
            if times.upper() in ("INFINITY", "CONTINUOUS"):
                times = "Infinity"
            else:
                # FIXME: should be a ValueError
                raise TypeError("Unsupported times argument %s" % times)
        elif isinstance(times, int):
            if times <= 0:
                raise ValueError(
                    "Times argument must be greater than zero, 'Infinity' or None"
                )
        else:
            # NOTE: why do not use ValueError only?
            raise TypeError("Unsupported times argument type %s" % type(times))
        if timeout is not None:
            if not isinstance(timeout, integer_types):
                raise ValueError("Timeout argument must be an integer or None")
            elif timeout <= 0:
                raise ValueError(
                    "Timeout argument must be greater than zero or None")
            statement = "$.web2py.component('%s','%s', %s, %s);" \
                % (url, target, timeout, times)
            attr['_data-w2p_timeout'] = timeout
            attr['_data-w2p_times'] = times
        else:
            statement = "$.web2py.component('%s','%s');" % (url, target)
        attr['_data-w2p_remote'] = url
        if target is not None:
            return html.DIV(content, **attr)

    else:
        if not isinstance(args, (list, tuple)):
            args = [args]
        c = c or request.controller
        other_request = Storage(request)
        other_request['env'] = Storage(request.env)
        other_request.controller = c
        other_request.function = f
        other_request.extension = extension or request.extension
        other_request.args = List(args)
        other_request.vars = vars
        other_request.get_vars = vars
        other_request.post_vars = post_vars
        other_response = Response()
        other_request.env.path_info = '/' + \
            '/'.join([request.application, c, f] +
                     [str(a) for a in other_request.args])
        other_request.env.query_string = \
            vars and html.URL(vars=vars).split('?')[1] or ''
        other_request.env.http_web2py_component_location = \
            request.env.path_info
        other_request.cid = target
        other_request.env.http_web2py_component_element = target
        other_request.restful = types.MethodType(request.restful.__func__,
                                                 other_request)
        # A bit nasty but needed to use LOAD on action decorates with @request.restful()
        other_response.view = '%s/%s.%s' % (c, f, other_request.extension)

        other_environment = copy.copy(current.globalenv)  # FIXME: NASTY

        other_response._view_environment = other_environment
        other_response.generic_patterns = \
            copy.copy(current.response.generic_patterns)
        other_environment['request'] = other_request
        other_environment['response'] = other_response

        ## some magic here because current are thread-locals

        original_request, current.request = current.request, other_request
        original_response, current.response = current.response, other_response
        page = run_controller_in(c, f, other_environment)
        if isinstance(page, dict):
            other_response._vars = page
            other_response._view_environment.update(page)
            page = run_view_in(other_response._view_environment)

        current.request, current.response = original_request, original_response
        js = None
        if ajax_trap:
            link = html.URL(request.application,
                            c,
                            f,
                            r=request,
                            args=args,
                            vars=vars,
                            extension=extension,
                            user_signature=user_signature)
            js = "$.web2py.trap_form('%s','%s');" % (link, target)
        script = js and html.SCRIPT(js, _type="text/javascript") or ''
        return html.TAG[''](html.DIV(html.XML(page), **attr), script)
示例#20
0
    def __call__(self,
                 c=None,
                 f='index',
                 args=None,
                 vars=None,
                 extension=None,
                 target=None,
                 ajax=False,
                 ajax_trap=False,
                 url=None,
                 user_signature=False,
                 content='loading...',
                 **attr):
        if args is None:
            args = []
        vars = Storage(vars or {})
        target = target or 'c' + str(random.random())[2:]
        attr['_id'] = target
        request = current.request
        if '.' in f:
            f, extension = f.rsplit('.', 1)
        if url or ajax:
            url = url or html.URL(request.application,
                                  c,
                                  f,
                                  r=request,
                                  args=args,
                                  vars=vars,
                                  extension=extension,
                                  user_signature=user_signature)
            script = html.SCRIPT('$.web2py.component("%s","%s")' %
                                 (url, target),
                                 _type="text/javascript")
            return html.TAG[''](script, html.DIV(content, **attr))
        else:
            if not isinstance(args, (list, tuple)):
                args = [args]
            c = c or request.controller

            other_request = Storage(request)
            other_request['env'] = Storage(request.env)
            other_request.controller = c
            other_request.function = f
            other_request.extension = extension or request.extension
            other_request.args = List(args)
            other_request.vars = vars
            other_request.get_vars = vars
            other_request.post_vars = Storage()
            other_response = Response()
            other_request.env.path_info = '/' + \
                '/'.join([request.application, c, f] +
                         [str(a) for a in other_request.args])
            other_request.env.query_string = \
                vars and html.URL(vars=vars).split('?')[1] or ''
            other_request.env.http_web2py_component_location = \
                request.env.path_info
            other_request.cid = target
            other_request.env.http_web2py_component_element = target
            other_response.view = '%s/%s.%s' % (c, f, other_request.extension)
            other_environment = copy.copy(self.environment)
            other_response._view_environment = other_environment
            other_response.generic_patterns = \
                copy.copy(current.response.generic_patterns)
            other_environment['request'] = other_request
            other_environment['response'] = other_response

            ## some magic here because current are thread-locals

            original_request, current.request = current.request, other_request
            original_response, current.response = current.response, other_response
            page = run_controller_in(c, f, other_environment)
            if isinstance(page, dict):
                other_response._vars = page
                other_response._view_environment.update(page)
                page = run_view_in(other_response._view_environment)

            current.request, current.response = original_request, original_response
            js = None
            if ajax_trap:
                link = html.URL(request.application,
                                c,
                                f,
                                r=request,
                                args=args,
                                vars=vars,
                                extension=extension,
                                user_signature=user_signature)
                js = "$.web2py.trap_form('%s','%s');" % (link, target)
            script = js and html.SCRIPT(js, _type="text/javascript") or ''
            return html.TAG[''](html.DIV(html.XML(page), **attr), script)
示例#21
0
    def include_files(self, extensions=None):
        """
        Includes files (usually in the head).
        Can minify and cache local files
        By default, caches in ram for 5 minutes. To change,
        response.cache_includes = (cache_method, time_expire).
        Example: (cache.disk, 60) # caches to disk for 1 minute.
        """
        app = current.request.application

        # We start by building a files list in which adjacent files internal to
        # the application are placed in a list inside the files list.
        #
        # We will only minify and concat adjacent internal files as there's
        # no way to know if changing the order with which the files are apppended
        # will break things since the order matters in both CSS and JS and
        # internal files may be interleaved with external ones.
        files = []
        # For the adjacent list we're going to use storage List to both distinguish
        # from the regular list and so we can add attributes
        internal = List()
        internal.has_js = False
        internal.has_css = False
        done = set() # to remove duplicates
        for item in self.files:
            if not isinstance(item, list):
                if item in done:
                    continue
                done.add(item)
            if isinstance(item, (list, tuple)) or not item.startswith('/' + app): # also consider items in other web2py applications to be external
                if internal:
                    files.append(internal)
                    internal = List()
                    internal.has_js = False
                    internal.has_css = False
                files.append(item)
                continue
            if extensions and not item.rpartition('.')[2] in extensions:
                continue
            internal.append(item)
            if item.endswith('.js'):
                internal.has_js = True
            if item.endswith('.css'):
                internal.has_css = True
        if internal:
            files.append(internal)

        # We're done we can now minify
        if have_minify:
            for i, f in enumerate(files):
                if isinstance(f, List) and ((self.optimize_css and f.has_css) or (self.optimize_js and f.has_js)):
                    # cache for 5 minutes by default
                    key = hashlib_md5(repr(f)).hexdigest()
                    cache = self.cache_includes or (current.cache.ram, 60 * 5)
                    def call_minify(files=f):
                        return List(minify.minify(files,
                                             URL('static', 'temp'),
                                             current.request.folder,
                                             self.optimize_css,
                                             self.optimize_js))
                    if cache:
                        cache_model, time_expire = cache
                        files[i] = cache_model('response.files.minified/' + key,
                                            call_minify,
                                            time_expire)
                    else:
                        files[i] = call_minify()

        def static_map(s, item):
            if isinstance(item, str):
                f = item.lower().split('?')[0]
                ext = f.rpartition('.')[2]
                # if static_version we need also to check for
                # static_version_urls. In that case, the _.x.x.x
                # bit would have already been added by the URL()
                # function
                if self.static_version and not self.static_version_urls:
                    item = item.replace(
                        '/static/', '/static/_%s/' % self.static_version, 1)
                tmpl = template_mapping.get(ext)
                if tmpl:
                    s.append(tmpl % item)
            elif isinstance(item, (list, tuple)):
                f = item[0]
                tmpl = template_mapping.get(f)
                if tmpl:
                    s.append(tmpl % item[1])

        s = []
        for item in files:
            if isinstance(item, List):
                for f in item:
                    static_map(s, f)
            else:
                static_map(s, item)
        self.write(''.join(s), escape=False)
示例#22
0
def feed():
    request.args = List(request.raw_args.split('/'))
    if not URL.verify(request, hmac_key=SIGN_KEY):
        raise HTTP(403, 'invalid signature')
    rtn = recalc_token(user=request.get_vars.user)
    if request.args(0) not in rtn:
        raise HTTP(403, 'instance not valid')
    request.extension = 'rss'
    rargs = List(request.raw_args.split('/'))
    fname = "%s.rss" % hashlib.md5('/'.join(rargs)).hexdigest()
    basepath = os.path.join(request.folder, 'private', 'temp_feeds')
    if not os.path.exists(basepath):
        os.makedirs(basepath)
    fpath = os.path.join(basepath, fname)
    lock_file = fpath + '.__lock'
    return_cached = os.path.isfile(lock_file)
    if not return_cached:
        return_cached = os.path.isfile(
            fpath) and time.time() - os.path.getmtime(fpath) < CACHE_FOR
    if return_cached:
        x = 0
        while x < 60:
            x += 1
            if os.path.isfile(fpath):
                with open(fpath, 'rb') as g:
                    rtn = g.read()
                return rtn
    with open(lock_file, 'w') as g:
        g.write('%s' % request.utcnow)
    folder = rargs(2) if rargs(1) == 'folder' else 'all'
    project = rargs(4) if rargs(3) == 'project' else 'all'
    status = rargs(6) if rargs(5) == 'status' else 'all'
    package = rargs(8) if rargs(7) == 'package' else 'all'
    folder_pattern = folder != 'all' and fixup_like_param(folder) or '%'
    project_name = project != 'all' and fixup_like_param(project) or '%'
    package_pattern = package != 'all' and fixup_like_param(package) or '%'
    status_ = status != 'all' and status or 'all'
    status = 0
    for k, v in STATUS_CODES.iteritems():
        if status_ == v:
            status = k
            break
    res = read_and_exec('package-list.sql',
                        placeholders=(HOURSPAN, folder_pattern, project_name,
                                      package_pattern, status),
                        as_dict=True)
    msgs = read_and_exec('package-messages.sql',
                         placeholders=(HOURSPAN, folder_pattern, project_name,
                                       package_pattern, status),
                         as_dict=True)
    res = massage_resultset(res)
    msgs = massage_resultset(msgs)
    msgs_dict = defaultdict(list)
    for msg in msgs:
        msgs_dict[msg.operation_id].append(msg)
    title = "SSIS Dashboard: Executions for %s" % request.args(0)
    title += folder != 'all' and ', folder %s' % folder or ''
    title += project != 'all' and ', project %s' % project or ''
    title += status != 'all' and ', status %s' % STATUS_CODES[status] or ''
    title += package != 'all' and ', package %s' % package or ''
    items = []
    for entry in res:
        now = request.utcnow.strftime('%Y%m%d%H%M%S')
        link = URL('console',
                   'overview',
                   host=True,
                   scheme='https',
                   extension='',
                   args=[
                       request.args(0), 'folder', entry.folder_name, 'project',
                       entry.project_name, 'status', 'all', 'package',
                       entry.package_name, 'execution', entry.execution_id
                   ])
        if entry.elapsed_time_min is None:
            guid = link + '@' + now
        else:
            guid = link + '@now'
        detailed_status = [['Status', STATUS_CODES[entry.status]],
                           ['Elapsed (min)', entry.elapsed_time_min]]
        errors = None
        if entry.execution_id in msgs_dict:
            errors = 0
            warnings = 0
            for m in msgs_dict[entry.execution_id]:
                if m.event_name == 'OnWarning':
                    warnings += 1
                if m.event_name == 'OnError':
                    errors += 1
            detailed_status.extend([
                ['Errors', errors],
                ['Warnings', warnings],
            ])
            err_rows = msgs_dict[entry.execution_id]
            messages = [[
                TDNW('Event Name'),
                TDNW('Message Time (UTC)'),
                TDNW('Message'),
                TDNW('Package'),
                TDNW('Package Path'),
                TDNW('Subcomponent Name'),
                TDNW('Execution Path')
            ]]
            for msg in err_rows:
                messages.append([
                    TDNW(msg.event_name),
                    TDNW(msg.message_time),
                    TD(msg.message),
                    TDNW(msg.package_name),
                    TD(msg.package_path),
                    TDNW(msg.subcomponent_name),
                    TD(msg.execution_path)
                ])
            errors = TABLE([TR([a for a in msg]) for msg in messages],
                           _border=1)
        detailed_status = TABLE(
            [TR([TD(el) for el in row]) for row in detailed_status])
        if errors:
            detailed_status = detailed_status.xml() + '<hr />' + errors.xml()
        else:
            detailed_status = detailed_status.xml()
        pubdate = request.now
        if entry.end_time:
            pubdate = datetime.datetime.strptime(entry.end_time,
                                                 '%Y-%m-%d %H:%M:%S')
        items.append(
            CDATARSS2(
                title="%s - %s - (%s)" %
                (entry.execution_id, entry.package_name,
                 STATUS_CODES[entry.status]),
                link=link,
                author="%s/%s@%s" %
                (entry.folder_name, entry.project_name, request.args(0)),
                guid=guid,
                description=detailed_status,
                pubDate=pubdate,
            ))
    rss = rss2.RSS2(title=title,
                    link=URL(args=request.args, scheme='https', host=True),
                    description="Execution Packages",
                    ttl=CACHE_FOR,
                    lastBuildDate=request.utcnow,
                    items=items)
    rtn = rss.to_xml(encoding='utf-8')
    if not os.path.exists(os.path.dirname(fpath)):
        os.makedirs(os.path.dirname(fpath))
    with open(fpath, 'wb') as g:
        g.write(rtn)
    try:
        os.unlink(lock_file)
    except:
        pass

    # can we do some cleaning ?
    allfiles = glob.glob(os.path.join(basepath, '*.rss'))
    for fpath in allfiles:
        if os.path.isfile(
                fpath) and time.time() - os.path.getmtime(fpath) > PRUNING:
            try:
                os.unlink(fpath)
            except:
                pass
    # this should never happen
    allfiles = glob.glob(os.path.join(basepath, '*.__lock'))
    for fpath in allfiles:
        if os.path.isfile(
                fpath) and time.time() - os.path.getmtime(fpath) > 120:
            try:
                os.unlink(fpath)
            except:
                pass
    return rtn
示例#23
0
 def call_minify(files=f):
     return List(minify.minify(files,
                          URL('static', 'temp'),
                          current.request.folder,
                          self.optimize_css,
                          self.optimize_js))