from .models import * from .serializers import * from rest_framework.response import Response from rest_framework.decorators import (api_view, authentication_classes, permission_classes) from rest_framework_simplejwt.authentication import JWTAuthentication from rest_framework.permissions import AllowAny, IsAuthenticated from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string from django.core.mail import send_mail , get_connection from django.contrib.auth.models import User from mnfapp.models import centralDatabase import os from threading import Thread import pandas as pd from auto_email.models import emails_creadentials , auto_email_function_errors from MNF.settings import BasePath import MNF.settings as setting from django.conf import settings import re basepath = BasePath() def newfunc(list): for i in list: t = User.objects.filter(email=i) if t.exists(): t = t[0] username = t.first_name if t.first_name else t.email return True def poster(user,email_code=None): try: # data = User.objects.filter(email = user).values() # id = data[0]['id'] if email_code.startswith("MR"): return '1' elif email_code.startswith("IM"): return '2' elif email_code.startswith("SD"): return '3' elif email_code.startswith("SB"): return '4' elif email_code.startswith("CP"): return '5' # elif email_code.startswith("PP"): # return '6' elif email_code.startswith("PP"): return '7' elif email_code.startswith("ID"): return '8' elif email_code.startswith("IP"): return '9' elif email_code.startswith("GT"): return '10' elif email_code.startswith("RM"): return '11' elif email_code.startswith("RN"): return '12' elif email_code.startswith("TN"): return '13' elif email_code.startswith("BL"): return '14' elif email_code.startswith("PM"): return '15' elif email_code.startswith("VW"): return '16' elif email_code.startswith("JP"): return '18' elif email_code.startswith("SS"): return '19' elif email_code.startswith("PB"): return '3' else : pass except Exception as e: print(e) auto_email_function_errors.objects.create(email = str(user),email_code=email_code,key_value={"test":"test"},error=str(e)) return '20' def mailer(all_email,email_code,key_value,filePath): print('basepath',basepath) try: if not key_value: key_value={} for to_email in all_email: try: try: name = centralDatabase.objects.filter(email = to_email) if name.exists(): name = name[0] userName = name.firstName.capitalize() if name.firstName else name.email except: name = User.objects.filter(email = to_email) if name.exists(): name = name[0] userName = name.first_name.capitalize() if name.first_name else name.email except: userName = to_email key_value.update({"User":userName}) sheet_code_excel = f'{basepath}/auto_email/email/sheetCodeExcel.xlsx' fileexcel = pd.read_excel(sheet_code_excel) s1 = slice(0, 2) string = email_code[s1] value = fileexcel[fileexcel['sheet code'] == string] if len(value) <= 0: auto_email_function_errors.objects.create(email = to_email,email_code=email_code,key_value=key_value,error="Email code does not match with any sheet ") return print(('Email code does not match with any sheet')) sheet_name = value.iloc[0, 1] file_path = f"{basepath}/auto_email/email/EMAILSheet40.xlsx" file = pd.read_excel(file_path, sheet_name=sheet_name, header=1) row_data = file[file['Email Code'] == str(email_code)] if len(row_data) <= 0: auto_email_function_errors.objects.create(email = to_email,email_code=email_code,key_value=key_value,error="Email code is not present in any sheet") return print('Email code is not present in any sheet') row_data = row_data.fillna('') if row_data.iloc[0,7]: additional_option_code = row_data.iloc[0, 7] print(additional_option_code,"option code") option_file = pd.read_excel( file_path, sheet_name='Common Options', header=1) option_details = option_file[option_file['CODE'] == str( additional_option_code)] value = option_details['Language of the option'].iloc[0] print(value,"fkjsdljfsl") try: match = re.search(r'\((.*?)\)', str(value)) if match: value_in_parentheses = match.group(1) else: value_in_parentheses="#" except: value_in_parentheses="#" print(option_details) else: value_in_parentheses="#" # if len(option_details) <= 0: # auto_email_function_errors.objects.create(email = to_email,email_code=email_code,key_value=key_value,error='options code does not match with sheet options code') # return print('options code does not match with sheet options code') # poster_file = pd.read_excel(file_path, sheet_name='poster', header=1) # poster_code = poster(user=to_email,email_code = email_code) # postercode = 'P'+str(poster_code) # print("POSTER CODE:---------------",postercode) # if poster_code == '11': # auto_email_function_errors.objects.create(email = to_email,email_code=email_code,key_value=key_value,error="user has not any priority to send poster") # return print('user has not any priority to send poster') # poster_url = poster_file[poster_file['Poster code'] == str(postercode)] # posterurl = poster_url.iloc[0, 2] # page_url = poster_url.iloc[0,3] # doodle_url = poster_url.iloc[0,4] # print("poster id ##########################8888888#") if row_data.iloc[0, 4]: doodle_url = str(row_data.iloc[0, 12]) posterurl = str(row_data.iloc[0, 13]) dic_subject = str(row_data.iloc[0, 4]).replace('{}','').format_map(key_value) dic_invitation = str(row_data.iloc[0, 6]).replace('{}','').format_map(key_value) if len(option_details) <= 0: dic_additional=None else: dic_additional = str(option_details.iloc[0, 2]).replace('{}','').format_map(key_value) title = str(row_data.iloc[0, 5]).replace('{}','').format_map(key_value).split('/') message = {'subject': dic_subject, 'title1': title[0], 'title2': title[1], 'invitation':dic_invitation, 'additional': dic_additional, 'poster': posterurl, 'doodlelink':value_in_parentheses, 'doodleurl':doodle_url, 'emailcode':email_code } msg_html = render_to_string('email_template.html', message) print(message['subject'],'------------------',dic_invitation,dic_additional) from_email = settings.EMAIL_HOST_USER_NAME sendMail = EmailMultiAlternatives(message['subject'],title[1],from_email,[to_email]) sendMail.attach_alternative(msg_html,"text/html") if filePath is not None: sendMail.attach_file(f'{filePath}') try: sendMail.send() print("mail sent") except Exception as e: auto_email_function_errors.objects.create( email=to_email, email_code=email_code, key_value=key_value, error=f"Failed to send email: {str(e)}" ) print(f"Error while sending email to {to_email}: {e}") notification_title=False news=False if row_data.iloc[0, 10]: notification_title = str(row_data.iloc[0, 10]).replace('{}','').format_map(key_value) if row_data.iloc[0, 11]: news = str(row_data.iloc[0, 11]).replace('{}','').format_map(key_value) print("dgdfd",notification_title,news,"fddddddddddddddddddddddddd") mnf_notification=False mnf_news=False if "Idea Mall" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Idea Mall",image_url = "/static/Notification_image/Idea_Mall.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Idea Mall",image_url = "/static/Notification_image/Idea_Mall.png") elif "Conversion - LPP" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Conversion - LPP",image_url = "/static/Notification_image/Conversion_LPP.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Conversion - LPP",image_url = "/static/Notification_image/Conversion_LPP.png") elif "Script Builder" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Script Builder",image_url = "/static/Notification_image/Script_Builder.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Script Builder",image_url = "/static/Notification_image/Script_Builder.png") elif "Messiah" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Messiah",image_url = "/static/Notification_image/Messiah.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Messiah",image_url = "/static/Notification_image/Messiah.png") elif "Project Centre" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Project Centre",image_url = "/static/Notification_image/Project_Centre.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Project Centre",image_url = "/static/Notification_image/Project_Centre.png") elif "Harkat" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Harkat",image_url = "/static/Notification_image/Harkat.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Harkat",image_url = "/static/Notification_image/Harkat.png") elif "Pitchdeck Bundle" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Pitchdeck Bundle",image_url = "/static/Notification_image/Pitchdeck_Bundle.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Pitchdeck Bundle",image_url = "/static/Notification_image/Pitchdeck_Bundle.png") elif "Script page" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Script Page",image_url = "/static/Notification_image/Script_page.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Script Page",image_url = "/static/Notification_image/Script_page.png") elif "subtitle" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Subtitle",image_url = "/static/Notification_image/subtitle.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Subtitle",image_url = "/static/Notification_image/subtitle.png") elif "script pad" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "script pad",image_url = "/static/Notification_image/subtitle.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "script pad",image_url = "/static/Notification_image/subtitle.png") elif "narration" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Narration",image_url = "/static/Notification_image/narration.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "narration",image_url = "/static/Notification_image/narration.png") elif "Institutional Membership" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Institutional Membership",image_url = "/static/Notification_image/Institutional_Membership.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Institutional Membership",image_url = "/static/Notification_image/Institutional_Membership.png") elif "LPP - Admininstration" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "LPP - Admininstration",image_url = "/static/Notification_image/LPP_Admininstration.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "LPP - Admininstration",image_url = "/static/Notification_image/LPP_Admininstration.png") elif "Conversion ppt" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Conversion PPT",image_url = "/static/Notification_image/Conversion_ppt.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Conversion PPT",image_url = "/static/Notification_image/Conversion_ppt.png") elif "viewers lounge" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Viewers Lounge",image_url = "/static/Notification_image/viewers_lounge.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Viewers Lounge",image_url = "/static/Notification_image/viewers_lounge.png") elif "Ask a question" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Ask a question",image_url = "/static/Notification_image/subtitle.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Ask a question",image_url = "/static/Notification_image/subtitle.png") elif "Payment" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Payment",image_url = "/static/Notification_image/payment.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Payment",image_url = "/static/Notification_image/payment.png") elif "blockchain" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "blockchain",image_url = "/static/Notification_image/blockchain.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "blockchain",image_url = "/static/Notification_image/blockchain.png") elif "Sheet14" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Sheet14",image_url = "/static/Notification_image/subtitle.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Sheet14",image_url = "/static/Notification_image/subtitle.png") elif "Institutional Membership" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Institutional Membership",image_url = "/static/Notification_image/subtitle.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Institutional Membership",image_url = "/static/Notification_image/subtitle.png") elif "membershipReferences" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "membershipReferences",image_url = "/static/Notification_image/subtitle.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "membershipReferences",image_url = "/static/Notification_image/subtitle.png") elif "Juggernaut" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Juggernaut",image_url = "/static/Notification_image/Juggernaut.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Juggernaut",image_url = "/static/Notification_image/Juggernaut.png") elif "giftsubscription" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "giftsubscription",image_url = "/static/Notification_image/Juggernaut.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "giftsubscription",image_url = "/static/Notification_image/Juggernaut.png") elif "storytoscript" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "storytoscript",image_url = "/static/Notification_image/Juggernaut.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "storytoscript",image_url = "/static/Notification_image/Juggernaut.png") elif "Script Stall" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "Script Stall",image_url = "/static/Notification_image/Juggernaut.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "Script Stall",image_url = "/static/Notification_image/Juggernaut.png") elif "referfriend" in sheet_name: if notification_title: mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = "referfriend",image_url = "/static/Notification_image/Juggernaut.png") if news: mnf_news=Member_News(user=to_email,title=news,service_name = "referfriend",image_url = "/static/Notification_image/Juggernaut.png") # else: # else: # mnf_notification=Mnf_Notification(user=to_email,title=notification_title,service_name = sheet_name) if mnf_notification: mnf_notification.save() print('notification sent') if mnf_news: mnf_news.save() print('news sent') return except Exception as error: print(error,"-------------------------ERROR") import sys, os exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(f"Exception type: {exc_type}, Function Name: {fname}, Line NO: {exc_tb.tb_lineno}") auto_email_function_errors.objects.create(email = to_email,email_code=email_code,key_value=key_value,error=str(error)) pass def sendmail(to_email,email_code,key_value=None,filePath=None): t = Thread(target=mailer,args=(to_email,email_code,key_value,filePath)) t.start() return @api_view(["GET", "DELETE", "POST", "PUT", "PATCH"]) @authentication_classes([JWTAuthentication]) @permission_classes([IsAuthenticated]) def mnf_notification(request,pk=None): if request.method == "GET": if pk is not None: main = Mnf_Notification.objects.all().filter(user=pk).order_by('-created') if main: serializer = Mnf_NotificationSerializer(main,many=True) return Response(serializer.data) else: return Response([]) main = Mnf_Notification.objects.all() serializer = Mnf_NotificationSerializer(main, many=True) return Response(serializer.data) if request.method == "PATCH": main = Mnf_Notification.objects.get(id=pk) serializer = Mnf_NotificationSerializer(main, data=request.data ,partial=True) if serializer.is_valid(): serializer.save() return Response("partial data updated") return Response(serializer.errors) if request.method == "DELETE": if pk is not None: main = Mnf_Notification.objects.filter(id=pk) # else: # main = MemberProfile.objects.filter(projectid=pk) main.delete() return Response("data deleted") @api_view(["GET"]) @authentication_classes([JWTAuthentication]) @permission_classes([IsAuthenticated]) def getNews(request,pk=None): if request.method == "GET": if pk is not None: if centralDatabase.objects.filter(user_id=pk).exists(): target_user = centralDatabase.objects.get(user_id=pk) main = Member_News.objects.all().filter(user=target_user.email).order_by('-created') if main: serializer = Member_NewsSerializer(main,many=True) return Response(serializer.data) else: return Response([]) # if centralDatabase.objects.filter(user_id=pk).exists(): # target_user = centralDatabase.objects.get(user_id=pk) # main = Member_News.objects.all().filter(user=target_user.email).order_by('-created') # if main: # serializer = Member_News(main,many=True,context={'replacement_name': target_user.firstName}) # return Response(serializer.data) # else: # return Response([]) # else: # return Response({"msg":"user not found"})