Exemple #1
0
    def item(self, group_id):
        """
        获取某个用户组
        /group/<int:group_id>

        :param group_id:
        :return:
        """
        ## sqlalchemy版本
        group_model = MemberModel()
        group = group_model.members(group_id=group_id)
        if group:
            return self.render_json(data=group)
        return self.render_json(code=-1)

        ## mixin 版本
        group_model = TagModel().get_by_id(group_id)
        if not group_model:
            return self.render_json(code=-1)

        user_model = UserModel()
        user_info = user_model.fetch_by_uid(uids=group_model.users)

        group_info = group_model.to_dict()
        group_info['members'] = user_info
        group_info['users'] = len(user_info)
        group_info['group_name'] = group_info['name']
        group_info['group_id'] = group_info['id']
        return self.render_json(data=group_info)
Exemple #2
0
    def put(self, group_id):
        """
        update group
        /group/<int:group_id>

        :return:
        """
        super(GroupAPI, self).put()

        form = GroupForm(request.form, csrf_enabled=False)
        form.set_group_id(group_id)
        if form.validate_on_submit():
            # pass
            # user_ids = [int(uid) for uid in form.user_ids.data.split(',')]
            import json
            current_app.logger.info(form.uid_roles)

            current_app.logger.info(json.loads(form.uid_roles))

            group_model = MemberModel(group_id=group_id)
            for uid_role in json.loads(form.uid_roles):
                uid_role['project_id'] = 0
                current_app.logger.info(uid_role)
                group_model.create_or_update(uid_role, uid_role)

            # group_model.update(group_id=group_id,
            #                    group_name=form.group_name.data,
            #                    user_ids=user_ids)
            return self.render_json(data=group_model.item())

        return self.render_json(code=-1, message=form.errors)
Exemple #3
0
    def delete(self, group_id):
        """
        /group/<int:group_id>

        :return:
        """
        super(GroupAPI, self).delete()

        group_model = MemberModel()
        tag_model = TagModel()
        tag_model.remove(group_id)
        group_model.remove(group_id)

        return self.render_json(message='')
Exemple #4
0
    def post(self):
        """
        create a space
        /environment/

        :return:
        """
        super(SpaceAPI, self).post()

        form = SpaceForm(request.form, csrf_enabled=False)
        # return self.render_json(code=-1, data = form.form2dict())
        if form.validate_on_submit():
            # create space
            space_new = SpaceModel()
            data = form.form2dict()
            id = space_new.add(data)
            if not id:
                return self.render_json(code=-1)

            current_app.logger.info(request.json)
            # create group
            data['role'] = OWNER
            members = [data]
            MemberModel(group_id=id).update_group(members=members)
            return self.render_json(data=space_new.item())
        else:
            return self.render_json(code=-1, message=form.errors)
Exemple #5
0
    def put(self, space_id, action=None):
        """
        update environment
        /environment/<int:id>

        :return:
        """
        super(SpaceAPI, self).put()

        if action and action == 'switch':
            return self.switch(space_id)

        form = SpaceForm(request.form, csrf_enabled=False)
        form.set_id(space_id)
        if form.validate_on_submit():
            space = SpaceModel().get_by_id(space_id)
            data = form.form2dict()
            # a new type to update a model
            ret = space.update(data)
            # create group
            if request.form.has_key('members'):
                MemberModel(group_id=space_id).update_group(
                    members=json.loads(request.form['members']))
            return self.render_json(data=space.item())
        else:
            return self.render_json(code=-1, message=form.errors)
Exemple #6
0
 def members(self, space_id):
     page = int(request.args.get('page', 1))
     size = int(request.args.get('size', 10))
     members = MemberModel(group_id=space_id).members(page=page, size=size)
     return self.list_json(list=members['members'],
                           count=members['count'],
                           enable_create=permission.enable_role(OWNER))
Exemple #7
0
    def members(self, project_id, members):
        """

        :param project_id:
        :param members:
        :return:
        """
        # TODO login for group id
        group_id = 1

        group_model = MemberModel(project_id=project_id)
        ret = group_model.update_project(project_id=project_id,
                                         members=members)

        item = group_model.members()

        return self.render_json(data=item)
Exemple #8
0
    def delete(self, user_id):
        """
        remove a user with his group relation
        /user/<int:user_id>

        :param user_id:
        :return:
        """
        super(UserAPI, self).delete()

        UserModel(id=user_id).remove()
        MemberModel().remove(user_id=user_id)
        return self.render_json(message='')
Exemple #9
0
    def item(self, project_id):
        """
        获取某个用户组

        :param id:
        :return:
        """

        project_model = ProjectModel(id=project_id)
        project_info = project_model.item()
        if not project_info:
            return self.render_json(code=-1)

        group_info = MemberModel().members(project_id=project_id)
        current_app.logger.info(group_info)

        return self.render_json(data=dict(project_info, **group_info))
Exemple #10
0
    def list(self):
        """
        fetch user list or one user

        :return:
        """
        page = int(request.args.get('page', 0))
        page = page - 1 if page else 0
        size = float(request.args.get('size', 10))
        kw = request.values.get('kw', '')

        uids = []
        if current_user.role <> SUPER:
            members = MemberModel(group_id=current_user.last_space).members()
            uids = members['user_ids']

        user_model = UserModel()
        user_list, count = user_model.list(uids=uids, page=page, size=size, kw=kw)
        filters = {
            'username': ['线上', '线下'],
            'status': ['正常', '禁用']
        }
        return self.list_json(list=user_list, count=count, table=self.table(filters), enable_create=permission.enable_role(MASTER))