from argon2 import PasswordHasher
import morepath
from pony.orm import db_session
import webob
from webob.static import DirectoryApp, FileApp
from .app import App
from .model import db, User, Group


@db_session
def add_admin():   # pragma: no cover
    if not User.exists():
        if not Group.exists(name='Admin'):
            admin = Group(name='Admin')

        ph = PasswordHasher()
        password_hash = ph.hash('admin0')
        User(
            nickname='Admin',
            email='admin@example.com',
            password=password_hash,
            groups=[admin]
        )


def setup_db(app):
    provider = app.settings.database.provider
    args = app.settings.database.args
    kwargs = app.settings.database.kwargs
    try:
        db.bind(provider, *args, **kwargs)
    except TypeError:
        pass
    else:
        db.generate_mapping(create_tables=True)
    add_admin()


def wsgi_factory():   # pragma: no cover
    morepath.autoscan()
    App.commit()

    app = App()

    setup_db(app)

    @webob.dec.wsgify
    def morepath_with_static_absorb(request):
        popped = request.path_info_pop()
        if popped == 'api':
            return request.get_response(app)
        elif popped == 'static':
            static = DirectoryApp('static')
            return request.get_response(static)
        else:
            query = ''
            if request.query_string:
                query = '?' + request.query_string
            index_path = 'static/index.html' + query
            index = FileApp(index_path)
            return request.get_response(index)

    return morepath_with_static_absorb


application = wsgi_factory()   # pragma: no cover

@@ -970,24 +970,101 @@
 p.commit()%0A%0A
+    index = FileApp('static/index.html')%0A    static = DirectoryApp('static')%0A
     app = Ap
@@ -1155,16 +1155,151 @@
 quest):%0A
+        peek = request.path_info_peek()%0A        if peek != 'api' and peek != 'static':%0A            return request.get_response(index)%0A%0A
         
@@ -1441,52 +1441,8 @@
 c':%0A
-            static = DirectoryApp('static')%0A
     
@@ -1489,271 +1489,36 @@
 ic)%0A
-        else:%0A            query = ''%0A            if request.query_string:%0A                query = '?' + request.query_string%0A            index_path = 'static/index.html' + query%0A            index = FileApp(index_path)%0A            return request.get_response(index
+%0A        raise HTTPNotFound(
 )%0A%0A 

fix morepath_with_static_absorb