def get_model_data(request, model_name): logger.debug('run api_get_model_data') return_data = {'success': False, 'msg': ''} get_default_form = request.GET.get('get_default_form', False) if model_name == 'client': model = Client model_form = ClientForm data = model.get_all_in_json() elif model_name == 'endpoint': model = Endpoint model_form = EndpointForm data = model.get_all_in_json() elif model_name == 'client_endpoint': model = ClientEndpoint model_form = None client_id = request.GET.get('client_id') data = model.get_all_in_json(client_id) else: model = None model_form = None data = [] if model is None: raise error_404(request) # 获取一个缺省值用来添加和编辑数据 if get_default_form: t = model_form.get_default_form_json() return_data['default_form'] = t return_data['data'] = data return_data['success'] = True return http_response_json(return_data)
def help_page(request, page_name): request.page_title = page_name try: return render_to_response("dashboard/help/" + page_name.replace('-', '_').lower() + ".html", {'request': request}) except Exception as e: logger.error(e) return error_404(request)
def help_page(request, page_name): request.page_title = page_name try: return render_to_response( "dashboard/help/" + page_name.replace('-', '_').lower() + ".html", {'request': request}) except Exception as e: logger.error(e) return error_404(request)
def decorator(request, *args, **kwargs): if request.site_user.is_authenticated() and request.site_user.is_admin: return view(request, *args, **kwargs) else: # 如果是通过API调用的方法,则返回错误信息,而不是跳转到登录页面 content_type = request.META.get("CONTENT_TYPE", "").lower() http_accept = request.META.get("HTTP_ACCEPT", "").lower() if content_type.startswith("application/json") or "application/json" in http_accept: return http_response_json({"success": False, "msg": "登录会话已超时,请刷新页面重新登录"}) return error_404(request)
def create_admin(request): """ 网站刚开始运行时,没有管理员,需要创建一个 :param request: :return: """ if SiteUser.has_admin(): return error_404(request) else: request.page_title = '创建管理员' return render_to_response('accounts/create_admin.html', {'request': request})
def decorator(request, *args, **kwargs): if request.site_user.is_authenticated() and request.site_user.is_admin: return view(request, *args, **kwargs) else: # 如果是通过API调用的方法,则返回错误信息,而不是跳转到登录页面 content_type = request.META.get('CONTENT_TYPE', '').lower() http_accept = request.META.get('HTTP_ACCEPT', '').lower() if content_type.startswith("application/json") \ or 'application/json' in http_accept: return http_response_json({ 'success': False, 'msg': '登录会话已超时,请刷新页面重新登录' }) return error_404(request)
def import_config(request): """ 上传文件,导入配置 """ if request.FILES: success, msg, errors = False, '', [] for _file in request.FILES: # 关闭了分块上传,上传上来的就是完整的 # 只允许选择一份文件,处理完就break success, msg, errors = do_import_config(request.FILES[_file]) break return http_response_json({'success': success, 'msg': msg, 'errors': errors}) else: raise error_404(request)
def account_profile(request): """ 个人信息查看页面,也是密码修改页面 :param request: :return: """ request.page_title = '个人信息' request.current_page = 'accounts' if hasattr(request, 'site_user'): user = request.site_user else: return error_404(request) return render_to_response('accounts/profile.html', {'request': request, 'user': user})
def config(request, data_type='client'): """ 配置 :param request: :return: """ if data_type == 'client': request.page_title = '应用配置' display_breadcrumb = True elif data_type == 'endpoint': request.page_title = 'API 端点' display_breadcrumb = False elif data_type == 'client_endpoint': request.page_title = '应用配置' display_breadcrumb = True elif data_type == 'import-export': request.page_title = '导入导出' display_breadcrumb = False else: return error_404(request) request.current_page = 'config' config_info = { 'data_type': data_type, 'display_breadcrumb': display_breadcrumb, 'client_id': None, 'client_name': None } client_id = request.GET.get('client_id') if client_id: client = Client.get_client(client_id) if client: config_info['client_id'] = client.id config_info['client_name'] = client.name logger.debug(config_info) return render_to_response('dashboard/config.html', { 'request': request, 'config_info': config_info })
def password_reset_confirm(request, user_id, token): """ 密码重置页面 :param request: :param user_id: :param token: :return: """ request.page_title = '重置密码' try: user = SiteUser.objects.get(id=user_id) token_generator = PasswordResetTokenGenerator() check_result = token_generator.check_token(user, token) request.token = token if check_result: return render_to_response('accounts/password_reset_confirm.html', {'request': request, 'user': user, 'check_result': check_result}) except SiteUser.DoesNotExist: return error_404(request)
def api_create_admin(request): """ 网站刚开始运行时,没有管理员,需要创建一个 :param request: :return: """ logger.debug('api_create_admin') msg, success, data = '', False, [] if SiteUser.has_admin(): return error_404(request) else: logger.debug(request.body) post_data = json_loads(request.body) form = UserCreationForm(post_data) if form.is_valid(): form.save(is_admin=True) # 将数据保存到数据库中 success = True response = {'success': success, 'msg': msg, 'data': data} logger.debug(data) return http_response_json(response)
def config(request, data_type='client'): """ 配置 :param request: :return: """ if data_type == 'client': request.page_title = '应用配置' display_breadcrumb = True elif data_type == 'endpoint': request.page_title = 'API 端点' display_breadcrumb = False elif data_type == 'client_endpoint': request.page_title = '应用配置' display_breadcrumb = True elif data_type == 'import-export': request.page_title = '导入导出' display_breadcrumb = False else: return error_404(request) request.current_page = 'config' config_info = { 'data_type': data_type, 'display_breadcrumb': display_breadcrumb, 'client_id': None, 'client_name': None } client_id = request.GET.get('client_id') if client_id: client = Client.get_client(client_id) if client: config_info['client_id'] = client.id config_info['client_name'] = client.name logger.debug(config_info) return render_to_response('dashboard/config.html', {'request': request, 'config_info': config_info})
def test(request): from .tasks import transfer_access_logs transfer_access_logs() return error_404(request)