1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-04 16:31:13 +00:00

use djangorestframwork in api

This commit is contained in:
poet
2012-07-14 20:26:41 +08:00
parent f29db5b649
commit f441158f9e
4 changed files with 210 additions and 141 deletions

View File

@@ -4,12 +4,14 @@ from views import *
urlpatterns = patterns('',
url(r'^/$', list_repo),
url(r'^repo/list/$', list_repo),
url(r'^repo/(?P<repo_id>[^/]+)/$', get_repo_info),
url(r'^dir/(?P<repo_id>[^/]+)/root/$', get_repo_dir_path),
url(r'^dir/(?P<repo_id>[^/]+)/$', get_repo_dir_path),
url(r'^dir/(?P<repo_id>[^/]+)/(?P<dir_id>[^/]+)/$', get_repo_dir_id),
url(r'^file/(?P<repo_id>[^/]+)/(?P<file_id>[^/]+)/$', get_repo_file_id),
url(r'^$', ReposView.as_view()),
url(r'^/$', ReposView.as_view()),
url(r'^repo/list/$', ReposView.as_view(), name='repos'),
url(r'^repo/(?P<repo_id>[^/]+)/$', RepoView.as_view(), name='repo'),
url(r'^dir/(?P<repo_id>[^/]+)/root/$', RepoDirPathView.as_view()),
url(r'^dir/(?P<repo_id>[^/]+)/$', RepoDirPathView.as_view(), name='repo-dir-path'),
url(r'^dir/(?P<repo_id>[^/]+)/(?P<dir_id>[^/]+)/$', RepoDirIdView.as_view(), name='repo-dirr-id'),
url(r'^file/(?P<repo_id>[^/]+)/(?P<file_id>[^/]+)/$', RepoFileView.as_view(), name='repo-file'),
)

View File

