Exemple #1
0
    def __init__(self, name, include_private=True, include_living=True):
        """Initialize the database object for family tree `name`.

        This will raise if the database backend is not `sqlite`.
        The constructor does not open/lock the database yet.
        
        Parameters:

        - `include_private`: include records marked as private. Default True
        - `include_living`: include living people. Default True
        """
        self.name = name
        self.include_private = include_private
        self.include_living = include_living
        self.dbstate = DbState()
        self.dbman = CLIDbManager(self.dbstate)
        self.user = User()
        self.smgr = CLIManager(self.dbstate, True, self.user)
        self.path = self.dbman.get_family_tree_path(name)
        if not self.path:
            raise ValueError(
                "Family tree {} not found. Known trees: {}".format(
                    name, self.dbman.family_tree_list()))
        self.db_backend = self.get_dbid()
        if self.db_backend not in ALLOWED_DB_BACKENDS:
            raise ValueError(
                "Database backend '{}' of tree '{}' not supported.".format(
                    self.db_backend, name))
Exemple #2
0
 def write(self) -> None:
     """Write the example DB to a SQLite DB and change the DB path."""
     self.tmp_dbdir = tempfile.mkdtemp()
     self.old_path = getconfig("database.path")
     setconfig("database.path", self.tmp_dbdir)
     dbman = CLIDbManager(DbState())
     dbman.import_new_db(self.path, User())
Exemple #3
0
 def __init__(self, user=None, dbstate=None):
     ## Setup:
     from gramps.cli.clidbman import CLIDbManager
     self.dbstate = dbstate or DbState()
     #we need a manager for the CLI session
     self.user = user or User()
     self.climanager = CLIManager(self.dbstate, setloader=True, user=self.user)
     self.clidbmanager = CLIDbManager(self.dbstate)
Exemple #4
0
 def setUpClass(cls):
     cls.name = "Test Web API"
     cls.dbman = CLIDbManager(DbState())
     _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
     with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_CONFIG}):
         cls.app = create_app()
     cls.app.config["TESTING"] = True
     cls.client = cls.app.test_client()
Exemple #5
0
    def setUp(self):
        def dummy_callback(dummy):
            pass

        self.dbstate = DbState()
        self.dbman = CLIDbManager(self.dbstate)
        dirpath, name = self.dbman.create_new_db_cli("Test: bsddb", dbid="bsddb")
        self._db = make_database("bsddb")
        self._db.load(dirpath, None)
 def setUpClass(cls):
     # print("doing setup with", cls.param)
     set_det_id(True)  # handles must be deterministic for this test
     cls.dbstate = DbState()
     cls.dbman = CLIDbManager(cls.dbstate)
     dirpath, _name = cls.dbman.create_new_db_cli("Test: %s" % cls.param,
                                                  dbid=cls.param)
     cls.db = make_database(cls.param)
     cls.db.load(dirpath, None)
Exemple #7
0
 def _get_path(self) -> str:
     """Get the path of the family tree database."""
     dbstate = DbState()  # dbstate instance used only for this method
     dbman = CLIDbManager(dbstate)
     path = dbman.get_family_tree_path(self.name)
     if path is None:
         raise ValueError(
             "Database path for family tree '{}' not found in databse directory {}"
             .format(self.name, config.get("database.path")))
     return path
Exemple #8
0
    def setUp(self):
        def dummy_callback(dummy):
            pass

        self._tmpdir = tempfile.mkdtemp()

        self._db = DbBsddb()
        dbman = CLIDbManager(None)
        self._filename, title = dbman.create_new_db_cli(title=cuni("Test"))
        self._db.load(self._filename, dummy_callback, "w")
Exemple #9
0
 def setUp(self):
     from gramps.cli.clidbman import CLIDbManager
     from gramps.gen.config import set as setconfig, get as getconfig
     from gramps.gen.dbstate import DbState
     self.newpath = os.path.join(os.path.dirname(__file__),
                                 '\u0393\u03c1\u03b1\u03bc\u03c3\u03c0')
     self.newtitle = 'Gr\u00e4mps T\u00e9st'
     os.makedirs(self.newpath)
     self.old_path = getconfig('database.path')
     setconfig('database.path', self.newpath)
     self.cli = CLIDbManager(DbState())
