From da498f746df98841b2f7a7f84ff42a6be8e88be1 Mon Sep 17 00:00:00 2001 From: zhengxie Date: Tue, 9 Sep 2014 15:21:30 +0800 Subject: [PATCH] Fix api bug and refactor repo code Conflicts: seahub/urls.py seahub/views/__init__.py --- seahub/api2/views.py | 4 +- seahub/urls.py | 3 +- seahub/views/__init__.py | 129 +------------------------------------- seahub/views/repo.py | 132 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 135 insertions(+), 133 deletions(-) diff --git a/seahub/api2/views.py b/seahub/api2/views.py index 2ee6630603..d3b50d3205 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -2582,6 +2582,7 @@ class Groups(APIView): """ result = {} content_type = 'application/json; charset=utf-8' + username = request.user.username if not request.user.permissions.can_add_group(): return api_error(status.HTTP_403_FORBIDDEN, @@ -2590,7 +2591,6 @@ class Groups(APIView): # check plan num_of_groups = getattr(request.user, 'num_of_groups', -1) if num_of_groups > 0: - username = request.user.username current_groups = len(get_personal_groups_by_user(username)) if current_groups > num_of_groups: result['error'] = 'You can only create %d groups.' % num_of_groups @@ -2613,7 +2613,7 @@ class Groups(APIView): # Group name is valid, create that group. try: group_id = ccnet_threaded_rpc.create_group(group_name.encode('utf-8'), - request.user.username) + username) return HttpResponse(json.dumps({'success': True, 'group_id': group_id}), content_type=content_type) except SearpcError, e: diff --git a/seahub/urls.py b/seahub/urls.py index 13e8c2c029..4e03d30c4a 100644 --- a/seahub/urls.py +++ b/seahub/urls.py @@ -7,7 +7,8 @@ from seahub.views import * from seahub.views.file import view_file, view_history_file, view_trash_file,\ view_snapshot_file, file_edit, view_shared_file, view_file_via_shared_dir,\ text_diff, view_priv_shared_file, view_raw_file, view_raw_shared_file -from seahub.views.repo import repo, repo_history_view +from seahub.views.repo import repo, repo_history_view, view_shared_dir, \ + view_shared_upload_link from notifications.views import notification_list from group.views import group_list from message.views import user_msg_list, user_msg_remove, user_received_msg_remove diff --git a/seahub/views/__init__.py b/seahub/views/__init__.py index d19b76d044..35210aa4a5 100644 --- a/seahub/views/__init__.py +++ b/seahub/views/__init__.py @@ -12,7 +12,6 @@ import posixpath from django.core.cache import cache from django.core.urlresolvers import reverse from django.contrib import messages -from django.db.models import F from django.http import HttpResponse, HttpResponseBadRequest, Http404, \ HttpResponseRedirect from django.shortcuts import render_to_response, redirect @@ -27,7 +26,7 @@ from seaserv import get_repo, get_commits, is_valid_filename, \ seafserv_threaded_rpc, seafserv_rpc, is_repo_owner, check_permission, \ is_passwd_set, get_file_size, edit_repo, \ get_session_info, set_repo_history_limit, get_commit, \ - MAX_DOWNLOAD_DIR_SIZE, send_message, MAX_UPLOAD_FILE_SIZE + MAX_DOWNLOAD_DIR_SIZE, send_message from seaserv import seafile_api from pysearpc import SearpcError @@ -42,8 +41,7 @@ from seahub.contacts.models import Contact from seahub.options.models import UserOptions, CryptoOptionNotSetError from seahub.profile.models import Profile from seahub.share.models import FileShare, PrivateFileDirShare, \ - UploadLinkShare, check_share_link_access, set_share_link_access -from seahub.share.forms import SharedLinkPasswordForm + UploadLinkShare from seahub.forms import RepoPassowrdForm, RepoSettingForm from seahub.utils import render_permission_error, render_error, list_to_string, \ get_fileserver_root, gen_shared_upload_link, \ @@ -1361,129 +1359,6 @@ def file_revisions(request, repo_id): except Exception, e: return render_error(request, str(e)) -def view_shared_dir(request, token): - assert token is not None # Checked by URLconf - - fileshare = FileShare.objects.get_valid_dir_link_by_token(token) - if fileshare is None: - raise Http404 - - if fileshare.is_encrypted(): - if not check_share_link_access(request.user.username, token): - d = {'token': token, 'view_name': 'view_shared_dir', } - if request.method == 'POST': - post_values = request.POST.copy() - post_values['enc_password'] = fileshare.password - form = SharedLinkPasswordForm(post_values) - d['form'] = form - if form.is_valid(): - # set cache for non-anonymous user - if request.user.is_authenticated(): - set_share_link_access(request.user.username, token) - else: - return render_to_response('share_access_validation.html', d, - context_instance=RequestContext(request)) - else: - return render_to_response('share_access_validation.html', d, - context_instance=RequestContext(request)) - - username = fileshare.username - repo_id = fileshare.repo_id - path = request.GET.get('p', '') - path = fileshare.path if not path else path - if path[-1] != '/': # Normalize dir path - path += '/' - - if not path.startswith(fileshare.path): - path = fileshare.path # Can not view upper dir of shared dir - - repo = get_repo(repo_id) - if not repo: - raise Http404 - - dir_name = os.path.basename(path[:-1]) - current_commit = get_commits(repo_id, 0, 1)[0] - file_list, dir_list = get_repo_dirents(request, repo, current_commit, - path) - zipped = gen_path_link(path, '') - - if path == fileshare.path: # When user view the shared dir.. - # increase shared link view_cnt, - fileshare = FileShare.objects.get(token=token) - fileshare.view_cnt = F('view_cnt') + 1 - fileshare.save() - - traffic_over_limit = user_traffic_over_limit(fileshare.username) - - return render_to_response('view_shared_dir.html', { - 'repo': repo, - 'token': token, - 'path': path, - 'username': username, - 'dir_name': dir_name, - 'file_list': file_list, - 'dir_list': dir_list, - 'zipped': zipped, - 'traffic_over_limit': traffic_over_limit, - }, context_instance=RequestContext(request)) - -def view_shared_upload_link(request, token): - assert token is not None # Checked by URLconf - - uploadlink = UploadLinkShare.objects.get_valid_upload_link_by_token(token) - if uploadlink is None: - raise Http404 - - if uploadlink.is_encrypted(): - if not check_share_link_access(request.user.username, token): - d = {'token': token, 'view_name': 'view_shared_upload_link', } - if request.method == 'POST': - post_values = request.POST.copy() - post_values['enc_password'] = uploadlink.password - form = SharedLinkPasswordForm(post_values) - d['form'] = form - if form.is_valid(): - # set cache for non-anonymous user - if request.user.is_authenticated(): - set_share_link_access(request.user.username, token) - else: - return render_to_response('share_access_validation.html', d, - context_instance=RequestContext(request)) - else: - return render_to_response('share_access_validation.html', d, - context_instance=RequestContext(request)) - - username = uploadlink.username - repo_id = uploadlink.repo_id - path = uploadlink.path - dir_name = os.path.basename(path[:-1]) - - repo = get_repo(repo_id) - if not repo: - raise Http404 - - uploadlink.view_cnt = F('view_cnt') + 1 - uploadlink.save() - - max_upload_file_size = MAX_UPLOAD_FILE_SIZE - no_quota = True if seaserv.check_quota(repo_id) < 0 else False - - token = seafile_api.get_fileserver_access_token(repo_id, 'dummy', - 'upload', request.user.username) - ajax_upload_url = gen_file_upload_url(token, 'upload-api').replace('api', 'aj') - - return render_to_response('view_shared_upload_link.html', { - 'repo': repo, - 'token': token, - 'uploadlink': uploadlink, - 'path': path, - 'username': username, - 'dir_name': dir_name, - 'max_upload_file_size': max_upload_file_size, - 'no_quota': no_quota, - 'ajax_upload_url': ajax_upload_url - }, context_instance=RequestContext(request)) - def demo(request): """ Login as demo account. diff --git a/seahub/views/repo.py b/seahub/views/repo.py index 2c1005244e..dbf0cf6934 100644 --- a/seahub/views/repo.py +++ b/seahub/views/repo.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- +import os import logging from django.core.urlresolvers import reverse from django.contrib.sites.models import RequestSite +from django.db.models import F from django.http import Http404, HttpResponseRedirect from django.shortcuts import render_to_response from django.template import RequestContext @@ -17,14 +19,16 @@ from seahub.avatar.templatetags.group_avatar_tags import grp_avatar from seahub.contacts.models import Contact from seahub.forms import RepoPassowrdForm from seahub.options.models import UserOptions, CryptoOptionNotSetError -from seahub.share.models import FileShare, UploadLinkShare +from seahub.share.models import FileShare, UploadLinkShare, \ + check_share_link_access, set_share_link_access +from seahub.share.forms import SharedLinkPasswordForm from seahub.views import gen_path_link, get_repo_dirents, \ check_repo_access_permission from seahub.utils import gen_file_upload_url, is_org_context, \ get_fileserver_root, gen_dir_share_link, gen_shared_upload_link, \ get_max_upload_file_size, new_merge_with_no_conflict, \ - get_commit_before_new_merge + get_commit_before_new_merge, user_traffic_over_limit from seahub.settings import ENABLE_SUB_LIBRARY, FORCE_SERVER_CRYPTO # Get an instance of a logger @@ -342,4 +346,126 @@ def repo_history_view(request, repo_id): 'path': path, 'zipped': zipped, }, context_instance=RequestContext(request)) - + +########## shared dir/uploadlink +def view_shared_dir(request, token): + assert token is not None # Checked by URLconf + + fileshare = FileShare.objects.get_valid_dir_link_by_token(token) + if fileshare is None: + raise Http404 + + if fileshare.is_encrypted(): + if not check_share_link_access(request.user.username, token): + d = {'token': token, 'view_name': 'view_shared_dir', } + if request.method == 'POST': + post_values = request.POST.copy() + post_values['enc_password'] = fileshare.password + form = SharedLinkPasswordForm(post_values) + d['form'] = form + if form.is_valid(): + # set cache for non-anonymous user + if request.user.is_authenticated(): + set_share_link_access(request.user.username, token) + else: + return render_to_response('share_access_validation.html', d, + context_instance=RequestContext(request)) + else: + return render_to_response('share_access_validation.html', d, + context_instance=RequestContext(request)) + + username = fileshare.username + repo_id = fileshare.repo_id + path = request.GET.get('p', '') + path = fileshare.path if not path else path + if path[-1] != '/': # Normalize dir path + path += '/' + + if not path.startswith(fileshare.path): + path = fileshare.path # Can not view upper dir of shared dir + + repo = get_repo(repo_id) + if not repo: + raise Http404 + + dir_name = os.path.basename(path[:-1]) + current_commit = seaserv.get_commits(repo_id, 0, 1)[0] + file_list, dir_list = get_repo_dirents(request, repo, current_commit, + path) + zipped = gen_path_link(path, '') + + if path == fileshare.path: # When user view the shared dir.. + # increase shared link view_cnt, + fileshare = FileShare.objects.get(token=token) + fileshare.view_cnt = F('view_cnt') + 1 + fileshare.save() + + traffic_over_limit = user_traffic_over_limit(fileshare.username) + + return render_to_response('view_shared_dir.html', { + 'repo': repo, + 'token': token, + 'path': path, + 'username': username, + 'dir_name': dir_name, + 'file_list': file_list, + 'dir_list': dir_list, + 'zipped': zipped, + 'traffic_over_limit': traffic_over_limit, + }, context_instance=RequestContext(request)) + +def view_shared_upload_link(request, token): + assert token is not None # Checked by URLconf + + uploadlink = UploadLinkShare.objects.get_valid_upload_link_by_token(token) + if uploadlink is None: + raise Http404 + + if uploadlink.is_encrypted(): + if not check_share_link_access(request.user.username, token): + d = {'token': token, 'view_name': 'view_shared_upload_link', } + if request.method == 'POST': + post_values = request.POST.copy() + post_values['enc_password'] = uploadlink.password + form = SharedLinkPasswordForm(post_values) + d['form'] = form + if form.is_valid(): + # set cache for non-anonymous user + if request.user.is_authenticated(): + set_share_link_access(request.user.username, token) + else: + return render_to_response('share_access_validation.html', d, + context_instance=RequestContext(request)) + else: + return render_to_response('share_access_validation.html', d, + context_instance=RequestContext(request)) + + username = uploadlink.username + repo_id = uploadlink.repo_id + path = uploadlink.path + dir_name = os.path.basename(path[:-1]) + + repo = get_repo(repo_id) + if not repo: + raise Http404 + + uploadlink.view_cnt = F('view_cnt') + 1 + uploadlink.save() + + no_quota = True if seaserv.check_quota(repo_id) < 0 else False + + token = seafile_api.get_fileserver_access_token(repo_id, 'dummy', + 'upload', request.user.username) + ajax_upload_url = gen_file_upload_url(token, 'upload-aj') + + return render_to_response('view_shared_upload_link.html', { + 'repo': repo, + 'token': token, + 'path': path, + 'username': username, + 'dir_name': dir_name, + 'max_upload_file_size': seaserv.MAX_UPLOAD_FILE_SIZE, + 'no_quota': no_quota, + 'ajax_upload_url': ajax_upload_url, + 'uploadlink': uploadlink, + }, context_instance=RequestContext(request))