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

improve apis

This commit is contained in:
poet
2012-07-24 22:10:26 +08:00
parent 703b07e452
commit 229bad0493

View File

@@ -4,7 +4,7 @@ import sys
import os import os
import stat import stat
import simplejson as json import simplejson as json
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError from django.http import HttpResponse, HttpResponseServerError
from auth.decorators import login_required, api_login_required from auth.decorators import login_required, api_login_required
@@ -15,7 +15,6 @@ from seaserv import ccnet_rpc, ccnet_threaded_rpc, get_groups, get_users, get_re
from seahub.utils import list_to_string, \ from seahub.utils import list_to_string, \
get_httpserver_root, gen_token, \ get_httpserver_root, gen_token, \
calculate_repo_last_modify, valid_previewed_file, \
check_filename_with_rename, get_accessible_repos, EMPTY_SHA1 check_filename_with_rename, get_accessible_repos, EMPTY_SHA1
from seahub.views import access_to_repo, validate_owner from seahub.views import access_to_repo, validate_owner
@@ -34,6 +33,31 @@ from django.views.decorators.csrf import csrf_exempt
json_content_type = 'application/json; charset=utf-8' json_content_type = 'application/json; charset=utf-8'
def calculate_repo_info(repo_list, username):
"""
Get some info for repo.
"""
for repo in repo_list:
try:
commit = get_commits(repo.id, 0, 1)[0]
repo.latest_modify = commit.ctime
repo.commit = commit.id
repo.size = seafserv_threaded_rpc.server_repo_size(repo.id),
password_need = False
if repo.props.encrypted:
try:
ret = seafserv_rpc.is_passwd_set(repo.id, username)
if ret != 1:
password_need = True
except SearpcErroe, e:
pass
repo.password_need = password_need
except:
repo.latest_modify = None
repo.commit = None
repo.size = -1
repo.password_need = None
def api_error(request, code='404', msg=None): def api_error(request, code='404', msg=None):
err_resp = {'error_msg':msg} err_resp = {'error_msg':msg}
@@ -61,16 +85,16 @@ def get_dir_entrys_by_path(reqquest, commit, path):
except SearpcError, e: except SearpcError, e:
return api_error(request, "404", e.msg) return api_error(request, "404", e.msg)
for dirent in dirs: for dirent in dirs:
is_dir = False dtype = "file"
entry={} entry={}
if stat.S_ISDIR(dirent.props.mode): if stat.S_ISDIR(dirent.props.mode):
is_dir = True dtype = "dir"
else: else:
try: try:
entry["size"] = seafserv_rpc.get_file_size(dirent.obj_id) entry["size"] = seafserv_threaded_rpc.get_file_size(dirent.obj_id)
except: except:
entry["size"]=0 entry["size"]=0
entry["is_dir"]=is_dir entry["type"]=dtype
entry["name"]=dirent.obj_name entry["name"]=dirent.obj_name
entry["id"]=dirent.obj_id entry["id"]=dirent.obj_id
dentrys.append(entry) dentrys.append(entry)
@@ -84,16 +108,16 @@ def get_dir_entrys_by_id(reqquest, dir_id):
except SearpcError, e: except SearpcError, e:
return api_error(request, "404", e.msg) return api_error(request, "404", e.msg)
for dirent in dirs: for dirent in dirs:
is_dir = False dtype = "file"
entry={} entry={}
if stat.S_ISDIR(dirent.props.mode): if stat.S_ISDIR(dirent.props.mode):
is_dir = True dtype = "dir"
else: else:
try: try:
entry["size"] = seafserv_rpc.get_file_size(dirent.obj_id) entry["size"] = seafserv_threaded_rpc.get_file_size(dirent.obj_id)
except: except Exception, e:
entry["size"]=0 entry["size"]=0
entry["is_dir"]=is_dir entry["type"]=dtype
entry["name"]=dirent.obj_name entry["name"]=dirent.obj_name
entry["id"]=dirent.obj_id entry["id"]=dirent.obj_id
dentrys.append(entry) dentrys.append(entry)
@@ -109,7 +133,6 @@ def api_login(request):
if form.is_valid(): if form.is_valid():
auth_login(request, form.get_user()) auth_login(request, form.get_user())
print ">>",request.session.session_key
return HttpResponse(json.dumps(request.session.session_key), status=200, return HttpResponse(json.dumps(request.session.session_key), status=200,
content_type=json_content_type) content_type=json_content_type)
else: else:
@@ -132,32 +155,40 @@ class ReposView(ResponseMixin, View):
email = request.user.username email = request.user.username
owned_repos = seafserv_threaded_rpc.list_owned_repos(email) owned_repos = seafserv_threaded_rpc.list_owned_repos(email)
calculate_repo_last_modify(owned_repos) calculate_repo_info (owned_repos, email)
owned_repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify)) owned_repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify))
n_repos = seafserv_threaded_rpc.list_share_repos(email, n_repos = seafserv_threaded_rpc.list_share_repos(email,
'to_email', -1, -1) 'to_email', -1, -1)
calculate_repo_last_modify(owned_repos) calculate_repo_info (n_repos, email)
owned_repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify)) owned_repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify))
repos_json = [] repos_json = []
for r in owned_repos: for r in owned_repos:
repo = { repo = {
"type":"repo",
"id":r.props.id, "id":r.props.id,
"owner":"self", "owner":email,
"name":r.props.name, "name":r.props.name,
"desc":r.props.desc, "desc":r.props.desc,
"mtime":r.lastest_modify, "mtime":r.lastest_modify,
"commit":r.commit,
"size":r.size,
"password_need":r.password_need,
} }
repos_json.append(repo) repos_json.append(repo)
for r in n_repos: for r in n_repos:
repo = { repo = {
"type":"repo",
"id":r.props.id, "id":r.props.id,
"owner":r.props.shared_email, "owner":r.props.shared_email,
"name":r.props.name, "name":r.props.name,
"desc":r.props.desc, "desc":r.props.desc,
"mtime":r.lastest_modify, "mtime":r.lastest_modify,
"commit":r.commit,
"size":r.size,
"password_need":r.password_need,
} }
repos_json.append(repo) repos_json.append(repo)
@@ -190,12 +221,12 @@ class RepoView(ResponseMixin, View):
repo.latest_modify = None repo.latest_modify = None
# query whether set password if repo is encrypted # query whether set password if repo is encrypted
password_set = False password_need = False
if repo.props.encrypted: if repo.props.encrypted:
try: try:
ret = seafserv_rpc.is_passwd_set(repo_id, request.user.username) ret = seafserv_rpc.is_passwd_set(repo_id, request.user.username)
if ret == 1: if ret != 1:
password_set = True password_need = True
except SearpcError, e: except SearpcError, e:
return api_error(request, '403', e.msg) return api_error(request, '403', e.msg)
@@ -204,12 +235,13 @@ class RepoView(ResponseMixin, View):
current_commit = get_commits(repo_id, 0, 1)[0] current_commit = get_commits(repo_id, 0, 1)[0]
repo_json = { repo_json = {
"type":"repo",
"id":repo.props.id, "id":repo.props.id,
"owner":owner, "owner":owner,
"name":repo.props.name, "name":repo.props.name,
"desc":repo.props.desc, "desc":repo.props.desc,
"mtime":repo.lastest_modify, "mtime":repo.lastest_modify,
"password_set":password_set, "password_need":password_need,
"size":repo_size, "size":repo_size,
"commit":current_commit.id, "commit":current_commit.id,
} }
@@ -310,7 +342,7 @@ class RepoFileView(ResponseMixin, View):
file_name, op, file_name, op,
token, token,
request.user.username) request.user.username)
response = Response(200, redirect_url)
return HttpResponseRedirect(redirect_url) return self.render(response)