Exemple #10
0
 def setUpClass(cls):
     cls.name = "Test Web API"
     cls.dbman = CLIDbManager(DbState())
     _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
     with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
         cls.app = create_app()
     cls.app.config["TESTING"] = True
     cls.client = cls.app.test_client()
     sqlauth = cls.app.config["AUTH_PROVIDER"]
     sqlauth.create_table()
     sqlauth.add_user(name="user", password="******")
 def setUp(self):
     self.name = "Test Web API"
     self.dbman = CLIDbManager(DbState())
     _, _name = self.dbman.create_new_db_cli(self.name, dbid="sqlite")
     with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
         self.app = create_app()
     self.app.config["TESTING"] = True
     self.client = self.app.test_client()
     sqlauth = self.app.config["AUTH_PROVIDER"]
     sqlauth.create_table()
     self.ctx = self.app.test_request_context()
     self.ctx.push()
Exemple #12
0
 def __init__(self, name: str = None) -> None:
     """Prepare and import the example DB."""
     ExampleDbBase.__init__(self)
     self.db_path = os.path.join(os.environ["GRAMPSHOME"], "gramps",
                                 "grampsdb")
     os.makedirs(self.db_path, exist_ok=True)
     setconfig("database.path", self.db_path)
     dbstate = DbState()
     dbman = CLIDbManager(dbstate)
     user = User()
     smgr = CLIManager(dbstate, True, user)
     smgr.do_reg_plugins(dbstate, uistate=None)
     self.path, self.name = dbman.import_new_db(self.path, User())
     WebDbManager.__init__(self, self.name)
 def setUpClass(cls):
     cls.name = "Test Web API"
     cls.dbman = CLIDbManager(DbState())
     cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
     with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
         cls.app = create_app()
     cls.app.config["TESTING"] = True
     cls.media_base_dir = tempfile.mkdtemp()
     cls.app.config["MEDIA_BASE_DIR"] = cls.media_base_dir
     cls.client = cls.app.test_client()
     sqlauth = cls.app.config["AUTH_PROVIDER"]
     sqlauth.create_table()
     sqlauth.add_user(name="user", password="******", role=ROLE_GUEST)
     sqlauth.add_user(name="admin", password="******", role=ROLE_OWNER)
 def setUpClass(cls):
     cls.name = "Test Web API"
     cls.dbman = CLIDbManager(DbState())
     cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
     with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
         cls.app = create_app()
     cls.app.config["TESTING"] = True
     cls.client = cls.app.test_client()
     sqlauth = cls.app.config["AUTH_PROVIDER"]
     sqlauth.create_table()
     sqlauth.add_user(name="user", password="******", role=ROLE_GUEST)
     sqlauth.add_user(name="contributor", password="******", role=ROLE_CONTRIBUTOR)
     sqlauth.add_user(name="admin", password="******", role=ROLE_OWNER)
     sqlauth.add_user(name="editor", password="******", role=ROLE_EDITOR)
     sqlauth.add_user(name="member", password="******", role=ROLE_MEMBER)
Exemple #15
0
    def setUpClass(cls):
        cls.name = "Test Web API"
        cls.dbman = CLIDbManager(DbState())
        _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
        cls.config_file = tempfile.NamedTemporaryFile(delete=False)
        cls.user_db = tempfile.NamedTemporaryFile(delete=False)
        config = """TREE="Test Web API"
SECRET_KEY="C2eAhXGrXVe-iljXTjnp4paeRT-m68pq"
USER_DB_URI="sqlite:///{}"
""".format(cls.user_db.name)
        with open(cls.config_file.name, "w") as f:
            f.write(config)
        with patch.dict("os.environ", {ENV_CONFIG_FILE: cls.config_file.name}):
            cls.app = create_app()
        cls.app.config["TESTING"] = True
        cls.client = cls.app.test_client()
        cls.runner = CliRunner()
Exemple #16
0
 def test_example_db_sqlite(self):
     """Test the SQLite example DB."""
     test_grampshome = tempfile.mkdtemp()
     os.environ["GRAMPSHOME"] = test_grampshome
     db = ExampleDbSQLite()
     self.assertEqual(getconfig("database.path"), db.db_path)
     dbman = CLIDbManager(DbState())
     db_info = dbman.current_names
     # there is only one DB here
     self.assertEqual(len(db_info), 1)
     # DB name
     self.assertEqual(db_info[0][0], "example")
     # DB path
     self.assertEqual(os.path.dirname(db_info[0][1]), db.db_path)
     # DB cleanup
     shutil.rmtree(test_grampshome)
     self.assertTrue(not os.path.exists(test_grampshome))
