# Path: gengine/app/cache.py
# def init_caches():
#     global cache_general
#     cache_general = create_cache("general")
# 
#     global cache_achievement_eval
#     cache_achievement_eval = create_cache("achievement_eval")
# 
#     global cache_achievements_by_subject_for_today
#     cache_achievements_by_subject_for_today = create_cache("achievements_by_subject_for_today")
# 
#     global cache_achievements_subjects_levels
#     cache_achievements_subjects_levels = create_cache("achievements_subjects_levels")
# 
#     global cache_translations
#     cache_translations = create_cache("translations")
# 
#     # The Goal evaluation Cache is implemented as a two-level cache (persistent in db, non-persistent as dogpile)
#     global cache_goal_evaluation
#     cache_goal_evaluation = create_cache("goal_evaluation")
# 
#     #global cache_goal_statements
#     #cache_goal_statements = create_memory_cache("goal_statements")
# 
# Path: gengine/app/permissions.py
# def yield_all_perms():
# 
# Path: gengine/base/model.py
# def exists_by_expr(t, expr):
#     # TODO: use exists instead of count
#     q = select([func.count("*").label("c")], from_obj=t).where(expr)
#     r = meta.DBSession.execute(q).fetchone()
#     if r.c > 0:
#         return True
#     else:
#         return False


import sys
import os
import pyramid_dogpile_cache
import transaction
from pyramid.config import Configurator
from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )
from pyramid.scripts.common import parse_vars
from sqlalchemy import engine_from_config
from sqlalchemy.sql.expression import and_, select
from sqlalchemy.sql.schema import Table
from gengine.app.cache import init_caches
from gengine.app.permissions import perm_global_delete_subject, perm_global_increase_value, perm_global_manage_subjects, \
    perm_global_access_admin_ui, perm_global_read_messages, perm_global_register_device, yield_all_perms
from gengine.base.model import exists_by_expr
    from gengine.metadata import (
        init_session,
        init_declarative_base,
        init_db
    )
    from gengine.metadata import (
        Base,
        DBSession
    )
    from alembic.config import Config
    from alembic import command
    from alembic.runtime.migration import MigrationContext
        from gengine.app import model
    from gengine.app.model import AuthRole
    from gengine.app.model import (
        AuthUser,
        Subject,
        AuthRole,
        AuthRolePermission,
        SubjectType,
        t_auth_roles_permissions
    )
    from gengine.app.model import (
        Achievement,
        AchievementCategory,
        Variable,
        Subject,
        Language,
        TranslationVariable,
        Translation,
        Reward,
        AchievementProperty,
        AchievementAchievementProperty,
        AchievementReward,
        AuthUser,
        AuthRole,
        AuthRolePermission,
        SubjectType,
        t_auth_roles_permissions
    )
        user1.part_of_subjects.append(subject_manager)

        user2.part_of_subjects.append(subject_bielefeld)
        user2.part_of_subjects.append(subject_sales)
        user2.part_of_subjects.append(subject_promoter)

        user3.part_of_subjects.append(subject_paderborn)
        user3.part_of_subjects.append(subject_sales)
        user3.part_of_subjects.append(subject_promoter)

        DBSession.add(user2)
        DBSession.add(user3)
        DBSession.flush()

        try:
            auth_user = DBSession.query(AuthUser).filter_by(email="admin@gamification-software.com").first()

            if not auth_user:
                auth_user = AuthUser(subject=user1, email="admin@gamification-software.com", password="test123", active=True)
                DBSession.add(auth_user)

            auth_role = DBSession.query(AuthRole).filter_by(name="Global Admin").first()

            if not auth_role:
                auth_role = AuthRole(name="Global Admin")
                DBSession.add(auth_role)

            DBSession.flush()

            for perm in yield_all_perms():

# Path: gengine/app/cache.py
# def init_caches():
#     global cache_general
#     cache_general = create_cache("general")
# 
#     global cache_achievement_eval
#     cache_achievement_eval = create_cache("achievement_eval")
# 
#     global cache_achievements_by_subject_for_today
#     cache_achievements_by_subject_for_today = create_cache("achievements_by_subject_for_today")
# 
#     global cache_achievements_subjects_levels
#     cache_achievements_subjects_levels = create_cache("achievements_subjects_levels")
# 
#     global cache_translations
#     cache_translations = create_cache("translations")
# 
#     # The Goal evaluation Cache is implemented as a two-level cache (persistent in db, non-persistent as dogpile)
#     global cache_goal_evaluation
#     cache_goal_evaluation = create_cache("goal_evaluation")
# 
#     #global cache_goal_statements
#     #cache_goal_statements = create_memory_cache("goal_statements")
# 
# Path: gengine/app/permissions.py
# def yield_all_perms():
# 
# Path: gengine/base/model.py
# def exists_by_expr(t, expr):
#     # TODO: use exists instead of count
#     q = select([func.count("*").label("c")], from_obj=t).where(expr)
#     r = meta.DBSession.execute(q).fetchone()
#     if r.c > 0:
#         return True
#     else:
#         return False


# Path: gengine/maintenance/scripts/initializedb.py
import sys
import os
import pyramid_dogpile_cache
import transaction
from pyramid.config import Configurator
from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )
from pyramid.scripts.common import parse_vars
from sqlalchemy import engine_from_config
from sqlalchemy.sql.expression import and_, select
from sqlalchemy.sql.schema import Table
from gengine.app.cache import init_caches
from gengine.app.permissions import perm_global_delete_subject, perm_global_increase_value, perm_global_manage_subjects, \
    perm_global_access_admin_ui, perm_global_read_messages, perm_global_register_device, yield_all_perms
from gengine.base.model import exists_by_expr
    from gengine.metadata import (
        init_session,
        init_declarative_base,
        init_db
    )
    from gengine.metadata import (
        Base,
        DBSession
    )
    from alembic.config import Config
    from alembic import command
    from alembic.runtime.migration import MigrationContext
        from gengine.app import model
    from gengine.app.model import AuthRole
    from gengine.app.model import (
        AuthUser,
        Subject,
        AuthRole,
        AuthRolePermission,
        SubjectType,
        t_auth_roles_permissions
    )
    from gengine.app.model import (
        Achievement,
        AchievementCategory,
        Variable,
        Subject,
        Language,
        TranslationVariable,
        Translation,
        Reward,
        AchievementProperty,
        AchievementAchievementProperty,
        AchievementReward,
        AuthUser,
        AuthRole,
        AuthRolePermission,
        SubjectType,
        t_auth_roles_permissions
    )

        user1.part_of_subjects.append(subject_manager)

        user2.part_of_subjects.append(subject_bielefeld)
        user2.part_of_subjects.append(subject_sales)
        user2.part_of_subjects.append(subject_promoter)

        user3.part_of_subjects.append(subject_paderborn)
        user3.part_of_subjects.append(subject_sales)
        user3.part_of_subjects.append(subject_promoter)

        DBSession.add(user2)
        DBSession.add(user3)
        DBSession.flush()

        try:
            auth_user = DBSession.query(AuthUser).filter_by(email="admin@gamification-software.com").first()

            if not auth_user:
                auth_user = AuthUser(subject=user1, email="admin@gamification-software.com", password="test123", active=True)
                DBSession.add(auth_user)

            auth_role = DBSession.query(AuthRole).filter_by(name="Global Admin").first()

            if not auth_role:
                auth_role = AuthRole(name="Global Admin")
                DBSession.add(auth_role)

            DBSession.flush()

            for perm in yield_all_perms():

                if not exists_by_expr(t_auth_roles_permissions, and_(