@@ -6,7 +6,7 @@ import stat
import simplejson as json
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
from auth.decorators import login_required
from auth.decorators import login_required, api_login_required
from seaserv import ccnet_rpc, ccnet_threaded_rpc, get_groups, get_users, get_repos, \
get_repo, get_commits, get_branches, \
@@ -19,6 +19,15 @@ from seahub.utils import list_to_string, \
check_filename_with_rename, get_accessible_repos, EMPTY_SHA1
from seahub.views import access_to_repo, validate_owner
from pysearpc import SearpcError
from djangorestframework.renderers import JSONRenderer
from djangorestframework.compat import View
from djangorestframework.mixins import ResponseMixin
from djangorestframework.response import Response
from django.core.urlresolvers import reverse
json_content_type = 'application/json; charset=utf-8'
@@ -36,6 +45,7 @@ def can_access_repo(request, repo_id):
# check whether user can view repo
return access_to_repo(request, repo_id, repo_ap)
def get_dir_entrys_by_path(reqquest, commit, path):
dentrys = []
if path[-1] != '/':
@@ -43,7 +53,7 @@ def get_dir_entrys_by_path(reqquest, commit, path):
if not commit.root_id == EMPTY_SHA1:
try:
dirs = seafserv_rpc.list_dir_by_path(commit.id,
dirs = seafserv_threaded_rpc.list_dir_by_path(commit.id,
path.encode('utf-8'))
except SearpcError, e:
return api_error(request, "404", e.msg)
@@ -67,7 +77,7 @@ def get_dir_entrys_by_path(reqquest, commit, path):
def get_dir_entrys_by_id(reqquest, dir_id):
dentrys = []
try:
dirs = seafserv_rpc.list_dir(dir_id)
dirs = seafserv_threaded_rpc.list_dir(dir_id)
except SearpcError, e:
return api_error(request, "404", e.msg)
for dirent in dirs:
@@ -87,8 +97,11 @@ def get_dir_entrys_by_id(reqquest, dir_id):
return HttpResponse(json.dumps(dentrys), status=200,
content_type=json_content_type)
@login_required
def list_repo(request):
class ReposView(ResponseMixin, View):
renderers = (JSONRenderer,)
@api_login_required
def get(self, request):
email = request.user.username
owned_repos = seafserv_threaded_rpc.list_owned_repos(email)
@@ -120,12 +133,15 @@ def list_repo(request):
"mtime":r.lastest_modify,
}
repos_json.append(repo)
return HttpResponse(json.dumps(repos_json), status=200,
content_type=json_content_type)
response = Response(200, repos_json)
return self.render(response)
@login_required
def get_repo_info(request, repo_id):
class RepoView(ResponseMixin, View):
renderers = (JSONRenderer,)
@api_login_required
def get_repo_info(request, repo_id):
# check whether user can view repo
if not can_access_repo(request, repo_id):
return api_error(request, '403', "can not access repo")
@@ -144,8 +160,8 @@ def get_repo_info(request, repo_id):
repo.latest_modify = get_commits(repo.id, 0, 1)[0].ctime
except:
repo.latest_modify = None
# query whether set password if repo is encrypted
# query whether set password if repo is encrypted
password_set = False
if repo.props.encrypted:
try:
@@ -170,12 +186,15 @@ def get_repo_info(request, repo_id):
"commit":current_commit.id,
}
return HttpResponse(json.dumps(repo_json), status=200,
content_type=json_content_type)
response = Response(200, repo_json)
return self.render(response)
@login_required
def get_repo_dir_path(request, repo_id):
class RepoDirPathView(ResponseMixin, View):
renderers = (JSONRenderer,)
@api_login_required
def get(self, request, repo_id):
if not can_access_repo(request, repo_id):
return api_error(request, '403', "can not access repo")
current_commit = get_commits(repo_id, 0, 1)[0]
@@ -192,6 +211,7 @@ def get_repo_dir_path(request, repo_id):
password_set = True
except SearpcError, e:
return api_error(request, '403', e.msg)
if repo.props.encrypted and not password_set:
return api_error(request, '403', "password needed")
@@ -199,8 +219,12 @@ def get_repo_dir_path(request, repo_id):
return get_dir_entrys_by_path(request, current_commit, path)
@login_required
def get_repo_dir_id(request, repo_id, dir_id):
class RepoDirIdView(ResponseMixin, View):
renderers = (JSONRenderer,)
@api_login_required
def get(self, request, repo_id, dir_id):
if not can_access_repo(request, repo_id):
return api_error(request, '403', "can not access repo")
@@ -222,8 +246,12 @@ def get_repo_dir_id(request, repo_id, dir_id):
return get_dir_entrys_by_id(request, dir_id)
@login_required
def get_repo_file_id(request, repo_id, file_id):
class RepoFileView(ResponseMixin, View):
renderers = (JSONRenderer,)
@api_login_required
def get(self, request, repo_id, file_id):
if not can_access_repo(request, repo_id):
return api_error(request, '403', "can not access repo")
@@ -256,3 +284,5 @@ def get_repo_file_id(request, repo_id, file_id):
request.user.username)
return HttpResponseRedirect(redirect_url)

View File

@@ -44,6 +44,44 @@ def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
return actual_decorator
def api_user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
if not login_url:
from django.conf import settings
login_url = settings.LOGIN_URL
def decorator(view_func):
def _wrapped_view(obj, request, *args, **kwargs):
if test_func(request.user):
return view_func(obj, request, *args, **kwargs)
path = urlquote(request.get_full_path())
tup = login_url, redirect_field_name, path
json_content_type = 'application/json; charset=utf-8'
return HttpResponse(json.dumps('%s?%s=%s' % tup), status=401,
content_type=json_content_type)
return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
return decorator
def api_login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the user is logged in, redirecting
to the log-in page if necessary.
"""
actual_decorator = api_user_passes_test(
lambda u: u.is_authenticated(),
redirect_field_name=redirect_field_name
)
if function:
return actual_decorator(function)
return actual_decorator
def permission_required(perm, login_url=None):
"""
Decorator for views that checks whether a user has a particular permission

View File

@@ -43,7 +43,6 @@ urlpatterns = patterns('',
(r'^share/', include('share.urls')),
(r'^api/', include('api.urls')),
(r'^rest/', include('djangorestframework.urls')),
url(r'^shareadmin/$', share_admin, name='share_admin'),
(r'^shareadmin/removeshare/$', repo_remove_share),
(r'^sharedlink/get/$', get_shared_link),