Files @ e9e85b209c6f
Branch filter:

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

Dennis Fink
Use rt instead of rtkit
# Ënnstatus
# Copyright (C) 2015  Dennis Fink
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

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)