Exemple #17
0
 def test_example_db_sqlite(self):
     """Test the SQLite example DB."""
     db = ExampleDbSQLite()
     db.write()
     self.assertEqual(getconfig("database.path"), db.tmp_dbdir)
     dbman = CLIDbManager(DbState())
     db_info = dbman.current_names
     # there is only one DB here
     self.assertEqual(len(db_info), 1)
     # DB name
     self.assertEqual(db_info[0][0], "example")
     # DB path
     self.assertEqual(os.path.dirname(db_info[0][1]), db.tmp_dbdir)
     db.delete()
     # just check that this is not at the tmp dir anymore
     self.assertNotEqual(getconfig("database.path"), db.tmp_dbdir)
     # ... and that the tmp dir is gone
     self.assertTrue(not os.path.exists(db.tmp_dbdir))
Exemple #18
0
 def setUpClass(cls):
     cls.name = "Test Web API"
     cls.dbman = CLIDbManager(DbState())
     _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
     with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
         cls.app = create_app()
     cls.app.config["TESTING"] = True
     cls.client = cls.app.test_client()
     sqlauth = cls.app.config["AUTH_PROVIDER"]
     sqlauth.create_table()
     sqlauth.add_user(name="user", password="******", role=ROLE_GUEST)
     sqlauth.add_user(name="admin", password="******", role=ROLE_OWNER)
     dbstate = cls.app.config["DB_MANAGER"].get_db(force_unlock=True)
     with DbTxn("Add test objects", dbstate.db) as trans:
         _add_person(Person.MALE, "John", "Allen", trans, dbstate.db)
         _add_person(
             Person.FEMALE, "Jane", "Secret", trans, dbstate.db, private=True
         )
     dbstate.db.close()
 def setUp(self):
     self.name = "Test Web API"
     self.dbman = CLIDbManager(DbState())
     _, _name = self.dbman.create_new_db_cli(self.name, dbid="sqlite")
     with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
         self.app = create_app()
     self.app.config["TESTING"] = True
     self.client = self.app.test_client()
     sqlauth = self.app.config["AUTH_PROVIDER"]
     sqlauth.create_table()
     sqlauth.add_user(name="user",
                      password="******",
                      email="*****@*****.**",
                      role=ROLE_MEMBER)
     sqlauth.add_user(name="owner",
                      password="******",
                      email="*****@*****.**",
                      role=ROLE_OWNER)
     self.assertTrue(self.app.testing)
     self.ctx = self.app.test_request_context()
     self.ctx.push()
Exemple #20
0
 def setUp(self):
     self.name = "Test Web API"
     self.dbman = CLIDbManager(DbState())
     _, _name = self.dbman.create_new_db_cli(self.name, dbid="sqlite")
     with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}):
         self.app = create_app()
     self.app.config["TESTING"] = True
     self.client = self.app.test_client()
     sqlauth = self.app.config["AUTH_PROVIDER"]
     sqlauth.create_table()
     sqlauth.add_user(name="user",
                      password="******",
                      email="*****@*****.**",
                      role=ROLE_MEMBER)
     sqlauth.add_user(name="admin",
                      password="******",
                      email="*****@*****.**",
                      role=ROLE_OWNER)
     self.ctx = self.app.test_request_context()
     self.ctx.push()
     rv = self.client.post(BASE_URL + "/token/",
                           json={
                               "username": "******",
                               "password": "******"
                           })
     self.header_member = {
         "Authorization": f"Bearer {rv.json['access_token']}"
     }
     rv = self.client.post(BASE_URL + "/token/",
                           json={
                               "username": "******",
                               "password": "******"
                           })
     self.header_owner = {
         "Authorization": f"Bearer {rv.json['access_token']}"
     }
 def setUpClass(cls):
     """Test class setup."""
     cls.name = "empty"
     cls.dbman = CLIDbManager(DbState())
     _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
     cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
     with patch.dict(
         "os.environ",
         {
             ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG,
             "TREE": "empty",
         },
     ):
         cls.test_app = create_app()
     cls.test_app.config["TESTING"] = True
     cls.client = cls.test_app.test_client()
     sqlauth = cls.test_app.config["AUTH_PROVIDER"]
     sqlauth.create_table()
     for role in TEST_USERS:
         sqlauth.add_user(
             name=TEST_USERS[role]["name"],
             password=TEST_USERS[role]["password"],
             role=role,
         )
