コード例 #1
0
 def test_record_activity_invalid_template(self):
     """Test to record activity for invalid template"""
     with app.test_request_context():
         test_user = create_user(email="*****@*****.**", password="******")
         record_activity('invalid_template', login_user=test_user, user=test_user)
         user_id_format = ' (' + str(test_user.id) + ')'
         test_actor = test_user.email + user_id_format
         self.assertTrue('[ERROR LOGGING] invalid_template',
                         db.session.query(Activity).filter_by(actor=test_actor).first().action)
コード例 #2
0
def send_notification(user, action, title, message):
    if not current_app.config['TESTING']:
        notification = Notification(user_id=user.id,
                                    title=title,
                                    message=message,
                                    action=action
                                    )
        save_to_db(notification, msg="Notification saved")
        record_activity('notification_event', user=user, action=action, title=title)
コード例 #3
0
 def test_record_activity_valid_template(self):
     """Test to record activity for valid template"""
     with self.app.test_request_context():
         test_user = create_user(email="*****@*****.**", password="******")
         record_activity('create_user', login_user=test_user, user=test_user)
         user_id_format = ' (' + str(test_user.id) + ')'
         test_actor = test_user.email + user_id_format
         assert 'User [email protected]' + user_id_format + ' created', \
             db.session.query(Activity).filter_by(actor=test_actor).first().action
コード例 #4
0
def send_notification(user, action, title, message):
    if not current_app.config['TESTING']:
        notification = Notification(user_id=user.id,
                                    title=title,
                                    message=message,
                                    action=action)
        save_to_db(notification, msg="Notification saved")
        record_activity('notification_event',
                        user=user,
                        action=action,
                        title=title)
コード例 #5
0
def send_notification(user, title, message, actions=None):
    """
    Helper function to send notifications.
    :param user:
    :param title:
    :param message:
    :param actions:
    :return:
    """
    notification = Notification(user_id=user.id, title=title, message=message)
    if not actions:
        actions = []
    notification.actions = actions
    save_to_db(notification, msg="Notification saved")
    record_activity('notification_event', user=user, title=title, actions=actions)
コード例 #6
0
def send_notification(user, title, message, actions=None):
    """
    Helper function to send notifications.
    :param user:
    :param title:
    :param message:
    :param actions:
    :return:
    """
    if not current_app.config['TESTING']:
        notification = Notification(user_id=user.id,
                                    title=title,
                                    message=message
                                    )
        if not actions:
            actions = []
        notification.actions = actions
        save_to_db(notification, msg="Notification saved")
        record_activity('notification_event', user=user, title=title, actions=actions)
コード例 #7
0
def send_email(to, action, subject, html, attachments=None):
    """
    Sends email and records it in DB
    """
    from .tasks import send_email_task_sendgrid, send_email_task_smtp

    if not string_empty(to):
        email_service = get_settings()['email_service']
        email_from_name = get_settings()['email_from_name']
        if email_service == 'smtp':
            email_from = email_from_name + '<' + get_settings(
            )['email_from'] + '>'
        else:
            email_from = get_settings()['email_from']
        payload = {
            'to': to,
            'from': email_from,
            'subject': subject,
            'html': html,
            'attachments': attachments,
        }

        if not current_app.config['TESTING']:
            smtp_encryption = get_settings()['smtp_encryption']
            if smtp_encryption == 'tls':
                smtp_encryption = 'required'
            elif smtp_encryption == 'ssl':
                smtp_encryption = 'ssl'
            elif smtp_encryption == 'tls_optional':
                smtp_encryption = 'optional'
            else:
                smtp_encryption = 'none'

            smtp_config = {
                'host': get_settings()['smtp_host'],
                'username': get_settings()['smtp_username'],
                'password': get_settings()['smtp_password'],
                'encryption': smtp_encryption,
                'port': get_settings()['smtp_port'],
            }
            smtp_status = check_smtp_config(smtp_encryption)
            if smtp_status:
                if email_service == 'smtp':
                    send_email_task_smtp.delay(payload=payload,
                                               headers=None,
                                               smtp_config=smtp_config)
                else:
                    key = get_settings().get('sendgrid_key')
                    if key:
                        headers = {
                            "Authorization": ("Bearer " + key),
                            "Content-Type": "application/json",
                        }
                        payload['fromname'] = email_from_name
                        send_email_task_sendgrid.delay(payload=payload,
                                                       headers=headers,
                                                       smtp_config=smtp_config)
                    else:
                        logging.exception(
                            'SMTP & sendgrid have not been configured properly'
                        )

            else:
                logging.exception(
                    'SMTP is not configured properly. Cannot send email.')
        # record_mail(to, action, subject, html)
        mail = Mail(
            recipient=to,
            action=action,
            subject=subject,
            message=html,
            time=datetime.utcnow(),
        )

        save_to_db(mail, 'Mail Recorded')
        record_activity('mail_event', email=to, action=action, subject=subject)
    return True
