Esempio n. 1
0
        def pid(local_id, package_name=None):
            page_id = local_id

            if package_name:
                package = site.get_package(package_name)
            else:
                package = self

            assert package.base_page_index + 1 <= 127
            page_id |= (1 + package.base_page_index) << 24

            local_site_id = site_id & 0xffffffff
            if not local:
                # special case for shared page site.
                local_site_id = 0
            assert local_site_id <= 0xff
            page_id |= local_site_id << 16

            return utils.encode_uid(WEB_PAGES_TABLE_ID, page_id)
Esempio n. 2
0
def migrate_0_to_1(project, db):
    """
    Version 1:
    * Rename admin ids.
    * Populate t086_pt_services_configurations from t025_sites.
    """

    # Update admin page ids
    # TODO: delete old pages if there is both the new and old in the database.
    db.query(
        'UPDATE t063_web_pages SET id=17732923532771328 WHERE id=177329235327713281;'
    )
    db.query(
        'UPDATE t063_web_pages SET id=17732923532771329 WHERE id=177329235327713282;'
    )
    db.query(
        'UPDATE t063_web_pages SET id=17732923532771330 WHERE id=177329235327713283;'
    )
    db.query(
        'UPDATE t063_web_pages SET up_id=17732923532771328 WHERE up_id=177329235327713281;'
    )

    # Populate the new t086_pt_services_configurations table which was split
    # from t025_sites
    try:
        db.query('SELECT * from t086_pt_services_configurations')
    except:
        log.info('Creating t086_pt_services_configurations table')
        # Create the table
        if db.name == 'sqlite':
            db.query("""
                CREATE TABLE t086_pt_services_configurations
                    ("id" INTEGER UNIQUE PRIMARY KEY ON CONFLICT ROLLBACK,
                    "name" TEXT, "online_booking" BOOLEAN,
                    "use_old_data" BOOLEAN, "max_connections" INTEGER,
                    "use_dates_range" INTEGER,
                    "periods" TEXT,
                    "display_road_approach_detail" BOOLEAN)""")
        else:
            db.query("""
                CREATE TABLE `t086_pt_services_configurations` (
                 `id` bigint(20) NOT NULL,
                 `name` text,
                 `online_booking` tinyint(1) DEFAULT NULL,
                 `use_old_data` tinyint(1) DEFAULT NULL,
                 `max_connections` bigint(20) DEFAULT NULL,
                 `use_dates_range` bigint(20) DEFAULT NULL,
                 `periods` text,
                 `display_road_approach_detail` tinyint(1) DEFAULT NULL,
                 PRIMARY KEY (`id`)
                )""")

    COLUMNS_TO_MOVE = (
        "id name online_booking use_old_data max_connections "
        "use_dates_range periods display_road_approach_detail").split()

    for site in db.query('select * from t025_sites'):
        id = site['id']
        uid_info = utils.decode_uid(id)
        config_id = utils.encode_uid(86, uid_info.object_id,
                                     uid_info.grid_node_id)

        count = db.query(
            'select count(1) as c from '
            't086_pt_services_configurations where id = ?', [config_id],
            one=True)['c']
        if count > 0:
            continue

        cols = ','.join(COLUMNS_TO_MOVE)

        def to_sql(val):
            if val is None:
                return ''
            if isinstance(val, basestring):
                return str(val.encode('utf-8'))
            return str(val)

        try:
            vals = ["'{0}'".format(to_sql(site[c])) for c in COLUMNS_TO_MOVE]
        except KeyError, e:
            log.warn('Table t025_sites already migrated? (%s)', e)
            continue
        # Sets the id
        vals[0] = "'{0}'".format(config_id)

        db.query('insert into t086_pt_services_configurations '
                 '({0}) values ({1})'.format(','.join(COLUMNS_TO_MOVE),
                                             ','.join(vals)))
Esempio n. 3
0
    def _load_pages(self, site, local, overwrite):
        pages_dir = join(self.path, 'pages_local' if local else 'pages')
        if not os.path.isdir(pages_dir):
            return

        WEB_PAGES_TABLE_ID = 63
        SITES_TABLE_ID = 25
        # Maybe 127 would have been better for this.
        SHARED_PAGES_SITE_LOCAL_ID = 100
        SHARED_PAGES_SITE_ID = utils.encode_uid(
            SITES_TABLE_ID, SHARED_PAGES_SITE_LOCAL_ID)

        # id structure:
        # The lower 32bits of the 128bits page id are available for page numbers.
        # 0xNN00 0000, bits 30-24: package id
        # 0x00NN 0000, bits 23-16: site id
        # 0x0000 NNNN, bits 0-15: pages
        #
        # Pages above 0x1000 0000 are reserved for new pages,
        # Pages with package id == 0 are reserved for existing pages.
        # Available packages: 126
        # Available sites: 255
        # Available pages per package+site: 65536

        assert self.base_page_index >= 0, \
            'Package %s is missing a base_page_index value' % self

        site_id = site.id if (site and local) else SHARED_PAGES_SITE_ID

        def pid(local_id, package_name=None):
            page_id = local_id

            if package_name:
                package = site.get_package(package_name)
            else:
                package = self

            assert package.base_page_index + 1 <= 127
            page_id |= (1 + package.base_page_index) << 24

            local_site_id = site_id & 0xffffffff
            if not local:
                # special case for shared page site.
                local_site_id = 0
            assert local_site_id <= 0xff
            page_id |= local_site_id << 16

            return utils.encode_uid(WEB_PAGES_TABLE_ID, page_id)

        pages_config = {}
        execfile(join(pages_dir, 'pages.py'), {
            'pid': pid,
        }, pages_config)

        # TODO: implement smart_url lookup on some attributes (up_id,...) or in page content.

        log.debug('pages_config:\n%s', pprint.pformat(pages_config))
        for page in pages_config['pages']:
            if not 'site_id' in page:
                page['site_id'] = site_id
            if not 'do_not_use_template' in page:
                page['do_not_use_template'] = True
            if page['content1'].startswith('file:'):
                file_path = page['content1'][len('file:'):]
                page['content1'] = unicode(
                    open(join(pages_dir, file_path), 'rb').read(), 'utf-8')
            if ('title' not in page and
                page.get('smart_url_path', '').startswith(':')):
                page['title'] = page['smart_url_path'][1:]

            if not overwrite and self.project.db_backend.query(
                    'select 1 from t063_web_pages where id = ?', [page['id']]):
                log.debug('Page %r already exists. Not overwriting.', page['id'])
                continue

            self.project.db_backend.replace_into('t063_web_pages', page)