def get_full_path(self, file_path, request, file_name): path = Path() if file_path == 'output': return path.get_output_path(request, file_name) elif file_path == 'upload': return path.get_upload_path(request, file_name) else: raise AttributeError(gettext('無此file_path:') + file_path)
def get(self, request, *arg, **kwargs): path = Path() file_name = kwargs.get('csv_name') caller = path.get_caller(request) request_dict = {} request_dict = self.set_url_path(request_dict, caller, file_name) request_dict['file_name'] = file_name request_dict['caller'] = caller request_dict['machine_learning_list'] = MachineLearning.SUPPORT_LIST return render(request, 'general/utility.html', request_dict)
def get(self, request, *arg, **kwargs): path = Path() caller = path.get_caller(request) file_name = kwargs.get('csv_name') form = self.get_empty_form() request_dict = {} request_dict = self.set_url_path(request_dict, caller, file_name) request_dict['caller'] = caller request_dict['file_name'] = file_name request_dict['form'] = form return render(request, caller + '/' + caller + '.html', request_dict)
def get(self, request, *arg, **kwargs): path = Path() caller = path.get_caller(request) username = request.user.get_username() file_name = str(request.GET.get('csv_name', None)) form = self.get_form(request.GET) if form.is_valid(): if ExecuteModel.objects.filter(user_name=username).exists(): return JsonResponse( { 'message': gettext('您正在執行另一個檔案,檔名為:' + ExecuteModel.objects.get( user_name=username).file_name + ',若想執行目前的檔案,請先回到首頁選擇執行新的去識別化') }, status=423) else: ExecuteModel.objects.create(user_name=username, file_name=file_name, caller=caller, finish=False) try: self.method_run(request) except BreakProgramException as e: logging.info(username + ' success stop file') ExecuteModel.objects.filter(user_name=username).delete() return JsonResponse({'message': gettext('程式已終止')}, status=404) except ParameterException as e: logging.error(username + ' parameter error') ExecuteModel.objects.filter(user_name=username).delete() return JsonResponse({'message': str(e)}, status=404) except Exception as e: logging.critical(username + 'AbstractMethodView unknown error, stop run', exc_info=True) ExecuteModel.objects.filter(user_name=username).delete() return JsonResponse( {'message': gettext('程式執行失敗,請稍後再試,若多次執行失敗,請聯絡服務人員為您服務')}, status=404) else: logging.info(username + ' run finish', exc_info=True) ExecuteModel.objects.filter(user_name=username).update( finish=True) return HttpResponse(status=204) logging.critical(username + 'AbstractMethodView unknown error, stop run', exc_info=True) return JsonResponse({'message': gettext('有尚未捕捉到的例外,請回報服務人員,謝謝')}, status=404) else: logging.info(username + ' form error', exc_info=True) return JsonResponse({'message': gettext('表單格式錯誤')}, status=400)
def handle_upload_file(self, request, file, dataframe): path = Path() fs = FileSystemStorage() file_path = path.get_upload_path(request, file.name) directory_path = path.get_upload_directory(request, file.name) if fs.exists(file_path): fs.delete(file_path) if not os.path.isdir(directory_path): os.makedirs(directory_path) dataframe.to_csv(file_path, index=False)
def get(self, request, *arg, **kwargs): path = Path() method = kwargs.get('method').lower() file_name = request.GET.get('File', None) caller = path.get_caller(request) if method == 'output': file_path = path.get_output_path(request, file_name, caller=caller) elif method == 'upload': file_path = path.get_upload_path(request, file_name, caller=caller) else: raise AttributeError(gettext('無此method:') + method) df = pd.read_csv(file_path, keep_default_na=False) tables = self.get_html(df.head(200)) return JsonResponse(tables, safe=False)
def post(self, request, *arg, **kwargs): path = Path() caller = path.get_caller(request) machine_learning_method = request.POST.get('machine_learning_method', None) machine_learning_mode = request.POST.get('machine_learning_mode', None) train_file_path = request.POST.get('train_file_path', None) test_file_path = request.POST.get('test_file_path', None) file_name = request.POST.get('csv_name', None) train_file_path = self.get_full_path(train_file_path, request, file_name) test_file_path = self.get_full_path(test_file_path, request, file_name) accuracy = 0 try: ml = MachineLearning(machine_learning_method, machine_learning_mode, train_file_path, test_file_path) ml.fit() if machine_learning_mode == 'classification': accuracy = ml.score() * 100 elif machine_learning_mode == 'regression': accuracy = ml.score() except ValueError as e: logging.critical('CheckUtilityView unknown error', exc_info=True) return JsonResponse( { 'accuracy': accuracy, 'message': gettext('程式執行失敗,請嘗試切換模型預測目標,若仍然失敗,請聯絡服務人員為您服務') }, status=404) except Exception as e: logging.critical('CheckUtilityView unknown error', exc_info=True) return JsonResponse( { 'accuracy': accuracy, 'message': gettext('程式執行失敗,請稍後再試,若多次執行失敗,請聯絡服務人員為您服務') }, status=404) else: return JsonResponse({'accuracy': accuracy}, status=200) return JsonResponse({'message': gettext('有尚未捕捉到的例外,請回報服務人員,謝謝')}, status=404)
def get(self, request, *arg, **kwargs): path = Path() file_name = kwargs.get('csv_name') caller = path.get_caller(request) username = request.user.get_username() try: ExecuteModel.objects.filter(user_name=username).delete() except Exception as e: logging.warning(username + ' not in ExecuteModel(FinishView)', exc_info=True) request_dict = {} request_dict = self.set_url_path(request_dict, caller, file_name) request_dict['file_name'] = file_name request_dict['caller'] = caller return render(request, 'general/execute_finish.html', request_dict)
def get_request_dict(self, request, *arg, **kwargs): parser = JsonParser() path = Path() string_element_dict = {} # column_title - element file_name = kwargs.get('csv_name') caller = path.get_caller(request) file_path = path.get_upload_path(request, file_name, caller=caller) data_frame = DataframeDetection(file_path) string_element_dict = data_frame.get_file_string_element(almost_number_filter=False) request_dict = {} request_dict = self.set_url_path(request_dict, caller, file_name) request_dict['string_element_dict'] = string_element_dict request_dict['caller'] = caller request_dict['file_name'] = file_name request_dict['custom_mode'] = 'json_parser' return request_dict
def get(self, request, *arg, **kwargs): path = Path() username = request.user.get_username() file = None if ExecuteModel.objects.filter(user_name=username).exists(): file = ExecuteModel.objects.get(user_name=username) request_dict = {} request_dict['finish'] = file.finish return JsonResponse(request_dict) return JsonResponse(None, safe=False)
def post(self, request, *arg, **kwargs): parser = JsonParser() path = Path() username = request.user.get_username() file_path = str(request.POST.get('path', None)) file_name = str(request.POST.get('csv_name',None)) pair_dict = request.POST.get('number_title_pair_dict', None) almost_number_dict = request.POST.get('almost_number_dict', None) almost_number_is_empty_dict = request.POST.get('almost_number_is_empty_dict', None) interval_dict = request.POST.get('interval_dict', None) type_pair = request.POST.get('type_pair', None) if pair_dict: pair_dict = json.loads(pair_dict) if almost_number_dict: almost_number_dict = json.loads(almost_number_dict) if almost_number_is_empty_dict: almost_number_is_empty_dict = json.loads(almost_number_is_empty_dict) if type_pair: type_pair = json.loads(type_pair) caller = path.get_caller(request) file_path = path.get_upload_root(request, caller=caller) try: if interval_dict: interval_dict = json.loads(interval_dict) parser.create_DPView_json_file(file_path, file_name,pair_dict, type_pair=type_pair, interval_dict=interval_dict, almost_number_dict=almost_number_dict, almost_number_is_empty_dict=almost_number_is_empty_dict,) except Exception as e: logging.critical(username + ' ParserView run fail', exc_info=True) JsonResponse({"message":gettext("程式執行失敗,請稍後再試,若多次執行失敗,請聯絡服務人員為您服務")}, status=404) else: logging.info(username + ' ParserView run success') return HttpResponse(status=204) logging.critical(username + ' unknown error', exc_info=True) return JsonResponse({"message":gettext("有尚未捕捉到的例外,請回報服務人員,謝謝")}, status=404)
def get(self, request, *arg, **kwargs): path = Path() file_name = kwargs.get('csv_name') directory_name = file_name.split('.')[-2] username = request.user.get_username() caller = path.get_caller(request) file_path = path.get_output_path(request, file_name) df = pd.read_csv(file_path, keep_default_na=False) if caller == 't_Closeness': caller = 'k_Anonymity' download_name = caller + '_' + directory_name + '_output.csv' download_name = urllib.parse.quote(download_name) response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = "attachment; filename=\"%s\"; filename*=utf-8''%s" % ( download_name, download_name) df.to_csv(path_or_buf=response, index=False, decimal=',') return response
def get_request_dict(self, request, *arg, **kwargs): path = Path() file_name = kwargs.get('csv_name') caller = path.get_caller(request) file_path = path.get_upload_path(request, file_name, caller=caller) data_frame = DataframeDetection(file_path) type_pair = data_frame.get_type_pair() number_title_list = data_frame.get_number_title(type_pair=type_pair) number_type_pair = data_frame.get_number_type_pair(number_title_list, type_pair=type_pair) max_value_dict, min_value_dict = data_frame.get_number_limit(number_title_list, number_type_pair=number_type_pair) request_dict = super().get_request_dict(request, *arg, **kwargs) request_dict['type_pair'] = type_pair request_dict['advanced_settings'] = True request_dict['number_title_list'] = number_title_list request_dict['number_type_pair'] = number_type_pair request_dict['max_value_dict'] = max_value_dict request_dict['min_value_dict'] = min_value_dict return request_dict
def get(self, request, *arg, **kwargs): path = Path() file_name = request.GET.get('csv_name', None) caller = path.get_caller(request) file_path = path.get_upload_path(request, file_name, caller=caller) try: df = pd.read_csv(file_path) result = self.title_check(df) if result: return result except Exception as e: logging.critical('TitleCheckView unknown error', exc_info=True) return JsonResponse( {'message': gettext('程式執行失敗,請稍後再試,若多次執行失敗,請聯絡服務人員為您服務')}, status=404) else: return HttpResponse(status=204) logging.critical('TitleCheckView unknown error', exc_info=True) return JsonResponse({'message': gettext('有尚未捕捉到的例外,請回報服務人員,謝謝')}, status=404)
def post(self, request, *arg, **kwargs): parser = JsonParser() path = Path() username = request.user.get_username() file_name = request.POST.get('csv_name', None) structure_mode = json.loads(request.POST.get('structure_mode', None)) structure_dict = json.loads(request.POST.get('structure_dict', None)) interval_dict = request.POST.get('interval_dict', None) type_pair = request.POST.get('type_pair', None) if type_pair: type_pair = json.loads(type_pair) caller = path.get_caller(request) file_path = path.get_upload_root(request, caller=caller) try: for key in structure_mode: if structure_mode[key] == 'custom': structure_dict[key] = self.pair_check(structure_dict[key]) if interval_dict: interval_dict = json.loads(interval_dict) parser.create_json_file(file_path, file_name, structure_mode, structure_dict, type_pair=type_pair, interval_dict=interval_dict,) except NotAddressException as e: return JsonResponse({"message":str(e)}, status=400) except PairLoopException as e: return JsonResponse({"message":str(e)}, status=400) except Exception as e: logging.critical(username + ' ParserView run fail', exc_info=True) return JsonResponse({"message":gettext("程式執行失敗,請稍後再試,若多次執行失敗,請聯絡服務人員為您服務")}, status=404) else: logging.info(username + ' ParserView run success') return HttpResponse(status=204) logging.critical(username + ' unknown error', exc_info=True) return JsonResponse({"message":gettext("有尚未捕捉到的例外,請回報服務人員,謝謝")}, status=404)