Ejemplo n.º 1
0
 def close_process(self, process):
     if process.poll() is None:
         log.debug('kill process ' + str(process.pid))
         if platform == 'win32':
             sub.call(['taskkill', '/F', '/T', '/PID', str(process.pid)])
         else:
             process.kill()
Ejemplo n.º 2
0
def get_object(model, tablename, id):
    """
    Get cached object from redis
    
    if id is None then return None:
    """
    from uliweb.utils.common import log
    
    if not id:
        return 
    
    if not check_enable():
        return
    
    redis = get_redis()
    if not redis: return
    _id = get_id(model.get_engine_name(), tablename, id)
    try:
        if redis.exists(_id):
            v = redis.hgetall(_id)
            o = model.load(v)
            log.debug("objcache:get:id="+_id)
            return o
    except Exception, e:
        log.exception(e)
Ejemplo n.º 3
0
 def _find_tree_permissions(self, perms, page=None, user=None):
     if user and user.is_superuser:
         result = dict([(x, True) for x in perms])
         return result
     
     result, _perms = self._find_permissions(perms, page.acl, user, check_default=False)
     while _perms:
         parent = page.get_parent()
         if parent:
             _result, _perms = self._find_permissions(_perms, parent.acl, user, check_default=False, pagename=page.name)
             result.update(_result)
             page = parent
             if not _perms:
                 break
         else:
             _result, _perms = self._find_permissions(_perms, None, user)
             result.update(_result)
             break
         
     if user:
         username = user.username
     else:
         username = '******'
     log.debug("find_tree_permissions: perms=%r, user=%s, result=%r, page=%s", perms, username, result, page.name)
     return result
Ejemplo n.º 4
0
def xmlrpc():
    import uliweb.contrib.xmlrpc as rpc
    import xmlrpclib
    from werkzeug import Response
    from uliweb.utils.common import log
    from uliweb import application as app, response

    p, m = xmlrpclib.loads(request.data)
    try:
        f = rpc.__xmlrpc_functions__.get(m)
        if f:
            mod, handler_cls, handler = app.prepare_request(request, f)
            result = app.call_view(mod,
                                   handler_cls,
                                   handler,
                                   request,
                                   response,
                                   _wrap_result,
                                   args=p)
            xml = xmlrpclib.dumps((result, ), methodresponse=1)
        else:
            xml = xmlrpclib.dumps(xmlrpclib.Fault(
                -32400, 'system error: Cannot find or call %s' % m),
                                  methodresponse=1)
            log.debug('xmlrpc error: Cannot find or call %s' % m)
    except Exception as e:
        xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % e),
                              methodresponse=1)
        log.exception('xmlrpc error')
    response = Response(xml, content_type='text/xml; charset=utf-8')
    return response
Ejemplo n.º 5
0
 def f():
     #check if the record has changed
     flag = created
     if not flag:
         flag = bool(filter(lambda x:x in data, fields))
     if flag:
         set_object(model, tablename, instance)
         log.debug("objcache:post_save:id=%d" % instance.id)
Ejemplo n.º 6
0
 def close_process(self, p):
     if p.process and p.process.poll() is None:
         log.debug('kill process ' + str(p.process.pid))
         if platform == 'win32':
             sub.call(['taskkill', '/F', '/T', '/PID', str(p.process.pid)])
         else:
             p.process.kill()
     p.status = 1
Ejemplo n.º 7
0
 def f():
     _id = get_id(model.get_engine_name(), tablename, instance.id)
     redis = get_redis()
     if not redis: return
     
     try:
         redis.delete(_id)
         log.debug("objcache:post_delete:id="+_id)
     except Exception, e:
         log.exception(e)
Ejemplo n.º 8
0
def post_delete(sender, instance):
    from uliweb.utils.common import log

    tablename = sender.tablename

    _key = get_key(tablename)
    if _key:
        _id = get_id(tablename, sender.c[_key] == instance.id)
        log.debug("post_delete:id=" + _id)
        cache = get_cache()
        ret = cache.delete(_id)
        return ret
Ejemplo n.º 9
0
def post_delete(sender, instance):
    from uliweb.utils.common import log

    tablename = sender.tablename
    
    _key = get_key(tablename)
    if _key:
        _id = get_id(tablename, sender.c[_key]==instance.id)
        log.debug("post_delete:id=" + _id)
        cache = get_cache()
        ret = cache.delete(_id)
        return ret
