Changeset - 414cbe0f5a4b
[Not reviewed]
Dennis Fink - 9 years ago 2016-03-03 23:14:20
dennis.fink@c3l.lu
Create ticket instead of sending a mail
6 files changed with 41 insertions and 92 deletions:
0 comments (0 inline, 0 general)
ennstatus/__init__.py
Show inline comments
 
@@ -66,9 +66,6 @@ def create_app():
 
    csrf.init_app(app)
 
    moment.init_app(app)
 

	
 
    from .status.functions import mail
 
    mail.init_app(app)
 

	
 
    from .root.views import root_page
 
    app.register_blueprint(root_page)
 

	
ennstatus/root/functions.py
Show inline comments
 
@@ -16,14 +16,18 @@
 

	
 
from flask import render_template, current_app, flash
 

	
 
from flask_mail import Mail, Message
 

	
 
from ennstatus.status.functions import mail
 
import rt
 

	
 

	
 
def send_membership_mail(form):
 
    try:
 
        body = render_template('root/membership_mail.txt',
 
def create_membership_ticket(form):
 
    url = current_app.config['ENNSTATUS_RT_URL'] + '/REST/1.0'
 
    tracker = rt.Rt(url,
 
                    current_app.config['ENNSTATUS_RT_USER'],
 
                    current_app.config['ENNSTATUS_RT_PASSWORD']
 
                    )
 

	
 
    body = render_template(
 
        'root/membership_mail.txt',
 
                               username=form.username.data,
 
                               email=form.email.data,
 
                               membership=form.membership.data,
 
@@ -36,93 +40,48 @@ def send_membership_mail(form):
 
                               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'
 
    tracker.login()
 
    try:
 
        tracker.create_ticket(
 
            Queue=current_app.config['ENNSTATUS_MEMBERSHIP_RT_QUEUE'],
 
            Subject='New membership application: %s' % form.username.data,
 
            Text=body
 
                )
 
                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:
 
    except:
 
        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)
 
    finally:
 
        tracker.logout()
 

	
 

	
 
def send_bridgeprogram_mail(form):
 
def create_bridgeprogram_ticket(form):
 
    url = current_app.config['ENNSTATUS_RT_URL'] + '/REST/1.0'
 
    tracker = rt.Rt(url,
 
                    current_app.config['ENNSTATUS_RT_USER'],
 
                    current_app.config['ENNSTATUS_RT_PASSWORD']
 
                    )
 

	
 
    try:
 
        body = render_template('root/bridgeprogram_mail.txt',
 
    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
 
                        )
 
    tracker.login()
 
    try:
 
        tracker.create_ticket(
 
            Queue=current_app.config['ENNSTATUS_BRIDGEPROGRAM_RT_QUEUE'],
 
            Subject='New bridgeprogram application: %s' % form.email.data,
 
            Text=body
 
                    )
 
                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:
 
    except:
 
        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)
 
    finally:
 
        tracker.logout()
ennstatus/root/views.py
Show inline comments
 
@@ -19,8 +19,8 @@ from flask import (Blueprint, render_tem
 

	
 
from ennstatus.root.forms import BPMForm, MembershipForm, BridgeprogramForm
 
from ennstatus.root.constants import BPM_ADDRESSES
 
from ennstatus.root.functions import (send_membership_mail,
 
                                      send_bridgeprogram_mail)
 
from ennstatus.root.functions import (create_membership_ticket,
 
                                      create_bridgeprogram_ticket)
 

	
 
root_page = Blueprint('root', __name__)
 

	
 
@@ -49,7 +49,7 @@ def bridgeprogram():
 
    if request.method == 'POST':
 
        current_app.logger.debug('Validation form')
 
        if form.validate_on_submit():
 
            send_bridgeprogram_mail(form)
 
            create_bridgeprogram_ticket(form)
 
            return redirect(url_for('root.bridgeprogram'))
 

	
 
    return render_template('root/bridgeprogram.html', form=form)
 
@@ -69,7 +69,7 @@ def membership():
 
    if request.method == 'POST':
 
        current_app.logger.debug('Validating form')
 
        if form.validate_on_submit():
 
            send_membership_mail(form)
 
            create_membership_ticket(form)
 
            return redirect(url_for('root.member'))
 

	
 
    return render_template('root/membership.html', form=form)
ennstatus/status/functions.py
Show inline comments
 
@@ -22,12 +22,8 @@ from collections import defaultdict
 
from datetime import datetime
 

	
 
from flask import current_app
 
from flask_mail import Mail, Message
 

	
 
from ..api.model import Server
 

	
 
mail = Mail()
 

	
 

	
 
def single_server(name):
 

	
requirements.in
Show inline comments
 
@@ -2,7 +2,6 @@ Babel
 
click
 
Flask-Bootstrap
 
Flask-HTTPAuth
 
Flask-Mail
 
Flask-Moment
 
Flask-WTF
 
Flask
requirements.txt
Show inline comments
 
@@ -5,15 +5,13 @@
 
#    pip-compile requirements.in
 
#
 
Babel==2.2.0
 
blinker==1.4              # via flask-mail
 
click==6.3
 
dominate==2.1.17          # via flask-bootstrap
 
Flask-Bootstrap==3.3.5.7
 
Flask-HTTPAuth==2.7.2
 
Flask-Mail==0.9.1
 
Flask-Moment==0.5.1
 
Flask-WTF==0.12
 
Flask==0.10.1             # via flask-bootstrap, flask-httpauth, flask-mail, flask-moment, flask-wtf
 
Flask==0.10.1             # via flask-bootstrap, flask-httpauth, flask-moment, flask-wtf
 
itsdangerous==0.24        # via flask
 
Jinja2==2.8               # via flask
 
jsonschema==2.5.1
0 comments (0 inline, 0 general)