def getchildren(name, childtype, field='', parenttype='', from_doctype=0): import frappe from frappe.model.doclist import DocList condition = "" values = [] if field: condition += ' and parentfield=%s ' values.append(field) if parenttype: condition += ' and parenttype=%s ' values.append(parenttype) dataset = frappe.db.sql("""select * from `tab%s` where parent=%s %s order by idx""" \ % (childtype, "%s", condition), tuple([name]+values)) desc = frappe.db.get_description() l = DocList() for i in dataset: d = Document() d.doctype = childtype d._load_values(i, desc) l.append(d) return l
def create_sms_log(self, arg, sent_sms): sl = Document('SMS Log') sl.sender_name = arg['sender_name'] sl.sent_on = nowdate() sl.receiver_list = cstr(arg['receiver_list']) sl.message = arg['message'] sl.no_of_requested_sms = len(arg['receiver_list']) sl.no_of_sent_sms = sent_sms sl.save(new=1)
def validate_incoming(self): """ Checks support ticket email settings """ if self.doc.sync_support_mails and self.doc.support_host: from frappe.utils.email_lib.receive import POP3Mailbox from frappe.model.doc import Document import _socket, poplib inc_email = Document('Incoming Email Settings') inc_email.encode() inc_email.host = self.doc.support_host inc_email.use_ssl = self.doc.support_use_ssl try: err_msg = 'User Name or Support Password missing. Please enter and try again.' if not (self.doc.support_username and self.doc.support_password): raise AttributeError, err_msg inc_email.username = self.doc.support_username inc_email.password = self.doc.support_password except AttributeError, e: frappe.msgprint(err_msg) raise pop_mb = POP3Mailbox(inc_email) try: pop_mb.connect() except _socket.error, e: # Invalid mail server -- due to refusing connection frappe.msgprint( 'Invalid POP3 Mail Server. Please rectify and try again.') raise
def load_from_db(self, dt=None, dn=None): """ Load doclist from dt """ from frappe.model.doc import getchildren if not dt: dt = self.doc.doctype if not dn: dn = self.doc.name doc = Document(dt, dn) # get all children types tablefields = frappe.model.meta.get_table_fields(dt) # load chilren doclist = frappe.doclist([ doc, ]) for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt) self.set_doclist(doclist) if dt == dn: self.convert_type(self.doc)
def append(self, doc): if not isinstance(doc, Document): doc = Document(fielddata=doc) self._prepare_doc(doc) super(DocList, self).append(doc)
def get(dt, dn='', with_children=1, from_controller=0): """ Returns a doclist containing the main record and all child records """ import frappe import frappe.model from frappe.model.doclist import DocList dn = dn or dt # load the main doc doc = Document(dt, dn) if dt == 'Page' and frappe.session['user'] == 'Guest': check_page_perm(doc) if not with_children: # done return DocList([ doc, ]) # get all children types tablefields = frappe.model.meta.get_table_fields(dt) # load chilren doclist = DocList([ doc, ]) for t in tablefields: doclist += getchildren(doc.name, t[0], t[1], dt) return doclist
def copy(self): out = [] for d in self: if isinstance(d, dict): fielddata = d else: fielddata = d.fields fielddata.update({"name": None}) out.append(Document(fielddata=fielddata)) return DocList(out)
def set_doclist(self, doclist): for i, d in enumerate(doclist): if isinstance(d, dict): doclist[i] = Document(fielddata=d) self.doclist = frappe.doclist(doclist) self.doc = self.doclist[0] if self.obj: self.obj.doclist = self.doclist self.obj.doc = self.doc
def allocate_leave(self): self.validate_values() for d in self.get_employees(): la = Document('Leave Allocation') la.employee = cstr(d[0]) la.employee_name = frappe.db.get_value('Employee', cstr(d[0]), 'employee_name') la.leave_type = self.doc.leave_type la.fiscal_year = self.doc.fiscal_year la.posting_date = nowdate() la.carry_forward = cint(self.doc.carry_forward) la.new_leaves_allocated = flt(self.doc.no_of_days) la_obj = get_obj(doc=la) la_obj.doc.docstatus = 1 la_obj.validate() la_obj.on_update() la_obj.doc.save(1) msgprint("Leaves Allocated Successfully")
def addchild(self, fieldname, childtype='', doclist=None): """ 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 """ from frappe.model.doc import Document d = Document() d.parent = self.name d.parenttype = self.doctype d.parentfield = fieldname d.doctype = childtype d.docstatus = 0 d.name = '' d.owner = frappe.session['user'] d.fields['__islocal'] = 1 # for Client to identify unsaved doc if doclist != None: doclist.append(d) return d
def save_report(): """save report""" from frappe.model.doc import Document data = frappe.local.form_dict if frappe.db.exists('Report', data['name']): d = Document('Report', data['name']) else: d = Document('Report') d.report_name = data['name'] d.ref_doctype = data['doctype'] d.report_type = "Report Builder" d.json = data['json'] frappe.bean([d]).save() frappe.msgprint("%s saved." % d.name) return d.name
def set_series_for(self, doctype, ol): options = self.scrub_options_list(ol) # validate names for i in options: self.validate_series_name(i) if self.doc.user_must_always_select: options = [''] + options default = '' else: default = options[0] # update in property setter from frappe.model.doc import Document prop_dict = {'options': "\n".join(options), 'default': default} for prop in prop_dict: ps_exists = frappe.db.sql("""SELECT name FROM `tabProperty Setter` WHERE doc_type = %s AND field_name = 'naming_series' AND property = %s""", (doctype, prop)) if ps_exists: ps = Document('Property Setter', ps_exists[0][0]) ps.value = prop_dict[prop] ps.save() else: ps = Document('Property Setter', fielddata = { 'doctype_or_field': 'DocField', 'doc_type': doctype, 'field_name': 'naming_series', 'property': prop, 'value': prop_dict[prop], 'property_type': 'Select', }) ps.save(1) self.doc.set_options = "\n".join(options) frappe.clear_cache(doctype=doctype)
def post(arg=None): import frappe """post message""" if not arg: arg = {} arg.update(frappe.local.form_dict) if isinstance(arg, basestring): import json arg = json.loads(arg) from frappe.model.doc import Document d = Document('Comment') d.parenttype = arg.get("parenttype") d.comment = arg['txt'] d.comment_docname = arg['contact'] d.comment_doctype = 'Message' d.save() delete_notification_count_for("Messages") import frappe.utils if frappe.utils.cint(arg.get('notify')): notify(arg)
def objectify(doclist): from frappe.model.doc import Document return map(lambda d: isinstance(d, Document) and d or Document(d), doclist)
def copy_doclist(doclist, no_copy = []): """ Save & return a copy of the given doclist Pass fields that are not to be copied in `no_copy` """ cl = [] # main doc c = Document(fielddata = doclist[0].fields.copy()) # clear no_copy fields for f in no_copy: if c.fields.has_key(f): c.fields[f] = None c.name = None c.save(1) cl.append(c) # new parent name parent = c.name # children for d in doclist[1:]: c = Document(fielddata = d.fields.copy()) c.name = None # clear no_copy fields for f in no_copy: if c.fields.has_key(f): c.fields[f] = None c.parent = parent c.save(1) cl.append(c) return cl
def create_lead_address_contact(self): if self.doc.lead_name: if not frappe.db.get_value("Address", { "lead": self.doc.lead_name, "customer": self.doc.customer }): frappe.db.sql( """update `tabAddress` set customer=%s, customer_name=%s where lead=%s""", (self.doc.name, self.doc.customer_name, self.doc.lead_name)) lead = frappe.db.get_value( "Lead", self.doc.lead_name, ["lead_name", "email_id", "phone", "mobile_no"], as_dict=True) c = Document('Contact') c.first_name = lead.lead_name c.email_id = lead.email_id c.phone = lead.phone c.mobile_no = lead.mobile_no c.customer = self.doc.name c.customer_name = self.doc.customer_name c.is_primary_contact = 1 try: c.save(1) except NameError, e: pass
def doc(doctype=None, name=None, fielddata=None): from frappe.model.doc import Document return Document(doctype, name, fielddata)
def make_feed(feedtype, doctype, name, owner, subject, color): "makes a new Feed record" #msgprint(subject) from frappe.model.doc import Document from frappe.utils import get_fullname if feedtype in ('Login', 'Comment', 'Assignment'): # delete old login, comment feed frappe.db.sql("""delete from tabFeed where datediff(curdate(), creation) > 7 and doc_type in ('Comment', 'Login', 'Assignment')""" ) else: # one feed per item frappe.db.sql( """delete from tabFeed where doc_type=%s and doc_name=%s and ifnull(feed_type,'') != 'Comment'""", (doctype, name)) f = Document('Feed') f.owner = owner f.feed_type = feedtype f.doc_type = doctype f.doc_name = name f.subject = subject f.color = color f.full_name = get_fullname(owner) f.save()
def prepare_to_set(self, prop, new_d, ref_d, dt_doclist, delete=0): """ Prepares docs of property setter sets delete property if it is required to be deleted """ # Check if property has changed compared to when it was loaded if self.has_property_changed(ref_d, new_d, prop): #frappe.msgprint("new: " + str(new_d.fields[prop]) + " | old: " + str(ref_d.fields[prop])) # Check if the new property is same as that in original doctype # If yes, we need to delete the property setter entry for dt_d in dt_doclist: if dt_d.name == ref_d.name \ and (new_d.fields.get(prop) == dt_d.fields.get(prop) \ or \ ( \ new_d.fields.get(prop) in [None, 0] \ and dt_d.fields.get(prop) in [None, 0] \ ) or \ ( \ new_d.fields.get(prop) in [None, ''] \ and dt_d.fields.get(prop) in [None, ''] \ )): delete = 1 break value = new_d.fields.get(prop) if prop in self.property_restrictions: allow_change = False for restrict_list in self.property_restrictions.get(prop): if value in restrict_list and \ ref_d.fields.get(prop) in restrict_list: allow_change = True break if not allow_change: frappe.msgprint("""\ You cannot change '%s' of '%s' from '%s' to '%s'. %s can only be changed among %s. <i>Ignoring this change and saving.</i>""" % \ (self.defaults.get(prop, {}).get("label") or prop, new_d.fields.get("label") or new_d.fields.get("idx"), ref_d.fields.get(prop), value, self.defaults.get(prop, {}).get("label") or prop, " -or- ".join([", ".join(r) for r in \ self.property_restrictions.get(prop)])), raise_exception=True) return None # If the above conditions are fulfilled, # create a property setter doc, but dont save it yet. from frappe.model.doc import Document d = Document('Property Setter') d.doctype_or_field = ref_d.doctype=='DocField' and 'DocField' or 'DocType' d.doc_type = self.doc.doc_type d.field_name = ref_d.fieldname d.property = prop d.value = value d.property_type = self.defaults[prop]['fieldtype'] #d.default_value = self.defaults[prop]['default'] if delete: d.delete = 1 if d.select_item: d.select_item = self.remove_forbidden(d.select_item) # return the property setter doc return d else: return None