Example #1
0
    def put(self, *args, **kwargs):
        if args or 'user' not in kwargs:
            self.raise401()

        username = self.get_argument('username', None)
        email = self.get_argument('email', None)
        password = self.get_argument('password', None)
        first_name = self.get_argument('first_name', None)
        last_name = self.get_argument('last_name', None)

        user = kwargs['user']
        update = {}
        if username:
            update['set__username'] = username
        if email:
            update['set__email'] = email
        if password:
            update['set__password'] = create_password(password)
        if first_name:
            update['set__first_name'] = first_name
        if last_name:
            update['set__last_name'] = last_name

        try:
            User.objects(username=user.username).update_one(**update)
            user = User.objects(name=username or user.username).first()
            self.set_status(201)
            self.write(document_to_json(user, filter_set=_FILTER))
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #2
0
    def post(self, *args, **kwargs):
        """Create a new project by providing the details about the project.
        If the validation and saving are successful, it response with the
        project data and 201 status.
        Otherwise, it gives 4XX status and error messages.
        """
        name = self.get_argument('name', None)
        description = self.get_argument('description', None)
        url = self.get_argument('url', None)
        leader = self.get_argument('leader', None)
        members = self.get_argument('members', None)
        teams = self.get_argument('teams', None)
        repos = self.get_argument('repos', None)
        tags = self.get_argument('tags', None)
        if 'user' not in kwargs:
            self.raise401()

        try:
            # todo - better arguments handler
            url = url.strip()
            url = url if url else None
            members_list = []
            repos_list = []
            teams_list = []
            project_leader = kwargs['user']
            if leader:
                project_leader = User.objects(username=leader).first()

            if repos:
                for repo in parse_listed_strs(repos):
                    r = Repo.objects(name=repo).first()
                    if not r:
                        continue
                    repos_list.append(r)
            if members:
                for member in parse_listed_strs(members):
                    u = User.objects(username=member).first()
                    if not u or u == project_leader:
                        continue
                    members_list.append(u)
            if teams:
                for team in parse_listed_strs(teams):
                    t = Team.objects(name=team).first()
                    if not t:
                        continue
                    teams_list.append(t)
            members_list.append(project_leader)
            tags_list = parse_listed_strs(tags)
            project = Project(
                name=name, description=description,
                url=url, repos=repos_list,
                leader=project_leader, members=members_list,
                teams=teams_list, tags=tags_list)
            project.save()
            project_data = document_to_json(project, filter_set=_FILTER)
            self.set_status(201)
            self.write(project_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #3
0
 def put(self,  *args, **kwargs):
     if 'user' not in kwargs or not args:
         self.raise401()
     name = self.get_argument('name', None)
     description = self.get_argument('description', None)
     path = self.get_argument('path', None)
     scm = self.get_argument('scm', None)
     team = self.get_argument('team', None)
     tags = self.get_argument('tags', None)
     user = kwargs['user']
     update = {}
     if name:
         update['set__name'] = name
     if description:
         update['set__description'] = description
     if path:
         update['set__path'] = path
     if scm:
         update['set__scm'] = scm
     if team:
         update['set__team'] = team
     if tags:
         tags_list = parse_listed_strs(tags)
         update['set__tags'] = tags_list
     try:
         path = parse_path(args[0])
         Repo.objects(owner=user, name=path[0]).update_one(**update)
         repo = Repo.objects(owner=user, name=name or path[0]).first()
         repo_data = document_to_json(repo, filter_set=_FILTER)
         self.set_status(201)
         self.write(repo_data)
     except Exception as e:
         reason = e.message
         self.raise400(reason=reason)
Example #4
0
    def get(self, *args, **kwargs):
        # /clients
        # /clients/:app_name
        if 'user' not in kwargs:
            self.raise401()
        user = kwargs['user']

        if args:
            path = parse_path(args[0])
            client = Client.objects(user=user, app_name=path[0]).first()
            if not client:
                self.raise404()
            client_data = document_to_json(client, filter_set=_FILTER)
        else:
            limit = self.get_argument('limit', None)
            start = self.get_argument('start', None)
            try:
                limit = int(limit)
            except:
                limit = None
            try:
                start = int(start)
            except:
                start = None
            clients = Client.objects(user=user)
            if limit and start:
                clients = clients[start: start+limit]
            elif limit:
                clients = clients[:limit]
            elif start:
                clients = clients[start:]
            client_data = query_to_json(clients, filter_set=_FILTER)
        self.write(client_data)
Example #5
0
    def put(self, *args, **kwargs):
        if 'user' not in kwargs or not args:
            self.raise401()

        # redirect_uris = self.get_argument('redirect_uris', None)
        app_name = self.get_argument('app_name', None)
        description = self.get_argument('description', None)
        website = self.get_argument('website', None)
        update = {}
        if app_name:
            update['set__app_name'] = app_name
        if description:
            update['set__description'] = description
        if website:
            update['set__website'] = website
        # if redirect_uris:
        #     update['set_redirect_uris'] = parse_listed_strs(redirect_uris)
        user = kwargs['user']
        path = parse_path(args[0])
        client = Client.objects(app_name=path[0]).first()
        if not client or user != client.user:
            self.raise401()
        try:
            Client.objects(app_name=path[0]).update_one(**update)
            client = Client.objects(app_name=app_name or path[0]).first()
            client_data = document_to_json(client, filter_set=_FILTER)
            self.set_status(201)
            self.write(client_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #6
0
    def put(self, *args, **kwargs):
        if 'user' not in kwargs or not args:
            self.raise401()
        update = {}
        user = kwargs['user']
        task_id = parse_path(args[0])[0]
        task = Task.objects(id=task_id).first()
        project = task.project
        if not project or user not in project.members:
            self.raise401()
        category = self.get_argument('category', None)
        description = self.get_argument('description', None)
        project_name = self.get_argument('project', None)
        status = self.get_argument('status', None)
        priority = self.get_argument('priority', None)
        assign_to = self.get_argument('assign_to', None)
        due = self.get_argument('due', None)
        tags = self.get_argument('tags', None)

        if category:
            update['set__category'] = category
        if description:
            update['set__description'] = description
        if project_name:
            project = Project.objects(name=project_name).first()
            if not project or user not in project.members:
                self.raise401()
            update['set__project'] = project
        if assign_to:
            assign_to_list = []
            for member in parse_listed_strs(assign_to):
                u = User.objects(username=member).first()
                if not u:
                    continue
                assign_to_list.append(u)
            update['set__assign_to'] = assign_to_list
        if status:
            update['set__status'] = status
        if priority:
            update['set__priority'] = priority
        if due:
            try:
                due_day = int(due)
            except ValueError:
                due_day = 0
            due_time = get_utc_time(due_day * 24 * 3600)
            update['set__due'] = due_time
        if tags:
            tags_list = parse_listed_strs(tags)
            update['set__tags'] = tags_list
        try:
            Task.objects(id=task_id).update_one(**update)
            task = Task.objects(id=task_id).first()
            task_data = document_to_json(task, filter_set=_FILTER)
            self.set_status(201)
            self.write(task_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #7
0
    def put(self, *args, **kwargs):
        if 'user' not in kwargs or not args:
            self.raise401()
        update = {}
        user = kwargs['user']
        task_id = parse_path(args[0])[0]
        task = Task.objects(id=task_id).first()
        project = task.project
        if not project or user not in project.members:
            self.raise401()
        category = self.get_argument('category', None)
        description = self.get_argument('description', None)
        project_name = self.get_argument('project', None)
        status = self.get_argument('status', None)
        priority = self.get_argument('priority', None)
        assign_to = self.get_argument('assign_to', None)
        due = self.get_argument('due', None)
        tags = self.get_argument('tags', None)

        if category:
            update['set__category'] = category
        if description:
            update['set__description'] = description
        if project_name:
            project = Project.objects(name=project_name).first()
            if not project or user not in project.members:
                self.raise401()
            update['set__project'] = project
        if assign_to:
            assign_to_list = []
            for member in parse_listed_strs(assign_to):
                u = User.objects(username=member).first()
                if not u:
                    continue
                assign_to_list.append(u)
            update['set__assign_to'] = assign_to_list
        if status:
            update['set__status'] = status
        if priority:
            update['set__priority'] = priority
        if due:
            try:
                due_day = int(due)
            except ValueError:
                due_day = 0
            due_time = get_utc_time(due_day * 24 * 3600)
            update['set__due'] = due_time
        if tags:
            tags_list = parse_listed_strs(tags)
            update['set__tags'] = tags_list
        try:
            Task.objects(id=task_id).update_one(**update)
            task = Task.objects(id=task_id).first()
            task_data = document_to_json(task, filter_set=_FILTER)
            self.set_status(201)
            self.write(task_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #8
0
    def get(self, *args, **kwargs):
        # need project/team information
        if 'user' not in kwargs:
            self.raise401()

        user = kwargs['user']
        if args:
            path = parse_path(args[0])
            if user.username != path[0]:
                user = User.objects(username=path[0]).first()
        self.write(document_to_json(user, filter_set=_FILTER))
Example #9
0
    def get(self, *args, **kwargs):
        if 'user' not in kwargs:
            self.raise401()

        user = kwargs['user']

        if args:
            path = parse_path(args[0])
            task = Task.objects(id=path[0]).first()
            if not task:
                self.raise404()
            if user not in task.project.members:
                self.raise401()
            task_data = document_to_json(task, filter_set=_FILTER)
        else:
            project_name = self.get_argument('project', None)
            limit = self.get_argument('limit', None)
            start = self.get_argument('start', None)
            try:
                limit = int(limit)
            except:
                limit = None
            try:
                start = int(start)
            except:
                start = None
            try:
                project_name = parse_path(project_name)[0]
            except IndexError:
                project_name = None
            if project_name:
                project = Project.objects(name=project_name).first()
                if not project:
                    self.raise404()
                if user not in project.members:
                    self.raise403()
                tasks = Task.objects(project=project)
            else:
                projects = Project.objects(members__in=[user]).all()
                tasks = []
                for project in projects:
                    ts = Task.objects(project=project).all()
                    tasks += list(ts)
            if limit and start:
                tasks = tasks[start: start+limit]
            elif limit:
                tasks = tasks[:limit]
            elif start:
                tasks = tasks[start:]
            task_data = query_to_json(tasks, filter_set=_FILTER)
        self.write(task_data)
Example #10
0
    def get(self, *args, **kwargs):
        if 'user' not in kwargs:
            self.raise401()

        user = kwargs['user']

        if args:
            path = parse_path(args[0])
            task = Task.objects(id=path[0]).first()
            if not task:
                self.raise404()
            if user not in task.project.members:
                self.raise401()
            task_data = document_to_json(task, filter_set=_FILTER)
        else:
            project_name = self.get_argument('project', None)
            limit = self.get_argument('limit', None)
            start = self.get_argument('start', None)
            try:
                limit = int(limit)
            except:
                limit = None
            try:
                start = int(start)
            except:
                start = None
            try:
                project_name = parse_path(project_name)[0]
            except IndexError:
                project_name = None
            if project_name:
                project = Project.objects(name=project_name).first()
                if not project:
                    self.raise404()
                if user not in project.members:
                    self.raise403()
                tasks = Task.objects(project=project)
            else:
                projects = Project.objects(members__in=[user]).all()
                tasks = []
                for project in projects:
                    ts = Task.objects(project=project).all()
                    tasks += list(ts)
            if limit and start:
                tasks = tasks[start:start + limit]
            elif limit:
                tasks = tasks[:limit]
            elif start:
                tasks = tasks[start:]
            task_data = query_to_json(tasks, filter_set=_FILTER)
        self.write(task_data)
Example #11
0
 def put(self, *args, **kwargs):
     if 'user' not in kwargs or not args:
         self.raise401()
     user = kwargs['user']
     path = parse_path(args[0])
     team = Team.objects(name=path[0]).first()
     if not team or user not in team.members:
         self.raise401()
     team_leader = team.leader
     update = {}
     name = self.get_argument('name', None)
     description = self.get_argument('description', None)
     url = self.get_argument('url', None)
     leader = self.get_argument('leader', None)
     members = self.get_argument('members', None)
     tags = self.get_argument('tags', None)
     if name:
         update['set__name'] = name
     if description:
         update['set__description'] = description
     if url:
         update['set__url'] = url
     if leader:
         team_leader = User.objects(username=leader).first()
         update['set__leader'] = team_leader
     if members:
         members_list = []
         for member in parse_listed_strs(members):
             u = User.objects(username=member).first()
             if not u or u == team_leader:
                 continue
             members_list.append(u)
         members_list.append(team_leader)
         update['set__members'] = members_list
     if tags:
         tags_list = parse_listed_strs(tags)
         update['set__tags'] = tags_list
     try:
         Team.objects(name=path[0]).update_one(**update)
         team = Team.objects(name=name or path[0]).first()
         team_data = document_to_json(team, filter_set=_FILTER)
         self.set_status(201)
         self.write(team_data)
     except Exception as e:
         reason = e.message
         self.raise400(reason=reason)
Example #12
0
    def post(self, *args, **kwargs):
        if 'user' not in kwargs or args:
            self.raise401()
        category = self.get_argument('category', None)
        description = self.get_argument('description', None)
        project_name = self.get_argument('project', None)
        status = self.get_argument('status', None)
        priority = self.get_argument('priority', None)
        assign_to = self.get_argument('assign_to', None)
        due = self.get_argument('due', None)  # days or date
        tags = self.get_argument('tags', None)
        user = kwargs['user']
        project = Project.objects(name=project_name).first()
        if not project or user not in project.members:
            self.raise401()
        try:
            due_day = int(due)
        except ValueError:
            due_day = 0

        try:
            assign_to_list = []
            if assign_to:
                for member in parse_listed_strs(assign_to):
                    u = User.objects(username=member).first()
                    if not u:
                        continue
                    assign_to_list.append(u)
            due_time = get_utc_time(due_day * 24 * 3600)
            tags_list = parse_listed_strs(tags)
            task = Task(category=category,
                        description=description,
                        project=project,
                        status=status,
                        priority=priority,
                        assign_to=assign_to_list,
                        due=due_time,
                        tags=tags_list)
            task.save()
            task_data = document_to_json(task, filter_set=_FILTER)
            self.set_status(201)
            self.write(task_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #13
0
    def post(self, *args, **kwargs):
        username = self.get_argument('username', None)
        email = self.get_argument('email', None)
        password = self.get_argument('password', None)
        first_name = self.get_argument('first_name', None)
        last_name = self.get_argument('last_name', None)
        ip = self.get_argument('user_ip', None)

        try:
            password = create_password(password) if password else None
            user = User(username=username, email=email,
                        password=password, first_name=first_name,
                        last_name=last_name, ip=ip).save()
            self.set_status(201)
            self.write(document_to_json(user, filter_set=_FILTER))
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #14
0
    def post(self, *args, **kwargs):
        if 'user' not in kwargs or args:
            self.raise401()
        category = self.get_argument('category', None)
        description = self.get_argument('description', None)
        project_name = self.get_argument('project', None)
        status = self.get_argument('status', None)
        priority = self.get_argument('priority', None)
        assign_to = self.get_argument('assign_to', None)
        due = self.get_argument('due', None)  # days or date
        tags = self.get_argument('tags', None)
        user = kwargs['user']
        project = Project.objects(name=project_name).first()
        if not project or user not in project.members:
            self.raise401()
        try:
            due_day = int(due)
        except ValueError:
            due_day = 0

        try:
            assign_to_list = []
            if assign_to:
                for member in parse_listed_strs(assign_to):
                    u = User.objects(username=member).first()
                    if not u:
                        continue
                    assign_to_list.append(u)
            due_time = get_utc_time(due_day * 24 * 3600)
            tags_list = parse_listed_strs(tags)
            task = Task(
                category=category, description=description, project=project,
                status=status, priority=priority, assign_to=assign_to_list,
                due=due_time, tags=tags_list)
            task.save()
            task_data = document_to_json(task, filter_set=_FILTER)
            self.set_status(201)
            self.write(task_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #15
0
    def get(self, *args, **kwargs):
        if 'user' not in kwargs:
            self.raise401()

        user = kwargs['user']
        if args:
            path = parse_path(args[0])
            team = Team.objects(name=path[0]).first()
            if not team:
                self.raise404()
            team_data = document_to_json(team, filter_set=_FILTER)
        else:
            # username = self.get_argument('username', None)
            # try:
            #     username = parse_path(username)[0]
            # except IndexError:
            #     username = None
            # if username:
            #     user = User.objects(username=username).fisrt()
            limit = self.get_argument('limit', None)
            start = self.get_argument('start', None)
            try:
                limit = int(limit)
            except:
                limit = None
            try:
                start = int(start)
            except:
                start = None
            teams = Team.objects(members__in=[user])
            if limit and start:
                teams = teams[start: start+limit]
            elif limit:
                teams = teams[:limit]
            elif start:
                teams = teams[start:]
            team_data = query_to_json(teams, filter_set=_FILTER)
        self.write(team_data)
Example #16
0
    def post(self, *args, **kwargs):
        if 'user' not in kwargs or args:
            self.raise401()

        grant_type = self.get_argument('grant_type', None)
        response_type = self.get_argument('response_type', None)
        redirect_uris = self.get_argument('redirect_uris', None)
        app_name = self.get_argument('app_name', None)
        description = self.get_argument('description', None)
        website = self.get_argument('website', None)

        try:
            user = kwargs['user']
            client_id = create_id()
            client_secret = create_secret()
            grant_type = grant_type or 'authorization_code'
            response_type = response_type or 'code'
            # todo scopes
            default_scopes = ['tasks', 'projects', 'repos', 'users', 'teams']
            scopes = default_scopes
            redirect_uris = parse_listed_strs(redirect_uris)
            # todo default
            default_redirect_uri = redirect_uris[0] if redirect_uris else ''

            client = Client(
                client_id=client_id, client_secret=client_secret,
                user=user, grant_type=grant_type,
                response_type=response_type, scopes=scopes,
                default_scopes=default_scopes, redirect_uris=redirect_uris,
                default_redirect_uri=default_redirect_uri, website=website,
                app_name=app_name, description=description)
            client.save()
            client_data = document_to_json(client, filter_set=_FILTER)
            self.set_status(201)
            self.write(client_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #17
0
    def post(self, *args, **kwargs):
        if 'user' not in kwargs or args:
            self.raise401()
        name = self.get_argument('name', None)
        description = self.get_argument('description', None)
        url = self.get_argument('url', None)
        leader = self.get_argument('leader', None)
        members = self.get_argument('members', None)
        tags = self.get_argument('tags', None)

        try:
            # todo - better arguments handler
            url = url.strip()
            url = url if url else None
            members_list = []
            team_leader = kwargs['user']
            if leader:
                team_leader = User.objects(username=leader).first()
            if members:
                for member in parse_listed_strs(members):
                    u = User.objects(username=member).first()
                    if not u or u == team_leader:
                        continue
                    members_list.append(u)
            members_list.append(team_leader)
            tags_list = parse_listed_strs(tags)
            team = Team(
                name=name, description=description,
                url=url, leader=team_leader,
                members=members_list, tags=tags_list)
            team.save()
            team_data = document_to_json(team, filter_set=_FILTER)
            self.set_status(201)
            self.write(team_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #18
0
    def post(self, *args, **kwargs):
        if 'user' not in kwargs:
            self.raise401()
        if not args:
            self.raise404()

        path = parse_path(args[0])
        task = Task.objects(id=path[0]).first()
        if not task:
            self.raise404()
        user = kwargs['user']
        if user not in task.project.members:
            self.raise401()
        content = self.get_argument('content', None)
        try:
            comment = TaskComment(content=content, author=user)
            Task.objects(id=path[0]).update_one(push__comments=comment)
            comment_data = document_to_json(
                comment, filter_set=_COMMENT_FILTER)
            self.set_status(201)
            self.write(comment_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #19
0
    def post(self, *args, **kwargs):
        if 'user' not in kwargs:
            self.raise401()
        if not args:
            self.raise404()

        path = parse_path(args[0])
        task = Task.objects(id=path[0]).first()
        if not task:
            self.raise404()
        user = kwargs['user']
        if user not in task.project.members:
            self.raise401()
        content = self.get_argument('content', None)
        try:
            comment = TaskComment(content=content, author=user)
            Task.objects(id=path[0]).update_one(push__comments=comment)
            comment_data = document_to_json(comment,
                                            filter_set=_COMMENT_FILTER)
            self.set_status(201)
            self.write(comment_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #20
0
 def post(self, *args, **kwargs):
     name = self.get_argument('name', None)
     description = self.get_argument('description', None)
     path = self.get_argument('path', None)  # verify
     scm = self.get_argument('scm', None)
     team = self.get_argument('team', None)  # just for test
     tags = self.get_argument('tags', None)
     if 'user' not in kwargs:
         self.raise401()
     user = kwargs['user']
     tags_list = parse_listed_strs(tags)
     try:
         name = name.strip()
         name = name if name else None
         repo = Repo(name=name, description=description,
                     path=path, scm=scm, owner=user,
                     team=team, tags=tags_list)
         repo.save()
         repo_data = document_to_json(repo, filter_set=_FILTER)
         self.set_status(201)
         self.write(repo_data)
     except Exception as e:
         reason = e.message
         self.raise400(reason=reason)
Example #21
0
    def get(self, *args, **kwargs):
        """Retrieve the resources of projects for the current user.

        If `*args` is provided by matching the URL pattern, the first element
        in the args is considered as a project name, then the project data will
        be retrieved from Database and send back to the client and the source
        owner in the format of JSON.
        Otherwise, it responses with a list of projects parcipated by the
        user. The request can provide three arugments: `team`, `limit` and
        `start`. `team` is used for querying the projects of one team by
        its name, which the user is one of its memebers. `limit` is
        the max number of items sent back to the client. `start` is the
        starting index of the querying results.

        Only authenticated user/resouce owner can access by using access_token,
        and his/her scopes must include `projects`.

        The retrieved resource should always be related to the user, and it is
        not allowed to access others' projects or other teams' projects.

        .. todo::
            restrict the response data and add default limits
        """
        if 'user' not in kwargs:
            self.raise401()

        user = kwargs['user']
        if args:
            path = parse_path(args[0])
            project = Project.objects(name=path[0]).first()
            if not project:
                self.raise404()
            if project and user not in project.members:
                self.raise401()
            project_data = document_to_json(project, filter_set=_FILTER)
        else:
            team_name = self.get_argument('team', None)
            limit = self.get_argument('limit', None)
            start = self.get_argument('start', None)
            try:
                team_name = parse_path(team_name)[0]
            except IndexError:
                team_name = None
            try:
                limit = int(limit)
            except Exception:
                limit = None
            try:
                start = int(start)
            except Exception:
                start = None
            if team_name:
                team = Team.objects(name=team_name).first()
                if not team:
                    self.raise404()
                if user not in team.members:
                    self.raise403()
                project = Project.objects(teams__in=[team])
            else:
                project = Project.objects(members__in=[user])
            if limit and start:
                project = project[start:start + limit]
            elif limit:
                project = project[:limit]
            elif start:
                project = project[start:]
            project_data = query_to_json(project, filter_set=_FILTER)
        self.write(project_data)
Example #22
0
    def put(self, *args, **kwargs):
        """Update a project by its name and other information.
        """
        if 'user' not in kwargs or not args:
            self.raise401()
        name = self.get_argument('name', None)
        description = self.get_argument('description', None)
        url = self.get_argument('url', None)
        leader = self.get_argument('leader', None)
        members = self.get_argument('members', None)
        teams = self.get_argument('teams', None)
        repos = self.get_argument('repos', None)
        tags = self.get_argument('tags', None)

        user = kwargs['user']
        path = parse_path(args[0])
        project = Project.objects(name=path[0]).first()
        if not project or user not in project.members:
            self.raise401()
        project_leader = project.leader
        update = {}
        if name:
            update['set__name'] = name
        if description:
            update['set__description'] = description
        if url:
            update['set__url'] = url
        if leader:
            project_leader = User.objects(username=leader).first()
            update['set__leader'] = project_leader
        if members:
            members_list = []
            for member in parse_listed_strs(members):
                u = User.objects(username=member).first()
                if not u or u == project_leader:
                    continue
                members_list.append(u)
            members_list.append(project_leader)
            update['set__members'] = members_list
        if teams:
            teams_list = []
            for team in parse_listed_strs(teams):
                t = Team.objects(name=team).first()
                if not t:
                    continue
                teams_list.append(t)
            update['set__teams'] = teams_list
        if repos:
            repos_list = []
            for repo in parse_listed_strs(repos):
                r = Repo.objects(name=repo).first()
                if not r:
                    continue
                repos_list.append(r)
            update['set__repos'] = repos_list
        if tags:
            tags_list = parse_listed_strs(tags)
            update['set__tags'] = tags_list
        try:
            Project.objects(name=path[0]).update_one(**update)
            project = Repo.objects(name=name or path[0]).first()
            project_data = document_to_json(project, filter_set=_FILTER)
            self.set_status(201)
            self.write(project_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #23
0
    def put(self, *args, **kwargs):
        """Update a project by its name and other information.
        """
        if 'user' not in kwargs or not args:
            self.raise401()
        name = self.get_argument('name', None)
        description = self.get_argument('description', None)
        url = self.get_argument('url', None)
        leader = self.get_argument('leader', None)
        members = self.get_argument('members', None)
        teams = self.get_argument('teams', None)
        repos = self.get_argument('repos', None)
        tags = self.get_argument('tags', None)

        user = kwargs['user']
        path = parse_path(args[0])
        project = Project.objects(name=path[0]).first()
        if not project or user not in project.members:
            self.raise401()
        project_leader = project.leader
        update = {}
        if name:
            update['set__name'] = name
        if description:
            update['set__description'] = description
        if url:
            update['set__url'] = url
        if leader:
            project_leader = User.objects(username=leader).first()
            update['set__leader'] = project_leader
        if members:
            members_list = []
            for member in parse_listed_strs(members):
                u = User.objects(username=member).first()
                if not u or u == project_leader:
                    continue
                members_list.append(u)
            members_list.append(project_leader)
            update['set__members'] = members_list
        if teams:
            teams_list = []
            for team in parse_listed_strs(teams):
                t = Team.objects(name=team).first()
                if not t:
                    continue
                teams_list.append(t)
            update['set__teams'] = teams_list
        if repos:
            repos_list = []
            for repo in parse_listed_strs(repos):
                r = Repo.objects(name=repo).first()
                if not r:
                    continue
                repos_list.append(r)
            update['set__repos'] = repos_list
        if tags:
            tags_list = parse_listed_strs(tags)
            update['set__tags'] = tags_list
        try:
            Project.objects(name=path[0]).update_one(**update)
            project = Repo.objects(name=name or path[0]).first()
            project_data = document_to_json(project, filter_set=_FILTER)
            self.set_status(201)
            self.write(project_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)
Example #24
0
    def get(self, *args, **kwargs):
        """Retrieve the resources of projects for the current user.

        If `*args` is provided by matching the URL pattern, the first element
        in the args is considered as a project name, then the project data will
        be retrieved from Database and send back to the client and the source
        owner in the format of JSON.
        Otherwise, it responses with a list of projects parcipated by the
        user. The request can provide three arugments: `team`, `limit` and
        `start`. `team` is used for querying the projects of one team by
        its name, which the user is one of its memebers. `limit` is
        the max number of items sent back to the client. `start` is the
        starting index of the querying results.

        Only authenticated user/resouce owner can access by using access_token,
        and his/her scopes must include `projects`.

        The retrieved resource should always be related to the user, and it is
        not allowed to access others' projects or other teams' projects.

        .. todo::
            restrict the response data and add default limits
        """
        if 'user' not in kwargs:
            self.raise401()

        user = kwargs['user']
        if args:
            path = parse_path(args[0])
            project = Project.objects(name=path[0]).first()
            if not project:
                self.raise404()
            if project and user not in project.members:
                self.raise401()
            project_data = document_to_json(project, filter_set=_FILTER)
        else:
            team_name = self.get_argument('team', None)
            limit = self.get_argument('limit', None)
            start = self.get_argument('start', None)
            try:
                team_name = parse_path(team_name)[0]
            except IndexError:
                team_name = None
            try:
                limit = int(limit)
            except Exception:
                limit = None
            try:
                start = int(start)
            except Exception:
                start = None
            if team_name:
                team = Team.objects(name=team_name).first()
                if not team:
                    self.raise404()
                if user not in team.members:
                    self.raise403()
                project = Project.objects(teams__in=[team])
            else:
                project = Project.objects(members__in=[user])
            if limit and start:
                project = project[start:start + limit]
            elif limit:
                project = project[:limit]
            elif start:
                project = project[start:]
            project_data = query_to_json(project, filter_set=_FILTER)
        self.write(project_data)
Example #25
0
 def get(self, *args, **kwargs):
     if 'user' not in kwargs:
         self.raise401()
     user = kwargs['user']
     repo_type = None
     repo_query = None
     repo_contents = None
     repo_branches = None
     repo_tags = None
     repo_info = None
     limit = self.get_argument('limit', None)
     start = self.get_argument('start', None)
     try:
         limit = int(limit)
     except:
         limit = None
     if args:
         # author = self.get_argument('author', None)
         path = parse_path(args[0])
         if not path:
             self.raise404()
         repo = Repo.objects(owner=user, name=path[0]).first()
         if repo:
             scm_repo = GitRepo(repo.path)
             repo_info = scm_repo.get_info()
             repo_branches, repo_tags = get_repo_branches_tags(scm_repo)
             repo_type, repo_query, repo_contents = get_repo_contents(
                 scm_repo, path[1:], limit=limit, start=start)
         if not repo_contents:
             self.raise404()
         repo_data = document_to_json(repo, filter_set=_FILTER)
     else:
         team_name = self.get_argument('team_name', None)
         try:
             start = int(start)
         except:
             start = None
         try:
             team_name = parse_path(team_name)[0]
         except IndexError:
             team_name = None
         if team_name:
             team = Team.objects(name=team_name).first()
             if not team:
                 self.raise404()
             if user not in team.member:
                 self.raise403()
             repos = Repo.objects(team=team)
         else:
             repos = Repo.objects(owner=user)
         if limit and start:
             repos = repos[start: start+limit]
         elif limit:
             repos = repos[:limit]
         elif start:
             repos = repos[start:]
         repo_data = query_to_json(repos, filter_set=_FILTER)
     if repo_type and repo_contents:
         repo_data['repo_info'] = repo_info
         repo_data['repo_type'] = repo_type
         repo_data['repo_query'] = repo_query
         repo_data['repo_branches'] = repo_branches
         repo_data['repo_tags'] = repo_tags
         repo_data['repo_contents'] = repo_contents
     self.write(repo_data)
Example #26
0
    def post(self, *args, **kwargs):
        """Create a new project by providing the details about the project.
        If the validation and saving are successful, it response with the
        project data and 201 status.
        Otherwise, it gives 4XX status and error messages.
        """
        name = self.get_argument('name', None)
        description = self.get_argument('description', None)
        url = self.get_argument('url', None)
        leader = self.get_argument('leader', None)
        members = self.get_argument('members', None)
        teams = self.get_argument('teams', None)
        repos = self.get_argument('repos', None)
        tags = self.get_argument('tags', None)
        if 'user' not in kwargs:
            self.raise401()

        try:
            # todo - better arguments handler
            url = url.strip()
            url = url if url else None
            members_list = []
            repos_list = []
            teams_list = []
            project_leader = kwargs['user']
            if leader:
                project_leader = User.objects(username=leader).first()

            if repos:
                for repo in parse_listed_strs(repos):
                    r = Repo.objects(name=repo).first()
                    if not r:
                        continue
                    repos_list.append(r)
            if members:
                for member in parse_listed_strs(members):
                    u = User.objects(username=member).first()
                    if not u or u == project_leader:
                        continue
                    members_list.append(u)
            if teams:
                for team in parse_listed_strs(teams):
                    t = Team.objects(name=team).first()
                    if not t:
                        continue
                    teams_list.append(t)
            members_list.append(project_leader)
            tags_list = parse_listed_strs(tags)
            project = Project(name=name,
                              description=description,
                              url=url,
                              repos=repos_list,
                              leader=project_leader,
                              members=members_list,
                              teams=teams_list,
                              tags=tags_list)
            project.save()
            project_data = document_to_json(project, filter_set=_FILTER)
            self.set_status(201)
            self.write(project_data)
        except Exception as e:
            reason = e.message
            self.raise400(reason=reason)