Ejemplo n.º 10
0
 def _check_permission(self, perm, acl=None, user=None, page=None,
     err_msg=_("You have no right to access the page."), raise_exception=True):
     from uliweb.orm import Model
     
     if not user:
         user = request.user
     
     #superuser can do everything
     if user and user.is_superuser:
         return True
     
     if not user:
         err_msg = _("You are not logged in. ") + err_msg
         
     if isinstance(acl, Model):
         acl = acl.acl
     else:
         acl = acl
         
     if page:
         pagename = page.name
     else:
         pagename = ''
         
     result, _perms = self._find_permissions(perm, acl, user, check_default=False, pagename=pagename)
     while _perms:
         if page:
             parent = page.get_parent()
             if parent:
                 _result, _perms = self._find_permissions(_perms, parent.acl, user, check_default=False, pagename=page.name)
                 result.update(_result)
                 page = parent
                 if not _perms:
                     break
             else:
                 _result, _perms = self._find_permissions(_perms, None, user)
                 result.update(_result)
                 break
         else:
             _result, _perms = self._find_permissions(_perms, None, user)
             result.update(_result)
             break
     if user:
         log.debug('check permission perm=%s, user=%s, result=%r', perm, user.username, result)
     else:
         log.debug('check permission perm=%s, result=%r', perm, result)
     if not result.get(perm, False):
         if raise_exception:
             error(err_msg)
         else:
             return False
     return True
Ejemplo n.º 11
0
def post_save(sender, instance, created, data, old_data):
    from uliweb.utils.common import log
    from uliweb import settings

    tablename = sender.tablename

    _key = get_key(tablename)
    if _key:
        _id = get_id(tablename, sender.c[_key] == instance.id)
        log.debug("post_save:id=" + _id)
        cache = get_cache()
        v = repr(instance.to_dict())
        log.debug("post_save:value=" + v)
        ret = cache.set(_id, v, settings.get_var('OBJCACHE/expiry_time'))
        return ret
Ejemplo n.º 12
0
def post_save(sender, instance, created, data, old_data):
    from uliweb.utils.common import log
    from uliweb import settings

    tablename = sender.tablename
    
    _key = get_key(tablename)
    if _key:
        _id = get_id(tablename, sender.c[_key]==instance.id)
        log.debug("post_save:id=" + _id)
        cache = get_cache()
        v = repr(instance.to_dict())
        log.debug("post_save:value="+v)
        ret = cache.set(_id, v, settings.get_var('OBJCACHE/expiry_time'))
        return ret
Ejemplo n.º 13
0
def set_object(sender, condition, instance):
    """
    Only support simple condition, for example: Model.c.id == n
    """
    from sqlalchemy.sql.expression import _BinaryExpression
    from uliweb import settings
    from uliweb.utils.common import log
    
    tablename = sender.tablename
    
    _key = get_key(tablename)
    if _key and (isinstance(condition, _BinaryExpression) and (condition.left.name == _key)):
        v = repr(instance.to_dict())
        cache = get_cache()
        _id = get_id(tablename, condition)
        log.debug("set:id=" + _id)
        ret = cache.set(_id, v, settings.get_var('OBJCACHE/expiry_time'))
        log.debug("set:value="+v)
        return ret
Ejemplo n.º 14
0
def set_object(sender, condition, instance):
    """
    Only support simple condition, for example: Model.c.id == n
    """
    from sqlalchemy.sql.expression import _BinaryExpression
    from uliweb import settings
    from uliweb.utils.common import log

    tablename = sender.tablename

    _key = get_key(tablename)
    if _key and (isinstance(condition, _BinaryExpression) and
                 (condition.left.name == _key)):
        v = repr(instance.to_dict())
        cache = get_cache()
        _id = get_id(tablename, condition)
        log.debug("set:id=" + _id)
        ret = cache.set(_id, v, settings.get_var('OBJCACHE/expiry_time'))
        log.debug("set:value=" + v)
        return ret
Ejemplo n.º 15
0
def xmlrpc():
    import uliweb.contrib.xmlrpc as rpc
    import xmlrpclib
    from werkzeug import Response
    from uliweb.utils.common import log
    from uliweb import application as app, response
    
    p, m = xmlrpclib.loads(request.data)
    try:
        f = rpc.__xmlrpc_functions__.get(m)
        if f:
            mod, handler_cls, handler = app.prepare_request(request, f)
            result = app.call_view(mod, handler_cls, handler, request, response, _wrap_result, args=p)
            xml = xmlrpclib.dumps((result,), methodresponse=1)
        else:
            xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: Cannot find or call %s' % m), methodresponse=1)
            log.debug('xmlrpc error: Cannot find or call %s' % m)
    except Exception, e:
        xml = xmlrpclib.dumps(xmlrpclib.Fault(-32400, 'system error: %s' % e), methodresponse=1)
        log.exception('xmlrpc error')
Ejemplo n.º 16
0
def set_object(model, tablename, instance, fields=None):
    """
    Only support simple condition, for example: Model.c.id == n
    """
    from uliweb import settings
    from uliweb.utils.common import log
    
    if not check_enable():
        return
    
    if not fields:
        fields = get_fields(tablename)
    if fields:
        redis = get_redis()
        if not redis: return
        
        v = instance.dump(fields)
        _id = get_id(model.get_engine_name(), tablename, instance.id)
        try:
            pipe = redis.pipeline()
            r = pipe.delete(_id).hmset(_id, v).expire(_id, settings.get_var('OBJCACHE/timeout')).execute()
            log.debug("objcache:set:id="+_id)
        except Exception, e:
            log.exception(e)
