def sendPblEmails( className, academicYear, quarter, emailRecipients, selectedEmailRecipients, emailTemplate, ): pblEmailRecipients = getPblCommunicationsRecipients( className, academicYear, quarter, emailRecipients, selectedEmailRecipients, ) template = p2mtTemplates.query.get_or_404(emailTemplate) print("Email Template:", template.templateTitle) for pblTeamMember in pblEmailRecipients: print( "Email recipient:", pblTeamMember.Student.firstName, pblTeamMember.Student.lastName, ) # Get PBL team members to include in parameters: pblTeamList = [] membersOfPblTeam = (PblTeams.query.outerjoin(Pbls).filter( PblTeams.className == className, PblTeams.academicYear == academicYear, PblTeams.quarter == quarter, ).join(Student).filter( PblTeams.pblTeamNumber == pblTeamMember.pblTeamNumber).order_by( Student.lastName)) for member in membersOfPblTeam: memberName = f"{member.Student.firstName} {member.Student.lastName}" pblTeamList.append(memberName) pblTeamParams = { "pblTeamMembers": pblTeamList, "pblTeamNumber": pblTeamMember.pblTeamNumber, } if pblTeamMember.Pbls: pblName = pblTeamMember.Pbls.pblName pblParams = { "pblId": pblTeamMember.pbl_id, "pblName": pblTeamMember.Pbls.pblName, "pblSponsor": pblTeamMember.Pbls.pblSponsor, "pblSponsorPersonName": pblTeamMember.Pbls.pblSponsorPersonName, "pblComments": pblTeamMember.Pbls.pblComments, } # Get kickoff details kickoffEvent = PblEvents.query.filter( PblEvents.pbl_id == pblTeamMember.pbl_id, PblEvents.eventCategory == "Kickoff", ).first() finalEvent = PblEvents.query.filter( PblEvents.pbl_id == pblTeamMember.pbl_id, PblEvents.eventCategory == "Final", ).first() eventParams = { "kickoffEventDate": kickoffEvent.eventDate, "kickoffStartTime": kickoffEvent.startTime, "kickoffEndTime": kickoffEvent.endTime, "kickoffEventLocation": kickoffEvent.eventLocation, "kickoffEventStreetAddress": kickoffEvent.eventStreetAddress1, "kickoffEventCity": kickoffEvent.eventCity, "kickoffEventState": kickoffEvent.eventState, "kickoffEventZip": kickoffEvent.eventZip, "kickoffEventComments": kickoffEvent.eventComments, "finalEventDate": finalEvent.eventDate, "finalStartTime": finalEvent.startTime, "finalEndTime": finalEvent.endTime, "finalEventLocation": finalEvent.eventLocation, "finalEventStreetAddress": finalEvent.eventStreetAddress1, "finalEventCity": finalEvent.eventCity, "finalEventState": finalEvent.eventState, "finalEventZip": finalEvent.eventZip, "finalEventComments": finalEvent.eventComments, } else: pblParams = {} eventParams = {} basicParams = { "chattStateANumber": pblTeamMember.chattStateANumber, "studentFirstName": pblTeamMember.Student.firstName, "studentLastName": pblTeamMember.Student.lastName, "academicYear": pblTeamMember.academicYear, "semester": pblTeamMember.semester, "quarter": pblTeamMember.quarter, "quarterOrdinal": getQuarterOrdinal(pblTeamMember.quarter), } templateParams = { **basicParams, **pblTeamParams, **pblParams, **eventParams } email_to = pblTeamMember.Student.email emailSubject, emailContent, is_render_error = renderEmailTemplate( template.emailSubject, template.templateContent, templateParams) if is_render_error: flash( """Unable to render email content. Correct the email template and try again.<br> Note: some template variables may not be available for this intervention type.""", "error", ) return try: email_cc = current_user.email except: email_cc = "" # try: sendEmail(email_to, email_cc, emailSubject, emailContent) # except: # print("Error sending email") return
def sendInterventionEmail(chattStateANumber, intervention_id, interventionLevel, startDate, endDate, comment, **kwargs): # Prepare and send intervention notification emails interventionType = getInterventionType(intervention_id) try: template = (p2mtTemplates.query.filter( InterventionType.interventionType == interventionType, p2mtTemplates.interventionLevel == interventionLevel, ).join(InterventionType).first()) print("Using template:", template.templateTitle) except: print( "No template exists for interventionType =", interventionType, "and interventionLevel =", interventionLevel, ) return studentFirstName, studentLastName = getStudentFirstNameAndLastName( chattStateANumber) studentScheduleLink = getStudentScheduleLink(chattStateANumber) # Set email_to based on template parameters studentEmail = getStudentEmail(chattStateANumber) parentEmailList = getParentEmails(chattStateANumber) if template.sendToStudent and not template.sendToParent: email_to = studentEmail if template.sendToStudent and template.sendToParent: email_to = [studentEmail] + parentEmailList if not template.sendToStudent and template.sendToParent: email_to = parentEmailList if not template.sendToStudent and not template.sendToParent: email_to = current_user.email # Use templateParams if they were passed to the function; otherwise use the default params if "templateParams" in kwargs: templateParams = kwargs["templateParams"] templateParams["chattStateANumber"] = chattStateANumber templateParams["studentFirstName"] = studentFirstName templateParams["studentLastName"] = studentLastName templateParams["startDate"] = startDate templateParams["endDate"] = endDate templateParams["comment"] = comment templateParams["studentScheduleLink"] = studentScheduleLink else: templateParams = { "chattStateANumber": chattStateANumber, "studentFirstName": studentFirstName, "studentLastName": studentLastName, "startDate": startDate, "endDate": endDate, "comment": comment, "studentScheduleLink": studentScheduleLink, } emailSubject, emailContent = renderEmailTemplate(template.emailSubject, template.templateContent, templateParams) try: email_cc = current_user.email except: email_cc = "" try: sendEmail(email_to, email_cc, emailSubject, emailContent) interventionStatus = "Intervention notification sent" except: interventionStatus = "Error sending email" return interventionStatus
def calculateTmi( startTmiPeriod, endTmiPeriod, tmiDate, sendStudentTmiNotification, sendParentTmiNotification, **kwargs, ): printLogEntry("calculateTMI() function called") # Check whether a chattStateANumber is passed as an optional kwarg # If so, process the request for a single student instead of all students # This case is used to send TMI notifications for an individual student if "chattStateANumber" in kwargs: chattStateANumber = kwargs["chattStateANumber"] studentsWithAssignTmi = getStudentsWithAssignTmi( startTmiPeriod, endTmiPeriod, chattStateANumber=chattStateANumber) else: # Remove tmi interventions for students with no classes marked assignTmi=True tmiInterventionLogs = getStudentsWithTmiInterventions(tmiDate) # Cycle through list of tmi logs for tmiLog in tmiInterventionLogs: # Search for classes with assignTmi=True tmiClassesSearchResult = (db.session.query( ClassAttendanceLog).join(ClassSchedule).filter( ClassSchedule.chattStateANumber == tmiLog.chattStateANumber, ClassAttendanceLog.classDate >= startTmiPeriod, ClassAttendanceLog.classDate <= endTmiPeriod, ClassAttendanceLog.assignTmi == True, ).all()) # If no classes found, then delete the log from the intervention log if len(tmiClassesSearchResult) == 0: db.session.delete(tmiLog) studentsWithAssignTmi = getStudentsWithAssignTmi( startTmiPeriod, endTmiPeriod) for student in studentsWithAssignTmi: student_id = student[0] chattStateANumber = student[1] studentFirstName = student[2] studentLastName = student[3] studentEmail = student[4] tmiClasses = findTmiClassesForStudent(startTmiPeriod, endTmiPeriod, chattStateANumber) tmiMinutes = 0 tardyFlag = False classAttendanceLogList = [] for log in tmiClasses: attendanceCode = log.attendanceCode classAttendanceLogID = log.id className = log.ClassSchedule.className classDate = log.classDate teacherLastName = log.ClassSchedule.teacherLastName chattStateANumber = log.ClassSchedule.chattStateANumber if attendanceCode == "T" and tardyFlag == True: classAttendanceArrayItem = { "classDate": classDate, "className": className, "attendanceType": "Tardy", "teacherLastName": teacherLastName, } classAttendanceLogList.append(classAttendanceArrayItem) elif attendanceCode == "T" and tardyFlag == False: tmiMinutes = tmiMinutes + 90 tardyFlag = True classAttendanceArrayItem = { "classDate": classDate, "className": className, "attendanceType": "Tardy", "teacherLastName": teacherLastName, } classAttendanceLogList.append(classAttendanceArrayItem) elif attendanceCode == "E": tmiMinutes = tmiMinutes + 120 classAttendanceArrayItem = { "classDate": classDate, "className": className, "attendanceType": "Excused Absence (But Missing Work)", "teacherLastName": teacherLastName, } classAttendanceLogList.append(classAttendanceArrayItem) elif attendanceCode == "U": tmiMinutes = tmiMinutes + 120 classAttendanceArrayItem = { "classDate": classDate, "className": className, "attendanceType": "Unexcused Absence", "teacherLastName": teacherLastName, } classAttendanceLogList.append(classAttendanceArrayItem) maxTmiMinutes = 420 if tmiMinutes > maxTmiMinutes: tmiMinutes = maxTmiMinutes interventionStatus = "Pending" studentNotification = None parentNotification = None print(chattStateANumber, classAttendanceLogList, tmiMinutes) print("tmiMinutes:", type(tmiMinutes), tmiMinutes) # Prepare and send TMI notification emails # Ignore cases where tmiMinutes = 0 (e.g., attendanceType is Q for question) # If sendStudentNotification == True, then get email recipient and appropriate email template if sendStudentTmiNotification and tmiMinutes > 0: print( "sendStudentTmiNotification is", sendStudentTmiNotification, "and tmiMinutes =", tmiMinutes, ) email_to = studentEmail interventionStatus = "Student notification sent" studentNotification = datetime.utcnow() try: template = (p2mtTemplates.query.filter( InterventionType.interventionType == "Attendance", p2mtTemplates.interventionLevel == 1, p2mtTemplates.sendToParent == False, p2mtTemplates.sendToStudent == True, ).join(InterventionType).first()) print("Using template:", template.templateTitle) except: print( "No template exists for attendance intervention, level 1, for student notifications" ) # If sendParentTmiNotification == True, then get email recipient and appropriate email template if sendParentTmiNotification and tmiMinutes > 0: print( "sendParentTmiNotification is", sendParentTmiNotification, "and tmiMinutes =", tmiMinutes, ) email_to = [studentEmail] + getParentEmails(chattStateANumber) interventionStatus = "Parent notification sent" parentNotification = datetime.utcnow() try: template = (p2mtTemplates.query.filter( InterventionType.interventionType == "Attendance", p2mtTemplates.interventionLevel == 1, p2mtTemplates.sendToParent == True, p2mtTemplates.sendToStudent == True, ).join(InterventionType).first()) print("Using template:", template.templateTitle) except: print( "No template exists for attendance intervention, level 1, for parent notifications" ) # Render email template with data and send the email if (sendStudentTmiNotification or sendParentTmiNotification) and tmiMinutes > 0: print( "sendStudentTmiNotification is", sendStudentTmiNotification, "sendParentTmiNotification is", sendParentTmiNotification, "and tmiMinutes =", tmiMinutes, ) templateParams = { "chattStateANumber": chattStateANumber, "studentFirstName": studentFirstName, "studentLastName": studentLastName, "tmiDate": tmiDate, "tmiMinutes": tmiMinutes, "classAttendanceLogList": classAttendanceLogList, } emailSubject, emailContent, is_render_error = renderEmailTemplate( template.emailSubject, template.templateContent, templateParams) if is_render_error: flash( """Unable to render email content. Correct the email template and try again.<br> Note: some template variables may not be available for this intervention type.""", "error", ) return try: email_cc = current_user.email except: email_cc = "" try: sendEmail(email_to, email_cc, emailSubject, emailContent) except: interventionStatus = "Error sending email" # Update intervention log updateInterventionLogForTmi( chattStateANumber, tmiDate, tmiMinutes, interventionStatus, studentNotification, parentNotification, ) return