Exemple #22
0
    def get(self) -> Response:
        """Get active database and application related metadata information."""
        catalog = GRAMPS_LOCALE.get_language_dict()
        for entry in catalog:
            if catalog[entry] == GRAMPS_LOCALE.language[0]:
                language_name = entry
                break

        db_handle = self.db_handle
        db_name = db_handle.get_dbname()
        db_type = "Unknown"
        dbstate = DbState()
        db_summary = CLIDbManager(dbstate).family_tree_summary(
            database_names=[db_name])
        if len(db_summary) == 1:
            db_key = GRAMPS_LOCALE.translation.sgettext("Database")
            for key in db_summary[0]:
                if key == db_key:
                    db_type = db_summary[0][key]

        result = {
            "database": {
                "id": db_handle.get_dbid(),
                "name": db_name,
                "type": db_type,
            },
            "default_person": db_handle.get_default_handle(),
            "gramps": {
                "version": ENV["VERSION"],
            },
            "gramps_webapi": {
                "schema": VERSION,
                "version": VERSION,
            },
            "locale": {
                "lang":
                GRAMPS_LOCALE.lang,
                "language":
                GRAMPS_LOCALE.language[0],
                "description":
                language_name,
                "incomplete_translation":
                bool(GRAMPS_LOCALE.language[0] in INCOMPLETE_TRANSLATIONS),
            },
            "object_counts": {
                "people": db_handle.get_number_of_people(),
                "families": db_handle.get_number_of_families(),
                "sources": db_handle.get_number_of_sources(),
                "citations": db_handle.get_number_of_citations(),
                "events": db_handle.get_number_of_events(),
                "media": db_handle.get_number_of_media(),
                "places": db_handle.get_number_of_places(),
                "repositories": db_handle.get_number_of_repositories(),
                "notes": db_handle.get_number_of_notes(),
                "tags": db_handle.get_number_of_tags(),
            },
            "researcher": db_handle.get_researcher(),
            "surnames": db_handle.get_surname_list(),
        }
        data = db_handle.get_summary()
        db_version_key = GRAMPS_LOCALE.translation.sgettext("Database version")
        db_module_key = GRAMPS_LOCALE.translation.sgettext(
            "Database module version")
        db_schema_key = GRAMPS_LOCALE.translation.sgettext("Schema version")
        for item in data:
            if item == db_version_key:
                result["database"]["version"] = data[item]
            elif item == db_module_key:
                result["database"]["module"] = data[item]
            elif item == db_schema_key:
                result["database"]["schema"] = data[item]
        return self.response(200, result)
Exemple #23
0

def print_db_content(db):
    for h in db.get_person_handles():
        print("DB contains: person %s" % h)
    for h in db.get_source_handles():
        print("DB contains: source %s" % h)


tmpdir = tempfile.mkdtemp()
try:
    filename1 = os.path.join(tmpdir, 'test1.grdb')
    filename2 = os.path.join(tmpdir, 'test2.grdb')
    print("\nUsing Database file: %s" % filename1)
    dbstate = DbState()
    dbman = CLIDbManager(dbstate)
    dirpath, name = dbman.create_new_db_cli(filename1, dbid="bsddb")
    db = dbstate.make_database("bsddb")
    db.load(dirpath, None)
    print("Add person 1")
    add_person(db, "Anton", "Albers", True, False)
    print("Add source")
    add_source(db, "A short test", True, False)
    print("Add person 2 without commit")
    add_person(db, "Bernd", "Beta", False, False)
    print("Add source")
    add_source(db, "A short test", True, False)
    print("Add person 3")
    add_person(db, "Chris", "Connor", True, False)
    print_db_content(db)
    print("Closing Database file: %s" % filename1)
Exemple #24
0
 def setUpClass(cls):
     cls.name = "Test Web Db Manager"
     cls.dbman = CLIDbManager(DbState())
     dirpath, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
     cls.db = make_database("sqlite")