From 37654cb4ec6d91677e4e8c8799d4b575880cc168 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Tue, 30 Apr 2024 12:29:42 +0000 Subject: [PATCH] conversion lpp s3 output file saving --- .../translation/external_conversion.py | 155 ++++++++++-------- 1 file changed, 90 insertions(+), 65 deletions(-) diff --git a/kitchen_counter/conversion/translation/external_conversion.py b/kitchen_counter/conversion/translation/external_conversion.py index eb8d3be..f2b594a 100644 --- a/kitchen_counter/conversion/translation/external_conversion.py +++ b/kitchen_counter/conversion/translation/external_conversion.py @@ -466,70 +466,6 @@ class Conversion: - """Blockchain Upload Starts here""" - # sys.stdout = original_stdout - scriptconversion = {} - try: - print("trying blockchain 1") - current_datetime = datetime.datetime.now() - if UserCredentialsForBlockchain.objects.filter(user=self.user).exists(): - obj = self.translated_script_object - obj2 = self.original_script_object - temp1 = str(obj2.script) - temp2 = str(obj.translated_script_path) - uploaded_script = f"{basePath}/media/{temp1}" - translated_scripttt = f"{basePath}{temp2}" - print("blockchain script conversion 3", uploaded_script, translated_scripttt) - with open(uploaded_script, 'rb') as f: - hash = uploadDataToIPFSNode(f) - scriptconversion["original_scriptFile_hash"] = hash - scriptconversion["original_scriptFile_path"] = uploaded_script - scriptconversion["date_at"] = current_datetime.strftime("%Y-%m-%d %H:%M:%S") - print("blockchain script conversion 4") - with open(translated_scripttt, 'rb') as f1: - hash = uploadDataToIPFSNode(f1) - scriptconversion["translated_scriptFile_hash"] = hash - scriptconversion["translated_scriptFile_path"] = translated_scripttt - print("blockchain script conversion 5") - blockchain_obj = UserCredentialsForBlockchain.objects.get(user=self.user) - UserId = blockchain_obj.user_id - Project = obj.translation_id - Data = str(scriptconversion) - userPrivateKey = blockchain_obj.privateKey - userkey = decryptionOfPrivate(userPrivateKey) - tx_id, tx_gas_fee = UploadConversionData(OWNER_KEY, blockchain_obj.publicKey, UserId, str(Project), - Data) - print("Tx id -> ", tx_id,tx_gas_fee) - # user_infos = user_info(tx_hash=tx_id, service="Conversion", gas_fee=2345432345123456) - # addition_result = user_infos.update_info(request) - try: - user_infos = user_info(tx_hash=tx_id, service="Conversion", gas_fee=tx_gas_fee, - script_name=self.name_script) - addition_result = user_infos.update_info(self) - print("Blockchain Result -> ",addition_result) - except Exception as e: - print("Error:", e) - - print("blockchain script conversion 6") - certificatepath = certificateGenrate(self.user.username, "script conversion", hash) - hash = hash_decrypation(hash) - to_email = [self.user.email] - email_code = 'BL1' - key_value = { - "service": "script conversion", - "hash": hash, - "public key": blockchain_obj.publicKey, - "private key": userkey.decode('utf-8'), - "Transaction Hash": tx_id - } - sendmail(to_email=to_email, email_code=email_code, key_value=key_value, filePath=certificatepath) - except Exception as e: - print("Error in blockchain is", e) - - """Blockchain Upload Ends here""" - - - """Translation Completion mail here""" to = self.user.email @@ -556,8 +492,97 @@ class Conversion: X.amoutgiventolpp_dialogue = round((temp_amount / 2), 2) X.save() task_assigner(int(X.task_id), "conversion") + - """Vetting Process Done""" + # adding file to s3 output folder + + object_name = "OUTPUT/" + (self.translated_script_object.translated_script_path.split("/"))[-1] + filee = basePath + self.translated_script_object.translated_script_path + # Upload the file + import os + import boto3 + bucket = "conversion-kitchen" + s3_client = boto3.client('s3', + aws_access_key_id="AKIAQVLBBGCB45RMLKVW", + aws_secret_access_key="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", + region_name="ap-south-1" + ) + try: + response = s3_client.upload_file(filee, bucket, object_name) + s3_url = f"https://{bucket}.s3.ap-south-1.amazonaws.com/{object_name}" + self.translated_script_object.translated_script_pdf = s3_url + print(response) + except Exception as e: + print("Error is", e) + + + + + else: + + """Blockchain Upload Starts here""" + # sys.stdout = original_stdout + scriptconversion = {} + try: + print("trying blockchain 1") + current_datetime = datetime.datetime.now() + if UserCredentialsForBlockchain.objects.filter(user=self.user).exists(): + obj = self.translated_script_object + obj2 = self.original_script_object + temp1 = str(obj2.script) + temp2 = str(obj.translated_script_path) + uploaded_script = f"{basePath}/media/{temp1}" + translated_scripttt = f"{basePath}{temp2}" + print("blockchain script conversion 3", uploaded_script, translated_scripttt) + with open(uploaded_script, 'rb') as f: + hash = uploadDataToIPFSNode(f) + scriptconversion["original_scriptFile_hash"] = hash + scriptconversion["original_scriptFile_path"] = uploaded_script + scriptconversion["date_at"] = current_datetime.strftime("%Y-%m-%d %H:%M:%S") + print("blockchain script conversion 4") + with open(translated_scripttt, 'rb') as f1: + hash = uploadDataToIPFSNode(f1) + scriptconversion["translated_scriptFile_hash"] = hash + scriptconversion["translated_scriptFile_path"] = translated_scripttt + print("blockchain script conversion 5") + blockchain_obj = UserCredentialsForBlockchain.objects.get(user=self.user) + UserId = blockchain_obj.user_id + Project = obj.translation_id + Data = str(scriptconversion) + userPrivateKey = blockchain_obj.privateKey + userkey = decryptionOfPrivate(userPrivateKey) + tx_id, tx_gas_fee = UploadConversionData(OWNER_KEY, blockchain_obj.publicKey, UserId, str(Project), + Data) + print("Tx id -> ", tx_id,tx_gas_fee) + # user_infos = user_info(tx_hash=tx_id, service="Conversion", gas_fee=2345432345123456) + # addition_result = user_infos.update_info(request) + try: + user_infos = user_info(tx_hash=tx_id, service="Conversion", gas_fee=tx_gas_fee, + script_name=self.name_script) + addition_result = user_infos.update_info(self) + print("Blockchain Result -> ",addition_result) + except Exception as e: + print("Error:", e) + + print("blockchain script conversion 6") + certificatepath = certificateGenrate(self.user.username, "script conversion", hash) + hash = hash_decrypation(hash) + to_email = [self.user.email] + email_code = 'BL1' + key_value = { + "service": "script conversion", + "hash": hash, + "public key": blockchain_obj.publicKey, + "private key": userkey.decode('utf-8'), + "Transaction Hash": tx_id + } + sendmail(to_email=to_email, email_code=email_code, key_value=key_value, filePath=certificatepath) + except Exception as e: + print("Error in blockchain is", e) + + """Blockchain Upload Ends here""" + + """Process Completed""" self.translated_script_object.status = "Completed"