示例#1
0
    def dispatch(self, request):
        if request.path not in ('/', self.login_url):
            return redirect('')

        if request.path == self.login_url:
            if request.is_somebody:
                return redirect('')
            elif request.authorization:
                if 'username' in request.authorization:
                    username = request.authorization.get('username')
                    password = request.authorization.get('password')
                    user = self.app.database_engine.execute(
                        users.select(users.c.username==username)
                    ).fetchone()
                    if user and check_pwhash(user.pw_hash, password):
                        request.session['uid'] = user.user_id
                        request.session['lt'] = time()
                        request.is_somebody = True
                        return redirect('')

            response = Response()
            response.www_authenticate.set_basic()
            response.status_code = 401
            return response

        if not request.is_admin:
            response = render_response(request, 'upgrade_maintenance.html',
                                       login_url=self.login_url)
            response.status_code = 503
            return response

        if request.method == 'POST':
            open(self.lockfile, 'w').write('locked on database upgrade\n')
            mdb = ManageDatabase(request.app)
            db.session.close()  # close open sessions
            def finish():
                # this is run from the template after the upgrade finishes
                remove(self.lockfile)
                db.session.close()  # close open sessions
                self.wants_reload = True    # force application reload
                return ''   # just because I need to return something to jinja

            return render_response(request, 'admin/perform_upgrade.html',
                                   live_log=mdb.cmd_upgrade(), _stream=True,
                                   finish=finish, blog_url=self.index_url,
                                   maintenance_url=self.maintenance_url,
                                   in_progress=False)

        return render_response(request, 'admin/perform_upgrade.html',
                               in_progress=isfile(self.lockfile),
                               repo_ids=self.repo_ids)
示例#2
0
def create_temporary_instance():
    """Create a sqlite based test instance in a temporary directory"""
    dbname = 'sqlite://database.db'
    instance_folder = mkdtemp(prefix='pyclanspheretest')

    # create database and all tables
    from pyClanSphere.database import db, init_database
    e = db.create_engine(dbname, instance_folder)
    from pyClanSphere.schema import users, user_privileges, privileges
    init_database(e)

    # create admin account
    from pyClanSphere.privileges import CLAN_ADMIN
    user_id = e.execute(users.insert(),
        username=u'TestAdmin',
        pw_hash=gen_pwhash('TestPassWord'),
        email=u'*****@*****.**',
        real_name=u'',
        description=u'',
        extra={},
        display_name='$username'
    ).inserted_primary_key[0]

    # insert a privilege for the user
    privilege_id = e.execute(privileges.insert(),
        name=CLAN_ADMIN.name
    ).inserted_primary_key[0]
    e.execute(user_privileges.insert(),
        user_id=user_id,
        privilege_id=privilege_id
    )

    # set up the initial config
    from pyClanSphere.config import Configuration
    config_filename = join(instance_folder, 'pyClanSphere.ini')
    cfg = Configuration(config_filename)
    t = cfg.edit()
    t.update(
        maintenance_mode=False,
        site_url='http://localtest',
        secret_key=gen_secret_key(),
        database_uri=dbname,
        iid=new_iid()
    )
    t.commit()
    
    from pyClanSphere import setup
    from pyClanSphere.upgrades.webapp import WebUpgrades
    instance = setup(instance_folder)
    
    if str(type(instance)) == "<class 'pyClanSphere.upgrades.webapp.WebUpgrades'>":
        # Fast Migration
        from pyClanSphere.upgrades import ManageDatabase
        manage = ManageDatabase(instance)
        upgrade = manage.cmd_upgrade()
        while True:
            try:
                upgrade.next()
            except StopIteration:
                break
        from pyClanSphere._core import _unload_pyClanSphere
        _unload_pyClanSphere()

        instance = setup(instance_folder)
        if str(type(instance)) == "<class 'pyClanSphere.upgrades.webapp.WebUpgrades'>":
            sys.stderr.write('Automatic db migration failed, check your scripts!\n')
            sys.exit(1)
    return instance, instance_folder