# -*- coding: utf-8 -*-

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from flask.ext.babel import lazy_gettext
from flask.globals import current_app
from werkzeug.local import LocalProxy

from .ldap_utils import get_current_uid
from .netusers import Traffic


import logging
logger = logging.getLogger(__name__)


def init_db(app):
    # TODO: refactor these things
    # e.g.: db_atlantis is not needed directly anymore
    app.extensions['db_atlantis'] = create_engine(
        'mysql+pymysql://{0}:{1}@{2}:3306/netusers'.format(
            app.config['DB_ATLANTIS_USER'],
            app.config['DB_ATLANTIS_PASSWORD'],
            app.config['DB_ATLANTIS_HOST']),
        echo=False, connect_args={'connect_timeout': app.config['SQL_TIMEOUT']}
    )
    app.extensions['db_helios'] = create_engine(
        'mysql+pymysql://{0}:{1}@{2}:{3}/'.format(
            app.config['DB_HELIOS_USER'],
            app.config['DB_HELIOS_PASSWORD'],
            app.config['DB_HELIOS_HOST'],
            int(app.config['DB_HELIOS_PORT'])),
        echo=False, connect_args={'connect_timeout': app.config['SQL_TIMEOUT']}
    )
    Session = sessionmaker(bind=db_atlantis, binds={Traffic: create_engine(
        'mysql+pymysql://{0}:{1}@{2}:3306/traffic'.format(
            app.config['DB_ATLANTIS_USER'],
            app.config['DB_ATLANTIS_PASSWORD'],
            app.config['DB_ATLANTIS_HOST']),
        echo=False, connect_args={'connect_timeout': app.config['SQL_TIMEOUT']}
    )})
    app.extensions['wu_session_atlantis'] = Session()


db_atlantis = LocalProxy(lambda: current_app.extensions['db_atlantis'])
db_helios = LocalProxy(lambda: current_app.extensions['db_helios'])
session_atlantis = LocalProxy(
    lambda: current_app.extensions['wu_session_atlantis']
)

DORMITORIES = [
    'Wundstraße 5',
    'Wundstraße 7',
    'Wundstraße 9',
    'Wundstraße 11',
    'Wundstraße 1',
    'Wundstraße 3',
    'Zellescher Weg 41',
    'Zellescher Weg 41A',
    'Zellescher Weg 41B',
    'Zellescher Weg 41C',
    'Zellescher Weg 41D',
    'Borsbergstraße 34',
]

STATUS = {
    1: (lazy_gettext('ok'), 'success'),
    2: (lazy_gettext('Nicht bezahlt, Netzanschluss gesperrt'), 'warning'),
    7: (lazy_gettext('Verstoß gegen Netzordnung, Netzanschluss gesperrt'),
        'danger'),
    9: (lazy_gettext('Exaktiv'), 'muted'),
    12: (lazy_gettext('Trafficlimit überschritten, Netzanschluss gesperrt'),
         'danger')
}


def sql_query(query, args=(), database=db_helios):
    """Prepare and execute a raw sql query.
    'args' is a tuple needed for string replacement.
    """
    conn = database.connect()
    result = conn.execute(query, args)
    conn.close()
    return result

@@ -366,151 +366,36 @@
     
-# TODO: refactor these things%0A    # e.g.: db_atlantis is not needed directly anymore%0A    app.extensions%5B'db_atlantis'%5D = create_engine(%0A       
+atlantis_connection_string =
  'my
@@ -428,17 +428,8 @@
 3306
-/netusers
 '.fo
@@ -426,36 +426,32 @@
 %7D:3306'.format(%0A
-    
         app.conf
@@ -474,36 +474,32 @@
 USER'%5D,%0A        
-  
-  
 app.config%5B'DB_A
@@ -518,36 +518,32 @@
 WORD'%5D,%0A        
-    
 app.config%5B'DB_A
@@ -560,521 +560,594 @@
 ST'%5D
-),%0A        echo=False, connect_args=%7B'connect_timeout': app.config%5B'SQL_TIMEOUT'%5D%7D%0A    )%0A    app.extensions%5B'db_helios'%5D = create_engine(%0A        'mysql+pymysql://%7B0%7D:%7B1%7D@%7B2%7D:%7B3%7D/'.format(%0A            app.config%5B'DB_HELIOS_USER'%5D,%0A            app.config%5B'DB_HELIOS_PASSWORD'%5D,%0A            app.config%5B'DB_HELIOS_HOST'%5D,%0A            int(app.config%5B'DB_HELIOS_PORT'%5D)),%0A        echo=False, connect_args=%7B'connect_timeout': app.config%5B'SQL_TIMEOUT'%5D%7D%0A    )%0A    Session = sessionmaker(bind=db_atlantis, binds=%7BTraffic:
+%0A    ),%0A%0A    db_atlantis_netusers = create_engine(%0A        %22%7B%7D/netusers%22.format(atlantis_connection_string),%0A        echo=False, connect_args=%7B'connect_timeout': app.config%5B'SQL_TIMEOUT'%5D%7D%0A    )%0A    db_atlantis_traffic = create_engine(%0A        %22%7B%7D/traffic%22.format(atlantis_connection_string),%0A        echo=False, connect_args=%7B'connect_timeout': app.config%5B'SQL_TIMEOUT'%5D%7D%0A    )%0A    Session = sessionmaker(bind=db_atlantis_netusers,%0A                           binds=%7BTraffic: db_atlantis_traffic%7D)%0A    app.extensions%5B'wu_session_atlantis'%5D = Session()%0A%0A    app.extensions%5B'db_helios'%5D =
  cre
@@ -1199,20 +1199,12 @@
 %7B2%7D:
-3306/traffic
+%7B3%7D/
 '.fo
@@ -1228,39 +1228,37 @@
  app.config%5B'DB_
-ATLANTI
+HELIO
 S_USER'%5D,%0A      
@@ -1270,39 +1270,37 @@
  app.config%5B'DB_
-ATLANTI
+HELIO
 S_PASSWORD'%5D,%0A  
@@ -1324,31 +1324,29 @@
 fig%5B'DB_
-ATLANTI
+HELIO
 S_HOST'%5D
 ),%0A     
@@ -1337,16 +1337,63 @@
 S_HOST'%5D
+,%0A            int(app.config%5B'DB_HELIOS_PORT'%5D)
 ),%0A     
@@ -1476,138 +1476,10 @@
    )
-%7D)%0A    app.extensions%5B'wu_session_atlantis'%5D = Session()%0A%0A%0Adb_atlantis = LocalProxy(lambda: current_app.extensions%5B'db_atlantis'%5D)
+%0A%0A
 %0Adb_

Refactor `init_db` to only provide needed items