model Package

model Package

webnotes.model.check_if_doc_is_linked(dt, dn)[source]

Raises excption if the given doc(dt, dn) is linked in another record.

webnotes.model.clear_recycle_bin()[source]

Clears temporary records that have been deleted

webnotes.model.copytables(srctype, src, srcfield, tartype, tar, tarfield, srcfields, tarfields=[])[source]
webnotes.model.db_exists(dt, dn)[source]
webnotes.model.delete_doc(doctype=None, name=None, doclist=None)[source]

Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record

webnotes.model.get_dt_labels()[source]

Returns linked fields for dt as a tuple of (linked_doctype, linked_field)

webnotes.model.get_search_criteria(dt)[source]
webnotes.model.rename(dt, old, new, is_doctype=0)[source]

Renames a doc(dt, old) to doc(dt, new) and updates all linked fields of type “Link” or “Select” with “link:”

code Module

This is where all the plug-in code is executed. The standard method for DocTypes is declaration of a standardized DocType class that has the methods of any DocType. When an object is instantiated using the get_obj method, it creates an instance of the DocType class of that particular DocType and sets the doc and doclist attributes that represent the fields (properties) of that record.

methods in following modules are imported for backward compatibility

  • webnotes.*
  • webnotes.utils.*
  • webnotes.model.doc.*
  • webnotes.model.doclist.*
webnotes.model.code.check_syntax(code)[source]
webnotes.model.code.execute(code, doc=None, doclist=[])[source]

Execute the code, if doc is given, then return the instance of the DocType class created

webnotes.model.code.get_code(module, dt, dn, extn, is_static=None, fieldname=None)[source]
webnotes.model.code.get_custom_script(doctype, script_type)[source]

Returns custom script if set in doctype Custom Script

webnotes.model.code.get_obj(dt=None, dn=None, doc=None, doclist=[], with_children=0)[source]

Returns the instantiated DocType object. Here you can pass the DocType and name (ID) to get the object. If with_children is true, then all child records will be laoded and added in the doclist.

webnotes.model.code.get_server_obj(doc, doclist=[], basedoctype='')[source]

Returns the instantiated DocType object. Will also manage caching & compiling

webnotes.model.code.run_server_obj(server_obj, method_name, arg=None)[source]

Executes a method (method_name) from the given object (server_obj)

webnotes.model.code.updatedb(doctype, userfields=[], args={})[source]

db_schema Module

Syncs a database table to the DocType (metadata)

Note

This module is only used internally

class webnotes.model.db_schema.DbColumn(table, fieldname, fieldtype, length, default, set_index, options)[source]
check(current_def)[source]
get_definition(with_default=1)[source]
class webnotes.model.db_schema.DbManager(conn)[source]

Basically, a wrapper for oft-used mysql commands. like show tables,databases, variables etc...

#TODO:
0. Simplify / create settings for the restore database source folder 0a. Merge restore database and extract_sql(from webnotes_server_tools). 1. Setter and getter for different mysql variables. 2. Setter and getter for mysql variables at global level??
create_database(target)[source]
create_user(user, password)[source]
delete_user(target)[source]
drop_all_databases()[source]

Danger: will delete all databases except test,mysql.

drop_database(target)[source]
drop_table(table_name)[source]
flush_privileges()[source]
get_database_list()[source]
get_table_schema(table)[source]

Just returns the output of Desc tables.

get_tables_list(target)[source]
get_variables(regex)[source]

Get variables that match the passed pattern regex

grant_all_privileges(target, user)[source]
grant_select_privilges(db, table, user)[source]
restore_database(target, source, root_password)[source]
set_transaction_isolation_level(scope='SESSION', level='READ COMMITTED')[source]
class webnotes.model.db_schema.DbTable(doctype, prefix='tab')[source]
alter()[source]
create()[source]
drop_foreign_keys()[source]
get_column_definitions()[source]
get_columns_from_db()[source]
get_columns_from_docfields()[source]
get_foreign_keys()[source]
get_index_definitions()[source]
sync()[source]
webnotes.model.db_schema.remove_all_foreign_keys()[source]
webnotes.model.db_schema.updatedb(dt, archive=0)[source]
Syncs a DocType to the table
  • creates if required
  • updates columns
  • updates indices
webnotes.model.db_schema.validate_column_name(n)[source]

doc Module

Contains the Document class representing an object / record

class webnotes.model.doc.Document(doctype='', name='', fielddata={}, prefix='tab')[source]

The wn(meta-data)framework equivalent of a Database Record. Stores,Retrieves,Updates the record in the corresponding table. Runs the triggers required.

The Document class represents the basic Object-Relational Mapper (ORM). The object type is defined by DocType and the object ID is represented by name:

Please note the anamoly in the Web Notes Framework that `ID` is always called as `name`

If both doctype and name are specified in the constructor, then the object is loaded from the database. If only doctype is given, then the object is not loaded If fielddata is specfied, then the object is created from the given dictionary.

Note 1:

The getter and setter of the object are overloaded to map to the fields of the object that are loaded when it is instantiated.

For example: doc.name will be the name field and doc.owner will be the owner field

Note 2 - Standard Fields:

  • name: ID / primary key
  • owner: creator of the record
  • creation: datetime of creation
  • modified: datetime of last modification
  • modified_by : last updating user
  • docstatus : Status 0 - Saved, 1 - Submitted, 2- Cancelled
  • parent : if child (table) record, this represents the parent record
  • parenttype : type of parent record (if any)
  • parentfield : table fieldname of parent record (if any)
  • idx : Index (sequence) of the child record
addchild(fieldname, childtype='', local=0, doclist=None)[source]

