From 935e4878eaae4d0b0e0600ff0eb2c7aa4e710e6b Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Tue, 17 May 2016 20:50:13 +0200 Subject: [PATCH] Added mysqldb how mysql default --- paramecio/cromosoma/databases/mysqldb.py | 106 +++++++++++++++++++++++ paramecio/modules/admin/admin/ausers.py | 2 +- 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 paramecio/cromosoma/databases/mysqldb.py diff --git a/paramecio/cromosoma/databases/mysqldb.py b/paramecio/cromosoma/databases/mysqldb.py new file mode 100644 index 0000000..1496539 --- /dev/null +++ b/paramecio/cromosoma/databases/mysqldb.py @@ -0,0 +1,106 @@ +#!/usr/bin/python3 + +import sys +import sqlalchemy.pool as pool +import MySQLdb +import MySQLdb.cursors + +class SqlClass: + + connection={} + + def __init__(self): + + self.error_connection="" + self.connected=False + self.connection_method=self.connect_to_db_sql + + def dummy_connect(self, connection, name_connection="default"): + pass + + def connect_to_db(self, connection, name_connection="default"): + + self.connection_method(connection, name_connection) + + self.connection_method=self.dummy_connect + + def connect_to_db_sql(self, connection, name_connection="default"): + + try: + + def getcoon(): + c = MySQLdb.connect(connection['host'], + user=connection['user'], + passwd=connection['password'], + db=connection['db'], + charset='utf8mb4', + cursorclass=MySQLdb.cursors.DictCursor) + return c + + mypool=pool.QueuePool(getcoon, max_overflow=30, pool_size=15) + + self.connection[name_connection]=mypool.connect() + + self.connected=True + + except: + e = sys.exc_info()[0] + v = sys.exc_info()[1] + + self.error_connection="Error in connection: %s %s" % (e, v) + + #return False + raise NameError(self.error_connection) + + #Make def query more simple if not debugging. + + def query(self, sql_query, arguments=[], name_connection="default"): + + #if fetch_type=="ASSOC": + #fetch_type=MySQLdb.cursors.DictCursor + + #with self.connection[name_connection].cursor(MySQLdb.cursors.DictCursor) as cursor: + cursor=self.connection[name_connection].cursor(MySQLdb.cursors.DictCursor) + + try: + + cursor.execute(sql_query, arguments) + self.connection[name_connection].commit() + + return cursor + + except: + e = sys.exc_info()[0] + v = sys.exc_info()[1] + + if hasattr(cursor, '_last_executed'): + sql_query=cursor._last_executed + + self.error_connection="Error in query ||"+sql_query+"||: %s %s" % (e, v) + + #return False + raise NameError(self.error_connection) + + #Fetcho row return dictionary if is defined in query. + + #def fetch(self, cursor): + + #return cursor.fetchone() + """ + def __del__(self): + + for key in self.connection: + + self.close(self.connection) + """ + + def close(self, name_connection="default"): + + if self.connection[name_connection]: + + self.connection[name_connection].close() + #self.connection[name_connection]=False + + pass + + diff --git a/paramecio/modules/admin/admin/ausers.py b/paramecio/modules/admin/admin/ausers.py index e25334f..b4b76c9 100644 --- a/paramecio/modules/admin/admin/ausers.py +++ b/paramecio/modules/admin/admin/ausers.py @@ -19,7 +19,7 @@ def admin(**args): user_admin.create_forms(['username', 'password', 'email', 'privileges', 'lang']) - user_admin.forms['privileges'].arr_select={'0': I18n.lang('admin', 'without_privileges', 'Without privileges'), '1': I18n.lang('admin', 'selected_privileges', 'Selected privileges'), '2': I18n.lang('admin', 'administrator', 'Administrator')} + user_admin.forms['privileges'].arr_select={0: I18n.lang('admin', 'without_privileges', 'Without privileges'), 1: I18n.lang('admin', 'selected_privileges', 'Selected privileges'), 2: I18n.lang('admin', 'administrator', 'Administrator')} user_admin.fields['password'].protected=False