Przeglądaj źródła

[optimize] make things faster, lots of low hanging fruits fixed (#3495)

* [optimize] make things faster, lots of low hanging fruits fixed

* [fix] dont set gravatar for administrator and guest

* [fix] autocommit

* [fix] clear doctype cache
version-14
Rushabh Mehta 8 lat temu
committed by GitHub
rodzic
commit
d499ab619c
7 zmienionych plików z 34 dodań i 17 usunięć
  1. +1
    -0
      frappe/__init__.py
  2. +4
    -1
      frappe/commands/__init__.py
  3. +6
    -1
      frappe/core/doctype/doctype/doctype.py
  4. +10
    -10
      frappe/core/doctype/user/user.py
  5. +7
    -2
      frappe/model/meta.py
  6. +1
    -1
      frappe/model/utils/link_count.py
  7. +5
    -2
      frappe/utils/data.py

+ 1
- 0
frappe/__init__.py Wyświetl plik

@@ -149,6 +149,7 @@ def init(site, sites_path=None, new_site=False):
local.jenv = None local.jenv = None
local.jloader =None local.jloader =None
local.cache = {} local.cache = {}
local.meta_cache = {}
local.form_dict = _dict() local.form_dict = _dict()
local.session = _dict() local.session = _dict()




+ 4
- 1
frappe/commands/__init__.py Wyświetl plik

@@ -29,7 +29,10 @@ def pass_context(f):
ps = pstats.Stats(pr, stream=s)\ ps = pstats.Stats(pr, stream=s)\
.sort_stats('cumtime', 'tottime', 'ncalls') .sort_stats('cumtime', 'tottime', 'ncalls')
ps.print_stats() ps.print_stats()
print(s.getvalue())

# print the top-100
for line in s.getvalue().splitlines()[:100]:
print(line)


return ret return ret




+ 6
- 1
frappe/core/doctype/doctype/doctype.py Wyświetl plik

@@ -35,6 +35,7 @@ class DocType(Document):
- Check fieldnames (duplication etc) - Check fieldnames (duplication etc)
- Clear permission table for child tables - Clear permission table for child tables
- Add `amended_from` and `amended_by` if Amendable""" - Add `amended_from` and `amended_by` if Amendable"""

self.check_developer_mode() self.check_developer_mode()


self.validate_name() self.validate_name()
@@ -215,6 +216,10 @@ class DocType(Document):
if not frappe.flags.in_install and hasattr(self, 'before_update'): if not frappe.flags.in_install and hasattr(self, 'before_update'):
self.sync_global_search() self.sync_global_search()


# clear from local cache
if self.name in frappe.local.meta_cache:
del frappe.local.meta_cache[self.name]

def sync_global_search(self): def sync_global_search(self):
'''If global search settings are changed, rebuild search properties for this table''' '''If global search settings are changed, rebuild search properties for this table'''
global_search_fields_before_update = [d.fieldname for d in global_search_fields_before_update = [d.fieldname for d in
@@ -419,7 +424,7 @@ def validate_fields(meta):
def check_in_list_view(d): def check_in_list_view(d):
if d.in_list_view and (d.fieldtype in not_allowed_in_list_view): if d.in_list_view and (d.fieldtype in not_allowed_in_list_view):
frappe.throw(_("'In List View' not allowed for type {0} in row {1}").format(d.fieldtype, d.idx)) frappe.throw(_("'In List View' not allowed for type {0} in row {1}").format(d.fieldtype, d.idx))
def check_in_global_search(d): def check_in_global_search(d):
if d.in_global_search and d.fieldtype in no_value_fields: if d.in_global_search and d.fieldtype in no_value_fields:
frappe.throw(_("'In Global Search' not allowed for type {0} in row {1}") frappe.throw(_("'In Global Search' not allowed for type {0} in row {1}")


+ 10
- 10
frappe/core/doctype/user/user.py Wyświetl plik

@@ -59,7 +59,6 @@ class User(Document):
self.set_system_user() self.set_system_user()
self.set_full_name() self.set_full_name()
self.check_enable_disable() self.check_enable_disable()
self.update_gravatar()
self.ensure_unique_roles() self.ensure_unique_roles()
self.remove_all_roles_for_guest() self.remove_all_roles_for_guest()
self.validate_username() self.validate_username()
@@ -80,6 +79,8 @@ class User(Document):
clear_notifications(user=self.name) clear_notifications(user=self.name)
frappe.clear_cache(user=self.name) frappe.clear_cache(user=self.name)
self.send_password_notification(self.__new_password) self.send_password_notification(self.__new_password)
if self.name not in ('Administrator', 'Guest') and not self.user_image:
frappe.enqueue('frappe.core.doctype.user.user.update_gravatar', name=self.name)


def has_website_permission(self, ptype, verbose=False): def has_website_permission(self, ptype, verbose=False):
"""Returns true if current user is the session user""" """Returns true if current user is the session user"""
@@ -193,11 +194,6 @@ class User(Document):
print frappe.get_traceback() print frappe.get_traceback()
pass # email server not set, don't send email pass # email server not set, don't send email



def update_gravatar(self):
if not self.user_image:
self.user_image = has_gravatar(self.name)

@Document.hook @Document.hook
def validate_reset_password(self): def validate_reset_password(self):
pass pass
@@ -554,7 +550,6 @@ def test_password_strength(new_password, key=None, old_password=None, user_data=
if new_password: if new_password:
result = _test_password_strength(new_password, user_inputs=user_data) result = _test_password_strength(new_password, user_inputs=user_data)


enable_password_policy = cint(frappe.db.get_single_value("System Settings", "enable_password_policy")) and True or False
minimum_password_score = cint(frappe.db.get_single_value("System Settings", "minimum_password_score")) or 0 minimum_password_score = cint(frappe.db.get_single_value("System Settings", "minimum_password_score")) or 0


password_policy_validation_passed = False password_policy_validation_passed = False
@@ -620,16 +615,16 @@ def setup_user_email_inbox(email_account, awaiting_password, email_id, enable_ou
return return


for user in user_names: for user in user_names:
user = user.get("name")
user_name = user.get("name")


# check if inbox is alreay configured # check if inbox is alreay configured
user_inbox = frappe.db.get_value("User Email", { user_inbox = frappe.db.get_value("User Email", {
"email_account": email_account, "email_account": email_account,
"parent": user
"parent": user_name
}, ["name"]) or None }, ["name"]) or None


if not user_inbox: if not user_inbox:
add_user_email(user)
add_user_email(user_name)
else: else:
# update awaiting password for email account # update awaiting password for email account
udpate_user_email_settings = True udpate_user_email_settings = True
@@ -882,3 +877,8 @@ def handle_password_test_fail(result):
warning = result['feedback']['warning'] if 'warning' in result['feedback'] else '' warning = result['feedback']['warning'] if 'warning' in result['feedback'] else ''
suggestions += "<br>" + _("Hint: Include symbols, numbers and capital letters in the password") + '<br>' suggestions += "<br>" + _("Hint: Include symbols, numbers and capital letters in the password") + '<br>'
frappe.throw(_('Invalid Password: ' + ' '.join([warning, suggestions]))) frappe.throw(_('Invalid Password: ' + ' '.join([warning, suggestions])))

def update_gravatar(name):
gravatar = has_gravatar(name)
if gravatar:
frappe.db.set_value('User', name, 'user_image', gravatar)

+ 7
- 2
frappe/model/meta.py Wyświetl plik

@@ -28,7 +28,9 @@ from frappe import _


def get_meta(doctype, cached=True): def get_meta(doctype, cached=True):
if cached: if cached:
return frappe.cache().hget("meta", doctype, lambda: Meta(doctype))
if not frappe.local.meta_cache.get(doctype):
frappe.local.meta_cache[doctype] = frappe.cache().hget("meta", doctype, lambda: Meta(doctype))
return frappe.local.meta_cache[doctype]
else: else:
return Meta(doctype) return Meta(doctype)


@@ -469,7 +471,7 @@ def get_default_df(fieldname):
def trim_tables(doctype=None): def trim_tables(doctype=None):
"""Use this to remove columns that don't exist in meta""" """Use this to remove columns that don't exist in meta"""
ignore_fields = default_fields + optional_fields ignore_fields = default_fields + optional_fields
filters={ "issingle": 0 } filters={ "issingle": 0 }
if doctype: if doctype:
filters["name"] = doctype filters["name"] = doctype
@@ -490,6 +492,9 @@ def trim_tables(doctype=None):
def clear_cache(doctype=None): def clear_cache(doctype=None):
cache = frappe.cache() cache = frappe.cache()


if doctype in frappe.local.meta_cache:
del frappe.local.meta_cache[doctype]

for key in ('is_table', 'doctype_modules'): for key in ('is_table', 'doctype_modules'):
cache.delete_value(key) cache.delete_value(key)




+ 1
- 1
frappe/model/utils/link_count.py Wyświetl plik

@@ -30,7 +30,7 @@ def update_link_count():
if key[0] not in ignore_doctypes: if key[0] not in ignore_doctypes:
try: try:
frappe.db.sql('update `tab{0}` set idx = idx + {1} where name=%s'.format(key[0], count), frappe.db.sql('update `tab{0}` set idx = idx + {1} where name=%s'.format(key[0], count),
key[1])
key[1], auto_commit=1)
except Exception, e: except Exception, e:
if e.args[0]!=1146: # table not found, single if e.args[0]!=1146: # table not found, single
raise e raise e


+ 5
- 2
frappe/utils/data.py Wyświetl plik

@@ -55,7 +55,10 @@ def get_datetime(datetime_str=None):
if not datetime_str or (datetime_str or "").startswith("0000-00-00"): if not datetime_str or (datetime_str or "").startswith("0000-00-00"):
return None return None


return parser.parse(datetime_str)
try:
return datetime.datetime.strptime(datetime_str, DATETIME_FORMAT)
except ValueError:
return parser.parse(datetime_str)


def to_timedelta(time_str): def to_timedelta(time_str):
if isinstance(time_str, basestring): if isinstance(time_str, basestring):
@@ -438,7 +441,7 @@ def money_in_words(number, main_currency = None, fraction_currency=None):


number_format = frappe.db.get_value("Currency", main_currency, "number_format", cache=True) or \ number_format = frappe.db.get_value("Currency", main_currency, "number_format", cache=True) or \
frappe.db.get_default("number_format") or "#,###.##" frappe.db.get_default("number_format") or "#,###.##"
fraction_length = get_number_format_info(number_format)[2] fraction_length = get_number_format_info(number_format)[2]


n = "%.{0}f".format(fraction_length) % number n = "%.{0}f".format(fraction_length) % number


Ładowanie…
Anuluj
Zapisz