Exemplo n.º 1
0
    def reg_usr(self, user_info):
        # 注册用户
        user = User()
        user.name = user_info['display_name']
        user.email = user_info['email']
        user.reg_time = Date.time()
        user.save()

        # 绑定角色
        user_role = UserRole()
        user_role.user = user
        user_role.role = Role.get(Role.code == 'user')
        user_role.save()

        # 绑定 oauth
        user_oauth = UserOAuth()
        user_oauth.user = user
        user_oauth.oauth_id = self.uid
        user_oauth.set_token(self.access_token)
        user_oauth.save()

        # 为用户进行登陆
        user.login(self)

        # 添加同步任务
        self.add_task(route.url_for('task.SyncPath', user.id, 0), 99)

        self.redirect(route.url_for('wiki.Index'))
Exemplo n.º 2
0
    def reg_usr(self, user_info):
        # 注册用户
        user = User()
        user.name  = user_info['display_name']
        user.email = user_info['email']
        user.reg_time = Date.time()
        user.save()

        # 绑定角色
        user_role = UserRole()
        user_role.user = user
        user_role.role = Role.get(Role.code == 'user')
        user_role.save()

        # 绑定 oauth
        user_oauth = UserOAuth()
        user_oauth.user = user
        user_oauth.oauth_id = self.uid
        user_oauth.set_token(self.access_token)
        user_oauth.save()

        # 为用户进行登陆
        user.login(self)

        # 添加同步任务
        self.add_task(
            route.url_for('task.SyncPath', user.id, 0),
            99
        )

        self.redirect(route.url_for('wiki.Index'))
Exemplo n.º 3
0
    def add(metadata, parent_id):
        """添加事务"""
        parent_id = int(parent_id)

        # 删除大于1天的旧事务
        TaskAffairs.delete()\
                   .where(
                        TaskAffairs.time <= (Date.time() - 3600 * 24) 
                    )\
                    .where(
                        TaskAffairs.metadata == metadata
                    )\
                    .execute()

        # 检验上级事务
        parent_affair = False
        if 0 != parent_id:
            parent_affair = TaskAffairs.select()\
                                       .where(TaskAffairs.id == parent_id)

            if 0 == parent_affair.count():
                parent_id = 0
            else:
                parent_affair = parent_affair.get()
                if parent_affair.metadata.id != metadata.root_id:
                    parent_id = 0

        # 创建事务
        task_affair = TaskAffairs()
        task_affair.metadata = metadata
        task_affair.time = Date.time()
        task_affair.is_dir = metadata.is_dir
        task_affair.old_hash_key = metadata.hash_key

        if parent_affair and 0 != parent_id:
            task_affair.parent = parent_id
            task_affair.parents = "%s%s," % (parent_affair.parents, parent_id)
            
        task_affair.save()
        return task_affair
Exemplo n.º 4
0
    def add(metadata, parent_id):
        """添加事务"""
        parent_id = int(parent_id)

        # 删除大于1天的旧事务
        TaskAffairs.delete()\
                   .where(
                        TaskAffairs.time <= (Date.time() - 3600 * 24)
                    )\
                    .where(
                        TaskAffairs.metadata == metadata
                    )\
                    .execute()

        # 检验上级事务
        parent_affair = False
        if 0 != parent_id:
            parent_affair = TaskAffairs.select()\
                                       .where(TaskAffairs.id == parent_id)

            if 0 == parent_affair.count():
                parent_id = 0
            else:
                parent_affair = parent_affair.get()
                if parent_affair.metadata.id != metadata.root_id:
                    parent_id = 0

        # 创建事务
        task_affair = TaskAffairs()
        task_affair.metadata = metadata
        task_affair.time = Date.time()
        task_affair.is_dir = metadata.is_dir
        task_affair.old_hash_key = metadata.hash_key

        if parent_affair and 0 != parent_id:
            task_affair.parent = parent_id
            task_affair.parents = "%s%s," % (parent_affair.parents, parent_id)

        task_affair.save()
        return task_affair