Ejemplo n.º 17
0
def get_object(sender, condition):
    """
    Only support simple condition, for example: Model.c.id == n
    """
    from sqlalchemy.sql.expression import _BinaryExpression
    from uliweb.utils.common import log
    
    tablename = sender.tablename
    log.debug("get:begin")
    
    _key = get_key(tablename)
    log.debug("get:_key="+_key)
    if _key and (isinstance(condition, _BinaryExpression) and
        (condition.left.name == _key)):
        cache = get_cache()
        _id = get_id(tablename, condition)
        log.debug("get:id=" + _id)
        v = cache.get(_id, None)
        log.debug("get:value="+str(v))
        if v:
            d = eval(v)
            o = sender(**d)
            o.set_saved()
            return o
Ejemplo n.º 18
0
def get_object(sender, condition):
    """
    Only support simple condition, for example: Model.c.id == n
    """
    from sqlalchemy.sql.expression import _BinaryExpression
    from uliweb.utils.common import log

    tablename = sender.tablename
    log.debug("get:begin")

    _key = get_key(tablename)
    log.debug("get:_key=" + _key)
    if _key and (isinstance(condition, _BinaryExpression) and
                 (condition.left.name == _key)):
        cache = get_cache()
        _id = get_id(tablename, condition)
        log.debug("get:id=" + _id)
        v = cache.get(_id, None)
        log.debug("get:value=" + str(v))
        if v:
            d = eval(v)
            o = sender(**d)
            o.set_saved()
            return o
Ejemplo n.º 19
0
 def _find_permissions(self, perms, acl=None, user=None, check_default=True, pagename=''):
     """
     Check permission of one page, or just get default acl
     
     perms can be single value or a list value
     """
     from uliweb import request, settings
     from uliweb.utils.common import import_attr
     
     if check_default:
         _acl = self._get_default_acl
     else:
         _acl = []
     if isinstance(acl, (str, unicode)):
         page_acl, acl_lines, i = find_acl(acl, settings.WIKI_ACL_ALIAS)
     elif isinstance(acl, dict):
         page_acl = acl
     else:
         page_acl = []
     
     if not user:
         user = request.user
         
     if isinstance(perms, (tuple, list)):
         p = set(perms)
     elif isinstance(perms, set):
         p = perms.copy()
     else:
         p = set([perms])
         
     #perms result
     result = {}
     for d in page_acl + _acl:
         find = False
         if not p:
             break
         if d['type'] == 'user':
             if d['name'] == 'All':
                 find = True
             else:
                 if user and user.username == d['name']:
                     find = True
         elif d['type'] == 'role':
             if functions.has_role(user, d['name']):
                 find = True
         #add other type extention
         else:
             if d['type'] in settings.get_var('WIKI/WIKI_ACL_TYPES', {}):
                 func = import_attr(settings.get_var('WIKI/WIKI_ACL_TYPES')[d['type']])
                 if func(user, d['name']):
                     find = True
                 
         if find:
             f_perms = p.intersection(d['perms'])
             if f_perms:
                 p.difference_update(f_perms)
                 for x in f_perms:
                     #if mode is not '-', then think it's enable
                     #otherwise disable
                     result[x] = d['mode'] != '-'
             if p and d['mode'] in ('+', '-'):
                 continue
             else:
                 #set not found perm to False
                 for x in p:
                     result[x] = False
                 p = set()
                 break
     if user:
         username = user.username
     else:
         username = '******'
     log.debug("ACL check: perms=%r, user=%s, result=%r, rest=%r, pagename=%s, acl=%s", perms, username, result, p, pagename, acl)
     return result, p
Ejemplo n.º 20
0
        fields = get_fields(tablename)
    if fields:
        redis = get_redis()
        if not redis: return
        
        v = instance.dump(fields)
        _id = get_id(model.get_engine_name(), tablename, instance.id)
        try:
            pipe = redis.pipeline()
            r = pipe.delete(_id).hmset(_id, v).expire(_id, settings.get_var('OBJCACHE/timeout')).execute()
            log.debug("objcache:set:id="+_id)
        except Exception, e:
            log.exception(e)
        
    else:
        log.debug("There is no fields defined or not configured, so it'll not saved in cache, [%s:%d]" % (tablename, instance.id))
        
def post_save(model, instance, created, data, old_data):
    from uliweb.utils.common import log
    from uliweb import response

    if not check_enable():
        return
    
    tablename = model.tablename
    
    fields = get_fields(tablename)
    if fields:
        #if response is False, then it means you may in batch program
        #so it can't use post_commit machenism
        def f():