From 7d3197afe217bf06648b9ee653ef99d2735aabf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 21 Sep 2014 12:25:21 +0200 Subject: [PATCH] Factorize parsing of hostname and port and remove wrong constraint on database name. issue2911 review13521002 --- tryton/common/common.py | 33 ++++++--- tryton/gui/window/dbcreate.py | 125 ++++++++++++++-------------------- tryton/gui/window/dblogin.py | 23 ++----- 3 files changed, 80 insertions(+), 101 deletions(-) diff --git a/tryton/common/common.py b/tryton/common/common.py index 359a868a30..03e65d17df 100644 --- a/tryton/common/common.py +++ b/tryton/common/common.py @@ -322,19 +322,17 @@ def request_server(server_widget): dialog.show_all() dialog.set_default_response(gtk.RESPONSE_OK) - url_m = re.match('^([\w.-]+):(\d{1,5})', - server_widget.get_text()) - if url_m: - entry_server.set_text(url_m.group(1)) - entry_port.set_text(url_m.group(2)) + netloc = server_widget.get_text() + entry_server.set_text(get_hostname(netloc)) + entry_port.set_text(str(get_port(netloc))) res = dialog.run() if res == gtk.RESPONSE_OK: host = entry_server.get_text() - port = int(entry_port.get_text()) - url = '%s:%d' % (host, port) + port = entry_port.get_text() + url = '%s:%s' % (host, port) server_widget.set_text(url) - result = (host, port) + result = (get_hostname(url), get_port(url)) parent.present() dialog.destroy() return result @@ -1534,6 +1532,25 @@ def humanize(size): size /= 1000.0 +def get_hostname(netloc): + if '[' in netloc and ']' in netloc: + return netloc.split(']')[0][1:] + elif ':' in netloc: + return netloc.split(':')[0] + else: + return netloc + + +def get_port(netloc): + netloc = netloc.split(']')[-1] + if ':' in netloc: + try: + return int(netloc.split(':')[1]) + except ValueError: + pass + return 8000 + + def resize_pixbuf(pixbuf, width, height): img_height = pixbuf.get_height() height = min(img_height, height) if height != -1 else img_height diff --git a/tryton/gui/window/dbcreate.py b/tryton/gui/window/dbcreate.py index 739bcf9cf2..6dd4ee374c 100644 --- a/tryton/gui/window/dbcreate.py +++ b/tryton/gui/window/dbcreate.py @@ -3,7 +3,7 @@ import gtk import gobject import gettext -import re + import tryton.common as common from tryton.config import CONFIG, TRYTON_ICON from tryton.exceptions import TrytonServerError @@ -320,95 +320,72 @@ class DBCreate(object): self.dialog.props.sensitive = True res = self.dialog.run() dbname = self.entry_dbname.get_text() - url = self.entry_server_connection.get_text() - url_m = re.match('^([\w.\-]+):(\d{1,5})', url or '') + netloc = self.entry_server_connection.get_text() + host = common.get_hostname(netloc) + port = common.get_port(netloc) langidx = self.combo_language.get_active_iter() langreal = langidx \ and self.combo_language.get_model().get_value(langidx, 1) passwd = pass_widget.get_text() if res == gtk.RESPONSE_OK: - if (not dbname - or not re.match('^[a-zA-Z0-9_]+$', dbname)): - common.warning(_('The database name is restricted to ' - 'alpha-nummerical characters ' - 'and "_" (underscore). ' - 'Avoid all accents, space ' - 'and any other special characters.'), - _('Wrong characters in database name!')) - continue - elif admin_passwd.get_text() != admin_passwd2.get_text(): + if admin_passwd.get_text() != admin_passwd2.get_text(): common.warning( _("The new admin password " "doesn't match the confirmation field.\n"), _("Passwords doesn't match!")) continue - elif not admin_passwd.get_text(): - common.warning(_("Admin password and confirmation are " - "required to create a new database."), - _('Missing admin password!')) + try: + exist = rpc.db_exec(host, port, 'db_exist', dbname) + except TrytonServerError, exception: + common.process_exception(exception) continue - elif url_m.group(1) \ - and int(url_m.group(2)) \ - and dbname \ - and langreal \ - and passwd \ - and admin_passwd.get_text(): + if exist: + common.warning(_("A database with the same name " + "already exists.\n" + "Try another database name."), + _("This database name already exist!")) + self.entry_dbname.set_text("") + self.entry_dbname.grab_focus() + continue + else: # Everything runs fine, break the block here + self.dialog.props.sensitive = False try: - exist = rpc.db_exec(url_m.group(1), - int(url_m.group(2)), 'db_exist', dbname) + rpcprogress = common.RPCProgress('db_exec', + (host, port, 'create', dbname, passwd, langreal, + admin_passwd.get_text())) + rpcprogress.run(False) except TrytonServerError, exception: - common.process_exception(exception) - continue - if exist: - common.warning(_("A database with the same name " - "already exists.\n" - "Try another database name."), - _("This database name already exist!")) - self.entry_dbname.set_text("") - self.entry_dbname.grab_focus() - continue - else: # Everything runs fine, break the block here - host = url_m.group(1) - port = url_m.group(2) - self.dialog.props.sensitive = False - try: - rpcprogress = common.RPCProgress('db_exec', - (host, int(port), 'create', dbname, passwd, - langreal, admin_passwd.get_text())) - rpcprogress.run(False) - except TrytonServerError, exception: - if str(exception.faultCode) == "AccessDenied": - common.warning(_("Sorry, wrong password for " - "the Tryton server. " - "Please try again."), - _("Access denied!")) - self.entry_serverpasswd.set_text("") - self.entry_serverpasswd.grab_focus() - continue - else: # Unclassified error - common.warning(_("Can't create the " - "database, caused by an unknown reason.\n" - "If there is a database created, it could " - "be broken. Maybe drop this database! " - "Please check the error message for " - "possible informations.\n" - "Error message:\n") - + str(exception.faultCode), - _("Error creating database!")) - parent.present() - self.dialog.destroy() - rpc.logout() - break + if str(exception.faultCode) == "AccessDenied": + common.warning(_("Sorry, wrong password for " + "the Tryton server. " + "Please try again."), + _("Access denied!")) + self.entry_serverpasswd.set_text("") + self.entry_serverpasswd.grab_focus() + continue + else: # Unclassified error + common.warning(_("Can't create the " + "database, caused by an unknown reason.\n" + "If there is a database created, it could " + "be broken. Maybe drop this database! " + "Please check the error message for " + "possible informations.\n" + "Error message:\n") + + str(exception.faultCode), + _("Error creating database!")) parent.present() self.dialog.destroy() - if self.sig_login: - CONFIG['login.server'] = host - CONFIG['login.port'] = port - CONFIG['login.db'] = dbname - CONFIG['login.login'] = 'admin' - self.sig_login() + rpc.logout() break - + parent.present() + self.dialog.destroy() + if self.sig_login: + CONFIG['login.server'] = host + CONFIG['login.port'] = str(port) + CONFIG['login.db'] = dbname + CONFIG['login.login'] = 'admin' + self.sig_login() + break break parent.present() self.dialog.destroy() diff --git a/tryton/gui/window/dblogin.py b/tryton/gui/window/dblogin.py index 272d95e5e1..5a6cae691e 100644 --- a/tryton/gui/window/dblogin.py +++ b/tryton/gui/window/dblogin.py @@ -528,9 +528,9 @@ class DBLogin(object): def clear_profile_combo(self, entry, event): netloc = self.entry_host.get_text() - host = self.get_hostname(netloc) + host = common.get_hostname(netloc) try: - port = str(self.get_port(netloc)) + port = str(common.get_port(netloc)) except ValueError: host = '' port = '' @@ -555,21 +555,6 @@ class DBLogin(object): self.entry_database.props.visible = visibility self.label_database.props.visible = visibility - def get_hostname(self, netloc): - if '[' in netloc and ']' in netloc: - return netloc.split(']')[0][1:] - elif ':' in netloc: - return netloc.split(':')[0] - else: - return netloc - - def get_port(self, netloc): - netloc = netloc.split(']')[-1] - if ':' in netloc: - return int(netloc.split(':')[1]) - else: - return 8000 - def run(self): profile_name = CONFIG['login.profile'] can_use_profile = self.profiles.has_section(profile_name) @@ -620,9 +605,9 @@ class DBLogin(object): profile = self.profile_store[active_profile][0] CONFIG['login.profile'] = profile netloc = self.entry_host.get_text() - host = self.get_hostname(netloc) + host = common.get_hostname(netloc) try: - port = self.get_port(netloc) + port = common.get_port(netloc) except ValueError: continue try: