Esempio n. 1
0
    def it_updates_an_email_dao(self, db, db_session, sample_email):
        dao_update_email(sample_email.id,
                         send_starts_at='2019-06-05',
                         extra_txt='test update')

        email_from_db = Email.query.filter(Email.id == sample_email.id).first()

        assert email_from_db.extra_txt == 'test update'
    def it_updates_an_email_with_members_sent_to_dao(self, db, db_session,
                                                     sample_email,
                                                     sample_member):
        members = [sample_member]
        dao_update_email(sample_email.id, members_sent_to=members)

        email_from_db = Email.query.filter(Email.id == sample_email.id).first()

        assert email_from_db.members_sent_to == members
    def it_does_not_add_an_existing_member_sent_to_email(
            self, db, db_session, sample_email, sample_member):
        members = [sample_member]
        dao_update_email(sample_email.id, members_sent_to=members)

        with pytest.raises(expected_exception=IntegrityError):
            dao_add_member_sent_to_email(sample_email.id, sample_member.id)

        email_from_db = Email.query.filter(Email.id == sample_email.id).first()

        assert email_from_db.members_sent_to == [sample_member]
    def it_adds_a_member_sent_to_email(self, db, db_session, sample_email,
                                       sample_member):
        members = [sample_member]
        dao_update_email(sample_email.id, members_sent_to=members)

        member = create_member(name='New member',
                               email='*****@*****.**')

        dao_add_member_sent_to_email(sample_email.id, member.id)
        email_from_db = Email.query.filter(Email.id == sample_email.id).first()

        assert email_from_db.members_sent_to == [sample_member, member]
Esempio n. 5
0
def update_email(email_id):
    data = request.get_json(force=True)

    validate(data, post_update_email_schema)

    if data['email_type'] == EVENT:
        try:
            event = dao_get_event_by_id(data.get('event_id'))
        except NoResultFound:
            raise InvalidRequest(
                'event not found: {}'.format(data.get('event_id')), 400)

    email_data = {}
    for k in data.keys():
        if hasattr(Email, k):
            email_data[k] = data[k]

    current_app.logger.info('Update email: {}'.format(email_data))

    res = dao_update_email(email_id, **email_data)

    if res:
        email = dao_get_email_by_id(email_id)
        response = None
        emails_to = [user.email for user in dao_get_users()]

        if data.get('email_state') == READY:
            # send email to admin users and ask them to log in in order to approve the email
            review_part = '<div>Please review this email: {}/emails/{}</div>'.format(
                current_app.config['FRONTEND_ADMIN_URL'], str(email.id))

            subject = None
            if email.email_type == EVENT:
                event = dao_get_event_by_id(email.event_id)
                subject = 'Please review {}'.format(event.title)

                event_html = get_email_html(**data)
                response = send_smtp_email(emails_to, subject,
                                           review_part + event_html)
            elif email.email_type == MAGAZINE:
                magazine = dao_get_magazine_by_id(email.magazine_id)
                subject = 'Please review {}'.format(magazine.title)

                magazine_html = get_email_html(MAGAZINE,
                                               magazine_id=magazine.id)
                response = send_smtp_email(emails_to, subject,
                                           review_part + magazine_html)
        elif data.get('email_state') == REJECTED:
            dao_update_email(email_id, email_state=REJECTED)

            message = '<div>Please correct this email <a href="{}">{}</a></div>'.format(
                '{}/emails/{}'.format(current_app.config['FRONTEND_ADMIN_URL'],
                                      str(email.id)), email.get_subject())

            message += '<div>Reason: {}</div>'.format(
                data.get('reject_reason'))

            response = send_smtp_email(
                emails_to,
                '{} email needs to be corrected'.format(event.title), message)
        elif data.get('email_state') == APPROVED:
            later = datetime.utcnow() + timedelta(
                hours=current_app.config['EMAIL_DELAY'])
            if later < email.send_starts_at:
                later = email.send_starts_at + timedelta(hours=9)

            dao_update_email(email_id, send_after=later)

            review_part = '<div>Email will be sent after {}, log in to reject: {}/emails/{}</div>'.format(
                later.strftime("%d/%m/%Y %H:%M"),
                current_app.config['FRONTEND_ADMIN_URL'], str(email.id))

            if email.email_type == EVENT:
                event_html = get_email_html(**data)
                response = send_smtp_email(
                    emails_to,
                    "{} has been approved".format(email.get_subject()),
                    review_part + event_html)
        email_json = email.serialize()
        if response:
            email_json['email_status_code'] = response
        return jsonify(email_json), 200

    raise InvalidRequest('{} did not update email'.format(email_id), 400)
Esempio n. 6
0
def update_email(email_id):
    data = request.get_json(force=True)

    validate(data, post_update_email_schema)

    if data['email_type'] == EVENT:
        try:
            event = dao_get_event_by_id(data.get('event_id'))
        except NoResultFound:
            raise InvalidRequest('event not found: {}'.format(data.get('event_id')), 400)

    email_data = {}
    for k in data.keys():
        if hasattr(Email, k):
            email_data[k] = data[k]

    current_app.logger.info('Update email: {}'.format(email_data))

    res = dao_update_email(email_id, **email_data)

    if res:
        email = dao_get_email_by_id(email_id)
        response = None
        emails_to = [user.email for user in dao_get_users()]

        if data.get('email_state') == READY:
            subject = None
            if data['email_type'] == EVENT:
                event = dao_get_event_by_id(data.get('event_id'))
                subject = 'Please review {}'.format(event.title)

            # send email to admin users and ask them to log in in order to approve the email
            review_part = '<div>Please review this email: {}/emails/{}</div>'.format(
                current_app.config['FRONTEND_ADMIN_URL'], str(email.id))
            event_html = get_email_html(**data)
            response = send_email(emails_to, subject, review_part + event_html)
        elif data.get('email_state') == REJECTED:
            if email.task_id:
                revoke_task(email.task_id)

            message = '<div>Please correct this email <a href="{}">{}</a></div>'.format(
                '{}/emails/{}'.format(current_app.config['FRONTEND_ADMIN_URL'], str(email.id)),
                email.get_subject())

            message += '<div>Reason: {}</div>'.format(data.get('reject_reason'))

            response = send_email(emails_to, '{} email needs to be corrected'.format(event.title), message)
        elif data.get('email_state') == APPROVED:
            # send the email later in order to allow it to be rejected
            later = datetime.utcnow() + timedelta(seconds=current_app.config['EMAIL_DELAY'])
            if later < email.send_starts_at:
                later = email.send_starts_at + timedelta(hours=9)

            result = email_tasks.send_emails.apply_async(((str(email_id)),), eta=later)

            dao_update_email(email_id, task_id=result.id)
            current_app.logger.info('Task: send_email: %d, %r at %r', email_id, result.id, later)

            review_part = '<div>Email will be sent at {}, log in to reject: {}/emails/{}</div>'.format(
                later, current_app.config['FRONTEND_ADMIN_URL'], str(email.id))
            event_html = get_email_html(**data)
            response = send_email(
                emails_to, "{} has been approved".format(email.get_subject()), review_part + event_html)

        email_json = email.serialize()
        if response:
            email_json['email_status_code'] = response
        return jsonify(email_json), 200

    raise InvalidRequest('{} did not update email'.format(email_id), 400)