Files @ 6eea4a4d3b10
Branch filter:

Location: FVDE/ennstatus/ennstatus/root/functions.py

Dennis Fink
Added first version of the README
from flask import render_template, current_app, flash

from flask_mail import Mail, Message

from ennstatus.status.functions import mail


def send_membership_mail(form):
    try:
        body = render_template('root/membership_mail.txt',
                               username=form.username.data,
                               email=form.email.data,
                               membership=form.membership.data,
                               c3l=form.c3l.data,
                               fullname=form.fullname.data,
                               street=form.street.data,
                               zip=form.zip.data,
                               city=form.city.data,
                               country=form.country.data,
                               gpg=form.gpg.data
                               )

        recipients = current_app.config['ENNSTATUS_MEMBERSHIP_MAIL']
        with mail.connect() as conn:
            for recipient in recipients:
                msg = Message(
                    'New membership application',
                    sender='ennstatus@enn.lu'
                )
                msg.add_recipient(recipient)
                current_app.logger.debug('Before encryption')
                current_app.logger.debug(body)
                if current_app.extensions['gnupg']:
                    encrypted_body = str(
                        current_app.extensions['gnupg'].encrypt(
                            body,
                            recipient,
                            always_trust=True
                        )
                    )
                else:
                    encrypted_body = None
                current_app.logger.debug('After encryption')
                current_app.logger.debug(body)
                msg.body = encrypted_body if encrypted_body else body
                conn.send(msg)
        flash('Application successfully submitted!', 'success')
    except KeyError:
        flash(
            'Internal server error! Please get in touch with us at info@enn.lu!',
            'error'
        )
        current_app.logger.error('Membership admin not found!')
    except AssertionError:
        pass
    except Exception as e:
        flash(
            'Internal server error! Please get in touch with us at info@enn.lu!',
            'error'
        )
        current_app.logger.error('Unexpected error: %s' % e,
                                 exc_info=True)


def send_bridgeprogram_mail(form):

    try:
        body = render_template('root/bridgeprogram_mail.txt',
                               fullname=form.fullname.data,
                               email=form.email.data,
                               bridgename=form.bridgename.data,
                               duration=form.duration.data,
                               payment=form.payment.data
                               )

        recipients = current_app.config['ENNSTATUS_BRIDGEPROGRAM_MAIL']
        with mail.connect() as conn:
            for recipient in recipients:
                msg = Message('New bridge program application',
                              sender='ennstatus@enn.lu')
                msg.add_recipient(recipient)
                if current_app.extensions['gnupg']:
                    encrypted_body = str(
                        current_app.extensions['gnupg'].encrypt(
                            body,
                            recipient,
                            always_trust=True
                        )
                    )
                else:
                    encrypted_body = None
                msg.body = encrypted_body if encrypted_body else body
                conn.send(msg)
        flash(
            'Application successfully submitted! We will send you an email with further information!',
            'success'
        )
    except KeyError:
        flash(
            'Internal server error! Please get in touch with us at info@enn.lu',
            'error'
        )
        current_app.logger.error('Bridgeprogram admin not found!')
    except AssertionError:
        pass
    except Exception as e:
        flash(
            'Internal server error! Please get in touch with us at info@enn.lu!',
            'error'
        )
        current_app.logger.error('Unexpected error: %s' % e,
                                 exc_info=True)