def post(self, request): if not request.user.admin_permissions.can_config_system(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') favicon_file = request.FILES.get('favicon', None) if not favicon_file: error_msg = 'Favicon can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(favicon_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get(IMAGE)) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if favicon_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(favicon_file.size, 20 * 1024 * 1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: custom_favicon_file = os.path.join(SEAHUB_DATA_ROOT, CUSTOM_FAVICON_PATH) # save favicon file to custom dir with open(custom_favicon_file, 'wb') as fd: fd.write(favicon_file.read()) custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) # create symlink for custom dir if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({ 'favicon_path': get_service_url() + MEDIA_URL + CUSTOM_FAVICON_PATH })
def test_update_favicon_with_invalid_filetype(self): with open('test.noico', 'w') as f: f.write('hello') logo_url = reverse('api-v2.1-admin-favicon') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'test.noico') with open(logo_file) as f: resp = self.client.post(logo_url, {'favicon': f}) json_resp = json.loads(resp.content) assert resp.status_code == 400 assert json_resp['error_msg'] == file_type_error_msg('noico', PREVIEW_FILEEXT.get(IMAGE))
def test_update_favicon_with_invalid_filetype(self): with open('test.noico', 'w') as f: f.write('hello') logo_url = reverse('api-v2.1-admin-favicon') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'test.noico') with open(logo_file, 'rb') as f: resp = self.client.post(logo_url, {'favicon': f}) json_resp = json.loads(resp.content) assert resp.status_code == 400 assert json_resp['error_msg'] == file_type_error_msg('noico', PREVIEW_FILEEXT.get(IMAGE))
def test_update_logo_with_invalid_file_type(self): with open('test.noimage', 'w') as f: f.write('1') logo_url = reverse('api-v2.1-admin-logo') logo_url = urljoin(BASE_URL, logo_url) logo_file = os.path.join(os.getcwd(), 'test.noimage') with open(logo_file, 'rb') as f: resp = self.client.post(logo_url, {'logo': f}) json_resp = json.loads(resp.content) os.remove(logo_file) assert 400 == resp.status_code assert json_resp['error_msg'] == file_type_error_msg( 'noimage', PREVIEW_FILEEXT.get(IMAGE))
def test_update_logo_with_invalid_file_type(self): with open('test.noimage', 'w') as f: f.write('hello') image_url = reverse('api-v2.1-admin-login-background-image') image_url = urljoin(BASE_URL, image_url) image_file = os.path.join(os.getcwd(), 'test.noimage') with open(image_file, 'rb') as f: resp = self.client.post(image_url, {'login_bg_image': f}) json_resp = json.loads(resp.content) os.remove(image_file) assert 400 == resp.status_code assert json_resp['error_msg'] == file_type_error_msg("noimage", PREVIEW_FILEEXT.get('Image'))
def post(self, request): image_file = request.FILES.get('login_bg_image', None) if not image_file: error_msg = 'Image can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) file_type, ext = get_file_type_and_ext(image_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get('Image')) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if image_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(image_file.size, 20 * 1024 * 1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) custom_login_bg_image_path = get_custom_login_bg_image_path() custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(custom_login_bg_image_path)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: custom_login_bg_image_file = os.path.join( SEAHUB_DATA_ROOT, custom_login_bg_image_path) # save login background image file to custom dir with open(custom_login_bg_image_file, 'wb') as fd: fd.write(image_file.read()) custom_symlink = os.path.join( MEDIA_ROOT, os.path.dirname(custom_login_bg_image_path)) # create symlink for custom dir if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({ 'login_bg_image_path': get_service_url() + MEDIA_URL + CUSTOM_LOGIN_BG_PATH })
def post(self, request): if not request.user.admin_permissions.can_config_system(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') logo_file = request.FILES.get('logo', None) if not logo_file: error_msg = 'Logo can not be found.' logger.error(error_msg) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(logo_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get(IMAGE)) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if logo_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(logo_file.size, 20 * 1024 * 1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(CUSTOM_LOGO_PATH)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: # save logo file to custom dir custom_logo_file = os.path.join(SEAHUB_DATA_ROOT, CUSTOM_LOGO_PATH) image = Image.open(logo_file) image.save(custom_logo_file) # create symlink for custom dir custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_LOGO_PATH)) if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response( {'logo_path': get_service_url() + MEDIA_URL + CUSTOM_LOGO_PATH})
def post(self, request): favicon_file = request.FILES.get('favicon', None) if not favicon_file: error_msg = 'Favicon can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(favicon_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get(IMAGE)) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if favicon_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(favicon_file.size, 20*1024*1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: custom_favicon_file = os.path.join(SEAHUB_DATA_ROOT, CUSTOM_FAVICON_PATH) # save favicon file to custom dir with open(custom_favicon_file, 'w') as fd: fd.write(favicon_file.read()) custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) # create symlink for custom dir if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def post(self, request): image_file = request.FILES.get('login_bg_image', None) if not image_file: error_msg = 'Image can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) file_type, ext = get_file_type_and_ext(image_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get('Image')) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if image_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(image_file.size, 20*1024*1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) custom_login_bg_image_path = get_custom_login_bg_image_path() custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(custom_login_bg_image_path)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: custom_login_bg_image_file = os.path.join(SEAHUB_DATA_ROOT, custom_login_bg_image_path) # save login background image file to custom dir with open(custom_login_bg_image_file, 'w') as fd: fd.write(image_file.read()) custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(custom_login_bg_image_path)) # create symlink for custom dir if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def post(self, request): logo_file = request.FILES.get('logo', None) if not logo_file: error_msg = 'Logo can not be found.' logger.error(error_msg) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(logo_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get(IMAGE)) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if logo_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(logo_file.size, 20*1024*1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(CUSTOM_LOGO_PATH)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: # save logo file to custom dir custom_logo_file = os.path.join(SEAHUB_DATA_ROOT, CUSTOM_LOGO_PATH) image = Image.open(logo_file) image.save(custom_logo_file) # create symlink for custom dir custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_LOGO_PATH)) if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})