diff --git a/kitchen_counter/conversion/management/commands/custom_runserver.py b/kitchen_counter/conversion/management/commands/custom_runserver.py index eaf4eb7..2151acb 100755 --- a/kitchen_counter/conversion/management/commands/custom_runserver.py +++ b/kitchen_counter/conversion/management/commands/custom_runserver.py @@ -12,11 +12,15 @@ from conversion.translation.external_conversion import Conversion from centralisedFileSystem.models import Script, File from scriptAudit.models import ScriptAuditModel from scriptAudit.models import States -from utils import filesystem +from utils import filesystem, utilities from scriptAudit.utils import update_audit_status from scriptAudit.mnf_script_audit import NeutralAudit from django.core.files.base import ContentFile from django.contrib.auth import get_user_model +from users.models import UserCredentialsForBlockchain +from django.core.files.base import File as DjangoFile +import tempfile +from io import BytesIO basePath = BasePath() User = get_user_model() @@ -62,6 +66,12 @@ def run_conversion(msg): def run_audit(msg): + from Blockchain2.decryption import decryptionOfPrivate, decryptionOfUrl, download_file_System,decryptionOflocalUrl,hash_decrypation + from Blockchain2.DataStorage import uploadDataToIPFSNode + from Blockchain2.scriptAudit import getUserprojectIds,UploadScriptAuditData,getScriptAudit + from Blockchain2.blockchainsetting import OWNER_KEY + + print("Hello World inside ") body_dict = json.loads(msg.body) user = body_dict.get("user") s3_url = body_dict.get("s3-file-path") @@ -70,8 +80,8 @@ def run_audit(msg): language = body_dict.get("language") script_ext = body_dict.get("script_ext") script_file_name = body_dict.get("script_file_name") - - + language = "en" + print("112") object_key = "INPUT/" + str(script_file_name) local_file_path = "/home/ubuntu/Conversion_Kitchen_Code/kitchen_counter/media/audit_counter_files/" + script_file_name s3_client = boto3.client('s3', @@ -79,8 +89,9 @@ def run_audit(msg): aws_secret_access_key="ZWc6KOc5LuBLuCEBDDfQTor+Q7rp3fFH74gVt+AA", region_name="ap-south-1" ) + print(object_key, local_file_path) s3_client.download_file("conversion-kitchen", object_key, local_file_path) - + print("113") with open(local_file_path, 'rb') as file: file_content = file.read() @@ -132,8 +143,252 @@ def run_audit(msg): ), defaults={"status" : "FAILURE"} ) + status = ScriptAuditModel.objects.get( + script = Script.objects.get( + id = script_id + )) + + + # blockchain + blockchain_obj = UserCredentialsForBlockchain.objects.get(user=user) + script_original= {} + audit_data={} + script_original["status"] = "STARTED" + script_original["script_id"] = script_id + with open(local_file_path, 'rb') as file: + hash = uploadDataToIPFSNode(file) + script_original["script_file"] = hash + script_original["type"] = "script-original" + script_original["script_file_path"] = local_file_path + audit_data["script-original"] = script_original + userkey= decryptionOfPrivate(blockchain_obj.privateKey) + print("userkey = ", str(userkey)) + # status,getData = getScriptAudit(userkey.decode('utf-8'),user_id,str(script_id)) + # privatekeyfordb = userkey.decode('utf-8') + # print(privatekeyfordb) + # status.bchain_privatekey = privatekeyfordb + # status.save() + print("blockchain_obj.publicKey",blockchain_obj.publicKey) + print("blockchain_obj.privateKey",blockchain_obj.privateKey) + if status.status == "SUCCESS": + msg.delete() + file_to_audit = File.objects.get( + script=script_id, + type="script-csv" + ) + file_to_audit_report = File.objects.get( + script=script_id, + type="audit-report" + ) + hash2 = "" + try: + file_to_audit_docx = File.objects.get( + script=script_id, + type="script-docx" + ) + script_docx = {} + script_path1 = file_to_audit_docx.file.path + script_size = file_to_audit_docx.file.size + with open(script_path1, 'rb') as _file: + hash2 = uploadDataToIPFSNode(_file) + script_docx["script_file_path"] = script_path1 + script_docx["script_file"] = hash2 + script_docx["type"] = "script-docx" + audit_data["script-docx"] = script_docx + except: + csv_script_path = file_to_audit.file.path + df = pd.read_csv(csv_script_path) + docx = utilities.csv_to_docx(df) + + temp_file_stream = BytesIO() + docx.save(temp_file_stream) + temp_file_stream.seek(0) + + docx_file = ContentFile( + temp_file_stream.getvalue(), + "from_audited_csv_to_document.docx", + ) + + query_file = File.objects.create( + script= file_to_audit.script, + file=docx_file, + type="script-docx", + ) + file_to_audit_docx = File.objects.get( + script=script_id, + type="script-docx" + ) + script_docx = {} + script_path1 = file_to_audit_docx.file.path + script_size = file_to_audit_docx.file.size + with open(script_path1, 'rb') as _file: + hash2 = uploadDataToIPFSNode(_file) + script_docx["script_file_path"] = script_path1 + script_docx["script_file"] = hash2 + script_docx["type"] = "script-docx" + audit_data["script-docx"] = script_docx + + ## code for pdf also + try: + temp_dir = tempfile.TemporaryDirectory() + pdf_file_path = utilities.docx_to_pdf( + script_path1, temp_dir.name) + + with open(pdf_file_path, "rb") as temp_pdf: + + pdf_file = DjangoFile(temp_pdf,pdf_file_path.rsplit('/', 1)[1],) + + query_file = File.objects.create( + script = file_to_audit.script, + file = pdf_file, + type="script-pdf", + ) + script_pdf = {} + script_path1 = pdf_file_path + # script_size = file_to_audit_docx.file.size + with open(script_path1, 'rb') as _file: + hash2 = uploadDataToIPFSNode(_file) + script_pdf["script_file_path"] = script_path1 + script_pdf["script_file"] = hash2 + script_pdf["type"] = "script-pdf" + audit_data["script-pdf"] = script_pdf + except: + pass + # convert csv to json and store JSON + try: + csv_script_path = file_to_audit.file.path + df = pd.read_csv(csv_script_path) + df = df.loc[:, ["content", "script_element"]] + script_json: dict = json.loads(utilities.csv_to_json(df)) + with tempfile.TemporaryDirectory() as temp_dir: + print("Temporary directory created:", temp_dir) + temp_filename = os.path.join(temp_dir, 'script_json_file.json') + print(temp_filename) + with open(temp_filename, 'w') as json_file: + json.dump(script_json, json_file, indent=4) + script_json = {} + script_path1 = temp_filename + # script_size = file_to_audit_docx.file.size + with open(script_path1, 'rb') as _file: + hash2 = uploadDataToIPFSNode(_file) + script_json["script_file_path"] = script_path1 + script_json["script_file"] = hash2 + script_json["type"] = "script-json" + audit_data["script-json"] = script_json + except: + pass + + script_csv = {} + audit_report ={} + audit_report_path = file_to_audit_report.file.path + script_path = file_to_audit.file.path + script_size = file_to_audit.file.size + + print("script_file_path_is_here",script_path) + with open(script_path, 'rb') as _file: + hash1 = uploadDataToIPFSNode(_file) + script_csv["script_file"] = hash1 + script_csv["script_file_path"] = script_path + script_csv["type"] = "script-csv" + with open(audit_report_path, 'rb') as file1: + hash2 = uploadDataToIPFSNode(file1) + audit_report["script_file"] = hash2 + script_csv["script_file_path"] = audit_report_path + script_csv["type"] = "audit-report" + audit_data["script-csv"]= script_csv + audit_data["audit-report"]= audit_report + + with open("/home/ubuntu/Conversion_Kitchen_Code/kitchen_counter/Blockchain2/file_stroge.txt","a") as file01: + file01.write("audit userkey------------------SUCCESS1\n") + file01.write(str(audit_data) + "\n") + Response,gasprice = UploadScriptAuditData(OWNER_KEY,blockchain_obj.publicKey,blockchain_obj.user_id,script_id,str(audit_data)) + print("tx_hash",Response) + with open("/home/ubuntu/Conversion_Kitchen_Code/kitchen_counter/Blockchain2/file_stroge.txt","a") as file01: + file01.write("audit userkey------------------SUCCESS\n"+str(Response)) + transactioni_id = str(Response) + # with open("/home/mnfbeta/mnf/app/Blockchain2/file_stroge.txt","a") as file01: + # file01.write(f"Transaction hash for audit is {str(transactioni_id)}\n") + # current_time = datetime.now() + # formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S") + # file01.write(f"Transaction hash for audit is {str(type(transactioni_id))}\n") + # file01.write(f" time for the above transaction id {str(formatted_time)}\n") + # audit_model_obj = ScriptAuditModel.objects.get( + # script = Script.objects.get(id = script_id)) + status.transaction_hash =str(transactioni_id) + # audit_model_obj.transaction_hash = str(transactioni_id) + # audit_model_obj.save() + status.save() + + # user_infos = user_info(tx_hash=Response,service="Script Audit",gas_fee=gasprice) + # addition_result = user_infos.update_info(request) + hash2 = hash_decrypation(hash2) + tx_id = Response + certificate = certificateGenrate(user.username,"script audit",tx_id,projectname=script_file_name,matic=gasprice) + to_email = [user.email] + email_code = 'BL1' + key_value = { + "service":"Audited Script", + "hash": hash2, + "public key":blockchain_obj.publicKey, + "Transaction Hash": tx_id, + } + print("userkey = ", userkey) + sendmail(to_email=to_email , email_code=email_code, key_value=key_value, filePath=certificate) + print("mail send sucessfully:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") + data = {"message": "Success"} + # deleting the folder and files + + # Delete a file + media_path = os.path.join(settings.MEDIA_ROOT, "audit_counter_files", script_file_name) + if os.path.exists(media_path): + os.remove(media_path) + print(f"File '{media_path}' deleted successfully.") + else: + print(f"File '{media_path}' does not exist.") + + # Delete a folder and its contents + folder1_path = os.path.join(settings.MEDIA_ROOT, "scripts_folder", script_id) + if os.path.exists(folder1_path): + shutil.rmtree(folder1_path) + print(f"Folder '{folder1_path}' and its contents deleted successfully.") + else: + print(f"Folder '{folder1_path}' does not exist.") + + folder2_path = os.path.join(settings.MEDIA_ROOT, "audit_folder", script_id) + if os.path.exists(folder2_path): + shutil.rmtree(folder2_path) + print(f"Folder '{folder2_path}' and its contents deleted successfully.") + else: + print(f"Folder '{folder2_path}' does not exist.") + # return JsonResponse(data, status=200) + # return Response("Success", status=200) + + + else: + with open("/home/mnftaj/mnf_march22/MNF/app/Blockchain2/file_stroge.txt","a") as file01: + file01.write("audit userkey------------------else\n") + Response = UploadScriptAuditData(OWNER_KEY,blockchain_obj.publicKey,blockchain_obj.user_id,script_id,str(audit_data)) + print("tx_hash",Response) + hash = hash_decrypation(hash) + #certificate = certificateGenrate(request.user.username,"script audit",hash) + tx_id = Response + + # certificate = certificateGenrate(request.user.username,"script audit",tx_id,projectname=script_file_name,matic=gasprice) + + to_email = [user.email] + email_code = 'BL1' + key_value = { + "service":"Orginal Script Audit", + "hash": hash, + "public key":blockchain_obj.publicKey, + "private key":userkey, + "Transaction Hash": tx_id, + } + + print("::::::::::::::",key_value) + print("userkey = ", userkey) + # sendmail(to_email=to_email , email_code=email_code,key_value=key_value,filePath=certificate) - msg.delete()