Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | from __future__ import unicode_literals |
| 18 | |
| 19 | import webnotes |
| 20 | from core.doctype.communication.communication import make |
| 21 | |
| 22 | max_communications_per_hour = 300 |
| 23 | |
| 24 | @webnotes.whitelist(allow_guest=True) |
| 25 | def send_message(subject="Website Query", message="", sender="", status="Open"): |
| 26 | if not message: |
| 27 | webnotes.response["message"] = 'Please write something' |
| 28 | return |
| 29 | |
| 30 | if not sender: |
| 31 | webnotes.response["message"] = 'Email Id Required' |
| 32 | return |
| 33 | |
| 34 | # make lead / communication |
| 35 | |
| 36 | name = webnotes.conn.get_value("Lead", {"email_id": sender}, "name") |
| 37 | if name: |
| 38 | lead = webnotes.model_wrapper("Lead", name) |
| 39 | lead.doc.status = "Open" |
| 40 | lead.ignore_permissions = True |
| 41 | lead.save() |
| 42 | else: |
| 43 | lead = webnotes.model_wrapper({ |
| 44 | "doctype":"Lead", |
| 45 | "lead_name": sender, |
| 46 | "email_id": sender, |
| 47 | "status": "Open", |
| 48 | "source": "Website" |
| 49 | }) |
| 50 | lead.ignore_permissions = True |
| 51 | lead.insert() |
| 52 | |
Rushabh Mehta | 3ab6c5f | 2013-01-19 10:54:03 +0530 | [diff] [blame] | 53 | make(content=message, sender=sender, subject=subject, |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 54 | doctype="Lead", name=lead.doc.name, lead=lead.doc.name) |
| 55 | |
| 56 | |
| 57 | # guest method, cap max writes per hour |
| 58 | if webnotes.conn.sql("""select count(*) from `tabCommunication` |
| 59 | where TIMEDIFF(NOW(), modified) < '01:00:00'""")[0][0] > max_communications_per_hour: |
| 60 | webnotes.response["message"] = "Sorry: we believe we have received an unreasonably high number of requests of this kind. Please try later" |
| 61 | return |
| 62 | |
| 63 | webnotes.response["message"] = 'Thank You' |