mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-02 07:47:32 +00:00
license
This commit is contained in:
parent
b2cc92da36
commit
3149a4d49d
41
seahub/api2/endpoints/admin/license.py
Normal file
41
seahub/api2/endpoints/admin/license.py
Normal file
@ -0,0 +1,41 @@
|
||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||
import os
|
||||
import logging
|
||||
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework import status
|
||||
|
||||
from seahub.api2.authentication import TokenAuthentication
|
||||
from seahub.api2.throttling import UserRateThrottle
|
||||
from seahub.api2.utils import api_error
|
||||
from seahub.settings import LICENSE_PATH
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AdminLicense(APIView):
|
||||
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
||||
throttle_classes = (UserRateThrottle, )
|
||||
permission_classes = (IsAdminUser,)
|
||||
|
||||
def post(self, request):
|
||||
license_file = request.FILES.get('license', None)
|
||||
if not license_file:
|
||||
error_msg = 'license invalid.'
|
||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||
|
||||
license_dir = os.path.dirname(LICENSE_PATH)
|
||||
try:
|
||||
if not os.path.exists(license_dir):
|
||||
error_msg = 'path %s invalid.'% LICENSE_PATH
|
||||
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||
|
||||
with open(LICENSE_PATH, 'w') as fd:
|
||||
fd.write(license_file.read())
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
|
||||
return Response({'success': True}, status=status.HTTP_200_OK)
|
@ -412,6 +412,9 @@ SITE_TITLE = 'Private Seafile'
|
||||
# Base name used in email sending
|
||||
SITE_NAME = 'Seafile'
|
||||
|
||||
# Path to the license file(relative to the media path)
|
||||
LICENSE_PATH = os.path.join(PROJECT_ROOT, '../../seafile_license.txt')
|
||||
|
||||
# Path to the favicon file (relative to the media path)
|
||||
# tip: use a different name when modify it.
|
||||
FAVICON_PATH = 'img/favicon.ico'
|
||||
|
@ -40,3 +40,16 @@
|
||||
</div>
|
||||
</form>
|
||||
{% endif %}
|
||||
|
||||
{% if type == 'license' %}
|
||||
<form class="web-setting-form" action="">{% csrf_token %}
|
||||
<h5 class="web-setting-name">{{ setting_display_name }}</h5>
|
||||
<div class="web-setting-input web-setting-file-input">
|
||||
<p class="tip">{{ help_tip }}</p>
|
||||
<div class="web-setting-file-upload">
|
||||
<input type="file" name="{{ setting_name }}" class="web-setting-file-upload-input transparent-file-input" />
|
||||
<button type="button" class="web-setting-file-upload-btn">{% trans "Change" %}</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% endif %}
|
||||
|
@ -33,6 +33,10 @@
|
||||
{% include "snippets/web_settings_form.html" %}
|
||||
{% endwith %}
|
||||
|
||||
{% with type="license" setting_display_name="License" setting_name="license" help_tip="seafile_license.txt" %}
|
||||
{% include "snippets/web_settings_form.html" %}
|
||||
{% endwith %}
|
||||
|
||||
<h4>{% trans "User" %}</h4>
|
||||
|
||||
{% with type="checkbox" setting_name="ENABLE_SIGNUP" setting_val=config_dict.ENABLE_SIGNUP %}
|
||||
@ -280,6 +284,7 @@ $('.web-setting-file-upload-input').change(function() {
|
||||
switch(input_name) {
|
||||
case 'logo': url = '{% url 'api-v2.1-admin-logo' %}'; break;
|
||||
case 'favicon': url = '{% url 'api-v2.1-admin-favicon' %}'; break;
|
||||
case 'license': url = '{% url 'api-v2.1-admin-license' %}'; break;
|
||||
}
|
||||
|
||||
fd.append(input_name, file);
|
||||
|
@ -71,6 +71,7 @@ from seahub.api2.endpoints.admin.logs import AdminLogs
|
||||
from seahub.api2.endpoints.admin.org_users import AdminOrgUsers, AdminOrgUser
|
||||
from seahub.api2.endpoints.admin.logo import AdminLogo
|
||||
from seahub.api2.endpoints.admin.favicon import AdminFavicon
|
||||
from seahub.api2.endpoints.admin.license import AdminLicense
|
||||
|
||||
# Uncomment the next two lines to enable the admin:
|
||||
#from django.contrib import admin
|
||||
@ -295,6 +296,7 @@ urlpatterns = patterns(
|
||||
## admin::logo
|
||||
url(r'^api/v2.1/admin/logo/$', AdminLogo.as_view(), name='api-v2.1-admin-logo'),
|
||||
url(r'^api/v2.1/admin/favicon/$', AdminFavicon.as_view(), name='api-v2.1-admin-favicon'),
|
||||
url(r'^api/v2.1/admin/license/$', AdminLicense.as_view(), name='api-v2.1-admin-license'),
|
||||
|
||||
(r'^avatar/', include('seahub.avatar.urls')),
|
||||
(r'^notification/', include('seahub.notifications.urls')),
|
||||
|
28
tests/api/endpoints/admin/test_license.py
Normal file
28
tests/api/endpoints/admin/test_license.py
Normal file
@ -0,0 +1,28 @@
|
||||
import os
|
||||
import json
|
||||
from tests.common.utils import urljoin
|
||||
from tests.common.common import BASE_URL
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from seahub.settings import LICENSE_PATH
|
||||
from seahub.test_utils import BaseTestCase
|
||||
|
||||
class AdminLicenseTest(BaseTestCase):
|
||||
def setUp(self):
|
||||
self.login_as(self.admin)
|
||||
|
||||
def test_update_license(self):
|
||||
|
||||
license_dir = os.path.dirname(LICENSE_PATH)
|
||||
if os.path.exists(LICENSE_PATH):
|
||||
os.remove(LICENSE_PATH)
|
||||
assert not os.path.exists(LICENSE_PATH)
|
||||
|
||||
url = reverse('api-v2.1-admin-license')
|
||||
url = urljoin(BASE_URL, url)
|
||||
license_file = os.path.join(os.getcwd(), 'media/hello.hi')
|
||||
with open(license_file) as f:
|
||||
json_resp = self.client.post(url, {'license': f})
|
||||
json_resp = json.loads(json_resp.content)
|
||||
assert json_resp['success'] == True
|
||||
assert os.path.exists(LICENSE_PATH)
|
Loading…
Reference in New Issue
Block a user