Returns a child record of the give childtype.

  • if local is set, it does not save the record
  • if doclist is passed, it append the record to the doclist
check_perm(verbose=0)[source]
clear_table(doclist, tablefield, save=0)[source]

Clears the child records from the given doclist for a particular tablefield

save(new=0, check_links=1, ignore_fields=0, make_autoname=1)[source]

Saves the current record in the database. If new = 1, creates a new instance of the record. Also clears temperory fields starting with __

  • if check_links is set, it validates all Link fields
  • if ignore_fields is sets, it does not throw an exception for any field that does not exist in the database table
class webnotes.model.doc.SuperDocType[source]
webnotes.model.doc.addchild(parent, fieldname, childtype='', local=0, doclist=None)[source]

Create a child record to the parent doc.

Example:

c = Document('Contact','ABC')
d = addchild(c, 'contact_updates', 'Contact Update', local = 1)
d.last_updated = 'Phone call'
d.save(1)
webnotes.model.doc.check_page_perm(doc)[source]
webnotes.model.doc.get(dt, dn='', with_children=1, from_get_obj=0, prefix='tab')[source]

Returns a doclist containing the main record and all child records

webnotes.model.doc.get_report_builder_code(doc)[source]
webnotes.model.doc.getchildren(name, childtype, field='', parenttype='', from_doctype=0, prefix='tab')[source]
webnotes.model.doc.getseries(key, digits, doctype='')[source]
webnotes.model.doc.make_autoname(key, doctype='')[source]

Creates an autoname from the given key:

Autoname rules:

  • The key is separated by ‘.’

  • ‘####’ represents a series. The string before this part becomes the prefix:

    Example: ABC.#### creates a series ABC0001, ABC0002 etc

  • ‘MM’ represents the current month

  • ‘YY’ and ‘YYYY’ represent the current year

Example:

  • DE/./.YY./.MM./.##### will create a series like DE/09/01/0001 where 09 is the year, 01 is the month and 0001 is the series
webnotes.model.doc.removechild(d, is_local=0)[source]

Sets the docstatus of the object d to 2 (deleted) and appends an ‘old_parent:’ to the parent name

doclist Module

webnotes.model.doclist.compress(doclist)[source]

Compress a doclist before sending it to the client side. (Internally used by the request handler)

webnotes.model.doclist.copy_doclist(doclist, no_copy=[])[source]

Save & return a copy of the given doclist Pass fields that are not to be copied in no_copy

webnotes.model.doclist.expand(docs)[source]

Expand a doclist sent from the client side. (Internally used by the request handler)

webnotes.model.doclist.getlist(doclist, field)[source]

Filter a list of records for a specific field from the full doclist

Example:

# find all phone call details     
dl = getlist(self.doclist, 'contact_updates')
pl = []
for d in dl:
  if d.type=='Phone':
    pl.append(d)
webnotes.model.doclist.getvaluelist(doclist, fieldname)[source]

Returns a list of values of a particualr fieldname from all Document object in a doclist

webnotes.model.doclist.to_html(doclist)[source]

Return a simple HTML format of the doclist

Validate link fields and return link fields that are not correct. Calls the validate_links function on the Document object

webnotes.model.doclist.xzip(a, b)[source]

doctype Module

DocType module

This module has the DocType class that represents a “DocType” as metadata. This is usually called by the form builder or report builder.

Key functions:
  • manage cache - read / write
  • merge client-side scripts
  • update properties from the modules .txt files
Cache management:
  • Cache is stored in __DocTypeCache
webnotes.model.doctype.clear_cache()[source]
webnotes.model.doctype.get(dt)[source]

Load “DocType” - called by form builder, report buider and from code.py (when there is no cache)

import_docs Module

class webnotes.model.import_docs.CSVImport[source]
convert_csv_data_into_list(csv_data)[source]
get_field_type_list()[source]
import_csv(csv_data, import_date_format='yyyy-mm-dd', overwrite=0)[source]
parse_date(r, c, d)[source]
update_data(fd, row)[source]
validate_data()[source]
validate_doctype(dt_list)[source]
validate_fields(lb_list)[source]
validate_headers()[source]
webnotes.model.import_docs.get_template()[source]
webnotes.model.import_docs.import_docs(docs=[])[source]

meta Module

webnotes.model.meta.get_dt_values(doctype, fields, as_dict=0)[source]
webnotes.model.meta.get_parent_dt(dt)[source]
webnotes.model.meta.get_print_format_html(name)[source]
webnotes.model.meta.get_search_criteria(dt)[source]
webnotes.model.meta.get_table_fields(doctype)[source]
webnotes.model.meta.is_single(doctype)[source]
webnotes.model.meta.set_dt_value(doctype, field, value)[source]
webnotes.model.meta.set_fieldname(field_id, fieldname)[source]

modules Module

webnotes.model.modules.get_module_items(mod, only_dt=0)[source]

triggers Module

Add, manage, fire triggers (events / observers) Standard events called by the framework are “save, submit, cancel”

webnotes.model.triggers.add_trigger(doctype, docname, event_name, method)[source]

Add a trigger to an event on a doctype, docname. The specified method will be called. Wild card ‘*’ is allowed in doctype, docname and/or event_name

webnotes.model.triggers.fire_event(doc, event_name)[source]

Fire all triggers on an event and passes the doc to the trigger

webnotes.model.triggers.insert_trigger(doctype, docname, event_name, method)[source]

inserts a new trigger record

webnotes.model.triggers.remove_trigger(doctype, docname, event_name, method)[source]

Remove a trigger on an event

webnotes.model.triggers.setup()[source]

creates the DocTrigger table from core

webnotes.model.triggers.trigger_exists(doctype, docname, event_name, method)[source]

returns true if trigger exists