Exemplo n.º 5
0
def uninstall(plugin_name):
  
    register = import_object(plugin_name.strip() + '.register')
    
    name = register._handler.__module__ + \
           '.' + register._handler.__name__

    if models.Plugins.filter(models.Plugins.name == name).count() == 1 :
        plugin = import_object(name)()
        plugin.uninstall()

        plugin_ar = models.Plugins.get(models.Plugins.name == name)

        for v in Json.decode(plugin_ar.ui_modules):
            ui_name = str(v.split('.').pop())
            if ui_name in _application.ui_modules:
                del _application.ui_modules[ui_name] 

        # 卸载路由
        routes = []

        for v in Json.decode(plugin_ar.handlers):
            ctr = import_object(v)
            if hasattr(ctr,'_menu'):
                from xcat.web import menu
                menu.remove(getattr(ctr,'_menu'))
            routes.append(ctr._route)

        if len(routes) > 0:
            # 重新注册路由
            from xcat.web import Application
            cache.client.set(Application._sync_key,Date.time())

        models.Plugins.delete().where(models.Plugins.name==name).execute()

        reset()
Exemplo n.º 6
0
    def callback(self, response):
        if response.error:
            self.write('Init User(%s) Metadata Error : %s' %
                       (self.user.id, response.error))
            self.finish()
            return

        json = Json.decode(response.body)

        if len(json.get('path', '')) > 255:
            self.write('Path Too long.')
            self.finish()
            return

        hash_key = json.get('hash', False) or json.get('rev', '')

        if '' == hash_key:
            # 中文目录,先删除处理
            self.metadata.remove()
            self.finish()
            return

        if self.metadata:
            metadata = self.metadata
            # 没有变更
            # if hash_key == str(metadata.hash_key):
            #     self.finish()
            #     return
        else:
            # 初始化根目录
            metadata = wiki.Metadata()
            metadata.user = self.user
            metadata.path = json['path']
            metadata.bytes = json['bytes']
            metadata.is_dir = json['is_dir'] and 1 or 0

        if json.get('modified', False):
            metadata.modified = Date.str_to_time(
                json['modified'].split('+')[0], '%a, %d %b %Y %H:%M:%S ')

        metadata.hash_key = hash_key
        metadata.save()

        task_affair = wiki.TaskAffairs.add(metadata, self.affair_parent)

        # 取目录下文件 path 列表, 与新数据比对
        # 存在的 path 删除, 列表中留下的path ,
        # 就是需要删除的 文件 / 目录
        path_list = []
        for v in wiki.Metadata.select(wiki.Metadata.path)\
                              .where(wiki.Metadata.root_id == metadata.id):
            path_list.append(v.path)

        for v in json.get('contents', []):
            if len(json['path']) < 255:
                if v['path'] in path_list:
                    path_list.remove(v['path'])

                is_dir = v['is_dir'] and 1 or 0
                hash_key = v.get('rev', False) or v['hash']

                if 0 == is_dir:
                    uri, ext = os.path.splitext(v['path'])
                    # 不支持的文件类型, 跳过
                    if ext.lower() not in self.settings['support_ext']:
                        continue

                # 判断是否存在
                ar = wiki.Metadata.select()\
                                  .where(wiki.Metadata.root_id == metadata.id)\
                                  .where(wiki.Metadata.path == v['path'])\
                                  .where(wiki.Metadata.is_dir == is_dir)

                if ar.count() == 0:
                    ar = wiki.Metadata()
                    ar.user = self.user
                    ar.root_id = metadata.id
                    ar.path = v['path']
                    ar.is_dir = is_dir
                else:
                    ar = ar.get()
                    # 没有更改, 跳过
                    if 0 == is_dir and hash_key == ar.hash_key:
                        continue

                ar.bytes = v['bytes']
                ar.hash_key = hash_key
                if v.get('modified', False):
                    ar.modified = Date.str_to_time(v['modified'].split('+')[0],
                                                   '%a, %d %b %Y %H:%M:%S ')
                ar.save()

                # 子事务
                child_task_affair = wiki.TaskAffairs.add(ar, task_affair.id)

                if v['is_dir']:
                    self.add_task(
                        route.url_for('task.SyncPath', self.user.id, ar.id) +
                        '?parent=%s' % child_task_affair.id, 1)
                else:
                    self.add_task(
                        route.url_for('task.SyncFile', ar.id) +
                        '?affair=%s' % child_task_affair.id, 2)

        # 删除不存在的文件/目录
        for v in path_list:
            ar = wiki.Metadata.select()\
                                  .where(wiki.Metadata.root_id == metadata.id)\
                                  .where(wiki.Metadata.path == v)
            if ar.count() != 0:
                ar.get().remove()

        self.finish()
