def col_rule_condition_check(request): """ Will check whether the conditions of a column-rm are contentually correct. """ if "when_is" in request.GET and "when_contains" in request.GET \ and "when_value" in request.GET and not ArgsChecker.str_is_malicious(request.GET["when_value"]) \ and "then_apply" in request.GET and "then_replace" in request.GET \ and "then_value" in request.GET and not ArgsChecker.str_is_malicious(request.GET["then_value"]) \ and "subject_id" in request.GET and ArgsChecker.is_number(request.GET["subject_id"]): return True return False
def do_select_all(request): """ Will automatically select all columns of the TQ (request['tq_id']) to the current TF of the project. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "tq_id" in request.GET and ArgsChecker.is_number( request.GET["tq_id"]): proj = Project.objects.get(pk=valid_user.last_opened_project_id) ff = FinalFusion.objects.get(project=proj) tq = TQFile.objects.get(pk=request.GET["tq_id"], project=proj, archived=False) for dic in json.loads(tq.content_json): for col in dic.keys(): if len( FinalFusionColumn.objects.filter( source_tq=tq, archived=False, source_column_name=col)) == 0: FinalFusionColumn.objects.create(final_fusion=ff, source_tq=tq, source_column_name=col, display_column_name=col, rows_json=json.dumps( tq.get_column(col))) break success = True return HttpResponse(json.dumps({"success": success}))
def do_rename(request): """ Will rename a TQ according to request['name']. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "name" in request.GET and not ArgsChecker.str_is_malicious(request.GET["name"]) \ and "id" in request.GET and not ArgsChecker.str_is_malicious(request.GET["id"]): tq = TQFile.objects.get(pk=request.GET["id"]) tq.display_file_name = request.GET["name"] tq.save() success = True return HttpResponse(json.dumps({"success": success}))
def do_select_column(request): """ Will add or remove a column of a contextual TQ to the TF of the project. If this column has been already added to the TF, its respective FFC will be archived and thereby removed. request["tq_id"]: ID of the TQ, needed to select the columnf rom. request["col_name"]: Name of the column to select/deselect from the TF. """ added = False valid_user = token_checker.token_is_valid(request) if valid_user and "tq_id" in request.GET and ArgsChecker.is_number(request.GET["tq_id"]) \ and "col_name" in request.GET and not ArgsChecker.str_is_malicious(request.GET["col_name"]): tq_id = request.GET["tq_id"] col_name = request.GET["col_name"] try: tq = TQFile.objects.get(pk=tq_id) col = tq.get_column(col_name) ef = FinalFusion.objects.get(project=Project.objects.get( pk=valid_user.last_opened_project_id)) ffc_fetch = FinalFusionColumn.objects.filter( final_fusion=ef, source_tq=tq, source_column_name=col_name) if len(ffc_fetch) == 0: # FFC/Column wasn't found, meaning it hasn't been selected yet = Add to TF by creation. FinalFusionColumn.objects.create(final_fusion=ef, source_tq=tq, source_column_name=col_name, display_column_name=col_name, rows_json=json.dumps(col)) added = True elif len(ffc_fetch) == 1: # FFC was found, meaning it was already selected = Remove by archiving. if not ffc_fetch[0].archived: ffc_fetch[0].archived = True ffc_fetch[0].save() added = False else: ffc_fetch[0].archived = False ffc_fetch[0].save() added = True except ObjectDoesNotExist: pass return HttpResponse(json.dumps({"added": added}))
def do_append_cols(request): """ Will append two columns of a TF into a new, dynamic one: A-Column + B-Column = AB-Column by appending B-Rows under A-Rows. If input instructs, the source A- and B-Columns will be removed from the TF after appending. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "a_id" in request.GET and ArgsChecker.is_number(request.GET["a_id"]) \ and "b_id" in request.GET and ArgsChecker.is_number(request.GET["b_id"]) and \ "remove_cols" in request.GET and not ArgsChecker.str_is_malicious(request.GET["remove_cols"]): try: a_col = FinalFusionColumn.objects.get(pk=request.GET["a_id"]) b_col = FinalFusionColumn.objects.get(pk=request.GET["b_id"]) appended_rows = json.loads(a_col.rows_json) for r in json.loads(b_col.rows_json): appended_rows.append(r) FinalFusionColumn.objects.create( final_fusion=a_col.final_fusion, source_column_name="%s~ + %s~" % (a_col.display_column_name[:2], b_col.display_column_name[:2]), display_column_name="%s~ + %s~" % (a_col.display_column_name[:2], b_col.display_column_name[:2]), rows_json=json.dumps(appended_rows), rm_dependency=None, manually_removable=True ) if request.GET["remove_cols"] == "true": a_col.archived = True a_col.save() b_col.archived = True b_col.save() success = True except ObjectDoesNotExist as oe: pass return HttpResponse(json.dumps({ "success": success, }))
def do_sign_up(request): """ do_sign_up """ success = False msg = "" token = None if "email" in request.POST and not ArgsChecker.str_is_malicious(request.POST["email"]) \ and "pw1" in request.POST and not ArgsChecker.str_is_malicious(request.POST["pw1"]) \ and "pw2" in request.POST and not ArgsChecker.str_is_malicious(request.POST["pw2"]): email = request.POST["email"] pw1 = request.POST["pw1"] pw2 = request.POST["pw2"] if "@daimler.com" not in email: msg = "Email not from company" elif pw1 != pw2: msg = "Passwords don't match" elif len(pw1) < 8: msg = "Password too short" else: if len(User.objects.filter(email=email)) == 0: try: user = User.objects.create(username=email, email=email, password=make_password(pw1)) user_group = Group.objects.get(name='Users') user_group.user_set.add(user) token = UserProfile.objects.get(user=user).token.code success = True except Exception as exc: print(exc) msg = str(exc) else: msg = "User already existing" response = HttpResponse(json.dumps({"success": success, "msg": msg})) response.set_cookie("token", token, max_age=86400 * 7) return response
def do_create(request): """ Will create a note and attach it to the current project of the user. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "name" in request.GET and not ArgsChecker.str_is_malicious(request.GET["name"]) \ and "content" in request.GET and not ArgsChecker.str_is_malicious(request.GET["content"]): ProjectNote.objects.create(name=request.GET["name"], content=request.GET["content"], project=Project.objects.get( pk=valid_user.last_opened_project_id, archived=False)) success = True return HttpResponse(json.dumps({ "success": success, }))
def request_to_col_rm(request, _id=None): """ :param request: The request from the rule module UI. :param _id: Is not none when this is a edit-case. If edit, only changes will be overridden. Will turn a web request for a column-rm to an actual rule module object. """ get_params = convert_request_bool_values(request.GET) subject_id = get_params["subject_id"] when_is = get_params["when_is"] name = get_params["name"] when_contains = get_params["when_contains"] when_value = get_params["when_value"] then_apply = get_params["then_apply"] then_replace = get_params["then_replace"] then_value = get_params["then_value"] rm = RuleModule() if _id: rm = RuleModule.objects.get(pk=_id) if len(when_value) > 0 and len(then_value) > 0: try: ffc = FinalFusionColumn.objects.get(pk=subject_id) rm.rule_type = "col" rm.col_subject = json.dumps([ffc.pk]) if not ArgsChecker.str_is_malicious(name) and ((when_is and not when_contains) or (not when_is and when_contains)) \ and ((then_apply and not then_replace) or (not then_apply and then_replace)): # Construct if condition if_condition = {} if when_is: if_condition["when_is"] = when_value elif when_contains: if_condition["when_contains"] = when_value # Construct then then_case = {} if then_apply: then_case["then_apply"] = then_value elif then_replace: then_case["then_replace"] = then_value rm.if_conditions = json.dumps(if_condition) rm.then_cases = json.dumps(then_case) rm.final_fusion = ffc.final_fusion rm.name = name return rm except ObjectDoesNotExist: return None
def do_rename(request): """ Will rename the currently opened project. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "name" in request.GET and not ArgsChecker.str_is_malicious(request.GET["name"]): project = Project.objects.get(pk=valid_user.last_opened_project_id) project.name = request.GET["name"] project.save() success = True return HttpResponse(json.dumps({"success": success}))
def do_rename(request): """ Will rename a single FFC. Will replace () to [] to prevent internal mismatching. If the FFC has a rule module dependency (i. e. the FFC is dynamic), the rule module itself will be updated also. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]) \ and "name" in request.GET and not ArgsChecker.str_is_malicious(request.GET["name"]): try: name = request.GET["name"] name = name.replace("(", "[") name = name.replace(")", "]") ffc = FinalFusionColumn.objects.get(pk=request.GET["id"], archived=False) ffc.display_column_name = name ffc.save() success = True # Check if this FFC is a dynamic column of if ffc.rm_dependency: rm = ffc.rm_dependency then_cases = json.loads(rm.then_cases) for tc in then_cases: if tc["id"] == ffc.pk: tc["ffc_name"] = request.GET["name"] break rm.then_cases = json.dumps(then_cases) rm.save() except ImportError: pass return HttpResponse(json.dumps({"success": success}))
def do_login(request): success = False msg = "" user = None pw_correct = False if "email" in request.POST and not ArgsChecker.str_is_malicious(request.POST["email"]) \ and "pw" in request.POST and not ArgsChecker.str_is_malicious(request.POST["pw"]): email = request.POST["email"] pw = request.POST["pw"] if "@daimler.com" not in email: msg = "Email not from company" elif len(pw) < 8: msg = "Password too short" else: try: user = UserProfile.objects.get(user__email=email) if check_password(pw, user.user.password): token = SecurityToken() token.generate_token_code() token.save() user.token = token user.save() pw_correct = True success = True except ObjectDoesNotExist: msg = "User not registered" response = HttpResponse(json.dumps({"success": success, "msg": msg})) if pw_correct: response.set_cookie("token", user.token.code, max_age=86400 * 7) return response
def render_filtered(request): """ Will render rule modules which contain 'filter_name' in their names. Used for rm-search. """ success = False items = [] valid_user = token_checker.token_is_valid(request) if valid_user and "filter" in request.GET and not ArgsChecker.str_is_malicious(request.GET["filter"]): success = True filter_name = request.GET["filter"].strip() types_display = { "col": "COL", "row": "ROW", "script": "SCR" } projects = Project.objects.filter(user_profile=valid_user, archived=False) for p in projects: ff = FinalFusion.objects.get(project=p) rule_modules = RuleModule.objects.filter(final_fusion=ff, archived=False) script_modules = ScriptModule.objects.filter(final_fusion=ff, archived=False) for rm in rule_modules: if filter_name.lower() in rm.name.lower(): items.append({ "project_name": p.name, "id": rm.pk, "name": rm.name, "type": rm.rule_type, "type_display": types_display[rm.rule_type] }) for sm in script_modules: if filter_name in sm.name: items.append({ "project_name": p.name, "id": sm.pk, "name": sm.name, "type": "script", "type_display": types_display["script"] }) return HttpResponse(json.dumps({ "success": success, "items": json.dumps(items) }))
def i_render_single_tq(request): """ Will render a single TQ with its metadata by inclusion. """ valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number( request.GET["id"]): tq = TQFile.objects.get(pk=request.GET["id"], archived=False) dic = { "id": tq.pk, "name": tq.display_file_name, "created": tq.creation_date.strftime('%d.%m.%Y') } return dashboard_includer.get_as_json("tq_file/_view.html", template_context=dic)
def do_save_edit_col(request): """ Will edit a col-rm by taking the existing one and overriding the new changes. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]) \ and col_rule_condition_check(request): rm = request_to_col_rm(request, request.GET["id"]) rm.save() if rm: success = True return HttpResponse(json.dumps({"success": success}))
def do_rename_rm(request): """ Will rename a rule module. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "name" in request.GET and not ArgsChecker.str_is_malicious(request.GET["name"]) \ and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]) \ and "type" in request.GET and not ArgsChecker.str_is_malicious(request.GET["type"]): try: rm = None if request.GET["type"] == "rm": rm = RuleModule.objects.get(pk=request.GET["id"]) elif request.GET["type"] == "script": rm = ScriptModule.objects.get(pk=request.GET["id"]) rm.name = request.GET["name"] rm.save() success = True except ObjectDoesNotExist: pass return HttpResponse(json.dumps({"success": success}))
def do_rename(request): """ Will rename a TF according to the submitted info. The name will be applied for the EF, too. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "name" in request.GET and not ArgsChecker.str_is_malicious(request.GET["name"]): proj = Project.objects.get(pk=valid_user.last_opened_project_id) ff = FinalFusion.objects.get(project=proj) ff.name = request.GET["name"] ff.save() success = True return HttpResponse(json.dumps({"success": success}))
def do_delete_project(request): """ Will delete a project by archiving. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user: if "id" in request.GET and ArgsChecker.is_number(request.GET["id"]): try: project = Project.objects.get(pk=request.GET["id"], user_profile=valid_user) project.archived = True project.save() success = True except ObjectDoesNotExist: pass return HttpResponse(json.dumps({"success": success}))
def do_delete(request): """ Will remove a TQ by archiving it. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and not ArgsChecker.str_is_malicious( request.GET["id"]): tq = TQFile.objects.get(pk=request.GET["id"]) tq.archived = True tq.save() success = True return HttpResponse(json.dumps({"success": success}))
def do_delete_rm(request): """ Will delete a rule module by archiving. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]): try: ff = FinalFusion.objects.get(project=Project.objects.get(pk=valid_user.last_opened_project_id)) rm = RuleModule.objects.get(pk=request.GET["id"], final_fusion=ff) rm.archived = True rm.save() success = True except ObjectDoesNotExist: pass return HttpResponse(json.dumps({"success": success}))
def do_save_edit_row(request): """ Will edit a row-rm by taking the existing one and overriding the new changes. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]) \ and "when_data" in request.GET and "then_data" in request.GET: try: when_data = json.loads(request.GET["when_data"]) then_data = json.loads(request.GET["then_data"]) rm = data_to_row_rm(when_data, then_data, request.GET["id"]) if rm: success = True except TypeError as te: print("do_create_row_rm: TypeError") return HttpResponse(json.dumps({"success": success}))
def do_edit(request): """ Will edit a rule module by overriding the differences. """ sucess = False valid_user = token_checker.token_is_valid(request) if valid_user and "code" in request.POST and "id" in request.POST and ArgsChecker.is_number( request.POST["id"]): try: sm = ScriptModule.objects.get(pk=request.POST["id"], archived=False) sm.code_content = request.POST["code"] sm.save() except ObjectDoesNotExist: pass sucess = True return HttpResponse(json.dumps({"success": sucess}))
def do_delete(request): """ Will remove a note by archiving. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number( request.GET["id"]): try: n = ProjectNote.objects.get(pk=request.GET["id"]) n.archived = True n.save() success = True except ObjectDoesNotExist: pass return HttpResponse(json.dumps({ "success": success, }))
def do_load(request): """ Will open/load a new project and save the project's ID into the user's profile, to make sure that the same project gets automatically reopened when the user return to the app. """ success = False name = None valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]): project = Project.objects.get(pk=request.GET["id"]) valid_user.last_opened_project_id = project.pk valid_user.save() success = True name = project.name return HttpResponse(json.dumps( { "success": success, "name": name }))
def do_apply_shared_settings(request): """ Will apply whether duplicates should be exported (in EF) or not, by saving that setting to the FF-Model itself. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "setting" in request.GET and not ArgsChecker.str_is_malicious(request.GET["setting"]): setting = request.GET["setting"] proj = Project.objects.get(pk=valid_user.last_opened_project_id) if setting == "false": proj.shared = False elif setting == "true": proj.shared = True proj.save() success = True return HttpResponse(json.dumps({"success": success}))
def render_single_tq_table(request): """ Will render the contents of a TQ. """ success = False table_data = None flattened = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number( request.GET["id"]): tq = TQFile.objects.get(pk=request.GET["id"]) table_data = tq.get_as_table(valid_user) flattened = tq.has_been_flattened success = True return HttpResponse( json.dumps({ "success": success, "table_data": table_data, "has_been_flattened": flattened }))
def do_remove_appended(request): """ Simply a remove function for FFCs. Since 'normal' FFCs can be removed by deselection (via TQ UI), dynamic or appended rows have no direct TQ-Source and therefore need to be removed manually. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]): try: app_col = FinalFusionColumn.objects.get(pk=request.GET["id"], manually_removable=True) app_col.archived = True app_col.save() success = True except ObjectDoesNotExist as oe: pass return HttpResponse(json.dumps({ "success": success, }))
def render_single(request): """ Will render a single script-rm for the UI. """ success = False single = None valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number( request.GET["id"]): try: sm = ScriptModule.objects.get(pk=request.GET["id"], archived=False) success = True single = {"name": sm.name, "code_content": sm.code_content} except ObjectDoesNotExist: pass return HttpResponse( json.dumps({ "success": success, "obj": json.dumps(single) }))
def render_single(request): """ Will return a JSON-Object of details about the current rm. """ success = False single = None valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]): try: rm = RuleModule.objects.get(pk=request.GET["id"], archived=False) success = True single = { "name": rm.name, "type": rm.rule_type, "if_conditions": rm.if_conditions, "then_cases": rm.then_cases, "dynamic": False } if rm.rule_type == "col": ffc = FinalFusionColumn.objects.get(pk=json.loads(rm.col_subject)[0]) single["col_subject_name"] = ffc.display_column_name single["col_subject_id"] = ffc.pk elif rm.rule_type == "row": try: FinalFusionColumn.objects.get(rm_dependency=rm) single["dynamic"] = True except Exception: pass except ObjectDoesNotExist: pass return HttpResponse(json.dumps({ "success": success, "obj": json.dumps(single) }))
def do_transfer_rm(request): """ Will take a rule module from another project to add it to the current project. """ success = False valid_user = token_checker.token_is_valid(request) if valid_user and "id" in request.GET and ArgsChecker.is_number(request.GET["id"]): rm_id = request.GET["id"] proj = Project.objects.get(pk=valid_user.last_opened_project_id) ff = FinalFusion.objects.get(project=proj) try: rm = RuleModule.objects.get(pk=rm_id) rm.pk = None rm.final_fusion = ff rm.save() success = True except ObjectDoesNotExist as exc: pass return HttpResponse(json.dumps({"success": success}))
def do_upload_tq(request): """ Will upload a TQ to the server to continue with further processes. 1. Upload file, 2. Check extension, 3. Delegate to a parser and create a TQ from its result. """ success = False msg = None data = None valid_user = token_checker.token_is_valid(request) if valid_user: if request.method == 'POST' and "file" in request.FILES: file = request.FILES["file"] task_id = request.GET["task_id"] if not ArgsChecker.str_is_malicious( task_id) and not ArgsChecker.str_is_malicious(file.name): filename_spl = file.name.split(".") extension = filename_spl[len(filename_spl) - 1] # Write into a file file_path = "%s/%s" % (settings.TQ_UPLOAD_DIR, file.name) with open(file_path, 'wb+') as destination: for chunk in file.chunks(): destination.write(chunk) # If the uploaded file is an Excel-File, it needs to be preparsed to get the sheet names of it. # The parser needs to know which sheet to parse. if (extension == "xlsx" or extension == "xlsb") and len(request.GET["sheet_names"]) < 1 \ and not ArgsChecker.str_is_malicious(request.GET["sheet_names"]): sheets = preparse_get_sheets(file_path, extension) msg = "sheet_check" if sheets: data = sheets else: msg = "syntax" else: sheet_names = None if not ArgsChecker.str_is_malicious( request.GET["sheet_names"]): sheet_names = [ x for x in request.GET["sheet_names"].split(",") if len(x) > 1 ] if len(sheet_names) > 0: # Create a single TQ for every sheet of the uploaded Excel file. for sheet in sheet_names: json_parsed = delegate_to_parser( file_path, extension, sheet) if json_parsed and TQFlattener.keys_are_even( json_parsed): TQFile.objects.create( project=valid_user.get_project(), source_file_name="%s/%s" % (file.name, sheet), display_file_name=file.name, content_json=json_parsed) success = True else: msg = "Uploaded file not supported" else: # Standard case, just parse the file. json_parsed = delegate_to_parser( file_path, extension, None) if json_parsed and TQFlattener.keys_are_even( json_parsed): json_parsed_flat = TQFlattener.flatten(json_parsed) has_been_flattened = json_parsed != json_parsed_flat json_parsed = json_parsed_flat TQFile.objects.create( project=valid_user.get_project(), source_file_name=file.name, display_file_name=file.name, content_json=json_parsed, has_been_flattened=has_been_flattened) success = True elif json_parsed and not TQFlattener.keys_are_even( json_parsed): msg = "not_even" else: msg = "syntax" else: msg = "User is not valid" return HttpResponse( json.dumps({ "success": success, "msg": msg, "data": data }))