'Find parent document based on subject and sender match'

def find_parent_based_on_subject_and_sender(self, communication, email):

    parent = None
    if (self.append_to and self.sender_field):
        if self.subject_field:
            subject = frappe.as_unicode(strip(re.sub(u'(^\\s*(Fw|FW|fwd)[^:]*:|\\s*(Re|RE)[^:]*:\\s*)*', u'', email.subject)))
            parent = frappe.db.get_all(self.append_to, filters={self.sender_field: email.from_email, self.subject_field: (u'like', u'%{0}%'.format(subject)), u'creation': (u'>', (get_datetime() - relativedelta(days=10)).strftime(DATE_FORMAT))}, fields=u'name')
            if ((not parent) and (len(subject) > 10) and is_system_user(email.from_email)):
                parent = frappe.db.get_all(self.append_to, filters={self.subject_field: (u'like', u'%{0}%'.format(subject)), u'creation': (u'>', (get_datetime() - relativedelta(days=10)).strftime(DATE_FORMAT))}, fields=u'name')
        if parent:
            parent = frappe._dict(doctype=self.append_to, name=parent[0].name)
            return parent
