From 0f9a6d57bf5dbb137501a0f0f53fca57c8f5cfed Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Sat, 23 Sep 2017 19:52:23 +0100 Subject: [PATCH] Add connection preferences for PostgreSQL databases --- gramps/gen/config.py | 4 ++++ gramps/gui/configure.py | 30 +++++++++++++++++++++++++++ gramps/plugins/db/dbapi/postgresql.py | 23 +++++++++++++++----- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/gramps/gen/config.py b/gramps/gen/config.py index a2b8472b1..66024cf6c 100644 --- a/gramps/gen/config.py +++ b/gramps/gen/config.py @@ -163,6 +163,10 @@ register('database.backup-path', USER_HOME) register('database.backup-on-exit', True) register('database.autobackup', 0) register('database.path', os.path.join(HOME_DIR, 'grampsdb')) +register('database.user', '') +register('database.password', '') +register('database.host', '') +register('database.port', '') register('export.proxy-order', [["privacy", 0], diff --git a/gramps/gui/configure.py b/gramps/gui/configure.py index fad31505b..b301d5299 100644 --- a/gramps/gui/configure.py +++ b/gramps/gui/configure.py @@ -1443,6 +1443,17 @@ class GrampsPreferences(ConfigureDialog): the_iter = obj.get_active_iter() db_choice = the_list.get_value(the_iter, 2) config.set('database.backend', db_choice) + self.set_connection_widgets(db_choice) + + def set_connection_widgets(self, db_choice): + """ + Sets the connection widgets sensitive for PostgreSQL. + """ + for widget in self.connection_widgets: + if db_choice == 'postgresql': + widget.set_sensitive(True) + else: + widget.set_sensitive(False) def add_famtree_panel(self, configdialog): grid = Gtk.Grid() @@ -1459,6 +1470,25 @@ class GrampsPreferences(ConfigureDialog): grid.attach(obox, 2, current_line, 1, 1) current_line += 1 + self.connection_widgets = [] + entry = self.add_entry(grid, _('Username'), current_line, + 'database.user', col_attach=1) + self.connection_widgets.append(entry) + current_line += 1 + entry = self.add_entry(grid, _('Password'), current_line, + 'database.password', col_attach=1) + self.connection_widgets.append(entry) + current_line += 1 + entry = self.add_entry(grid, _('Host'), current_line, + 'database.host', col_attach=1) + self.connection_widgets.append(entry) + current_line += 1 + entry = self.add_entry(grid, _('Port'), current_line, + 'database.port', col_attach=1) + self.connection_widgets.append(entry) + current_line += 1 + self.set_connection_widgets(config.get('database.backend')) + self.dbpath_entry = Gtk.Entry() self.add_path_box(grid, _('Family Tree Database path'), diff --git a/gramps/plugins/db/dbapi/postgresql.py b/gramps/plugins/db/dbapi/postgresql.py index 10d8e2620..0de3f3c5e 100644 --- a/gramps/plugins/db/dbapi/postgresql.py +++ b/gramps/plugins/db/dbapi/postgresql.py @@ -39,6 +39,7 @@ import re #------------------------------------------------------------------------- from gramps.plugins.db.dbapi.dbapi import DBAPI from gramps.gen.utils.configmanager import ConfigManager +from gramps.gen.config import config from gramps.gen.db.dbconst import ARRAYSIZE from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext @@ -67,11 +68,23 @@ class PostgreSQL(DBAPI): def _initialize(self, directory): config_file = os.path.join(directory, 'settings.ini') config_mgr = ConfigManager(config_file) - config_mgr.register('database.dbname', 'gramps') - config_mgr.register('database.host', 'localhost') - config_mgr.register('database.user', 'user') - config_mgr.register('database.password', 'password') - config_mgr.register('database.port', 'port') + config_mgr.register('database.dbname', '') + config_mgr.register('database.host', '') + config_mgr.register('database.user', '') + config_mgr.register('database.password', '') + config_mgr.register('database.port', '') + + if not os.path.exists(config_file): + name_file = os.path.join(directory, 'name.txt') + with open(name_file, 'r', encoding='utf8') as file: + dbname = file.readline().strip() + config_mgr.set('database.dbname', dbname) + config_mgr.set('database.host', config.get('database.host')) + config_mgr.set('database.user', config.get('database.user')) + config_mgr.set('database.password', config.get('database.password')) + config_mgr.set('database.port', config.get('database.port')) + config_mgr.save() + config_mgr.load() dbkwargs = {}