Add connection preferences for PostgreSQL databases

This commit is contained in:
Nick Hall 2017-09-23 19:52:23 +01:00
parent cd602170fd
commit 0f9a6d57bf
3 changed files with 52 additions and 5 deletions

View File

@ -163,6 +163,10 @@ register('database.backup-path', USER_HOME)
register('database.backup-on-exit', True) register('database.backup-on-exit', True)
register('database.autobackup', 0) register('database.autobackup', 0)
register('database.path', os.path.join(HOME_DIR, 'grampsdb')) 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', register('export.proxy-order',
[["privacy", 0], [["privacy", 0],

View File

@ -1443,6 +1443,17 @@ class GrampsPreferences(ConfigureDialog):
the_iter = obj.get_active_iter() the_iter = obj.get_active_iter()
db_choice = the_list.get_value(the_iter, 2) db_choice = the_list.get_value(the_iter, 2)
config.set('database.backend', db_choice) 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): def add_famtree_panel(self, configdialog):
grid = Gtk.Grid() grid = Gtk.Grid()
@ -1459,6 +1470,25 @@ class GrampsPreferences(ConfigureDialog):
grid.attach(obox, 2, current_line, 1, 1) grid.attach(obox, 2, current_line, 1, 1)
current_line += 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.dbpath_entry = Gtk.Entry()
self.add_path_box(grid, self.add_path_box(grid,
_('Family Tree Database path'), _('Family Tree Database path'),

View File

@ -39,6 +39,7 @@ import re
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gramps.plugins.db.dbapi.dbapi import DBAPI from gramps.plugins.db.dbapi.dbapi import DBAPI
from gramps.gen.utils.configmanager import ConfigManager from gramps.gen.utils.configmanager import ConfigManager
from gramps.gen.config import config
from gramps.gen.db.dbconst import ARRAYSIZE from gramps.gen.db.dbconst import ARRAYSIZE
from gramps.gen.const import GRAMPS_LOCALE as glocale from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext _ = glocale.translation.gettext
@ -67,11 +68,23 @@ class PostgreSQL(DBAPI):
def _initialize(self, directory): def _initialize(self, directory):
config_file = os.path.join(directory, 'settings.ini') config_file = os.path.join(directory, 'settings.ini')
config_mgr = ConfigManager(config_file) config_mgr = ConfigManager(config_file)
config_mgr.register('database.dbname', 'gramps') config_mgr.register('database.dbname', '')
config_mgr.register('database.host', 'localhost') config_mgr.register('database.host', '')
config_mgr.register('database.user', 'user') config_mgr.register('database.user', '')
config_mgr.register('database.password', 'password') config_mgr.register('database.password', '')
config_mgr.register('database.port', 'port') 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() config_mgr.load()
dbkwargs = {} dbkwargs = {}