def post(self, request, repo_id, format=None): """Post a comments of a file. """ path = request.GET.get('p', '/').rstrip('/') if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE try: obj_id = seafile_api.get_file_id_by_path(repo_id, path) except SearpcError as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') if not obj_id: return api_error(status.HTTP_404_NOT_FOUND, 'File not found.') comment = request.data.get('comment', '') if not comment: return api_error(status.HTTP_400_BAD_REQUEST, 'Comment can not be empty.') username = request.user.username o = FileComment.objects.add_by_file_path( repo_id=repo_id, file_path=path, author=username, comment=comment) comment = o.to_dict() comment.update(user_to_dict(request.user.username, request=request, avatar_size=avatar_size)) return Response(comment, status=201)
def post(self, request, repo_id, format=None): """Post a comments of a file. """ # argument check path = request.GET.get('p', '/').rstrip('/') if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') comment = request.data.get('comment', '') if not comment: return api_error(status.HTTP_400_BAD_REQUEST, 'Comment can not be empty.') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE # resource check try: file_id = seafile_api.get_file_id_by_path(repo_id, path) except SearpcError as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') if not file_id: return api_error(status.HTTP_404_NOT_FOUND, 'File not found.') # permission check if check_folder_permission(request, repo_id, '/') is None: return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') detail = request.data.get('detail', '') username = request.user.username file_comment = FileComment.objects.add_by_file_path( repo_id=repo_id, file_path=path, author=username, comment=comment, detail=detail) repo = seafile_api.get_repo(repo_id) repo_owner = get_repo_owner(request, repo.id) if is_draft_file(repo_id, path): draft = Draft.objects.get(origin_repo_id=repo_id, draft_file_path=path) comment_draft_successful.send(sender=None, draft=draft, comment=comment, author=username) else: comment_file_successful.send(sender=None, repo=repo, repo_owner=repo_owner, file_path=path, comment=comment, author=username) comment = file_comment.to_dict() comment.update(user_to_dict(username, request=request, avatar_size=avatar_size)) return Response(comment, status=201)
def get(self, request, repo_id, format=None): """List all comments of a file. """ path = request.GET.get('p', '/').rstrip('/') if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') resolved = request.GET.get('resolved', None) if resolved not in ('true', 'false', None): error_msg = 'resolved invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # permission check if check_folder_permission(request, repo_id, '/') is None: return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) page = int(request.GET.get('page', '1')) per_page = int(request.GET.get('per_page', '25')) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE page = 1 per_page = 25 start = (page - 1) * per_page end = page * per_page total_count = FileComment.objects.get_by_file_path(repo_id, path).count() comments = [] if resolved is None: file_comments = FileComment.objects.get_by_file_path(repo_id, path)[start: end] else: comment_resolved = to_python_boolean(resolved) file_comments = FileComment.objects.get_by_file_path(repo_id, path).filter(resolved=comment_resolved)[start: end] for file_comment in file_comments: comment = file_comment.to_dict() comment.update(user_to_dict(file_comment.author, request=request, avatar_size=avatar_size)) comments.append(comment) result = {'comments': comments, 'total_count': total_count} resp = Response(result) base_url = reverse('api2-file-comments', args=[repo_id]) links_header = generate_links_header_for_paginator(base_url, page, per_page, total_count) resp['Links'] = links_header return resp
def get(self, request, repo_id, pk, format=None): """Get a comment. """ try: o = FileComment.objects.get(pk=pk) except FileComment.DoesNotExist: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong comment id') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE comment = o.to_dict() comment.update(user_to_dict(o.author, request=request, avatar_size=avatar_size)) return Response(comment)
def get(self, request, pk, format=None): try: d = Draft.objects.get(pk=pk) except Draft.DoesNotExist: return api_error(status.HTTP_404_NOT_FOUND, 'Draft %s not found' % pk) # format user result try: avatar_size = int(request.GET.get('avatar_size', 32)) except ValueError: avatar_size = 32 # get reviewer list reviewers = [] for x in d.draftreviewer_set.all(): reviewer = user_to_dict(x.reviewer, request=request, avatar_size=avatar_size) reviewers.append(reviewer) return Response({'reviewers': reviewers})
def draft(request, pk): d = get_object_or_404(Draft, pk=pk) # check perm uuid = FileUUIDMap.objects.get_fileuuidmap_by_uuid(d.origin_file_uuid) origin_repo_id = d.origin_repo_id permission = check_folder_permission(request, origin_repo_id, '/') if not permission: return render_permission_error(request, _(u'Permission denied.')) origin_file_path = posixpath.join(uuid.parent_path, uuid.filename) origin_file = seafile_api.get_file_id_by_path(origin_repo_id, origin_file_path) origin_file_exists = True if not origin_file: origin_file_exists = False draft_file = seafile_api.get_file_id_by_path(origin_repo_id, d.draft_file_path) draft_file_exists = True if not draft_file: draft_file_exists = False draft_file_name = os.path.basename(d.draft_file_path) author_info = user_to_dict(d.username, avatar_size=32) return render(request, "draft.html", { "draft_id": d.id, "draft_repo_id": origin_repo_id, "draft_origin_file_path": origin_file_path, "draft_file_path": d.draft_file_path, "draft_file_name": draft_file_name, "permission": permission, "author": author_info['user_name'], "author_avatar_url": author_info['avatar_url'], "origin_file_exists": origin_file_exists, "draft_file_exists": draft_file_exists, "draft_status": d.status, "publish_file_version": d.publish_file_version, "origin_file_version": d.origin_file_version })
def get(self, request, repo_id, format=None): """List all comments of a file. """ path = request.GET.get('p', '/').rstrip('/') if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') try: avatar_size = int( request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) page = int(request.GET.get('page', '1')) per_page = int(request.GET.get('per_page', '25')) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE page = 1 per_page = 25 start = (page - 1) * per_page end = page * per_page total_count = FileComment.objects.get_by_file_path(repo_id, path).count() comments = [] for o in FileComment.objects.get_by_file_path(repo_id, path)[start:end]: comment = o.to_dict() comment.update( user_to_dict(o.author, request=request, avatar_size=avatar_size)) comments.append(comment) result = {'comments': comments, 'total_count': total_count} resp = Response(result) base_url = reverse('api2-file-comments', args=[repo_id]) links_header = generate_links_header_for_paginator( base_url, page, per_page, total_count) resp['Links'] = links_header return resp
def get(self, request, repo_id, comment_id, format=None): """Get a comment. """ try: file_comment = FileComment.objects.get(pk=comment_id) except FileComment.DoesNotExist: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong comment id') # permission check if check_folder_permission(request, repo_id, '/') is None: return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE comment = file_comment.to_dict() comment.update(user_to_dict(file_comment.author, request=request, avatar_size=avatar_size)) return Response(comment)
def get(self, request, pk, format=None): try: r = DraftReview.objects.get(pk=pk) except DraftReview.DoesNotExist: return api_error(status.HTTP_404_NOT_FOUND, 'Review %s not found' % pk) # format user result try: avatar_size = int(request.GET.get('avatar_size', 32)) except ValueError: avatar_size = 32 # get reviewer list reviewers = [] for x in r.reviewreviewer_set.all(): reviewer = user_to_dict(x.reviewer, request=request, avatar_size=avatar_size) reviewers.append(reviewer) return Response({'reviewers': reviewers})
def get(self, request, repo_id, format=None): """List all comments of a file. """ path = request.GET.get('p', '/').rstrip('/') if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE comments = [] for o in FileComment.objects.get_by_file_path(repo_id, path): comment = o.to_dict() comment.update(user_to_dict(o.author, request=request, avatar_size=avatar_size)) comments.append(comment) return Response({ "comments": comments, })
def post(self, request, review_id, format=None): """Post a comments of a review. """ # resource check try: r = DraftReview.objects.get(pk=review_id) except DraftReview.DoesNotExist: return api_error(status.HTTP_404_NOT_FOUND, 'Review %s not found' % review_id) # permission check if check_folder_permission(request, r.origin_repo_id, '/') is None: return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') # argument check comment = request.data.get('comment', '') if not comment: return api_error(status.HTTP_400_BAD_REQUEST, 'Comment can not be empty.') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE detail = request.data.get('detail', '') username = request.user.username review_comment = ReviewComment.objects.add(comment, detail, username, r) # Send notification to review creator comment_review_successful.send(sender=None, review=r, comment=comment, author=username) comment = review_comment.to_dict() comment.update(user_to_dict(username, request=request, avatar_size=avatar_size)) return Response(comment)
def get(self, request, repo_id, comment_id, format=None): """Get a comment. """ try: file_comment = FileComment.objects.get(pk=comment_id) except FileComment.DoesNotExist: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong comment id') # permission check if check_folder_permission(request, repo_id, '/') is None: return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') try: avatar_size = int( request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE comment = file_comment.to_dict() comment.update( user_to_dict(file_comment.author, request=request, avatar_size=avatar_size)) return Response(comment)
def review(request, pk): d_r = get_object_or_404(DraftReview, pk=pk) # check perm uuid = d_r.origin_file_uuid file_path = posixpath.join(uuid.parent_path, uuid.filename) origin_repo_id = d_r.origin_repo_id if request.user.username: permission = check_folder_permission(request, origin_repo_id, file_path) if permission is None: return render_permission_error(request, _(u'Permission denied.')) draft_file_name = os.path.basename(d_r.draft_file_path) user_info = user_to_dict(d_r.creator, avatar_size=32) return render( request, "draft_review.html", { "draft_id": d_r.draft_id_id, "review_id": pk, "draft_repo_id": d_r.origin_repo_id, "draft_origin_repo_id": d_r.origin_repo_id, "draft_origin_file_path": file_path, "draft_file_path": d_r.draft_file_path, "draft_file_name": draft_file_name, "origin_file_version": d_r.origin_file_version, "publish_file_version": d_r.publish_file_version, "status": d_r.status, "permission": permission, "author": user_info['user_name'], 'author_avatar_url': user_info['avatar_url'] })
def put(self, request, repo_id, comment_id, format=None): """Update a comment, only comment author or repo owner can perform this op 1.Change resolved of comment 2.Add comment_detail """ # argument check resolved = request.data.get('resolved') if resolved not in ('true', 'false', None): error_msg = 'resolved invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) detail = request.data.get('detail') # resource check try: file_comment = FileComment.objects.get(pk=comment_id) except FileComment.DoesNotExist: error_msg = 'FileComment %s not found.' % comment_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check if check_folder_permission(request, repo_id, '/') != PERMISSION_READ_WRITE: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) if resolved is not None: comment_resolved = to_python_boolean(resolved) try: file_comment.resolved = comment_resolved file_comment.save() except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') if detail is not None: try: file_comment.detail = detail file_comment.save() except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') comment = request.data.get('comment') if comment is not None: try: file_comment.comment = comment file_comment.save() except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') try: avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE comment = file_comment.to_dict() comment.update(user_to_dict(file_comment.author, request=request, avatar_size=avatar_size)) return Response(comment)
def put(self, request, repo_id, comment_id, format=None): """Update a comment, only comment author or repo owner can perform this op 1.Change resolved of comment 2.Add comment_detail """ # argument check resolved = request.data.get('resolved') if resolved not in ('true', 'false', None): error_msg = 'resolved invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) detail = request.data.get('detail') # resource check try: file_comment = FileComment.objects.get(pk=comment_id) except FileComment.DoesNotExist: error_msg = 'FileComment %s not found.' % comment_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check if check_folder_permission(request, repo_id, '/') != PERMISSION_READ_WRITE: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) if resolved is not None: comment_resolved = to_python_boolean(resolved) try: file_comment.resolved = comment_resolved file_comment.save() except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') if detail is not None: try: file_comment.detail = detail file_comment.save() except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') comment = request.data.get('comment') if comment is not None: try: file_comment.comment = comment file_comment.save() except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') try: avatar_size = int( request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE comment = file_comment.to_dict() comment.update( user_to_dict(file_comment.author, request=request, avatar_size=avatar_size)) return Response(comment)
def put(self, request, review_id, comment_id, format=None): """Update a comment, only comment author or review creator can perform this op 1.Change resolved of comment 2.Add comment_detail """ # argument check resolved = request.data.get('resolved') if resolved not in ('true', 'false', None): error_msg = 'resolved invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) detail = request.data.get('detail', '') # resource check try: r = DraftReview.objects.get(pk=review_id) except DraftReview.DoesNotExist: return api_error(status.HTTP_404_NOT_FOUND, 'Review %s not found' % review_id) try: review_comment = ReviewComment.objects.get(pk=comment_id) except ReviewComment.DoesNotExist: error_msg = 'Review comment %s not found.' % comment_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check if check_folder_permission(request, r.origin_repo_id, '/') is None: return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') if resolved is not None: comment_resolved = to_python_boolean(resolved) try: review_comment.resolved = comment_resolved review_comment.save() except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') if detail is not None: try: review_comment.detail = detail review_comment.save() except Exception as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal error.') try: avatar_size = int( request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE username = request.user.username comment = review_comment.to_dict() comment.update( user_to_dict(username, request=request, avatar_size=avatar_size)) return Response(comment)
def post(self, request, repo_id, format=None): """Post a comments of a file. """ # argument check path = request.GET.get('p', '/').rstrip('/') if not path: return api_error(status.HTTP_400_BAD_REQUEST, 'Wrong path.') comment = request.data.get('comment', '') if not comment: return api_error(status.HTTP_400_BAD_REQUEST, 'Comment can not be empty.') try: avatar_size = int( request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE)) except ValueError: avatar_size = AVATAR_DEFAULT_SIZE # resource check try: file_id = seafile_api.get_file_id_by_path(repo_id, path) except SearpcError as e: logger.error(e) return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, 'Internal Server Error') if not file_id: return api_error(status.HTTP_404_NOT_FOUND, 'File not found.') # permission check if check_folder_permission(request, repo_id, '/') is None: return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') detail = request.data.get('detail', '') username = request.user.username file_comment = FileComment.objects.add_by_file_path(repo_id=repo_id, file_path=path, author=username, comment=comment, detail=detail) repo = seafile_api.get_repo(repo_id) repo_owner = get_repo_owner(request, repo.id) if is_draft_file(repo_id, path): draft = Draft.objects.filter(origin_repo_id=repo_id, draft_file_path=path) if draft: draft = draft[0] comment_draft_successful.send(sender=None, draft=draft, comment=comment, author=username) else: Draft.DoesNotExist else: comment_file_successful.send(sender=None, repo=repo, repo_owner=repo_owner, file_path=path, comment=comment, author=username) comment = file_comment.to_dict() comment.update( user_to_dict(username, request=request, avatar_size=avatar_size)) return Response(comment, status=201)