def set_value(self, dt, dn, field, val, modified=None, modified_by=None, update_modified=True, debug=False): """Set a single value in the database, do not call the ORM triggers but update the modified timestamp (unless specified not to). **Warning:** this function will not call Document events and should be avoided in normal cases. :param dt: DocType name. :param dn: Document name. :param field: Property / field name or dictionary of values to be updated :param value: Value to be updated. :param modified: Use this as the `modified` timestamp. :param modified_by: Set this user as `modified_by`. :param update_modified: default True. Set as false, if you don't want to update the timestamp. :param debug: Print the query in the developer / js console. """ if not modified: modified = now() if not modified_by: modified_by = frappe.session.user to_update = {} if update_modified: to_update = {"modified": modified, "modified_by": modified_by} if isinstance(field, dict): to_update.update(field) else: to_update.update({field: val}) if dn and dt!=dn: # with table conditions, values = self.build_conditions(dn) values.update(to_update) set_values = [] for key in to_update: set_values.append('`{0}`=%({0})s'.format(key)) self.sql("""update `tab{0}` set {1} where {2}""".format(dt, ', '.join(set_values), conditions), values, debug=debug) else: # for singles keys = list(to_update) self.sql(''' delete from tabSingles where field in ({0}) and doctype=%s'''.format(', '.join(['%s']*len(keys))), list(keys) + [dt], debug=debug) for key, value in iteritems(to_update): self.sql('''insert into tabSingles(doctype, field, value) values (%s, %s, %s)''', (dt, key, value), debug=debug) if dt in self.value_cache: del self.value_cache[dt] frappe.clear_document_cache(dt, dn)
def set_value(self, dt, dn, field, val, modified=None, modified_by=None, update_modified=True, debug=False): """Set a single value in the database, do not call the ORM triggers but update the modified timestamp (unless specified not to). **Warning:** this function will not call Document events and should be avoided in normal cases. :param dt: DocType name. :param dn: Document name. :param field: Property / field name or dictionary of values to be updated :param value: Value to be updated. :param modified: Use this as the `modified` timestamp. :param modified_by: Set this user as `modified_by`. :param update_modified: default True. Set as false, if you don't want to update the timestamp. :param debug: Print the query in the developer / js console. """ if not modified: modified = now() if not modified_by: modified_by = frappe.session.user to_update = {} if update_modified: to_update = {"modified": modified, "modified_by": modified_by} if isinstance(field, dict): to_update.update(field) else: to_update.update({field: val}) if dn and dt!=dn: # with table conditions, values = self.build_conditions(dn) values.update(to_update) set_values = [] for key in to_update: set_values.append('`{0}`=%({0})s'.format(key)) self.sql("""update `tab{0}` set {1} where {2}""".format(dt, ', '.join(set_values), conditions), values, debug=debug) else: # for singles keys = list(to_update) self.sql(''' delete from tabSingles where field in ({0}) and doctype=%s'''.format(', '.join(['%s']*len(keys))), list(keys) + [dt], debug=debug) for key, value in iteritems(to_update): self.sql('''insert into tabSingles(doctype, field, value) values (%s, %s, %s)''', (dt, key, value), debug=debug) if dt in self.value_cache: del self.value_cache[dt] frappe.clear_document_cache(dt, dn)
def validate(self): self.validate_field_filters() self.validate_attribute_filters() self.validate_checkout() self.validate_search_index_fields() if self.enabled: self.validate_price_list_exchange_rate() frappe.clear_document_cache("E Commerce Settings", "E Commerce Settings")
def validate(self): if self.home_page_is_products: frappe.db.set_value("Website Settings", None, "home_page", "products") elif frappe.db.get_single_value("Website Settings", "home_page") == 'products': frappe.db.set_value("Website Settings", None, "home_page", "home") self.validate_field_filters() self.validate_attribute_filters() frappe.clear_document_cache("Product Settings", "Product Settings")
def update_config_hooks(doc, event): logger = get_module_logger() # This is called when user updates the single document SpineConsumerConfig # Clear cached version so that new calls will get updated data. doctype_updated = type(doc).__name__ logger.debug("Clearing {} Cache.".format(doctype_updated)) frappe.clear_document_cache(doctype_updated, doctype_updated) logger.debug("{} Cache cleared.".format(doctype_updated)) logger.debug("Updating new values in cache") cache_key = frappe.get_document_cache_key(doctype_updated, doctype_updated) cache_conn = frappe.cache() cache_conn.hset('document_cache', cache_key, doc.as_dict()) logger.debug("New values saved to document_cache")
def validate(self): self.validate_field_filters(self.filter_fields, self.enable_field_filters) self.validate_attribute_filters() self.validate_checkout() self.validate_search_index_fields() if self.enabled: self.validate_price_list_exchange_rate() frappe.clear_document_cache("E Commerce Settings", "E Commerce Settings") self.is_redisearch_enabled_pre_save = frappe.db.get_single_value( "E Commerce Settings", "is_redisearch_enabled")
def set_value_resolver(obj, info: GraphQLResolveInfo, **kwargs): doctype = kwargs.get("doctype") name = kwargs.get("name") value = kwargs.get('value') fieldname = kwargs.get("fieldname") if frappe.get_meta(doctype).get_field(fieldname).fieldtype \ in table_fields: value = frappe.parse_json(value) frappe.set_value(doctype=doctype, docname=name, fieldname=fieldname, value=value) frappe.clear_document_cache(doctype, name) doc = frappe.get_doc(doctype, name).as_dict() return { "doctype": doctype, "name": name, "fieldname": kwargs.get("fieldname"), "value": kwargs.get("value"), "doc": doc }
def clear_cache(self): frappe.clear_document_cache(self.doctype, self.name)
def set_value( self, dt, dn, field, val=None, modified=None, modified_by=None, update_modified=True, debug=False, for_update=True, ): """Set a single value in the database, do not call the ORM triggers but update the modified timestamp (unless specified not to). **Warning:** this function will not call Document events and should be avoided in normal cases. :param dt: DocType name. :param dn: Document name. :param field: Property / field name or dictionary of values to be updated :param value: Value to be updated. :param modified: Use this as the `modified` timestamp. :param modified_by: Set this user as `modified_by`. :param update_modified: default True. Set as false, if you don't want to update the timestamp. :param debug: Print the query in the developer / js console. :param for_update: Will add a row-level lock to the value that is being set so that it can be released on commit. """ is_single_doctype = not (dn and dt != dn) to_update = field if isinstance(field, dict) else {field: val} if update_modified: modified = modified or now() modified_by = modified_by or frappe.session.user to_update.update({ "modified": modified, "modified_by": modified_by }) if is_single_doctype: frappe.db.delete("Singles", filters={ "field": ("in", tuple(to_update)), "doctype": dt }, debug=debug) singles_data = ((dt, key, sbool(value)) for key, value in to_update.items()) query = (frappe.qb.into("Singles").columns( "doctype", "field", "value").insert(*singles_data)).run(debug=debug) frappe.clear_document_cache(dt, dt) else: table = DocType(dt) if for_update: docnames = tuple(x[0] for x in self.get_values( dt, dn, "name", debug=debug, for_update=for_update)) or ( NullValue(), ) query = frappe.qb.update(table).where( table.name.isin(docnames)) for docname in docnames: frappe.clear_document_cache(dt, docname) else: query = self.query.build_conditions(table=dt, filters=dn, update=True) # TODO: Fix this; doesn't work rn - [email protected] # frappe.cache().hdel_keys(dt, "document_cache") # Workaround: clear all document caches frappe.cache().delete_value("document_cache") for column, value in to_update.items(): query = query.set(column, value) query.run(debug=debug) if dt in self.value_cache: del self.value_cache[dt]
def on_update(self): frappe.clear_document_cache(self.doctype, self.name)
def on_submit(self): frappe.clear_document_cache("Expediente", self.expediente)
def clear_cache(self): frappe.clear_document_cache(self.doctype, self.name)