mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-16 07:08:55 +00:00
Merge branch 'swpd-master'
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -23,3 +23,19 @@ class AuthTokenSerializer(serializers.Serializer):
|
||||
else:
|
||||
raise serializers.ValidationError('Must include "username" and "password"')
|
||||
|
||||
class AccountSerializer(serializers.Serializer):
|
||||
email = serializers.EmailField()
|
||||
password = serializers.CharField()
|
||||
is_staff = serializers.BooleanField()
|
||||
is_active = serializers.BooleanField()
|
||||
|
||||
def validate(self, attrs):
|
||||
email = attrs.get('email')
|
||||
password = attrs.get('password')
|
||||
attrs['is_staff'] = attrs.get('is_staff', False)
|
||||
attrs['is_active'] = attrs.get('is_active', True)
|
||||
|
||||
if not password:
|
||||
raise serializers.ValidationError('Password is required')
|
||||
|
||||
return attrs
|
||||
|
@@ -9,7 +9,9 @@ urlpatterns = patterns('',
|
||||
url(r'^auth-token/', ObtainAuthToken.as_view()),
|
||||
|
||||
# RESTful API
|
||||
url(r'^account/info/$', Account.as_view()),
|
||||
url(r'^accounts/$', Accounts.as_view(), name="accounts"),
|
||||
url(r'^accounts/(?P<email>\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/$', Account.as_view(), name="api2-account"),
|
||||
url(r'^account/info/$', AccountInfo.as_view()),
|
||||
url(r'^repos/$', Repos.as_view(), name="api2-repos"),
|
||||
url(r'^repos/(?P<repo_id>[-0-9a-f]{36})/$', Repo.as_view(), name="api2-repo"),
|
||||
url(r'^repos/(?P<repo_id>[-0-9a-f]{36})/download-info/$', DownloadRepo.as_view()),
|
||||
|
@@ -9,7 +9,7 @@ import seahub.settings as settings
|
||||
from rest_framework import parsers
|
||||
from rest_framework import status
|
||||
from rest_framework import renderers
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.permissions import IsAuthenticated, IsAdminUser
|
||||
from rest_framework.reverse import reverse
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.throttling import AnonRateThrottle, UserRateThrottle
|
||||
@@ -19,7 +19,7 @@ from django.http import HttpResponse
|
||||
|
||||
from models import Token
|
||||
from authentication import TokenAuthentication
|
||||
from serializers import AuthTokenSerializer
|
||||
from serializers import AuthTokenSerializer, AccountSerializer
|
||||
from utils import is_repo_writable, is_repo_accessible
|
||||
from seahub.base.accounts import User
|
||||
from seahub.base.models import FileDiscuss, UserStarredFiles
|
||||
@@ -121,9 +121,87 @@ def api_error(code, msg):
|
||||
err_resp = {'error_msg': msg}
|
||||
return Response(err_resp, status=code)
|
||||
|
||||
class Account(APIView):
|
||||
class Accounts(APIView):
|
||||
"""List all accounts.
|
||||
Administator permission is required.
|
||||
"""
|
||||
Show account info.
|
||||
authentication_classes = (TokenAuthentication, )
|
||||
permission_classes = (IsAdminUser, )
|
||||
throttle_classes = (UserRateThrottle, )
|
||||
|
||||
def get(self, request, format=None):
|
||||
# list accounts
|
||||
start = int(request.GET.get('start', '0'))
|
||||
limit = int(request.GET.get('limit', '100'))
|
||||
accounts = get_emailusers(start, limit)
|
||||
|
||||
accounts_json = []
|
||||
for account in accounts:
|
||||
accounts_json.append({'email': account.email})
|
||||
|
||||
return Response(accounts_json)
|
||||
|
||||
class Account(APIView):
|
||||
"""Query/Add/Delete a specific account.
|
||||
Administator permission is required.
|
||||
"""
|
||||
authentication_classes = (TokenAuthentication, )
|
||||
permission_classes = (IsAdminUser, )
|
||||
throttle_classes = (UserRateThrottle, )
|
||||
|
||||
def get(self, request, email, format=None):
|
||||
# query account info
|
||||
try:
|
||||
user = User.objects.get(email=email)
|
||||
except User.DoesNotExist:
|
||||
return api_error(status.HTTP_404_NOT_FOUND, 'User not found.')
|
||||
|
||||
info = {}
|
||||
info['email'] = user.email
|
||||
info['id'] = user.id
|
||||
info['is_staff'] = user.is_staff
|
||||
info['is_active'] = user.is_active
|
||||
info['create_time'] = user.ctime
|
||||
|
||||
info['total'] = get_user_quota(email)
|
||||
if CALC_SHARE_USAGE:
|
||||
my_usage = get_user_quota_usage(email)
|
||||
share_usage = get_user_share_usage(email)
|
||||
info['usage'] = my_usage + share_usage
|
||||
else:
|
||||
info['usage'] = get_user_quota_usage(email)
|
||||
|
||||
return Response(info)
|
||||
|
||||
def put(self, request, email, format=None):
|
||||
# create or update account
|
||||
copy = request.DATA.copy()
|
||||
copy.update({'email': email})
|
||||
serializer = AccountSerializer(data=copy)
|
||||
if serializer.is_valid():
|
||||
user = User.objects.create_user(serializer.object['email'],
|
||||
serializer.object['password'],
|
||||
serializer.object['is_staff'],
|
||||
serializer.object['is_active'])
|
||||
|
||||
resp = Response('success', status=status.HTTP_201_CREATED)
|
||||
resp['Location'] = reverse('api2-account', args=[email])
|
||||
return resp
|
||||
else:
|
||||
return api_error(status.HTTP_400_BAD_REQUEST, serializer.errors)
|
||||
|
||||
def delete(self, request, email, format=None):
|
||||
# delete account
|
||||
try:
|
||||
user = User.objects.get(email=email)
|
||||
user.delete()
|
||||
return Response("success")
|
||||
except User.DoesNotExist:
|
||||
return api_error(status.HTTP_404_NOT_FOUND,
|
||||
'Failed to delete: account does not exist.')
|
||||
|
||||
class AccountInfo(APIView):
|
||||
""" Show account info.
|
||||
"""
|
||||
authentication_classes = (TokenAuthentication, )
|
||||
permission_classes = (IsAuthenticated,)
|
||||
@@ -278,10 +356,10 @@ class Repos(APIView):
|
||||
repo_id = seafserv_threaded_rpc.create_repo(repo_name, repo_desc,
|
||||
username, passwd)
|
||||
except:
|
||||
return api_error(status.HTTP_520_OPERATION_FAILED, \
|
||||
return api_error(HTTP_520_OPERATION_FAILED, \
|
||||
'Failed to create library.')
|
||||
if not repo_id:
|
||||
return api_error(status.HTTP_520_OPERATION_FAILED, \
|
||||
return api_error(HTTP_520_OPERATION_FAILED, \
|
||||
'Failed to create library.')
|
||||
else:
|
||||
repo_created.send(sender=None,
|
||||
@@ -553,7 +631,7 @@ def get_repo_file(request, repo_id, file_id, file_name, op):
|
||||
try:
|
||||
blks = seafile_api.list_file_by_file_id(file_id)
|
||||
except SearpcError, e:
|
||||
return api_error(status.HTTP_520_OPERATION_FAILED,
|
||||
return api_error(HTTP_520_OPERATION_FAILED,
|
||||
'Failed to get file block list')
|
||||
blklist = blks.split('\n')
|
||||
blklist = [i for i in blklist if len(i) == 40]
|
||||
|
Reference in New Issue
Block a user