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 63 insertions and 114 deletions:
0 comments (0 inline, 0 general)
ennstatus/__init__.py
Show inline comments
 
@@ -63,15 +63,12 @@ def create_app():
 
    app.wsgi_app = ProxyFix(app.wsgi_app)
 

	
 
    bootstrap.init_app(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)
 

	
 
    from .api.views import api_page
 
    app.register_blueprint(api_page, url_prefix='/api')
 

	
ennstatus/root/functions.py
Show inline comments
 
@@ -13,116 +13,75 @@
 
#
 
# 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
 
import rt
 

	
 

	
 
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
 
                               )
 
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']
 
                    )
 

	
 
        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:
 
    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
 
    )
 

	
 
    tracker.login()
 
    try:
 
        tracker.create_ticket(
 
            Queue=current_app.config['ENNSTATUS_MEMBERSHIP_RT_QUEUE'],
 
            Subject='New membership application: %s' % form.username.data,
 
            Text=body
 
        )
 
    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:
 
    finally:
 
        tracker.logout()
 

	
 

	
 
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']
 
                    )
 

	
 
    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
 
    )
 
    tracker.login()
 
    try:
 
        tracker.create_ticket(
 
            Queue=current_app.config['ENNSTATUS_BRIDGEPROGRAM_RT_QUEUE'],
 
            Subject='New bridgeprogram application: %s' % form.email.data,
 
            Text=body
 
        )
 
    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)
 

	
 

	
 
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)
 
    finally:
 
        tracker.logout()
ennstatus/root/views.py
Show inline comments
 
@@ -16,14 +16,14 @@
 

	
 
from flask import (Blueprint, render_template, current_app,
 
                   request, redirect, url_for, flash)
 

	
 
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__)
 

	
 

	
 
@root_page.route('/')
 
def index():
 
@@ -46,13 +46,13 @@ def bridgeprogram():
 
    current_app.logger.info('Handling bridgeprogram')
 
    form = BridgeprogramForm()
 

	
 
    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)
 

	
 

	
 
@root_page.route('/member')
 
@@ -66,13 +66,13 @@ def membership():
 
    current_app.logger.info('Handling membership')
 
    form = MembershipForm()
 

	
 
    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)
 

	
 

	
 
@root_page.route('/mirrors')
ennstatus/status/functions.py
Show inline comments
 
@@ -19,18 +19,14 @@ import os.path
 
import json
 

	
 
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):
 

	
 
    server = Server.from_file_by_name(name)
 

	
 
    if server:
requirements.in
Show inline comments
 
Babel
 
click
 
Flask-Bootstrap
 
Flask-HTTPAuth
 
Flask-Mail
 
Flask-Moment
 
Flask-WTF
 
Flask
 
jsonschema
 
pygeoip
 
python-gnupg
requirements.txt
Show inline comments
 
@@ -2,21 +2,19 @@
 
# This file is autogenerated by pip-compile
 
# Make changes in requirements.in, then run this to update:
 
#
 
#    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
 
MarkupSafe==0.23          # via jinja2
 
onionpy==0.3.2
 
pygeoip==0.3.2
0 comments (0 inline, 0 general)