Beispiel #1
0
def add_comment(request, project_id, issue_id):
    form = CommentForm(**json.loads(request.body))
    if not form.validate():
        raise Http400
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and request.user.key().id() not in project.member_ids:
        raise Http403
    issue = IssueModel.get_by_id(long(issue_id))
    if issue is None:
        raise Http404
    if issue.project.key() != project.key():
        raise Http403

    comment = CommentModel(
        comment=bleach.clean(
            form.comment.data,
            tags=utils.get_bleach_allow_tags(),
            attributes=utils.get_bleach_allow_attributes(),
            styles=utils.get_bleach_allow_styles(),
        ),
        author=request.user,
        issue=issue,
    )
    comment.put()
    return JsonResponse(comment)
Beispiel #2
0
def add_issue(request, project_id):
    form = IssueForm(**json.loads(request.body))
    if not form.validate():
        raise Http400

    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and request.user.key().id() not in project.member_ids:
        raise Http403

    issue = IssueModel(
        title=form.title.data,
        floor=form.floor.data,
        content=bleach.clean(
            form.content.data,
            tags=utils.get_bleach_allow_tags(),
            attributes=utils.get_bleach_allow_attributes(),
            styles=utils.get_bleach_allow_styles(),
        ),
        label_ids=form.label_ids.data,
        author=request.user,
        project=project,
    )
    issue.put()
    return JsonResponse(issue)
Beispiel #3
0
def delete_project(request, project_id):
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and\
                    request.user.key().id() not in project.root_ids:
        raise Http403
    project.delete()
    return HttpResponse()
Beispiel #4
0
def get_labels(request, project_id):
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and\
                    request.user.key().id() not in project.member_ids:
        raise Http403
    labels = LabelModel.all().filter('project =', project.key()).order('title').fetch(100)
    return JsonResponse([x.dict() for x in labels])
Beispiel #5
0
def get_project(request, project_id):
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404('not found the project %s' % project_id)
    if request.user.permission != UserPermission.root and\
                    request.user.key().id() not in project.member_ids:
        raise Http403
    labels = LabelModel.all().filter('project =', project.key()).order('title').fetch(100)

    result = project.dict()
    result['members'] = [x.dict() for x in UserModel.get_by_id(project.member_ids) if not x is None]
    result['labels'] = [x.dict() for x in labels]
    return JsonResponse(result)
Beispiel #6
0
def get_comments(request, project_id, issue_id):
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and request.user.key().id() not in project.member_ids:
        raise Http403
    issue = IssueModel.get_by_id(long(issue_id))
    if issue is None:
        raise Http404
    if issue.project.key() != project.key():
        raise Http403

    comments = CommentModel.all().filter("issue =", issue.key()).order("create_time").fetch(1000)
    return JsonResponse([x.dict() for x in comments])
Beispiel #7
0
def add_project_member(request, project_id):
    form = UserForm(name='invite', **json.loads(request.body))
    if not form.validate():
        raise Http400
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and\
                    request.user.key().id() not in project.root_ids:
        raise Http403
    user = UserModel.invite_user(request, form.email.data)
    project.member_ids.append(user.key().id())
    project.put()
    return JsonResponse(user)
Beispiel #8
0
def update_label(request, project_id, label_id):
    form = LabelForm(**json.loads(request.body))
    if not form.validate():
        raise Http400
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and\
                    request.user.key().id() not in project.root_ids:
        raise Http403
    label = LabelModel.get_by_id(long(label_id))
    label.title = form.title.data
    label.put()
    return JsonResponse(label)
Beispiel #9
0
def get_issues(request, project_id):
    request_dict = request.GET.dict()
    if "label_ids" in request_dict:
        del request_dict["label_ids"]
    form = IssueSearchForm(label_ids=request.GET.getlist("label_ids"), **request_dict)
    project_id = long(project_id)
    project = ProjectModel.get_by_id(project_id)
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and request.user.key().id() not in project.member_ids:
        raise Http403

    if form.keyword.data:
        # search by keyword
        issues, total = IssueModel.search(
            project_id=project_id,
            keyword=form.keyword.data,
            floor_lowest=None if form.floor_lowest.data == 0 else form.floor_lowest.data,
            floor_highest=None if form.floor_highest.data == 0 else form.floor_highest.data,
            label_ids=form.label_ids.data,
            index=form.index.data,
            size=utils.default_page_size,
        )
    else:
        query = IssueModel.all().filter("project =", project.key())
        # is_close filter
        if form.status.data == "open":
            query = query.filter("is_close =", False)
        elif form.status.data == "closed":
            query = query.filter("is_close =", True)

        # floor filter
        if form.floor_lowest.data != 0:
            query = query.filter("floor >=", form.floor_lowest.data)
        if form.floor_highest.data != 0:
            query = query.filter("floor <=", form.floor_highest.data)

        # label filter
        form.label_ids.data = [long(x) for x in form.label_ids.data if x]
        for label_id in form.label_ids.data:
            query = query.filter("label_ids in", [label_id])

        # order
        if form.floor_lowest.data | form.floor_highest.data == 0:
            query = query.order("-create_time")
        else:
            query = query.order("floor").order("-create_time")
        total = query.count()
        issues = query.fetch(utils.default_page_size, form.index.data * utils.default_page_size)
    return JsonResponse(PageList(form.index.data, utils.default_page_size, total, issues))
