<s> from __future__ import unicode_literals <EOL> import frappe <EOL> from frappe . utils import cstr <EOL> from frappe import _ <EOL> from frappe . model . document import Document <EOL> class CustomField ( Document ) : <EOL> def autoname ( self ) : <EOL> self . set_fieldname ( ) <EOL> self . name = self . dt + "<STR_LIT:->" + self . fieldname <EOL> def set_fieldname ( self ) : <EOL> if not self . fieldname : <EOL> if not self . label : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) ) <EOL> self . fieldname = filter ( lambda x : x . isdigit ( ) or x . isalpha ( ) or '<STR_LIT:_>' , <EOL> cstr ( self . label ) . lower ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) ) <EOL> self . fieldname = self . fieldname . lower ( ) <EOL> def validate ( self ) : <EOL> meta = frappe . get_meta ( self . dt ) <EOL> fieldnames = [ df . fieldname for df in meta . get ( "<STR_LIT>" ) ] <EOL> if self . insert_after and self . insert_after in fieldnames : <EOL> self . idx = fieldnames . index ( self . insert_after ) + <NUM_LIT:1> <EOL> if not self . idx : <EOL> self . idx = len ( fieldnames ) + <NUM_LIT:1> <EOL> if not self . fieldname : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) ) <EOL> def on_update ( self ) : <EOL> frappe . clear_cache ( doctype = self . dt ) <EOL> if not self . flags . ignore_validate : <EOL> from frappe . core . doctype . doctype . doctype import validate_fields_for_doctype <EOL> validate_fields_for_doctype ( self . dt ) <EOL> from frappe . model . db_schema import updatedb <EOL> updatedb ( self . dt ) <EOL> def on_trash ( self ) : <EOL> frappe . db . sql ( """<STR_LIT>""" , <EOL> ( self . dt , self . fieldname ) ) <EOL> frappe . clear_cache ( doctype = self . dt ) <EOL> def validate_insert_after ( self , meta ) : <EOL> if not meta . get_field ( self . insert_after ) : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) <EOL> . format ( self . insert_after , self . name , self . label ) , frappe . DoesNotExistError ) <EOL> if self . fieldname == self . insert_after : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) . format ( meta . get_label ( self . insert_after ) ) ) <EOL> @ frappe . whitelist ( ) <EOL> def get_fields_label ( doctype = None ) : <EOL> return [ { "<STR_LIT:value>" : df . fieldname or "<STR_LIT>" , "<STR_LIT:label>" : _ ( df . label or "<STR_LIT>" ) } for df in frappe . get_meta ( doctype ) . get ( "<STR_LIT>" ) ] <EOL> def create_custom_field_if_values_exist ( doctype , df ) : <EOL> df = frappe . _dict ( df ) <EOL> if df . fieldname in frappe . db . get_table_columns ( doctype ) and frappe . db . sql ( """<STR_LIT>""" . format ( doctype = doctype , fieldname = df . fieldname ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] : <EOL> create_custom_field ( doctype , df ) <EOL> def create_custom_field ( doctype , df ) : <EOL> if not frappe . db . get_value ( "<STR_LIT>" , { "<STR_LIT>" : doctype , "<STR_LIT>" : df . fieldname } ) : <EOL> frappe . get_doc ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : doctype , <EOL> "<STR_LIT>" : df . get ( "<STR_LIT>" ) or <NUM_LIT:0> , <EOL> "<STR_LIT:label>" : df . get ( "<STR_LIT:label>" ) , <EOL> "<STR_LIT>" : df . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : df . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : df . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : df . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : df . get ( "<STR_LIT>" ) <EOL> } ) . insert ( ) </s>
