From cb7d2e0b80574d8c7440d5d7c7dd799ff55c839f Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Fri, 27 Jan 2012 12:16:03 +0530 Subject: [PATCH] redid file system naming, fixed bug that did not clear roles on 'clear cache' --- css/legacy/bootstrap-buttons.css | 1 + py/core/doctype/file_data/file_data.py | 14 ++++++------- py/webnotes/auth.py | 8 +++++--- py/webnotes/db.py | 15 ++++---------- py/webnotes/install_lib/db_init.py | 4 +++- py/webnotes/install_lib/install.py | 7 ++++++- py/webnotes/model/doc.py | 27 ++++++++++++++++++++++++-- py/webnotes/profile.py | 4 ++-- py/webnotes/session_cache.py | 23 ++++++++-------------- py/webnotes/utils/__init__.py | 7 ------- py/webnotes/widgets/auto_master.py | 3 ++- 11 files changed, 63 insertions(+), 50 deletions(-) diff --git a/css/legacy/bootstrap-buttons.css b/css/legacy/bootstrap-buttons.css index 1ac4454a33..ae1e263289 100644 --- a/css/legacy/bootstrap-buttons.css +++ b/css/legacy/bootstrap-buttons.css @@ -196,4 +196,5 @@ button.btn::-moz-focus-inner, input[type=submit].btn::-moz-focus-inner { -moz-opacity: 0.4; opacity: 0.4; cursor: pointer; + font-weight: bold; } diff --git a/py/core/doctype/file_data/file_data.py b/py/core/doctype/file_data/file_data.py index c3982276ac..f474287d56 100644 --- a/py/core/doctype/file_data/file_data.py +++ b/py/core/doctype/file_data/file_data.py @@ -28,13 +28,13 @@ class DocType(): other_list = webnotes.conn.sql("""select name from `tabFile Data` where name like '%s-%%.%s' order by name desc""" % (parts[0], '.'.join(parts[1:]))) - if other_list: - last_name = other_list[0][0] - - from webnotes.utils import cint - new_id = str(cint(last_name.split('.')[0].split('-')[-1]) + 1) - else: - new_id = '1' + if other_list: + last_name = other_list[0][0] + + from webnotes.utils import cint + new_id = str(cint(last_name.split('.')[0].split('-')[-1]) + 1) + else: + new_id = '1' # new name self.doc.file_name = parts[0] + '-' + new_id + '.' + '.'.join(parts[1:]) diff --git a/py/webnotes/auth.py b/py/webnotes/auth.py index 293632dd5b..01abd08491 100644 --- a/py/webnotes/auth.py +++ b/py/webnotes/auth.py @@ -65,7 +65,7 @@ class HTTPRequest: if webnotes.session['data'].get('profile'): webnotes.user.load_from_session(webnotes.session['data']['profile']) else: - webnotes.user.load_profile() + webnotes.user.load_profile() # set database login # ------------------ @@ -318,7 +318,8 @@ class Session: r=None try: - r = webnotes.conn.sql("select user, sessiondata, status from tabSessions where sid='%s'" % self.sid) + r = webnotes.conn.sql("""select user, sessiondata, status from + tabSessions where sid='%s'""" % self.sid) except Exception, e: if e.args[0]==1054: self.add_status_column() @@ -344,7 +345,8 @@ class Session: webnotes.response['session_status'] = 'Logged Out' raise Exception, 'Logged Out' else: - self.data = {'data':eval(r[1]), 'user':r[0], 'sid': self.sid} + self.data = {'data': (r[1] and eval(r[1]) or {}), + 'user':r[0], 'sid': self.sid} else: webnotes.login_manager.login_as_guest() self.start() diff --git a/py/webnotes/db.py b/py/webnotes/db.py index 2197b81a1b..25f02ea013 100644 --- a/py/webnotes/db.py +++ b/py/webnotes/db.py @@ -64,7 +64,8 @@ class Database: Connect to a database """ self._conn = MySQLdb.connect(user=self.user, host=self.host, passwd=self.password) - self._conn.set_character_set('utf8') + self._conn.converter[246]=float + self._conn.set_character_set('utf8') self._cursor = self._conn.cursor() def use(self, db_name): @@ -119,7 +120,7 @@ class Database: """ * Execute a `query`, with given `values` * returns as a dictionary if as_dict = 1 - * returns as a list of lists (with cleaned up dates and decimals) if as_list = 1 + * returns as a list of lists (with cleaned up dates) if as_list = 1 """ # in transaction validations self.check_transaction_status(query) @@ -168,8 +169,6 @@ class Database: # ====================================================================================== def convert_to_simple_type(self, v, formatted=0): - try: import decimal # for decimal Python 2.5 onwards - except: pass import datetime from webnotes.utils import formatdate, fmt_money @@ -193,12 +192,6 @@ class Database: # long elif type(v)==long: v=int(v) - - # decimal - try: - if type(v)==decimal.Decimal: - v=float(v) - except: pass # convert to strings... (if formatted) if formatted: @@ -309,7 +302,7 @@ class Database: defkey=%s and parent = "Control Panel" """, key): # update - self.sql("""update `tabDefaultValue` set defvalue="%s" + self.sql("""update `tabDefaultValue` set defvalue=%s where parent = "Control Panel" and defkey=%s""", (val, key)) else: from webnotes.model.doc import Document diff --git a/py/webnotes/install_lib/db_init.py b/py/webnotes/install_lib/db_init.py index ae76889c2f..944aafebd7 100644 --- a/py/webnotes/install_lib/db_init.py +++ b/py/webnotes/install_lib/db_init.py @@ -94,7 +94,9 @@ class DatabaseInstance: def create_doctypecache(self): self.conn.sql("DROP TABLE IF EXISTS `__DocTypeCache`") self.conn.sql("create table `__DocTypeCache` (name VARCHAR(120), modified DATETIME, content TEXT, server_code_compiled TEXT)") - + self.conn.sql(""" + create table `__SessionCache` (user VARCHAR(120), country VARCHAR(120), cache LONGTEXT) + """) diff --git a/py/webnotes/install_lib/install.py b/py/webnotes/install_lib/install.py index c22dcdcb6f..9f21dd128f 100755 --- a/py/webnotes/install_lib/install.py +++ b/py/webnotes/install_lib/install.py @@ -42,7 +42,12 @@ class Installer: import webnotes self.dbman.drop_table('__DocTypeCache') - webnotes.conn.sql("create table `__DocTypeCache` (name VARCHAR(120), modified DATETIME, content TEXT, server_code_compiled TEXT)") + webnotes.conn.sql("""create table `__DocTypeCache` + (name VARCHAR(120), modified DATETIME, content TEXT, server_code_compiled TEXT)""") + + self.dbman.drop_table('__SessionCache') + webnotes.conn.sq.("""create table `__SessionCache` + (user VARCHAR(120), country VARCHAR(120), cache LONGTEXT)""") # set the basic passwords webnotes.conn.begin() diff --git a/py/webnotes/model/doc.py b/py/webnotes/model/doc.py index 48f30c7da5..bc49f8e3d7 100755 --- a/py/webnotes/model/doc.py +++ b/py/webnotes/model/doc.py @@ -244,7 +244,7 @@ class Document: # Insert # --------------------------------------------------------------------------- - def _makenew(self, autoname, istable, case='', make_autoname=1): + def insert(self, autoname, istable, case='', make_autoname=1): # set name if make_autoname: self._set_name(autoname, istable) @@ -371,9 +371,14 @@ class Document: res = webnotes.model.meta.get_dt_values(self.doctype, 'autoname, issingle, istable, name_case', as_dict=1) res = res and res[0] or {} + # add missing parentinfo (if reqd) + if self.parent and not (self.parenttype and self.parentfield): + self.update_parentinfo() + # if required, make new if new or (not new and self.fields.get('__islocal')) and (not res.get('issingle')): - r = self._makenew(res.get('autoname'), res.get('istable'), res.get('name_case'), make_autoname) + r = self.insert(res.get('autoname'), res.get('istable'), res.get('name_case'), \ + make_autoname) if r: return r @@ -381,6 +386,24 @@ class Document: self._update_values(res.get('issingle'), check_links and self.make_link_list() or {}, ignore_fields) self._clear_temp_fields() + def update_parentinfo(self): + """update parent type and parent field, if not explicitly specified""" + + tmp = webnotes.conn.sql("""select parent, fieldname from tabDocField + where fieldtype='Table' and options=%s""", self.doctype) + + if len(tmp)==0: + raise Exception, 'Incomplete parent info in child table (%s, %s)' \ + % (self.doctype, self.fields.get('name', '[new]')) + + elif len(tmp)>1: + raise Exception, 'Ambiguous parent info (%s, %s)' \ + % (self.doctype, self.fields.get('name', '[new]')) + + else: + self.parenttype = tmp[0][0] + self.parentfield = tmp[0][1] + # check permissions # --------------------------------------------------------------------------- diff --git a/py/webnotes/profile.py b/py/webnotes/profile.py index 656dad3fee..fe467d7fed 100644 --- a/py/webnotes/profile.py +++ b/py/webnotes/profile.py @@ -15,7 +15,7 @@ class Profile: self.can_get_report = [] def _load_roles(self): - res = webnotes.conn.sql('select role from tabUserRole where parent = "%s"' % self.name) + res = webnotes.conn.sql('select role from tabUserRole where parent = %s', self.name) self.roles = [] for t in res: if t[0]: self.roles.append(t[0]) @@ -32,7 +32,7 @@ class Profile: """ if self.roles: return self.roles - + return self._load_roles() def get_allow_list(self, key): diff --git a/py/webnotes/session_cache.py b/py/webnotes/session_cache.py index ff29f77416..b020042f10 100644 --- a/py/webnotes/session_cache.py +++ b/py/webnotes/session_cache.py @@ -13,16 +13,14 @@ def clear(): def clear_cache(user=''): """clear cache""" - try: - if user: - webnotes.conn.sql("delete from __SessionCache where user=%s", user) - else: - webnotes.conn.sql("delete from __SessionCache") - except Exception, e: - if e.args[0]==1146: - make_cache_table() - else: - raise e + if user: + webnotes.conn.sql("delete from __SessionCache where user=%s", user) + webnotes.conn.sql("update tabSessions set sessiondata=NULL where user=%s", user) + else: + webnotes.conn.sql("delete from __SessionCache") + webnotes.conn.sql("update tabSessions set sessiondata=NULL") + + webnotes.session['data'] = {} def get(): """get session boot info""" @@ -70,8 +68,3 @@ def add_to_cache(sd, country): # make new webnotes.conn.sql("insert into `__SessionCache` (user, country, cache) VALUES (%s, %s, %s)", (webnotes.session['user'], country, str(sd))) - -def make_cache_table(): - webnotes.conn.commit() - webnotes.conn.sql("create table `__SessionCache` (user VARCHAR(120), country VARCHAR(120), cache LONGTEXT)") - webnotes.conn.begin() diff --git a/py/webnotes/utils/__init__.py b/py/webnotes/utils/__init__.py index 3a309cd524..b1932a1b94 100644 --- a/py/webnotes/utils/__init__.py +++ b/py/webnotes/utils/__init__.py @@ -327,9 +327,6 @@ def parse_val(v): """ import datetime - try: import decimal # for decimal Python 2.5 (?) - except: pass - if type(v)==datetime.date: v = str(v) elif type(v)==datetime.timedelta: @@ -338,10 +335,6 @@ def parse_val(v): v = str(v) elif type(v)==long: v=int(v) - try: - if type(v)==decimal.Decimal: v=float(v) - except: pass - return v # ============================================================================== diff --git a/py/webnotes/widgets/auto_master.py b/py/webnotes/widgets/auto_master.py index 9adea1b39c..bfda896ba5 100644 --- a/py/webnotes/widgets/auto_master.py +++ b/py/webnotes/widgets/auto_master.py @@ -26,7 +26,8 @@ def get_master_fields(dt): webnotes.session['data']['auto_masters'] = {} if webnotes.session['data']['auto_masters'].get(dt, None)==None: - fl = webnotes.conn.sql("select fieldname from tabDocField where fieldtype='Data' and options='Suggest' and parent=%s", dt) + fl = webnotes.conn.sql("select fieldname from tabDocField where fieldtype='Data' \ + and options='Suggest' and parent=%s", dt) webnotes.session['data']['auto_masters'][dt] = fl return webnotes.session['data']['auto_masters'][dt]