mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-13 13:50:07 +00:00
add admin create repo api
This commit is contained in:
@@ -138,6 +138,40 @@ class AdminLibraries(APIView):
|
|||||||
|
|
||||||
return Response({"page_info": page_info, "repos": return_results})
|
return Response({"page_info": page_info, "repos": return_results})
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
""" Admin create library
|
||||||
|
|
||||||
|
Permission checking:
|
||||||
|
1. only admin can perform this action.
|
||||||
|
"""
|
||||||
|
|
||||||
|
repo_name = request.data.get('name', None)
|
||||||
|
if not repo_name:
|
||||||
|
error_msg = 'name invalid.'
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
username = request.user.username
|
||||||
|
repo_owner = request.data.get('owner', None)
|
||||||
|
if repo_owner:
|
||||||
|
try:
|
||||||
|
User.objects.get(email=repo_owner)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
error_msg = 'User %s not found.' % repo_owner
|
||||||
|
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
|
||||||
|
else:
|
||||||
|
repo_owner = username
|
||||||
|
|
||||||
|
try:
|
||||||
|
repo_id = seafile_api.create_repo(repo_name, '', repo_owner, None)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
error_msg = 'Internal Server Error'
|
||||||
|
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
||||||
|
|
||||||
|
repo = seafile_api.get_repo(repo_id)
|
||||||
|
repo_info = get_repo_info(repo)
|
||||||
|
return Response(repo_info)
|
||||||
|
|
||||||
class AdminLibrary(APIView):
|
class AdminLibrary(APIView):
|
||||||
|
|
||||||
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
||||||
|
@@ -308,6 +308,9 @@
|
|||||||
<% if (cur_tab == 'trash') { %>
|
<% if (cur_tab == 'trash') { %>
|
||||||
<button class="js-clean fright hide">{% trans "Clean" %}</button>
|
<button class="js-clean fright hide">{% trans "Clean" %}</button>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
<% if (cur_tab == 'all') { %>
|
||||||
|
<button class="js-add-library fright">{% trans "New Library" %}</button>
|
||||||
|
<% } %>
|
||||||
</div>
|
</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -336,6 +339,19 @@
|
|||||||
</div>
|
</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<script type="text/template" id="library-add-form-tmpl">
|
||||||
|
<form id="library-add-form" action="" method="post" class="hide">{% csrf_token %}
|
||||||
|
<h3 id="dialogTitle">{% trans "New Library" %}</h3>
|
||||||
|
<label for="library-name">{% trans "Name" %}</label><br />
|
||||||
|
<input type="text" name="library_name" value="" class="input" id="library-name" /><br />
|
||||||
|
<label for="library-owner">{% trans "Owner" %}</label>
|
||||||
|
<span class="tip">{% trans "(If left blank, owner will be admin)" %}</span><br />
|
||||||
|
<input type="hidden" name="library_owner" value="" id="library-owner" />
|
||||||
|
<p class="error hide"></p>
|
||||||
|
<input type="submit" class="submit" value="{% trans "Submit" %}" />
|
||||||
|
</form>
|
||||||
|
</script>
|
||||||
|
|
||||||
<script type="text/template" id="search-libraries-tmpl">
|
<script type="text/template" id="search-libraries-tmpl">
|
||||||
<h3>{% trans "Search Library"%}</h3>
|
<h3>{% trans "Search Library"%}</h3>
|
||||||
<form id="search-repo-form" method="get" action="">
|
<form id="search-repo-form" method="get" action="">
|
||||||
|
@@ -14,6 +14,7 @@ define([
|
|||||||
|
|
||||||
tabNavTemplate: _.template($("#libraries-tabnav-tmpl").html()),
|
tabNavTemplate: _.template($("#libraries-tabnav-tmpl").html()),
|
||||||
template: _.template($("#libraries-tmpl").html()),
|
template: _.template($("#libraries-tmpl").html()),
|
||||||
|
libraryAddFormtemplate: _.template($("#library-add-form-tmpl").html()),
|
||||||
|
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.repoCollection = new RepoCollection();
|
this.repoCollection = new RepoCollection();
|
||||||
@@ -36,10 +37,67 @@ define([
|
|||||||
},
|
},
|
||||||
|
|
||||||
events: {
|
events: {
|
||||||
|
'click .js-add-library': 'addLibrary',
|
||||||
'click #paginator .js-next': 'getNextPage',
|
'click #paginator .js-next': 'getNextPage',
|
||||||
'click #paginator .js-previous': 'getPreviousPage'
|
'click #paginator .js-previous': 'getPreviousPage'
|
||||||
},
|
},
|
||||||
|
|
||||||
|
addLibrary: function () {
|
||||||
|
var $form = $(this.libraryAddFormtemplate()),
|
||||||
|
repos = this.repoCollection,
|
||||||
|
_this = this;
|
||||||
|
|
||||||
|
$form.modal();
|
||||||
|
$('#simplemodal-container').css({'height':'auto'});
|
||||||
|
|
||||||
|
$('[name="library_owner"]', $form).select2($.extend(
|
||||||
|
Common.contactInputOptionsForSelect2(), {
|
||||||
|
width: '268px',
|
||||||
|
containerCss: {'margin-bottom': '5px'},
|
||||||
|
maximumSelectionSize: 1,
|
||||||
|
placeholder: gettext("Search user or enter email and press Enter"), // to override 'placeholder' returned by `Common.conta...`
|
||||||
|
formatSelectionTooBig: gettext("You cannot select any more choices")
|
||||||
|
}));
|
||||||
|
|
||||||
|
$form.submit(function() {
|
||||||
|
var library_name = $.trim($('[name="library_name"]', $form).val());
|
||||||
|
var library_owner = $.trim($('[name="library_owner"]', $form).val());
|
||||||
|
var $error = $('.error', $form);
|
||||||
|
var $submitBtn = $('[type="submit"]', $form);
|
||||||
|
|
||||||
|
if (!library_name) {
|
||||||
|
$error.html(gettext("Name is required.")).show();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$error.hide();
|
||||||
|
Common.disableButton($submitBtn);
|
||||||
|
|
||||||
|
repos.create({'name': library_name, 'owner': library_owner}, {
|
||||||
|
prepend: true,
|
||||||
|
wait: true,
|
||||||
|
success: function() {
|
||||||
|
if (repos.length == 1) {
|
||||||
|
repos.reset(repos.models);
|
||||||
|
}
|
||||||
|
Common.closeModal();
|
||||||
|
},
|
||||||
|
error: function(collection, response, options) {
|
||||||
|
var err_msg;
|
||||||
|
if (response.responseText) {
|
||||||
|
err_msg = response.responseJSON.error_msg;
|
||||||
|
} else {
|
||||||
|
err_msg = gettext('Please check the network.');
|
||||||
|
}
|
||||||
|
$error.html(err_msg).show();
|
||||||
|
Common.enableButton($submitBtn);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
initPage: function() {
|
initPage: function() {
|
||||||
this.$table.hide();
|
this.$table.hide();
|
||||||
this.$tableBody.empty();
|
this.$tableBody.empty();
|
||||||
@@ -142,9 +200,13 @@ define([
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
addOne: function(library) {
|
addOne: function(library, collection, options) {
|
||||||
var view = new RepoView({model: library});
|
var view = new RepoView({model: library});
|
||||||
this.$tableBody.append(view.render().el);
|
if (options.prepend) {
|
||||||
|
this.$tableBody.prepend(view.render().el);
|
||||||
|
} else {
|
||||||
|
this.$tableBody.append(view.render().el);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -34,6 +34,86 @@ class LibrariesTest(BaseTestCase):
|
|||||||
resp = self.client.get(self.libraries_url)
|
resp = self.client.get(self.libraries_url)
|
||||||
self.assertEqual(403, resp.status_code)
|
self.assertEqual(403, resp.status_code)
|
||||||
|
|
||||||
|
def test_can_create(self):
|
||||||
|
self.login_as(self.admin)
|
||||||
|
|
||||||
|
repo_name = randstring(6)
|
||||||
|
repo_owner = self.user.username
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'name': repo_name,
|
||||||
|
'owner': repo_owner,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = self.client.post(self.libraries_url, data)
|
||||||
|
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
|
assert json_resp['name'] == repo_name
|
||||||
|
assert json_resp['owner'] == repo_owner
|
||||||
|
|
||||||
|
self.remove_repo(json_resp['id'])
|
||||||
|
|
||||||
|
def test_can_create_without_owner_parameter(self):
|
||||||
|
self.login_as(self.admin)
|
||||||
|
|
||||||
|
repo_name = randstring(6)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'name': repo_name,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = self.client.post(self.libraries_url, data)
|
||||||
|
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
|
assert json_resp['name'] == repo_name
|
||||||
|
assert json_resp['owner'] == self.admin.username
|
||||||
|
|
||||||
|
self.remove_repo(json_resp['id'])
|
||||||
|
|
||||||
|
def test_create_with_invalid_user_permission(self):
|
||||||
|
self.login_as(self.user)
|
||||||
|
|
||||||
|
repo_name = randstring(6)
|
||||||
|
repo_owner = self.user.username
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'name': repo_name,
|
||||||
|
'owner': repo_owner,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = self.client.post(self.libraries_url, data)
|
||||||
|
self.assertEqual(403, resp.status_code)
|
||||||
|
|
||||||
|
def test_create_with_invalid_name_parameter(self):
|
||||||
|
self.login_as(self.admin)
|
||||||
|
|
||||||
|
repo_name = randstring(6)
|
||||||
|
repo_owner = self.user.username
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'invalid_name': repo_name,
|
||||||
|
'owner': repo_owner,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = self.client.post(self.libraries_url, data)
|
||||||
|
self.assertEqual(400, resp.status_code)
|
||||||
|
|
||||||
|
def test_create_with_unexisted_user(self):
|
||||||
|
self.login_as(self.admin)
|
||||||
|
|
||||||
|
repo_name = randstring(6)
|
||||||
|
repo_owner = '%s@email.com' % randstring(6)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'name': repo_name,
|
||||||
|
'owner': repo_owner,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = self.client.post(self.libraries_url, data)
|
||||||
|
self.assertEqual(404, resp.status_code)
|
||||||
|
|
||||||
|
|
||||||
class LibraryTest(BaseTestCase):
|
class LibraryTest(BaseTestCase):
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user