コード例 #8
0
ファイル: mail.py プロジェクト: pc-beast/open-event-server
def send_email(to, action, subject, html, attachments=None, bcc=None, reply_to=None):
    """
    Sends email and records it in DB
    """
    from .tasks import get_smtp_config, send_email_task_sendgrid, send_email_task_smtp

    if not MessageSettings.is_enabled(action):
        logger.info("Mail of type %s is not enabled. Hence, skipping...", action)
        return

    if isinstance(to, User):
        logger.warning('to argument should be an email string, not a User object')
        to = to.email

    if string_empty(to):
        logger.warning('Recipient cannot be empty')
        return False
    email_service = get_settings()['email_service']
    email_from_name = get_settings()['email_from_name']
    if email_service == 'smtp':
        email_from = email_from_name + '<' + get_settings()['email_from'] + '>'
    else:
        email_from = get_settings()['email_from']
    payload = {
        'to': to,
        'from': email_from,
        'subject': subject,
        'html': html,
        'attachments': attachments,
        'bcc': bcc,
        'reply_to': reply_to,
    }

    if not (current_app.config['TESTING'] or email_service == 'disable'):
        if email_service == 'smtp':
            smtp_status = check_smtp_config(get_smtp_config())
            if smtp_status:
                send_email_task_smtp.delay(payload)
            else:
                logger.error('SMTP is not configured properly. Cannot send email.')
        elif email_service == 'sendgrid':
            key = get_settings().get('sendgrid_key')
            if key:
                payload['fromname'] = email_from_name
                send_email_task_sendgrid.delay(payload)
            else:
                logger.error('SMTP & sendgrid have not been configured properly')
        else:
            logger.error(
                'Invalid Email Service Setting: %s. Skipping email', email_service
            )
    else:
        logger.warning('Email Service is disabled in settings, so skipping email')

    mail_recorder = current_app.config['MAIL_RECORDER']
    mail_recorder.record(payload)

    mail = Mail(
        recipient=to,
        action=action,
        subject=subject,
        message=html,
    )

    save_to_db(mail, 'Mail Recorded')
    record_activity('mail_event', email=to, action=action, subject=subject)

    return True
コード例 #9
0
def send_email(to, action, subject, html):
    """
    Sends email and records it in DB
    """
    if not string_empty(to):
        email_service = get_settings()['email_service']
        email_from_name = get_settings()['email_from_name']
        if email_service == 'smtp':
            email_from = email_from_name + '<' + get_settings(
            )['email_from'] + '>'
        else:
            email_from = get_settings()['email_from']
        payload = {
            'to': to,
            'from': email_from,
            'subject': subject,
            'html': html
        }

        if not current_app.config['TESTING']:
            if email_service == 'smtp':
                smtp_encryption = get_settings()['smtp_encryption']
                if smtp_encryption == 'tls':
                    smtp_encryption = 'required'
                elif smtp_encryption == 'ssl':
                    smtp_encryption = 'ssl'
                elif smtp_encryption == 'tls_optional':
                    smtp_encryption = 'optional'
                else:
                    smtp_encryption = 'none'

                config = {
                    'host': get_settings()['smtp_host'],
                    'username': get_settings()['smtp_username'],
                    'password': get_settings()['smtp_password'],
                    'encryption': smtp_encryption,
                    'port': get_settings()['smtp_port'],
                }

                from .tasks import send_mail_via_smtp_task
                send_mail_via_smtp_task.delay(config, payload)
            else:
                payload['fromname'] = email_from_name
                key = get_settings()['sendgrid_key']
                if not key:
                    print('Sendgrid key not defined')
                    return
                headers = {
                    "Authorization": ("Bearer " + key),
                    "Content-Type": "application/json"
                }
                from .tasks import send_email_task
                send_email_task.delay(payload, headers)

        # record_mail(to, action, subject, html)
        mail = Mail(recipient=to,
                    action=action,
                    subject=subject,
                    message=html,
                    time=datetime.utcnow())

        save_to_db(mail, 'Mail Recorded')
        record_activity('mail_event', email=to, action=action, subject=subject)
    return True
コード例 #10
0
def send_email(to, action, subject, html):
    """
    Sends email and records it in DB
    """
    if not string_empty(to):
        email_service = get_settings()['email_service']
        email_from_name = get_settings()['email_from_name']
        if email_service == 'smtp':
            email_from = email_from_name + '<' + get_settings()['email_from'] + '>'
        else:
            email_from = get_settings()['email_from']
        payload = {
            'to': to,
            'from': email_from,
            'subject': subject,
            'html': html
        }

        if not current_app.config['TESTING']:
            if email_service == 'smtp':
                smtp_encryption = get_settings()['smtp_encryption']
                if smtp_encryption == 'tls':
                    smtp_encryption = 'required'
                elif smtp_encryption == 'ssl':
                    smtp_encryption = 'ssl'
                elif smtp_encryption == 'tls_optional':
                    smtp_encryption = 'optional'
                else:
                    smtp_encryption = 'none'

                config = {
                    'host': get_settings()['smtp_host'],
                    'username': get_settings()['smtp_username'],
                    'password': get_settings()['smtp_password'],
                    'encryption': smtp_encryption,
                    'port': get_settings()['smtp_port'],
                }

                from .tasks import send_mail_via_smtp_task
                send_mail_via_smtp_task.delay(config, payload)
            else:
                payload['fromname'] = email_from_name
                key = get_settings()['sendgrid_key']
                if not key:
                    print('Sendgrid key not defined')
                    return
                headers = {
                    "Authorization": ("Bearer " + key),
                    "Content-Type": "application/json"
                }
                from .tasks import send_email_task
                send_email_task.delay(payload, headers)

        # record_mail(to, action, subject, html)
        mail = Mail(
            recipient=to, action=action, subject=subject,
            message=html, time=datetime.utcnow()
        )

        save_to_db(mail, 'Mail Recorded')
        record_activity('mail_event', email=to, action=action, subject=subject)
    return True