Example #1
0
def populate_db(config):
    """
    Sync the Warehouse database with initial sponsors list.
    Once this command is executed once, you shouldn't need to run
    it again.
    """
    # Imported here because we don't want to trigger an import from anything
    # but warehouse.cli at the module scope.
    from warehouse.db import Session

    session = Session(bind=config.registry["sqlalchemy.engine"])

    for data in SPONSORS_DICTS:
        name = data["name"]
        sponsor = session.query(Sponsor).filter_by(name=name).one_or_none()
        if sponsor:
            print(f"Skipping {name} sponsor because it already exists.")
            continue

        params = data.copy()
        img = params.pop("image")
        params["is_active"] = True
        params["link_url"] = params.pop("url")
        params["activity_markdown"] = "\n\n".join(params.pop("activity",
                                                             [])).strip()
        params["color_logo_url"] = BLACK_BASE_URL + img
        if params["footer"] or params["infra_sponsor"]:
            params["white_logo_url"] = WHITE_BASE_URL + img

        sponsor = Sponsor(**params)
        try:
            session.add(sponsor)
            session.commit()
            print(f"{name} sponsor created with success.")
        except Exception as e:
            session.rollback()
            print(f"Error while creating {name} sponsor:")
            print(f"\t{e}")