Conversion_Kitchen_Code/kitchen_counter/conversion/translation/external_conversion.py

434 lines
19 KiB
Python
Executable File

import sys
import os
import boto3
import pandas as pd
import datetime
import time
import json
import tempfile
# django
from django.core.files.base import ContentFile
# all external imports
from scriptAudit.models import States
from centralisedFileSystem.models import Script, File
from scriptAudit.models import ScriptAuditModel
from mnfapp.models import MNFScriptDatabase, SampleScript, ScriptTranslations
from users.models import UserCredentialsForBlockchain,BlockchainUserInfo
from lpp.models import LPPTASKDatabase
from centralizePayment.views import auto_refund
from juggernaut.views import update_juggernaut
from juggernaut.models import JuggernautPackage
from auto_email.views import sendmail
from lpp.views import task_assigner
from utils import filesystem, utilities
from conversion.translation.newconversion import ScriptTranslation
from scriptAudit.mnf_script_audit import NeutralAudit
from Blockchain2.DataStorage import uploadDataToIPFSNode
from Blockchain2.decryption import decryptionOfPrivate, decryptionOfUrl, download_file_System,viewDecryptionOfUrl,hash_decrypation
from Blockchain2.Conversion import UploadConversionData, getConversion, deleteConversion,scriptGetUserprojectIds
from Blockchain2.blockchainsetting import OWNER_KEY
from Blockchain2.block_user_info import user_info
from lpp.certificate.createCertificate import certificateGenrate
from django.conf import settings
from MNF.settings import BasePath
basePath = BasePath()
"""Converts a screenplay file"""
class Conversion:
def __init__(self, **conversion_params):
self.audit_user = conversion_params['audit_user']
self.user = conversion_params['user']
self.file_path = conversion_params['file_path']
self.original_script_object = MNFScriptDatabase.objects.get(
script_id=conversion_params['original_script_id'])
self.translated_script_object = ScriptTranslations.objects.get(
translation_id=conversion_params['translated_script_id'])
self.sample_id = conversion_params['sample_id']
self.existing_script = conversion_params['existing_script']
self.iteration_no = conversion_params['iteration_no']
self.juggernaut_pages_deduction = conversion_params["juggernaut_pages_deduction"]
self.language_set = conversion_params["language_set"]
self.amount_without_subscrption = conversion_params["amount_without_subscrption"]
self.response = {}
print("Conversion Process Initializing")
self.full_dialogue_option_choices = [True if value.strip() == "True" else False for value in
str(self.translated_script_object.full_dialogue_option_choices).split(",")]
# Getting all choices of user regarding Words of Dialogues Translation
self.sentence_dialogue_option_choices = [True if value.strip() == "True" else False for value in
str(self.translated_script_object.sentence_dialogue_option_choices).split(",")]
# Getting all choices of user regarding translation and transliteration choices of slug, speaker, etc.
self.other_option_choices = [True if value.strip() == "True" else False for value in
str(self.translated_script_object.other_option_choices).split(",")]
self.name_script = (str(((self.original_script_object.script.name).split("/"))[-1]).split("."))[0]
self.file_extension = (str(self.original_script_object.script.name).split("."))[-1]
def convert(self):
"""Audit Code starts here"""
print("Conversion Auditing is starting")
original_stdout = sys.stdout
script1 = str(self.file_path)
doc = open(script1, 'rb').read()
file = ContentFile(
doc,
(script1.split("/"))[-1],
)
language_code = "en"
result = filesystem.new_screenplay_without_audit_in_background(
self.audit_user,
self.audit_user.username,
str(self.name_script),
file,
"script-original",
language_code,
)
audit_id = result.get("script", {}).get("id")
ScriptAuditModel.objects.update_or_create(
script=Script.objects.get(
id=audit_id
),
defaults={"status": States.STARTED}
)
audit = NeutralAudit(audit_id, True)
status = ScriptAuditModel.objects.get(
script=Script.objects.get(
id=audit_id
)
)
try:
if self.file_extension == "fdx":
audit.audit_fdx()
else:
audit.audit()
status.status = "SUCCESS"
status.save()
to_email = [self.user.email]
email_code = 'SB1'
except Exception as e:
print("Error of Audit is:", e)
status.status = "FAILURE"
status.save()
to_email = [self.user.email]
email_code = 'SB2'
print(
"Script Audit failed due to some internal error."
" If you have made any payment for conversion "
"that will be refunded")
to = self.user.email
key_value = {
"User": self.user.username,
}
sendmail(to_email=[to], email_code="PP21", key_value=key_value)
# auto refund code
try:
auto_refund(self.translated_script_object.central_payment_id)
except Exception as e:
print("No Refund -> ", e)
return self.response
file_path_ = filesystem.get_file_path(audit_id, "script-csv")
script_data = MNFScriptDatabase.objects.get(script_id=self.original_script_object.script_id)
script_data.audit_id = str(audit_id)
script_data.save()
try:
df = pd.read_csv(file_path_, encoding="utf-8")
except UnicodeError:
df = pd.read_csv(file_path_, encoding="utf-16")
self.list_of_lists = df.values.tolist()
print("Audit Done")
sys.stdout = original_stdout
print("Audit Done")
self.translated_script_object.status = "Audited"
self.translated_script_object.save()
"""Translation Code Starts here"""
print("list of lists -> ", self.list_of_lists)
dataframe = self.list_of_lists
args = [self.full_dialogue_option_choices, self.sentence_dialogue_option_choices, self.other_option_choices]
print("printing args")
print("args[0] - full_dialogue_option_choices:", args[0])
print("args[1] - sentence_dialogue_option_choices:", args[1])
print("args[2] - other_option_choices:", args[2])
kwargs = {
"user": self.translated_script_object.user_id,
"iteration_no": self.iteration_no,
'dataframe': dataframe,
"non_dial_src_lang": self.original_script_object.nondial_src_language,
"non_dial_src_script": self.original_script_object.nondial_src_script,
"non_dial_dest_lang": self.translated_script_object.nondial_dest_language,
"non_dial_dest_script": self.translated_script_object.nondial_dest_script,
"dial_src_lang": self.original_script_object.dial_src_language,
"dial_src_script": self.original_script_object.dial_src_script,
"dial_dest_lang": self.translated_script_object.dial_dest_language,
"dial_dest_script": self.translated_script_object.dial_dest_script,
"translation_id": self.translated_script_object.translation_id,
"script_id": self.original_script_object.script_id,
"csv_path":file_path_,
"audit_id": audit_id,
}
print("printing kwargs")
for key, value in kwargs.items():
print(f"{key}: {value}")
try:
ScriptTranslation(*args, **kwargs)
except Exception as e:
self.translated_script_object.status = "Failed"
self.translated_script_object.error_desc = str(e)
self.translated_script_object.save()
print("Error in Conversion is:", e)
print("Script translation failed due to some code error. If you have made any payment for conversion"
" that will be refunded")
# email for failure
to = self.user.email
key_value = {
"User": self.user.username,
}
sendmail(to_email=[to], email_code="PP21", key_value=key_value)
# auto refund code
try:
auto_refund(self.translated_script_object.central_payment_id)
except Exception as e:
print("No Refund due to error", e)
return self.response
"""Translation Code Ends Here"""
sys.stdout = original_stdout
self.translated_script_object = ScriptTranslations.objects.get(
translation_id=self.translated_script_object.translation_id)
"""Translation Completion mail here"""
if not self.translated_script_object.lpp:
to = self.user.email
key_value = {
"User": self.user.username,
"Title": self.original_script_object.script_title,
"translated language":self.translated_script_object.dial_dest_language,
}
sendmail(to_email=[to], email_code="PP18", key_value=key_value)
# 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
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}"
print(response)
except Exception as e:
self.translated_script_object.error_desc = "Error in s3 output upload is " + str(e)
self.translated_script_object.save()
print("Error in s3 output upload is", e)
"""Vetting Process if Choosen"""
# sys.stdout = original_stdout
if self.translated_script_object.lpp:
X = LPPTASKDatabase()
X.user_id = self.translated_script_object.user_id
X.usernote = "Kindly check if the translated file is correct as per the Uploaded Document!"
X.translated_script = self.translated_script_object
X.generated_from = "conversion"
temp_amount = self.amount_without_subscrption
X.amoutgiventolpp_action = round((temp_amount / 2), 2)
X.amoutgiventolpp_dialogue = round((temp_amount / 2), 2)
X.save()
task_assigner(int(X.task_id), "conversion")
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")
# if obj.converted_audit_id:
print("audit blockchain")
print("blockchain script conversion 5.1")
# blockchain upload of csv-json data
try:
adit_id = obj.converted_audit_id
print(f"## aaaaaaaadit_id----> {adit_id}")
except:
print("audit id not found")
print(f"## audit id ---> {audit_id}")
file_to_audit = File.objects.get(
script=audit_id,
type="script-csv"
)
csv_script_path = file_to_audit.file.path
print(f"## csv_script_path ----> {csv_script_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)
print(f"## hash2 ----> {hash2}")
script_json["script_file_path"] = script_path1
script_json["script_file"] = hash2
script_json["type"] = "script-json"
script_json["screenplay_name"] = str(self.name_script)
scriptconversion["script-json"] = script_json
print("blockchain script conversion 5.2")
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)
print(type(tx_id))
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)
certificatepath = certificateGenrate(self.user.username, "script conversion", hash, projectname=self.name_script, matic = tx_gas_fee)
hash = hash_decrypation(hash)
to_email = [self.user.email]
email_code = 'BL1'
key_value = {
"Product Name": "script conversion",
"Profile url": f"{settings.SITE_DOMAIN}/memberpage/#/personaldetails",
"User url": f"{settings.SITE_DOMAIN}/memberpage/#/user/{self.user.id}/personaldetails",
"Product output card url": f"{settings.SITE_DOMAIN}/conversion/view_conversion/",
}
sendmail(to_email=to_email, email_code=email_code,key_value=key_value, filePath=certificatepath)
except Exception as e:
self.translated_script_object.error_desc = "Error in blockchain is " + str(e)
self.translated_script_object.save()
print("Error in blockchain is", e)
"""Blockchain Upload Ends here"""
"""Process Completed"""
self.translated_script_object = ScriptTranslations.objects.get(
translation_id=self.translated_script_object.translation_id)
self.translated_script_object.status = "Completed"
try:
self.translated_script_object.blockchain_txhash = tx_id
self.translated_script_object.translated_script_pdf = s3_url
except:
self.translated_script_object.translated_script_pdf = s3_url
self.translated_script_object.save()
return self.response
# from pathlib import Path
# basePath = Path(__file__).resolve().parent.parent
# translation_script = ScriptTranslations.objects.get(translation_id="")
#
# conversion_params = {
# "user": translation_script.user_id,
# "file_path": str(basePath) + "/media/" + translation_script.script_link_id.script.name,
# "original_script_object": translation_script.script_link_id,
# "translated_script_object": translation_script,
# "sample_id": None,
# "existing_scrip": None
# }
# obj = Conversion(**conversion_params)