def post(self):
        """Create appointment reminders."""
        appointments = Appointment.find_next_day_appointments()
        print('sending {} reminders'.format(len(appointments)))

        if appointments:
            for (appointment, office, timezone, user) in appointments:
                send_reminder = False
                if user and user.send_reminders:
                    send_reminder = True
                elif not user and is_valid_email(
                        appointment.contact_information):
                    send_reminder = True

                if send_reminder:

                    @copy_current_request_context
                    def async_email(subject, email, sender, body):
                        send_email(subject, email, sender, body)

                    thread = Thread(target=async_email,
                                    args=get_reminder_email_contents(
                                        appointment, user, office, timezone))
                    thread.daemon = True
                    thread.start()
Пример #2
0
    def get(self):
        """Return appointment reminders for next day."""
        appointments = Appointment.find_next_day_appointments()

        # Construct a custom response as teh payload can grow in size.
        reminders = {
            'appointments': []
        }

        if appointments:
            for (appointment, office, timezone, user) in appointments:
                send_reminder = False
                if user and user.send_reminders:
                    send_reminder = True
                elif not user and is_valid_email(appointment.contact_information):
                    send_reminder = True

                if send_reminder:
                    date, day = formatted_date(appointment.start_time, timezone)

                    reminders['appointments'].append(
                        {
                            'formatted_date': date,
                            'day': day,
                            'email': get_email(user, appointment),
                            'display_name': appointment.citizen_name,
                            'location': office.office_name,
                            'duration': get_duration(appointment.start_time, appointment.end_time),
                            'telephone': office.telephone
                        }
                    )
        return reminders
Пример #3
0
    def get(self, reminder_type: str = 'email'):
        """Return appointment reminders for next day."""
        appointments = Appointment.find_next_day_appointments()

        # Construct a custom response as teh payload can grow in size.
        reminders = {'appointments': []}

        if appointments:
            for (appointment, office, timezone, user) in appointments:
                send_reminder = False
                if reminder_type == 'email':
                    if user and user.send_email_reminders:
                        send_reminder = True
                    elif not user and is_valid_email(
                            appointment.contact_information):
                        send_reminder = True
                elif reminder_type == 'sms':
                    if user and user.send_sms_reminders:
                        send_reminder = True
                        user_telephone = user.telephone
                    elif not user and is_valid_phone(
                            appointment.contact_information):
                        send_reminder = True
                        user_telephone = appointment.contact_information

                if send_reminder:
                    if reminder_type == 'email':
                        date, day = formatted_date(appointment.start_time,
                                                   timezone)
                    else:
                        date, day = format_sms_date(appointment.start_time,
                                                    timezone), None

                    office_email_paragraph = appointment.office.office_email_paragraph
                    if office_email_paragraph:
                        office_email_paragraph = office_email_paragraph.replace(
                            '\r\n', '<br />')

                    service_email_paragraph = appointment.service.email_paragraph
                    if service_email_paragraph:
                        service_email_paragraph = service_email_paragraph.replace(
                            '\r\n', '<br />')

                    service_name = appointment.service.external_service_name \
                        if appointment.service.external_service_name else appointment.service.service_name

                    reminders['appointments'].append({
                        'formatted_date':
                        date,
                        'day':
                        day,
                        'email':
                        get_email(user, appointment),
                        'display_name':
                        appointment.citizen_name,
                        'location':
                        office.office_name,
                        'duration':
                        get_duration(appointment.start_time,
                                     appointment.end_time),
                        'telephone':
                        office.telephone,
                        'service_email_paragraph':
                        service_email_paragraph,
                        'office_email_paragraph':
                        office_email_paragraph,
                        'service_name':
                        service_name,
                        'civic_address':
                        appointment.office.civic_address,
                        'user_telephone':
                        user_telephone,
                    })
        return reminders