Coverage for app/controllers/events/email.py: 36%

55 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-11-19 23:53 +0000

1from app.models.emailTemplate import EmailTemplate 

2from app.models.emailLog import EmailLog 

3from app.models.event import Event 

4from app.controllers.main import main_bp 

5from app.logic.emailHandler import EmailHandler 

6from app.models.program import Program 

7from flask import request, flash, g, redirect, url_for 

8from urllib.parse import urlparse 

9 

10@main_bp.route('/email', methods=['POST']) 

11def email(): 

12 raw_form_data = request.form.copy() 

13 attachments = request.files.getlist('attachmentObject') 

14 if "@" in raw_form_data['emailSender']: 

15 # when people are sending emails as themselves (using mailto) --- Q: are we still going with the mailto option? 

16 pass 

17 else: 

18 url_domain = urlparse(request.base_url).netloc 

19 mail = EmailHandler(raw_form_data, url_domain, attachment_file=attachments) 

20 mail_sent = mail.send_email() 

21 

22 if mail_sent: 

23 message, status = 'Email successfully sent!', 'success' 

24 else: 

25 message, status = 'Error sending email', 'danger' 

26 flash(message, status) 

27 return redirect(url_for("main.events", selectedTerm = raw_form_data['selectedTerm'])) 

28 

29 

30@main_bp.route('/retrieveSenderList/<eventId>', methods=['GET']) 

31def retrieveSenderList(eventId): 

32 senderOptions = [["CELTS (celts@berea.edu)", "Celts"]] 

33 

34 event = Event.get_by_id(eventId) 

35 if event.program_id: 

36 programEmail = event.program.contactEmail if event.program.contactEmail else "No Program Email Found" 

37 programOption = [f"{event.program.programName} ({programEmail})", event.program.programName] 

38 senderOptions.append(programOption) 

39 

40 studentStaffOptions = [] 

41 senderOptions.extend(studentStaffOptions) 

42 

43 currentUserOption = [f"Current User ({g.current_user.email})", g.current_user.username] 

44 senderOptions.append(currentUserOption) 

45 

46 return senderOptions 

47 

48 

49@main_bp.route('/retrieveEmailTemplate/<eventId>', methods=['GET']) 

50def retrieveEmailTemplate(eventId): 

51 templateInfo = {} 

52 emailTemplates = EmailTemplate.select() 

53 

54 for index, template in enumerate(emailTemplates): 

55 templateInfo[index] = { 

56 'purpose': template.purpose, 

57 'subject':template.subject, 

58 'body': EmailHandler.replaceStaticPlaceholders(eventId, template.body) 

59 } 

60 return templateInfo 

61 

62@main_bp.route('/retrievePlaceholderList/<eventId>', methods=['GET']) 

63def retrievePlaceholderList(eventId): 

64 return EmailHandler.retrievePlaceholderList(eventId) 

65 

66 

67@main_bp.route('/fetchEmailLogData/<eventId>', methods=['GET']) 

68def fetchEmailLogData(eventId): 

69 last_email = EmailHandler.retrieve_last_email(eventId) 

70 if last_email: 

71 return {'last_log': "The last email was sent to " + last_email.recipientsCategory + " on " + last_email.dateSent.strftime('%m/%d/%Y') + " by " + last_email.sender + "." , 'last_log2': " Subject: " + last_email.subject} 

72 else: 

73 return {'exists': False} 

74 

75@main_bp.route("/getProgramSender/", methods=['GET']) 

76def getProgramSender(): 

77 programId = request.args.get("programId") 

78 return Program.get_by_id(programId).contactName