diff --git a/seahub/api2/endpoints/admin/license.py b/seahub/api2/endpoints/admin/license.py new file mode 100644 index 0000000000..a1adbf04d4 --- /dev/null +++ b/seahub/api2/endpoints/admin/license.py @@ -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) diff --git a/seahub/settings.py b/seahub/settings.py index 4b2278b3f5..8664ce50d0 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -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' diff --git a/seahub/templates/snippets/web_settings_form.html b/seahub/templates/snippets/web_settings_form.html index 22dda8d6c5..91c8cb6b0d 100644 --- a/seahub/templates/snippets/web_settings_form.html +++ b/seahub/templates/snippets/web_settings_form.html @@ -40,3 +40,16 @@ {% endif %} + +{% if type == 'license' %} +
+{% endif %} diff --git a/seahub/templates/sysadmin/settings.html b/seahub/templates/sysadmin/settings.html index aa4e246f68..59cb1d443e 100644 --- a/seahub/templates/sysadmin/settings.html +++ b/seahub/templates/sysadmin/settings.html @@ -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 %} +