'get columns from docfields and custom fields'

def get_columns_from_docfields(self):

    fl = frappe.db.sql(u'SELECT    *    FROM    tabDocField    WHERE    parent    =    %s', self.doctype, as_dict=1)
    lengths = {}
    precisions = {}
    uniques = {}
    if (not self.meta.istable):
        for fieldname in optional_columns:
            fl.append({u'fieldname': fieldname, u'fieldtype': u'Text'})
        if getattr(self.meta, u'track_seen', False):
            fl.append({u'fieldname': u'_seen', u'fieldtype': u'Text'})
    if ((not frappe.flags.in_install_db) and (frappe.flags.in_install != u'frappe')):
        custom_fl = frappe.db.sql(u' DCTB  DCTB  DCTB  DCTB SELECT    *    FROM    `tabCustom    Field`\n DCTB  DCTB  DCTB  DCTB WHERE    dt    =    %s    AND    docstatus    <    2', (self.doctype,), as_dict=1)
        if custom_fl:
            fl += custom_fl
        for ps in frappe.get_all(u'Property    Setter', fields=[u'field_name', u'property', u'value'], filters={u'doc_type': self.doctype, u'doctype_or_field': u'DocField', u'property': [u'in', [u'precision', u'length', u'unique']]}):
            if (ps.property == u'length'):
                lengths[ps.field_name] = cint(ps.value)
            elif (ps.property == u'precision'):
                precisions[ps.field_name] = cint(ps.value)
            elif (ps.property == u'unique'):
                uniques[ps.field_name] = cint(ps.value)
    for f in fl:
        self.columns[f[u'fieldname']] = DbColumn(self, f[u'fieldname'], f[u'fieldtype'], (lengths.get(f[u'fieldname']) or f.get(u'length')), f.get(u'default'), f.get(u'search_index'), f.get(u'options'), uniques.get(f[u'fieldname'], f.get(u'unique')), (precisions.get(f[u'fieldname']) or f.get(u'precision')))
