1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-16 15:19:06 +00:00

Refactor seahub ajax

This commit is contained in:
zhengxie
2013-07-27 17:07:44 +08:00
parent 3d73155fa8
commit ce44a242bf
6 changed files with 659 additions and 446 deletions

View File

@@ -131,7 +131,7 @@ class RepoNewDirentForm(forms.Form):
'required': _("It's required."),
})
def clean_new_dirent_name(self):
def clean_dirent_name(self):
dirent_name = self.cleaned_data['dirent_name']
try:
if not is_valid_filename(dirent_name):

View File

@@ -632,16 +632,18 @@ $('.file-star').click(function() {
status = op.data('status'),
file_name = op.parents('.file-item').data('name');
if (status == 'unstarred') {
var post_url = '{% url 'repo_star_file' repo.id %}?file=' + e(cur_path + file_name)
} else {
var post_url = '{% url 'repo_unstar_file' repo.id %}?file=' + e(cur_path + file_name)
}
$.ajax({
url: '{% url 'repo_star_file' repo.id %}?file=' + e(cur_path + file_name),
url: post_url,
type: 'POST',
cache: false,
beforeSend: prepareCSRFToken,
dataType: 'json',
data: {
status: status,
org_id: {% if org %} {{ org.org_id }} {% else %} -1 {% endif %}
},
success:function(data) {
if (data['success']) {
if (status == 'starred') {
@@ -733,7 +735,7 @@ $('#add-new-file-form, #add-new-dir-form, #rename-form, #mv-form').submit(functi
dirent_type;
if (!dirent_name) {
apply_form_error(form_id, "{% trans "It's required." %}");
apply_form_error(form_id, "{% trans "Its required." %}");
return false;
}
if (form_id == 'add-new-file-form') {
@@ -753,14 +755,19 @@ $('#add-new-file-form, #add-new-dir-form, #rename-form, #mv-form').submit(functi
reqDirData('{% url 'repo_dir_data' repo.id %}?p=' + e(path) + e(data['name']), changeLocation);
};
}
post_url = '{% url 'repo_new_dirent' repo.id %}?parent_dir=' + e(path) + '&type=' + dirent_type;
if (dirent_type == 'file') {
post_url = '{% url 'new_file' repo.id %}?parent_dir=' + e(path);
} else {
post_url = '{% url 'new_dir' repo.id %}?parent_dir=' + e(path);
}
post_data['dirent_name'] = dirent_name;
} else if (form_id == 'rename-form') {
var old_name = form.find('input[name="oldname"]').val(),
new_name = $.trim(form.find('input[name="newname"]').val()),
op_obj = form.data('op_obj');
if (!new_name) {
apply_form_error(form_id, "{% trans "It can't be blank." %}");
apply_form_error(form_id, "{% trans "It cannot be blank." %}");
return false;
}
if (new_name == old_name) {
@@ -808,11 +815,22 @@ $('#add-new-file-form, #add-new-dir-form, #rename-form, #mv-form').submit(functi
$('.error', form).html("{% trans "Invalid destination path" %}").removeClass('hide');
return false;
}
post_url = '{% url 'repo_mvcp_dirent' repo.id %}?path=' + e(path) + '&obj_name=' + e(obj_name) + '&obj_type=' + obj_type;
if (op == 'mv') {
if (obj_type == 'dir') { // move dir
post_url = '{% url 'mv_dir' repo.id%}?path=' + e(path) + '&obj_name=' + e(obj_name);
} else { // move file
post_url = '{% url 'mv_file' repo.id%}?path=' + e(path) + '&obj_name=' + e(obj_name);
}
} else {
if (obj_type == 'dir') { // copy dir
post_url = '{% url 'cp_dir' repo.id%}?path=' + e(path) + '&obj_name=' + e(obj_name);
} else { // copy file
post_url = '{% url 'cp_file' repo.id%}?path=' + e(path) + '&obj_name=' + e(obj_name);
}
}
post_data = {
'dst_repo': dst_repo,
'dst_path': dst_path,
'op': op
};
after_op_success = function(data) {
$.modal.close();

View File

@@ -15,9 +15,10 @@ from share.views import user_share_list
from seahub.views.wiki import personal_wiki, personal_wiki_pages, \
personal_wiki_create, personal_wiki_page_new, personal_wiki_page_edit, \
personal_wiki_page_delete
from seahub.views.sysadmin import sys_repo_admin, sys_user_admin, sys_group_admin, \
user_info, user_add, user_remove, user_make_admin, \
from seahub.views.sysadmin import sys_repo_admin, sys_user_admin, \
sys_group_admin, user_info, user_add, user_remove, user_make_admin, \
user_remove_admin, user_reset, user_activate, sys_publink_admin
from seahub.views.ajax import *
# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
@@ -107,12 +108,19 @@ urlpatterns = patterns('',
url(r'^ajax/repos/$', get_my_repos, name='get_my_repos'),
url(r'^ajax/contacts/$', get_contacts, name='get_contacts'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/dir_data/$', repo_dir_data, name='repo_dir_data'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/new_dirent/$', repo_new_dirent, name='repo_new_dirent'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/rename_dirent/$', repo_rename_dirent, name='repo_rename_dirent'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/delete_dirent/$', repo_delete_dirent, name='repo_delete_dirent'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/mvcp_dirent/$', repo_mvcp_dirent, name='repo_mvcp_dirent'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/dir/$', list_dir, name='repo_dir_data'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/dir/new/$', new_dir, name='new_dir'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/dir/rename/$', rename_dirent, name='repo_rename_dirent'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/dir/delete/$', delete_dirent, name='repo_delete_dirent'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/dir/mv/$', mv_dir, name='mv_dir'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/dir/cp/$', cp_dir, name='cp_dir'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/file/new/$', new_file, name='new_file'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/file/rename/$', rename_dirent, name='repo_rename_dirent'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/file/delete/$', delete_dirent, name='repo_delete_dirent'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/file/mv/$', mv_file, name='mv_file'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/file/cp/$', cp_file, name='cp_file'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/star_file/$', repo_star_file, name='repo_star_file'),
url(r'^ajax/repo/(?P<repo_id>[-0-9a-f]{36})/unstar_file/$', repo_unstar_file, name='repo_unstar_file'),
### Apps ###
(r'^api2/', include('seahub.api2.urls')),

View File

@@ -62,15 +62,15 @@ from seahub.group.signals import grpmsg_added
from seahub.notifications.models import UserNotification
from seahub.profile.models import Profile
from seahub.share.models import FileShare, PrivateFileDirShare
from seahub.forms import AddUserForm, RepoCreateForm, RepoNewDirentForm, \
RepoRenameDirentForm, RepoPassowrdForm, SharedRepoCreateForm,\
from seahub.forms import AddUserForm, RepoCreateForm, \
RepoPassowrdForm, SharedRepoCreateForm,\
SetUserQuotaForm, RepoSettingForm
from seahub.signals import repo_created, repo_deleted
from seahub.utils import render_permission_error, render_error, list_to_string, \
get_httpserver_root, get_ccnetapplet_root, \
gen_dir_share_link, gen_file_share_link, \
calculate_repo_last_modify, get_file_type_and_ext, get_user_repos, \
check_filename_with_rename, EMPTY_SHA1, normalize_file_path, \
EMPTY_SHA1, normalize_file_path, \
get_file_revision_id_size, get_ccnet_server_addr_port, \
gen_file_get_url, string2list, MAX_INT, IS_EMAIL_CONFIGURED, \
gen_file_upload_url, check_and_get_org_by_repo, \
@@ -499,72 +499,6 @@ def update_file_error(request, repo_id):
'err_msg': err_msg,
}, context_instance=RequestContext(request))
@login_required
def get_dirents(request, repo_id):
"""
Get dirents in a dir for file tree
"""
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
user_perm = get_user_permission(request, repo_id)
if not user_perm:
err_msg = _(u"You don't have permission to access the library.")
return HttpResponse(json.dumps({"err_msg": err_msg}), status=400,
content_type=content_type)
path = request.GET.get('path', '')
dir_only = request.GET.get('dir_only', '')
if not path:
err_msg = _(u"No path.")
return HttpResponse(json.dumps({"err_msg": err_msg}), status=400,
content_type=content_type)
try:
dirents = seafile_api.list_dir_by_path(repo_id, path.encode('utf-8'))
except SearpcError, e:
return HttpResponse(json.dumps({"err_msg": e.msg}), status=500,
content_type=content_type)
dirent_list = []
for dirent in dirents:
if stat.S_ISDIR(dirent.props.mode):
dirent.has_subdir = False
if dir_only:
dirent_path = os.path.join(path, dirent.obj_name)
try:
dirent_dirents = seafile_api.list_dir_by_path(repo_id, dirent_path.encode('utf-8'))
except SearpcError, e:
dirent_dirents = []
for dirent_dirent in dirent_dirents:
if stat.S_ISDIR(dirent_dirent.props.mode):
dirent.has_subdir = True
break
subdir = {
'name': dirent.obj_name,
'id': dirent.obj_id,
'type': 'dir',
'has_subdir': dirent.has_subdir, # to decide node 'state' ('closed' or not) in jstree
'repo_id': repo_id,
}
dirent_list.append(subdir)
else:
if not dir_only:
f = {
'repo_id': repo_id,
'id': dirent.obj_id,
'name': dirent.obj_name,
'type': 'file',
}
dirent_list.append(f)
return HttpResponse(json.dumps(dirent_list), content_type=content_type)
@login_required
def repo_history(request, repo_id):
"""
@@ -1171,42 +1105,6 @@ def repo_del_file(request, repo_id):
url = reverse('repo', args=[repo_id]) + ('?p=%s' % urllib2.quote(parent_dir.encode('utf-8')))
return HttpResponseRedirect(url)
@login_required
def repo_delete_dirent(request, repo_id):
'''
Delete a file/dir with ajax.
'''
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
repo = get_repo(repo_id)
if not repo:
err_msg = _(u'Library does not exist.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
if get_user_permission(request, repo_id) != 'rw':
err_msg = _(u'Permission denied.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
parent_dir = request.GET.get("parent_dir")
dirent_name = request.GET.get("name")
if not (parent_dir and dirent_name):
err_msg = _(u'Argument missing.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
try:
seafserv_threaded_rpc.del_file(repo_id, parent_dir, dirent_name, request.user.username)
return HttpResponse(json.dumps({'success': True}), content_type=content_type)
except:
err_msg = _(u'Internal error. Failed to delete %s.') % dirent_name
return HttpResponse(json.dumps({'error': err_msg}),
status=500, content_type=content_type)
def repo_access_file(request, repo_id, obj_id):
repo = get_repo(repo_id)
if not repo:
@@ -1313,90 +1211,6 @@ def repo_download(request):
return HttpResponseRedirect(download_url)
@login_required
def repo_mvcp_dirent(request, repo_id):
'''
Move/copy a file/dir in a repo, with ajax
'''
if request.method != 'POST' or not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
result = {}
repo = get_repo(repo_id)
if not repo:
result['error'] = _(u'Library does not exist.')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
if get_user_permission(request, repo_id) != 'rw':
result['error'] = _('Permission denied')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
path = request.GET.get('path')
obj_name = request.GET.get('obj_name')
obj_type = request.GET.get('obj_type') # dir or file
dst_repo_id = request.POST.get('dst_repo')
dst_path = request.POST.get('dst_path')
op = request.POST.get('op')
if not (path and obj_name and obj_type and dst_repo_id \
and dst_path and op):
result['error'] = _('Argument missing')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
# check file path
if len(dst_path+obj_name) > settings.MAX_PATH:
result['error'] = _('Destination path is too long.')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
# check whether user has write permission to dest repo
if check_permission(dst_repo_id, request.user.username) != 'rw':
result['error'] = _('Permission denied')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
# when dst is the same as src
if repo_id == dst_repo_id and (path == dst_path or obj_type == 'dir' and dst_path == os.path.join(path, obj_name) + '/'):
result['error'] = _('Invalid destination path')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
# Error when move/copy a dir to its subdir
if obj_type == 'dir':
src_dir = os.path.join(path, obj_name)
if dst_path.startswith(src_dir):
error_msg = _(u'Can not %(op)s directory %(src)s to its subdirectory %(des)s') \
% {'op': _(u"copy") if op == 'cp' else _(u"move"),
'src': src_dir,
'des': dst_path}
result['error'] = error_msg
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
new_obj_name = check_filename_with_rename(dst_repo_id, dst_path, obj_name)
try:
msg_url = reverse('repo', args=[dst_repo_id]) + u'?p=' + urllib2.quote(dst_path.encode('utf-8'))
if op == 'cp':
seafserv_threaded_rpc.copy_file(repo_id, path, obj_name,
dst_repo_id, dst_path, new_obj_name,
request.user.username)
msg = _(u'Successfully copied %(name)s<a href="%(url)s">view</a>') % \
{"name":obj_name, "url":msg_url}
elif op == 'mv':
seafserv_threaded_rpc.move_file(repo_id, path, obj_name,
dst_repo_id, dst_path, new_obj_name,
request.user.username)
msg = _(u'Successfully moved %(name)s <a href="%(url)s">view</a>') % \
{"name":obj_name, "url":msg_url}
result['msg'] = msg
result['success'] = True
return HttpResponse(json.dumps(result), content_type=content_type)
except Exception, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
@login_required
def seafile_access_check(request):
repo_id = request.GET.get('repo_id', '')
@@ -1431,164 +1245,6 @@ def file_upload_progress_page(request):
'upload_progress_con_id': upload_progress_con_id,
}, context_instance=RequestContext(request))
@login_required
def repo_dir_data(request, repo_id):
'''
Use ajax to get data of a dir
'''
if not request.is_ajax():
raise Http404
content_type='application/json; charset=utf-8'
repo = get_repo(repo_id)
if not repo:
err_msg = _(u'Library does not exist.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
username = request.user.username
user_perm = seafile_api.check_repo_access_permission(repo.id, username)
if user_perm is None:
err_msg = _(u'Permission denied.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
if repo.encrypted and not seafile_api.is_password_set(repo.id, username):
err_msg = _(u'Library is encrypted.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
head_commit = get_commit(repo.head_cmmt_id)
if not head_commit:
err_msg = _(u'Error.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
path = request.GET.get('p', '/')
if path[-1] != '/':
path = path + '/'
file_list, dir_list = get_repo_dirents(request, repo.id, head_commit, path)
zipped = gen_path_link(path, repo.name)
if path == '/': # no shared link for root dir
fileshare = None
else:
l = FileShare.objects.filter(repo_id=repo.id).filter(username=username).filter(path=path)
fileshare = l[0] if len(l) > 0 else None
if fileshare:
dir_shared_link = gen_dir_share_link(fileshare.token)
else:
dir_shared_link = ''
ctx = {
'repo': repo,
'zipped': zipped,
'user_perm': user_perm,
'path': path,
'fileshare': fileshare,
'dir_shared_link': dir_shared_link,
'dir_list': dir_list,
'file_list': file_list,
'ENABLE_SUB_LIBRARY': ENABLE_SUB_LIBRARY,
}
html = render_to_string('snippets/repo_dir_data.html', ctx, context_instance=RequestContext(request))
return HttpResponse(json.dumps({'html': html, 'path': path}), content_type=content_type)
@login_required
def repo_new_dirent(request, repo_id):
'''
Create a new file or dir with ajax.
'''
if request.method != 'POST' or not request.is_ajax():
raise Http404
result = {}
content_type = 'application/json; charset=utf-8'
repo = get_repo(repo_id)
if not repo:
result['error'] = _(u'Library does not exist.')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
if get_user_permission(request, repo_id) != 'rw':
result['error'] = _('Permission denied')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
form = RepoNewDirentForm(request.POST)
if form.is_valid():
dirent_name = form.cleaned_data["dirent_name"]
else:
result['error'] = str(form.errors.values()[0])
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
parent_dir = request.GET.get('parent_dir')
dirent_type = request.GET.get('type') # 'file' or 'dir'
if not (parent_dir and dirent_type):
result['error'] = _('Argument missing')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
dirent_name = check_filename_with_rename(repo_id, parent_dir, dirent_name)
user = request.user.username
try:
if dirent_type == 'file':
seafserv_threaded_rpc.post_empty_file(repo_id, parent_dir, dirent_name, user)
else:
seafserv_threaded_rpc.post_dir(repo_id, parent_dir, dirent_name, user)
except Exception, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
return HttpResponse(json.dumps({'success': True, 'name': dirent_name}), content_type=content_type)
@login_required
def repo_rename_dirent(request, repo_id):
'''
Rename a file/dir in a repo, with ajax
'''
if request.method != 'POST' or not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
result = {}
repo = get_repo(repo_id)
if not repo:
result['error'] = _(u'Library does not exist.')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
if get_user_permission(request, repo_id) != 'rw':
result['error'] = _('Permission denied')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
form = RepoRenameDirentForm(request.POST)
if form.is_valid():
oldname = form.cleaned_data["oldname"]
newname = form.cleaned_data["newname"]
else:
result['error'] = str(form.errors.values()[0])
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
if newname == oldname:
return HttpResponse(json.dumps({'success': True}), content_type=content_type)
parent_dir = request.GET.get('parent_dir')
if not parent_dir:
result['error'] = _('Argument missing')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
newname = check_filename_with_rename(repo_id, parent_dir, newname)
try:
seafserv_threaded_rpc.rename_file(repo_id, parent_dir, oldname, newname, request.user.username)
except Exception, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
return HttpResponse(json.dumps({'success': True}), content_type=content_type)
@login_required
def validate_filename(request):
repo_id = request.GET.get('repo_id')
@@ -2025,29 +1681,6 @@ def i18n(request):
return res
@login_required
def repo_star_file(request, repo_id):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
path = request.GET.get('file')
status = request.POST.get('status')
if not (path and status):
return HttpResponse(json.dumps({'error': _(u'Invalid arguments')}),
status=400, content_type=content_type)
org_id = int(request.POST.get('org_id'))
path = urllib2.unquote(path.encode('utf-8'))
is_dir = False
if status == 'unstarred':
star_file(request.user.username, repo_id, path, is_dir, org_id=org_id)
else:
unstar_file(request.user.username, repo_id, path)
return HttpResponse(json.dumps({'success':True}), content_type=content_type)
def repo_download_dir(request, repo_id):
repo = get_repo(repo_id)
if not repo:
@@ -2184,62 +1817,6 @@ def pdf_full_view(request):
'file_src': file_src,
}, context_instance=RequestContext(request))
@login_required
def get_group_repos(request, group_id):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
group_id_int = int(group_id)
group = get_group(group_id_int)
if not group:
err_msg = _(u"The group doesn't exist")
return HttpResponse(json.dumps({"err_msg": err_msg}), status=400, content_type=content_type)
joined = is_group_user(group_id_int, request.user.username)
if not joined and not request.user.is_staff:
err_msg = _(u"Permission denied")
return HttpResponse(json.dumps({"err_msg": err_msg}), status=400, content_type=content_type)
repos = seafile_api.get_group_repo_list(group_id_int)
repo_list = []
for repo in repos:
repo_list.append({"name": repo.props.name, "id": repo.props.id})
return HttpResponse(json.dumps(repo_list), content_type=content_type)
@login_required
def get_my_repos(request):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
repos = seafserv_threaded_rpc.list_owned_repos(request.user.username)
repo_list = []
for repo in repos:
repo_list.append({"name": repo.props.name, "id": repo.props.id})
return HttpResponse(json.dumps(repo_list), content_type=content_type)
@login_required
def get_contacts(request):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
username = request.user.username
contacts = Contact.objects.get_contacts_by_user(username)
contact_list = []
from seahub.avatar.templatetags.avatar_tags import avatar
for c in contacts:
contact_list.append({"email": c.contact_email, "avatar": avatar(c.contact_email, 16)})
return HttpResponse(json.dumps({"contacts":contact_list}), content_type=content_type)
@login_required
def convert_cmmt_desc_link(request):
"""Return user to file/directory page based on the changes in commit.

610
seahub/views/ajax.py Normal file
View File

@@ -0,0 +1,610 @@
# -*- coding: utf-8 -*-
import os
import stat
import logging
import simplejson as json
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.template import RequestContext
from django.template.loader import render_to_string
from django.utils.http import urlquote
from django.utils.translation import ugettext as _
import seaserv
from seaserv import seafile_api
from pysearpc import SearpcError
from seahub.auth.decorators import login_required
from seahub.forms import RepoNewDirentForm, RepoRenameDirentForm
from seahub.share.models import FileShare
from seahub.views import get_repo_dirents, gen_path_link
from seahub.views.repo import get_nav_path, get_fileshare, get_dir_share_link
import seahub.settings as settings
from seahub.utils import check_filename_with_rename, star_file, unstar_file
# Get an instance of a logger
logger = logging.getLogger(__name__)
########## Seafile API Wrapper
def get_repo(repo_id):
return seafile_api.get_repo(repo_id)
def get_commit(commit_id):
return seaserv.get_commit(commit_id)
def check_repo_access_permission(repo_id, username):
return seafile_api.check_repo_access_permission(repo_id, username)
########## repo related
@login_required
def get_dirents(request, repo_id):
"""
Get dirents in a dir for file tree
"""
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
username = request.user.username
# permission checking
user_perm = check_repo_access_permission(repo_id, username)
if user_perm is None:
err_msg = _(u"You don't have permission to access the library.")
return HttpResponse(json.dumps({"err_msg": err_msg}), status=403,
content_type=content_type)
path = request.GET.get('path', '')
dir_only = request.GET.get('dir_only', False)
if not path:
err_msg = _(u"No path.")
return HttpResponse(json.dumps({"err_msg": err_msg}), status=400,
content_type=content_type)
try:
dirents = seafile_api.list_dir_by_path(repo_id, path.encode('utf-8'))
except SearpcError, e:
return HttpResponse(json.dumps({"err_msg": e.msg}), status=500,
content_type=content_type)
dirent_list = []
for dirent in dirents:
if stat.S_ISDIR(dirent.mode):
dirent.has_subdir = False
if dir_only:
dirent_path = os.path.join(path, dirent.obj_name)
try:
dirent_dirents = seafile_api.list_dir_by_path(repo_id, dirent_path.encode('utf-8'))
except SearpcError, e:
dirent_dirents = []
for dirent_dirent in dirent_dirents:
if stat.S_ISDIR(dirent_dirent.props.mode):
dirent.has_subdir = True
break
subdir = {
'name': dirent.obj_name,
'id': dirent.obj_id,
'type': 'dir',
'has_subdir': dirent.has_subdir, # to decide node 'state' ('closed' or not) in jstree
'repo_id': repo_id,
}
dirent_list.append(subdir)
else:
if not dir_only:
f = {
'repo_id': repo_id,
'id': dirent.obj_id,
'name': dirent.obj_name,
'type': 'file',
}
dirent_list.append(f)
return HttpResponse(json.dumps(dirent_list), content_type=content_type)
@login_required
def get_group_repos(request, group_id):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
group_id_int = int(group_id)
group = get_group(group_id_int)
if not group:
err_msg = _(u"The group doesn't exist")
return HttpResponse(json.dumps({"err_msg": err_msg}), status=400, content_type=content_type)
joined = is_group_user(group_id_int, request.user.username)
if not joined and not request.user.is_staff:
err_msg = _(u"Permission denied")
return HttpResponse(json.dumps({"err_msg": err_msg}), status=400, content_type=content_type)
repos = seafile_api.get_group_repo_list(group_id_int)
repo_list = []
for repo in repos:
repo_list.append({"name": repo.props.name, "id": repo.props.id})
return HttpResponse(json.dumps(repo_list), content_type=content_type)
@login_required
def get_my_repos(request):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
repos = seafserv_threaded_rpc.list_owned_repos(request.user.username)
repo_list = []
for repo in repos:
repo_list.append({"name": repo.props.name, "id": repo.props.id})
return HttpResponse(json.dumps(repo_list), content_type=content_type)
@login_required
def list_dir(request, repo_id):
"""
List directory entries in AJAX.
"""
if not request.is_ajax():
raise Http404
content_type='application/json; charset=utf-8'
repo = get_repo(repo_id)
if not repo:
err_msg = _(u'Library does not exist.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
username = request.user.username
user_perm = check_repo_access_permission(repo.id, username)
if user_perm is None:
err_msg = _(u'Permission denied.')
return HttpResponse(json.dumps({'error': err_msg}),
status=403, content_type=content_type)
if repo.encrypted and not seafile_api.is_password_set(repo.id, username):
err_msg = _(u'Library is encrypted.')
return HttpResponse(json.dumps({'error': err_msg}),
status=403, content_type=content_type)
head_commit = get_commit(repo.head_cmmt_id)
if not head_commit:
err_msg = _(u'Error: no head commit id')
return HttpResponse(json.dumps({'error': err_msg}),
status=500, content_type=content_type)
path = request.GET.get('p', '/')
if path[-1] != '/':
path = path + '/'
file_list, dir_list = get_repo_dirents(request, repo.id, head_commit, path)
zipped = get_nav_path(path, repo.name)
fileshare = get_fileshare(repo.id, username, path)
dir_shared_link = get_dir_share_link(fileshare)
ctx = {
'repo': repo,
'zipped': zipped,
'user_perm': user_perm,
'path': path,
'fileshare': fileshare,
'dir_shared_link': dir_shared_link,
'dir_list': dir_list,
'file_list': file_list,
'ENABLE_SUB_LIBRARY': settings.ENABLE_SUB_LIBRARY,
}
html = render_to_string('snippets/repo_dir_data.html', ctx,
context_instance=RequestContext(request))
return HttpResponse(json.dumps({'html': html, 'path': path}),
content_type=content_type)
def new_dirent_common(func):
"""Decorator for common logic in creating directory and file.
"""
def _decorated(request, repo_id, *args, **kwargs):
if request.method != 'POST' or not request.is_ajax():
raise Http404
result = {}
content_type = 'application/json; charset=utf-8'
repo = get_repo(repo_id)
if not repo:
result['error'] = _(u'Library does not exist.')
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
# permission checking
username = request.user.username
if check_repo_access_permission(repo.id, username) != 'rw':
result['error'] = _('Permission denied')
return HttpResponse(json.dumps(result), status=403,
content_type=content_type)
# form validation
form = RepoNewDirentForm(request.POST)
if form.is_valid():
dirent_name = form.cleaned_data["dirent_name"]
else:
result['error'] = str(form.errors.values()[0])
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
# arguments checking
parent_dir = request.GET.get('parent_dir', None)
if not parent_dir:
result['error'] = _('Argument missing')
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
# rename duplicate name
dirent_name = check_filename_with_rename(repo.id, parent_dir,
dirent_name)
return func(repo.id, parent_dir, dirent_name, username)
return _decorated
@login_required
@new_dirent_common
def new_dir(repo_id, parent_dir, dirent_name, username):
"""
Create a new dir with ajax.
"""
result = {}
content_type = 'application/json; charset=utf-8'
# create new dirent
try:
seafile_api.post_dir(repo_id, parent_dir, dirent_name, username)
except SearpcError, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=500,
content_type=content_type)
return HttpResponse(json.dumps({'success': True, 'name': dirent_name}),
content_type=content_type)
@login_required
@new_dirent_common
def new_file(repo_id, parent_dir, dirent_name, username):
"""
Create a new file with ajax.
"""
result = {}
content_type = 'application/json; charset=utf-8'
# create new dirent
try:
seafile_api.post_empty_file(repo_id, parent_dir, dirent_name, username)
except SearpcError, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=500,
content_type=content_type)
return HttpResponse(json.dumps({'success': True, 'name': dirent_name}),
content_type=content_type)
@login_required
def rename_dirent(request, repo_id):
"""
Rename a file/dir in a repo, with ajax
"""
if request.method != 'POST' or not request.is_ajax():
raise Http404
result = {}
username = request.user.username
content_type = 'application/json; charset=utf-8'
repo = get_repo(repo_id)
if not repo:
result['error'] = _(u'Library does not exist.')
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
# permission checking
if check_repo_access_permission(repo.id, username) != 'rw':
result['error'] = _('Permission denied')
return HttpResponse(json.dumps(result), status=403,
content_type=content_type)
# form validation
form = RepoRenameDirentForm(request.POST)
if form.is_valid():
oldname = form.cleaned_data["oldname"]
newname = form.cleaned_data["newname"]
else:
result['error'] = str(form.errors.values()[0])
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
if newname == oldname:
return HttpResponse(json.dumps({'success': True}),
content_type=content_type)
# argument checking
parent_dir = request.GET.get('parent_dir', None)
if not parent_dir:
result['error'] = _('Argument missing')
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
# rename duplicate name
newname = check_filename_with_rename(repo_id, parent_dir, newname)
# rename file/dir
try:
seafile_api.rename_file(repo_id, parent_dir, oldname, newname, username)
except SearpcError, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=500,
content_type=content_type)
return HttpResponse(json.dumps({'success': True}),
content_type=content_type)
@login_required
def delete_dirent(request, repo_id):
"""
Delete a file/dir with ajax.
"""
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
repo = get_repo(repo_id)
if not repo:
err_msg = _(u'Library does not exist.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
# permission checking
username = request.user.username
if check_repo_access_permission(repo.id, username) != 'rw':
err_msg = _(u'Permission denied.')
return HttpResponse(json.dumps({'error': err_msg}),
status=403, content_type=content_type)
# argument checking
parent_dir = request.GET.get("parent_dir", None)
dirent_name = request.GET.get("name", None)
if not (parent_dir and dirent_name):
err_msg = _(u'Argument missing.')
return HttpResponse(json.dumps({'error': err_msg}),
status=400, content_type=content_type)
# delete file/dir
try:
seafile_api.del_file(repo_id, parent_dir, dirent_name, username)
return HttpResponse(json.dumps({'success': True}),
content_type=content_type)
except SearpcError, e:
logger.error(e)
err_msg = _(u'Internal error. Failed to delete %s.') % dirent_name
return HttpResponse(json.dumps({'error': err_msg}),
status=500, content_type=content_type)
def copy_move_common(func):
"""Decorator for common logic in copying/moving dir/file.
"""
def _decorated(request, repo_id, *args, **kwargs):
if request.method != 'POST' or not request.is_ajax():
raise Http404
result = {}
content_type = 'application/json; charset=utf-8'
repo = get_repo(repo_id)
if not repo:
result['error'] = _(u'Library does not exist.')
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
# permission checking
username = request.user.username
if check_repo_access_permission(repo.id, username) != 'rw':
result['error'] = _('Permission denied')
return HttpResponse(json.dumps(result), status=403,
content_type=content_type)
# arguments validation
path = request.GET.get('path')
obj_name = request.GET.get('obj_name')
dst_repo_id = request.POST.get('dst_repo')
dst_path = request.POST.get('dst_path')
if not (path and obj_name and dst_repo_id and dst_path):
result['error'] = _('Argument missing')
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
# check file path
if len(dst_path+obj_name) > settings.MAX_PATH:
result['error'] = _('Destination path is too long.')
return HttpResponse(json.dumps(result), status=400,
content_type=content_type)
# check whether user has write permission to dest repo
if check_repo_access_permission(dst_repo_id, username) != 'rw':
result['error'] = _('Permission denied')
return HttpResponse(json.dumps(result), status=403,
content_type=content_type)
# do nothing when dst is the same as src
if repo_id == dst_repo_id and path == dst_path:
url = reverse('repo', args=[repo_id]) + ('?p=%s' % urlquote(path))
return HttpResponseRedirect(url)
return func(repo_id, path, dst_repo_id, dst_path, obj_name, username)
return _decorated
@login_required
@copy_move_common
def mv_file(src_repo_id, src_path, dst_repo_id, dst_path, obj_name, username):
"""
"""
result = {}
content_type = 'application/json; charset=utf-8'
new_obj_name = check_filename_with_rename(dst_repo_id, dst_path, obj_name)
try:
msg_url = reverse('repo', args=[dst_repo_id]) + '?p=' + urlquote(dst_path)
seafile_api.move_file(src_repo_id, src_path, obj_name,
dst_repo_id, dst_path, new_obj_name, username)
msg = _(u'Successfully moved %(name)s <a href="%(url)s">view</a>') % \
{"name":obj_name, "url":msg_url}
result['msg'] = msg
result['success'] = True
return HttpResponse(json.dumps(result), content_type=content_type)
except SearpcError, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=500,
content_type=content_type)
@login_required
@copy_move_common
def cp_file(src_repo_id, src_path, dst_repo_id, dst_path, obj_name, username):
"""
"""
result = {}
content_type = 'application/json; charset=utf-8'
new_obj_name = check_filename_with_rename(dst_repo_id, dst_path, obj_name)
try:
msg_url = reverse('repo', args=[dst_repo_id]) + '?p=' + urlquote(dst_path)
seafile_api.copy_file(src_repo_id, src_path, obj_name,
dst_repo_id, dst_path, new_obj_name, username)
msg = _(u'Successfully copied %(name)s <a href="%(url)s">view</a>') % \
{"name":obj_name, "url":msg_url}
result['msg'] = msg
result['success'] = True
return HttpResponse(json.dumps(result), content_type=content_type)
except SearpcError, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=500,
content_type=content_type)
@login_required
@copy_move_common
def mv_dir(src_repo_id, src_path, dst_repo_id, dst_path, obj_name, username):
"""
"""
src_dir = os.path.join(src_path, obj_name)
if dst_path.startswith(src_dir):
error_msg = _(u'Can not move directory %(src)s to its subdirectory %(des)s') \
% {'src': src_dir, 'des': dst_path}
result['error'] = error_msg
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
result = {}
content_type = 'application/json; charset=utf-8'
new_obj_name = check_filename_with_rename(dst_repo_id, dst_path, obj_name)
try:
msg_url = reverse('repo', args=[dst_repo_id]) + '?p=' + urlquote(dst_path)
seafile_api.move_file(src_repo_id, src_path, obj_name,
dst_repo_id, dst_path, new_obj_name, username)
msg = _(u'Successfully moved %(name)s <a href="%(url)s">view</a>') % \
{"name":obj_name, "url":msg_url}
result['msg'] = msg
result['success'] = True
return HttpResponse(json.dumps(result), content_type=content_type)
except SearpcError, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=500,
content_type=content_type)
@login_required
@copy_move_common
def cp_dir(src_repo_id, src_path, dst_repo_id, dst_path, obj_name, username):
"""
"""
src_dir = os.path.join(src_path, obj_name)
if dst_path.startswith(src_dir):
error_msg = _(u'Can not copy directory %(src)s to its subdirectory %(des)s') \
% {'src': src_dir, 'des': dst_path}
result['error'] = error_msg
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
result = {}
content_type = 'application/json; charset=utf-8'
new_obj_name = check_filename_with_rename(dst_repo_id, dst_path, obj_name)
try:
msg_url = reverse('repo', args=[dst_repo_id]) + '?p=' + urlquote(dst_path)
seafile_api.copy_file(src_repo_id, src_path, obj_name,
dst_repo_id, dst_path, new_obj_name, username)
msg = _(u'Successfully copied %(name)s <a href="%(url)s">view</a>') % \
{"name":obj_name, "url":msg_url}
result['msg'] = msg
result['success'] = True
return HttpResponse(json.dumps(result), content_type=content_type)
except SearpcError, e:
result['error'] = str(e)
return HttpResponse(json.dumps(result), status=500,
content_type=content_type)
@login_required
def repo_star_file(request, repo_id):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
path = request.GET.get('file', '')
if not path:
return HttpResponse(json.dumps({'error': _(u'Invalid arguments')}),
status=400, content_type=content_type)
path = urlquote(path)
is_dir = False
star_file(request.user.username, repo_id, path, is_dir)
return HttpResponse(json.dumps({'success':True}), content_type=content_type)
@login_required
def repo_unstar_file(request, repo_id):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
path = request.GET.get('file', '')
if not path:
return HttpResponse(json.dumps({'error': _(u'Invalid arguments')}),
status=400, content_type=content_type)
path = urlquote(path)
unstar_file(request.user.username, repo_id, path)
return HttpResponse(json.dumps({'success':True}), content_type=content_type)
########## contacts related
@login_required
def get_contacts(request):
if not request.is_ajax():
raise Http404
content_type = 'application/json; charset=utf-8'
username = request.user.username
contacts = Contact.objects.get_contacts_by_user(username)
contact_list = []
from seahub.avatar.templatetags.avatar_tags import avatar
for c in contacts:
contact_list.append({"email": c.contact_email, "avatar": avatar(c.contact_email, 16)})
return HttpResponse(json.dumps({"contacts":contact_list}), content_type=content_type)

View File

@@ -114,7 +114,7 @@ def get_fileshare(repo_id, username, path):
username=username).filter(path=path)
return l[0] if len(l) > 0 else None
def get_shared_link(request, fileshare):
def get_dir_share_link(fileshare):
# dir shared link
if fileshare:
dir_shared_link = gen_dir_share_link(fileshare.token)
@@ -179,7 +179,7 @@ def render_repo(request, repo):
upload_url = get_upload_url(request, repo.id)
update_url = get_update_url(request, repo.id)
fileshare = get_fileshare(repo.id, username, path)
dir_shared_link = get_shared_link(request, fileshare)
dir_shared_link = get_dir_share_link(fileshare)
return render_to_response('repo.html', {
'repo': repo,