Conversion_Kitchen_Code/kitchen_counter/conversion/translation/external_conversion_bkp1704.py

549 lines
24 KiB
Python
Raw Permalink Normal View History

2024-04-27 09:33:09 +00:00
import pandas as pd
from conversion.translation.newconversion import ScriptTranslation
# django
from django.core.files.base import ContentFile
# all external imports
from scriptAudit.mnf_script_audit import NeutralAudit
from scriptAudit.models import States
from utils import filesystem
from auto_email.views import sendmail
from centralizePayment.views import auto_refund
from centralisedFileSystem.models import Script
from scriptAudit.models import ScriptAuditModel
from mnfapp.models import MNFScriptDatabase, SampleScript, ScriptTranslations
"""Converts a screenplay file"""
class Conversion:
def __init__(self, **conversion_params):
self.user = conversion_params['user']
self.file_path = conversion_params['file_path']
self.original_script_object = conversion_params['original_script_object']
self.translated_script_object = conversion_params['translated_script_object']
self.sample_id = conversion_params.get('sample_id', None)
self.existing_script = conversion_params.get('existing_script', None)
self.iteration_no = conversion_params.get('iteration_no', None)
self.response = {}
print("Conversion Process Initializing")
# original_stdout = sys.stdout
"""Initializing Variables"""
# script_id = request.session["script_id"]
# original_script = MNFScriptDatabase.objects.get(script_id=script_id)
# translated_script = ScriptTranslations.objects.get(translation_id=request.session.get("translation_id", None))
# Getting all choices of user regarding Full Dialogues Translation
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.file_extension = (str(self.original_script_object.script.name).split("."))[-1]
self.name_script = (str(self.original_script_object.script.name).split("."))[0]
# Dual dialogue
# if dual_dialogue_option := request.POST.get('dual_dialogue_option') == "on":
# dual_left_language = request.POST.get('dual_left_language')
# dual_left_script = request.POST.get('dual_left_script')
# # lpp vetting
# if lpp_vetting := request.POST.get('lpp_vetting') == "on":
# lpp_vetting_type = request.POST.get('radioGroup')
# if lpp_vetting_type == "radioOption1":
# pass
# elif lpp_vetting_type == "radioOption1":
# pass
def convert(self):
"""Audit Code starts here"""
print("Conversion Auditing is starting")
# x = MNFScriptDatabase.objects.get(script_id=request.POST['script_id'])
# scripttt = basePath + "/media/" + original_script.script.name
# exten = (str(self.original_script_object.script.name).split("."))[-1]
# name_script = str(((((scripttt).split("/"))[-1]).split("."))[0])
print("name of script", self.name_script)
print("sample script", self.sample_id)
print("existing_script", self.existing_script)
"""if file is not a sample script and not a existing script then only audit it"""
if not self.existing_script and not self.original_script_object.sample_script:
print("----Auditing----")
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.user,
self.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, False)
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'
sendmail(to_email=to_email, email_code=email_code)
except Exception as e:
print("Error of Audit is:", e)
status.status = "FAILURE"
status.save()
to_email = [self.user.email]
email_code = 'SB2'
sendmail(to_email=to_email, email_code=email_code)
print(
"Script Audit failed due to some internal 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 -> ", 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()
to_email = [self.user.email]
email_code = 'SB1'
sendmail(to_email=to_email, email_code=email_code)
try:
df = pd.read_csv(file_path_, encoding="utf-8")
except UnicodeError:
df = pd.read_csv(file_path_, encoding="utf-16")
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
self.list_of_lists = df.values.tolist()
print("Audit Done")
elif self.original_script_object.sample_script:
sample_script = SampleScript.objects.get(sample_id=self.original_script_object.sample_id.sample_id)
if sample_script.audit_id:
audit_id = sample_script.audit_id
file_path_ = filesystem.get_file_path(audit_id, "script-csv")
try:
df = pd.read_csv(file_path_, encoding="utf-8")
except UnicodeError:
df = pd.read_csv(file_path_, encoding="utf-16")
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
self.list_of_lists = df.values.tolist()
else:
print("----Auditing----")
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.user,
self.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, False)
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'
sendmail(to_email=to_email, email_code=email_code)
except:
status.status = "FAILURE"
status.save()
to_email = [self.user.email]
email_code = 'SB2'
sendmail(to_email=to_email, email_code=email_code)
print(
"Script Audit failed due to some internal 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 -> ", e)
return self.response
file_path_ = filesystem.get_file_path(
audit_id, "script-csv")
sample_script.audit_id = str(audit_id)
sample_script.save()
try:
df = pd.read_csv(file_path_, encoding="utf-8")
except UnicodeError:
df = pd.read_csv(file_path_, encoding="utf-16")
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
dataframe = df
self.list_of_lists = dataframe.values.tolist()
elif self.existing_script:
if MNFScriptDatabase.objects.get(script_id=self.original_script_object.script_id).audit_id:
audit_id = MNFScriptDatabase.objects.get(script_id=self.original_script_object.script_id).audit_id
file_path_ = filesystem.get_file_path(audit_id, "script-csv")
try:
df = pd.read_csv(file_path_, encoding="utf-8")
except UnicodeError:
df = pd.read_csv(file_path_, encoding="utf-16")
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
dataframe = df
self.list_of_lists = dataframe.values.tolist()
else:
print("----Auditing----")
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.user,
self.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, False)
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'
sendmail(to_email=to_email, email_code=email_code)
except:
status.status = "FAILURE"
status.save()
to_email = [self.user.email]
email_code = 'SB2'
sendmail(to_email=to_email, email_code=email_code)
print(
"Script Audit failed due to some internal 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 -> ", 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()
to_email = [self.user.email]
email_code = 'SB1'
sendmail(to_email=to_email, email_code=email_code)
try:
df = pd.read_csv(file_path_, encoding="utf-8")
except UnicodeError:
df = pd.read_csv(file_path_, encoding="utf-16")
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
dataframe = df
self.list_of_lists = dataframe.values.tolist()
# 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]
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
}
# original_stdout = sys.stdout
# 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
#
# """Juggernaut Payment Updation"""
# # sys.stdout = original_stdout
# if self.juggernaut_pages_deduction:
# # script_translated = ScriptTranslations.objects.get(
# # translation_id=request.session.get('translation_id', None))
# update_juggernaut(request, user_id=self.user.id, service_name="conversion",
# conversion_pages=request.session['juggernaut_pages_deduction'],
# associated_project=original_script,
# translation_language=request.session['language_set'])
# original_script.is_juggernaut_used = True
# # if script_original.languages_juggernaut != "":
# # # script_original.languages_juggernaut = str(script_original.languages_juggernaut) + "," + str(
# # # script_translated.dial_dest_language)
# # else:
# # script_original.languages_juggernaut = str(script_translated.dial_dest_language)
# original_script.save()
# """Juggernaut Payment Updation Done"""
#
# """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 = ScriptTranslations.objects.get(translation_id=request.session.get('translation_id', None))
# obj2 = MNFScriptDatabase.objects.get(script_id=script_id)
# 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")
# 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 = request.session.get('translation_id', None)
# 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)
# # 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)
# addition_result = user_infos.update_info(request)
# 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
# key_value = {
# "User": self.user.username,
# "title": original_script.script_title
# }
# sendmail(to_email=[to], email_code="PP18", key_value=key_value)
#
# """Translation Completion mail done"""
#
# """Vetting Process if Choosen"""
# # sys.stdout = original_stdout
# if translated_script.lpp:
# # obj = ScriptTranslations.objects.get(translation_id=request.session.get('translation_id', None))
# # obj.lpp = True
# # obj.save()
# X = LPPTASKDatabase()
# X.user_id = translated_script.user_id
# X.usernote = "Kindly check if the translated file is correct as per the Uploaded Document!"
# X.translated_script = translated_script
# X.generated_from = "conversion"
# temp_amount = request.session["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")
#
# """Vetting Process Done"""
#
# translated_script = ScriptTranslations.objects.get(translation_id=request.session.get("translation_id", None))
# translated_script.status = "Completed"
# translated_script.save()
#
# keys_to_delete = []
# for key, value in request.session.items():
# if key not in ['_auth_user_id', '_auth_user_backend', '_auth_user_hash', '_session_init_timestamp_']:
# keys_to_delete.append(key)
#
# print("Keys to be deleted is ->", keys_to_delete)
# for key in keys_to_delete:
# del request.session[key]
#
# return JsonResponse({}, status=200)
# 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)