Beispiel #10
0
def update_project(request, project_id):
    form = ProjectForm(**json.loads(request.body))
    if not form.validate():
        raise Http400

    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and\
                    request.user.key().id() not in project.root_ids:
        raise Http403
    project.title = form.title.data
    project.description = form.description.data
    project.member_ids = form.member_ids.data
    project.root_ids = form.root_ids.data
    project.floor_lowest = form.floor_lowest.data
    project.floor_highest = form.floor_highest.data
    project.room_options = form.room_options.data
    project.put()
    return JsonResponse(project)
Beispiel #11
0
def update_issue(request, project_id, issue_id):
    form = IssueForm(**json.loads(request.body))
    if not form.validate():
        raise Http400
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    if request.user.permission != UserPermission.root and request.user.key().id() not in project.member_ids:
        raise Http403
    issue = IssueModel.get_by_id(long(issue_id))
    if issue is None:
        raise Http404
    if issue.project.key() != project.key():
        raise Http404

    if issue.is_close != form.is_close.data:
        # close/reopen the issue
        issue.is_close = form.is_close.data
        issue.put()
    else:
        # update the issue
        if (
            request.user.permission != UserPermission.root
            and not request.user.key().id() in project.root_ids
            and request.user.key() != issue.author.key()
        ):
            raise Http403
        issue.title = form.title.data
        issue.content = bleach.clean(
            form.content.data,
            tags=utils.get_bleach_allow_tags(),
            attributes=utils.get_bleach_allow_attributes(),
            styles=utils.get_bleach_allow_styles(),
        )
        issue.floor = form.floor.data
        issue.label_ids = form.label_ids.data
        issue.put()
    return JsonResponse(issue)
Beispiel #12
0
def count_issues(request, project_id):
    request_dict = request.GET.dict()
    if "label_ids" in request_dict:
        del request_dict["label_ids"]
    form = IssueSearchForm(label_ids=request.GET.getlist("label_ids"), **request_dict)
    project = ProjectModel.get_by_id(long(project_id))
    if project is None:
        raise Http404
    labels = LabelModel.all().filter("project =", project.key()).fetch(100)

    def append_floor_query(query):
        # floor filter
        if form.floor_lowest.data != 0:
            query = query.filter("floor >=", form.floor_lowest.data)
        if form.floor_highest.data != 0:
            query = query.filter("floor <=", form.floor_highest.data)
        return query

    def append_label_query(query):
        # label filter
        form.label_ids.data = [long(x) for x in form.label_ids.data if x]
        for label_id in form.label_ids.data:
            query = query.filter("label_ids in", [label_id])
        return query

    def append_close_query(query):
        # is_close filter
        if form.status.data == "open":
            query = query.filter("is_close =", False)
        elif form.status.data == "closed":
            query = query.filter("is_close =", True)
        return query

    def append_order_query(query):
        # order
        if form.floor_lowest.data | form.floor_highest.data == 0:
            query = query.order("-create_time")
        else:
            query = query.order("floor").order("-create_time")
        return query

    query = IssueModel.all().filter("project =", project.key())
    query = append_floor_query(query)
    query = append_label_query(query)
    query = query.filter("is_close =", True)
    query = append_order_query(query)
    count_closed = query.count()

    query = IssueModel.all().filter("project =", project.key())
    query = append_floor_query(query)
    query = append_label_query(query)
    query = query.filter("is_close =", False)
    query = append_order_query(query)
    count_open = query.count()

    count_labels = {}
    for label in labels:
        query = IssueModel.all().filter("project =", project.key())
        query = append_floor_query(query)
        query = append_close_query(query)
        query = query.filter("label_ids in", [label.key().id()])
        query = append_order_query(query)
        count_labels[str(label.key().id())] = query.count()

    result = {"all": count_open + count_closed, "open": count_open, "closed": count_closed, "labels": count_labels}
    return JsonResponse(result)