Exemplo n.º 7
0
            )

        pl = models.Plugins()
        pl.name        = name
        pl.bind        = Json.encode(register._targets)
        pl.handlers    = Json.encode(handlers)
        pl.ui_modules  = Json.encode(ui_modules)

        if plugin.get_form() :
            pl.config = Json.encode(plugin.get_form().get_default_values())
        pl.save()

        # 安装路由
        if len(plugin._handlers) > 0 :
            from xcat.web import Application
            cache.client.set(Application._sync_key,Date.time())

        reset()
        
class Register(object):
    '''
    插件注册表
    '''
    
    def __init__(self):
        self._handler = False
        self._targets = {}
        self._events  = (
            'on_init' , 
            'before_execute' , 
            'before_render' ,
Exemplo n.º 8
0
    def callback(self, response):
        if response.error:
            self.write('Init User(%s) Metadata Error : %s' 
                        % (self.user.id, response.error) )
            self.finish()
            return 

        json = Json.decode(response.body)

        if len(json.get('path','')) > 255:
            self.write('Path Too long.')
            self.finish()
            return 
        
        hash_key = json.get('hash', False) or json.get('rev','')

        if '' == hash_key:
            # 中文目录,先删除处理
            self.metadata.remove()
            self.finish()
            return 

        if self.metadata:
            metadata = self.metadata
            # 没有变更
            # if hash_key == str(metadata.hash_key):
            #     self.finish()
            #     return 
        else:
            # 初始化根目录
            metadata = wiki.Metadata()
            metadata.user = self.user
            metadata.path = json['path']
            metadata.bytes = json['bytes']
            metadata.is_dir = json['is_dir'] and 1 or 0

        if json.get('modified', False):
            metadata.modified = Date.str_to_time(json['modified'].split('+')[0], '%a, %d %b %Y %H:%M:%S ')
        
        metadata.hash_key = hash_key
        metadata.save()

        task_affair = wiki.TaskAffairs.add(
                        metadata, 
                        self.affair_parent
                      )

        # 取目录下文件 path 列表, 与新数据比对
        # 存在的 path 删除, 列表中留下的path ,
        # 就是需要删除的 文件 / 目录
        path_list = []
        for v in wiki.Metadata.select(wiki.Metadata.path)\
                              .where(wiki.Metadata.root_id == metadata.id):
            path_list.append(v.path)

        for v in json.get('contents', []):
            if len(json['path']) < 255:
                if v['path'] in path_list:
                    path_list.remove(v['path'])

                is_dir = v['is_dir'] and 1 or 0
                hash_key = v.get('rev', False) or v['hash']

                if 0 == is_dir:
                    uri, ext = os.path.splitext(v['path'])
                    # 不支持的文件类型, 跳过
                    if ext.lower() not in self.settings['support_ext']:
                        continue  

                # 判断是否存在
                ar = wiki.Metadata.select()\
                                  .where(wiki.Metadata.root_id == metadata.id)\
                                  .where(wiki.Metadata.path == v['path'])\
                                  .where(wiki.Metadata.is_dir == is_dir)

                if ar.count() == 0:
                    ar = wiki.Metadata()
                    ar.user = self.user
                    ar.root_id = metadata.id
                    ar.path = v['path']
                    ar.is_dir = is_dir
                else:
                    ar = ar.get()
                    # 没有更改, 跳过
                    if 0 == is_dir and hash_key == ar.hash_key:
                        continue   

                ar.bytes = v['bytes']
                ar.hash_key = hash_key
                if v.get('modified', False):
                    ar.modified = Date.str_to_time(v['modified'].split('+')[0], '%a, %d %b %Y %H:%M:%S ')
                ar.save()

                # 子事务
                child_task_affair = wiki.TaskAffairs.add(
                    ar, 
                    task_affair.id
                )

                if v['is_dir']:
                    self.add_task(
                        route.url_for('task.SyncPath', self.user.id, ar.id) + '?parent=%s' % child_task_affair.id,
                        1
                    )
                else:
                    self.add_task(
                        route.url_for('task.SyncFile', ar.id) + '?affair=%s' % child_task_affair.id,
                        2
                    )
        
        # 删除不存在的文件/目录
        for v in path_list:
            ar = wiki.Metadata.select()\
                                  .where(wiki.Metadata.root_id == metadata.id)\
                                  .where(wiki.Metadata.path == v)
            if ar.count() != 0:                  
                ar.get().remove()
           

        self.finish()