From fb109aded0f565cdcce957d8c65f486d497a17f6 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 11 Sep 2013 18:58:20 +0530 Subject: [PATCH] [website] [minor] moving to framework --- .../sales_invoice/templates/pages/invoice.py | 2 +- .../sales_invoice/templates/pages/invoices.py | 4 +- portal/templates/includes/transactions.html | 3 +- portal/{website_transactions.py => utils.py} | 15 ++++- public/js/website_utils.js | 10 ++- .../sales_order/templates/pages/order.py | 2 +- .../sales_order/templates/pages/orders.py | 4 +- selling/utils/contact.py | 32 --------- .../delivery_note/templates/pages/shipment.py | 2 +- .../templates/pages/shipments.py | 4 +- .../support_ticket/get_support_mails.py | 58 +++++++++------- .../templates/pages/ticket.html | 65 ++++++++++++++++-- .../support_ticket/templates/pages/ticket.py | 16 +++++ .../templates/pages/tickets.html | 54 +++++++++++++++ .../support_ticket/templates/pages/tickets.py | 12 +++- .../support_ticket/templates/ticket.html | 66 ------------------- 16 files changed, 206 insertions(+), 143 deletions(-) rename portal/{website_transactions.py => utils.py} (75%) delete mode 100644 selling/utils/contact.py delete mode 100644 support/doctype/support_ticket/templates/ticket.html diff --git a/accounts/doctype/sales_invoice/templates/pages/invoice.py b/accounts/doctype/sales_invoice/templates/pages/invoice.py index 7196a3039b..200727c44e 100644 --- a/accounts/doctype/sales_invoice/templates/pages/invoice.py +++ b/accounts/doctype/sales_invoice/templates/pages/invoice.py @@ -7,7 +7,7 @@ import webnotes no_cache = True def get_context(): - from portal.website_transactions import get_transaction_context + from portal.utils import get_transaction_context context = get_transaction_context("Sales Invoice", webnotes.form_dict.name) context.update({ "parent_link": "invoices", diff --git a/accounts/doctype/sales_invoice/templates/pages/invoices.py b/accounts/doctype/sales_invoice/templates/pages/invoices.py index c72903ba12..ec957f3a78 100644 --- a/accounts/doctype/sales_invoice/templates/pages/invoices.py +++ b/accounts/doctype/sales_invoice/templates/pages/invoices.py @@ -7,7 +7,7 @@ import webnotes no_cache = True def get_context(): - from portal.website_transactions import get_currency_context + from portal.utils import get_currency_context context = get_currency_context() context.update({ "title": "Invoices", @@ -20,5 +20,5 @@ def get_context(): @webnotes.whitelist() def get_invoices(start=0): - from portal.website_transactions import get_transaction_list + from portal.utils import get_transaction_list return get_transaction_list("Sales Invoice", start) \ No newline at end of file diff --git a/portal/templates/includes/transactions.html b/portal/templates/includes/transactions.html index 65651ca5c5..036a77c181 100644 --- a/portal/templates/includes/transactions.html +++ b/portal/templates/includes/transactions.html @@ -6,6 +6,8 @@
  • Home
  • {{ title }}
  • +
    @@ -35,7 +37,6 @@ var get_transactions = function(btn) { callback: function(r) { $list.find(".progress").remove(); $show_more.toggleClass("hide", !(r.message && r.message.length===20)); - if(!(r.message && r.message.length)) { console.log("empty"); if(!$list.html().trim()) { diff --git a/portal/website_transactions.py b/portal/utils.py similarity index 75% rename from portal/website_transactions.py rename to portal/utils.py index 21e9111254..25da39cfde 100644 --- a/portal/website_transactions.py +++ b/portal/utils.py @@ -47,4 +47,17 @@ def get_transaction_context(doctype, name): "doclist": bean.doclist, "webnotes": webnotes, "utils": webnotes.utils - } \ No newline at end of file + } + +@webnotes.whitelist(allow_guest=True) +def send_message(subject="Website Query", message="", sender="", status="Open"): + from website.doctype.contact_us_settings.templates.pages.contact \ + import send_message as website_send_message + + if not website_send_message(subject, message, sender): + return + + # make lead / communication + from selling.doctype.lead.get_leads import add_sales_communication + add_sales_communication(subject or "Website Query", message, sender, sender, + mail=None, status=status) \ No newline at end of file diff --git a/public/js/website_utils.js b/public/js/website_utils.js index b42a986e2b..fceb8f655a 100644 --- a/public/js/website_utils.js +++ b/public/js/website_utils.js @@ -5,14 +5,18 @@ if(!window.erpnext) erpnext = {}; // Add / update a new Lead / Communication // subject, sender, description -erpnext.send_message = function(opts) { +wn.send_message = function(opts, btn) { return wn.call({ type: "POST", - method: "selling.utils.contact.send_message", + method: "portal.utils.send_message", + btn: btn, args: opts, callback: opts.callback }); -} +}; + +// for backward compatibility +erpnext.send_message = wn.send_message; // Setup the user tools // diff --git a/selling/doctype/sales_order/templates/pages/order.py b/selling/doctype/sales_order/templates/pages/order.py index f25a521e22..6152e173a9 100644 --- a/selling/doctype/sales_order/templates/pages/order.py +++ b/selling/doctype/sales_order/templates/pages/order.py @@ -7,7 +7,7 @@ import webnotes no_cache = True def get_context(): - from portal.website_transactions import get_transaction_context + from portal.utils import get_transaction_context context = get_transaction_context("Sales Order", webnotes.form_dict.name) context.update({ "parent_link": "orders", diff --git a/selling/doctype/sales_order/templates/pages/orders.py b/selling/doctype/sales_order/templates/pages/orders.py index 204e3f7a15..aea81a99ed 100644 --- a/selling/doctype/sales_order/templates/pages/orders.py +++ b/selling/doctype/sales_order/templates/pages/orders.py @@ -7,7 +7,7 @@ import webnotes no_cache = True def get_context(): - from portal.website_transactions import get_currency_context + from portal.utils import get_currency_context context = get_currency_context() context.update({ "title": "My Orders", @@ -20,6 +20,6 @@ def get_context(): @webnotes.whitelist() def get_orders(start=0): - from portal.website_transactions import get_transaction_list + from portal.utils import get_transaction_list return get_transaction_list("Sales Order", start) \ No newline at end of file diff --git a/selling/utils/contact.py b/selling/utils/contact.py deleted file mode 100644 index 35446a3d24..0000000000 --- a/selling/utils/contact.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. -# License: GNU General Public License v3. See license.txt - -from __future__ import unicode_literals - -import webnotes -from webnotes.utils import now - -max_communications_per_hour = 300 - -@webnotes.whitelist(allow_guest=True) -def send_message(subject="Website Query", message="", sender="", status="Open"): - if not message: - webnotes.response["message"] = 'Please write something' - return - - if not sender: - webnotes.response["message"] = 'Email Id Required' - return - - # make lead / communication - from selling.doctype.lead.get_leads import add_sales_communication - message = add_sales_communication(subject or "Website Query", message, sender, sender, - mail=None, status=status) - - # guest method, cap max writes per hour - if webnotes.conn.sql("""select count(*) from `tabCommunication` - where TIMEDIFF(%s, modified) < '01:00:00'""", now())[0][0] > max_communications_per_hour: - webnotes.response["message"] = "Sorry: we believe we have received an unreasonably high number of requests of this kind. Please try later" - return - - webnotes.response.status = "okay" diff --git a/stock/doctype/delivery_note/templates/pages/shipment.py b/stock/doctype/delivery_note/templates/pages/shipment.py index 60dc9d872a..a33203bf3b 100644 --- a/stock/doctype/delivery_note/templates/pages/shipment.py +++ b/stock/doctype/delivery_note/templates/pages/shipment.py @@ -7,7 +7,7 @@ import webnotes no_cache = True def get_context(): - from portal.website_transactions import get_transaction_context + from portal.utils import get_transaction_context context = get_transaction_context("Delivery Note", webnotes.form_dict.name) context.update({ "parent_link": "shipments", diff --git a/stock/doctype/delivery_note/templates/pages/shipments.py b/stock/doctype/delivery_note/templates/pages/shipments.py index 48c636d8dc..8b29149c02 100644 --- a/stock/doctype/delivery_note/templates/pages/shipments.py +++ b/stock/doctype/delivery_note/templates/pages/shipments.py @@ -7,7 +7,7 @@ import webnotes no_cache = True def get_context(): - from portal.website_transactions import get_currency_context + from portal.utils import get_currency_context context = get_currency_context() context.update({ "title": "Shipments", @@ -20,5 +20,5 @@ def get_context(): @webnotes.whitelist() def get_shipments(start=0): - from portal.website_transactions import get_transaction_list + from portal.utils import get_transaction_list return get_transaction_list("Delivery Note", start) diff --git a/support/doctype/support_ticket/get_support_mails.py b/support/doctype/support_ticket/get_support_mails.py index fa4f304e53..02f7ea8bfe 100644 --- a/support/doctype/support_ticket/get_support_mails.py +++ b/support/doctype/support_ticket/get_support_mails.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import webnotes -from webnotes.utils import cstr, cint, decode_dict +from webnotes.utils import cstr, cint, decode_dict, today from webnotes.utils.email_lib import sendmail from webnotes.utils.email_lib.receive import POP3Mailbox from core.doctype.communication.communication import make @@ -22,32 +22,13 @@ class SupportMailbox(POP3Mailbox): if mail.from_email == self.email_settings.fields.get('support_email'): return thread_id = mail.get_thread_id() - ticket = None new_ticket = False - if thread_id and webnotes.conn.exists("Support Ticket", thread_id): - ticket = webnotes.bean("Support Ticket", thread_id) - ticket.doc.status = 'Open' - ticket.doc.save() - - else: - ticket = webnotes.bean([decode_dict({ - "doctype":"Support Ticket", - "description": mail.content, - "subject": mail.subject, - "raised_by": mail.from_email, - "content_type": mail.content_type, - "status": "Open", - })]) - - ticket.insert() + if not (thread_id and webnotes.conn.exists("Support Ticket", thread_id)): new_ticket = True - - mail.save_attachments_in_doc(ticket.doc) - - make(content=mail.content, sender=mail.from_email, subject = ticket.doc.subject, - doctype="Support Ticket", name=ticket.doc.name, - date=mail.date) + + ticket = add_support_communication(mail.subject, mail.content, mail.from_email, + docname=thread_id if new_ticket else None, mail=mail) if new_ticket and cint(self.email_settings.send_autoreply) and \ "mailer-daemon" not in mail.from_email.lower(): @@ -78,4 +59,31 @@ Original Query: def get_support_mails(): if cint(webnotes.conn.get_value('Email Settings', None, 'sync_support_mails')): - SupportMailbox() \ No newline at end of file + SupportMailbox() + +def add_support_communication(subject, content, sender, docname=None, mail=None): + if docname: + ticket = webnotes.bean("Support Ticket", docname) + ticket.doc.status = 'Open' + ticket.ignore_permissions = True + ticket.doc.save() + else: + ticket = webnotes.bean([decode_dict({ + "doctype":"Support Ticket", + "description": content, + "subject": subject, + "raised_by": sender, + "content_type": mail.content_type if mail else None, + "status": "Open", + })]) + ticket.ignore_permissions = True + ticket.insert() + + make(content=content, sender=sender, subject = subject, + doctype="Support Ticket", name=ticket.doc.name, + date=mail.date if mail else today()) + + if mail: + mail.save_attachments_in_doc(ticket.doc) + + return ticket \ No newline at end of file diff --git a/support/doctype/support_ticket/templates/pages/ticket.html b/support/doctype/support_ticket/templates/pages/ticket.html index 1cfab9b00f..3584d7bdc7 100644 --- a/support/doctype/support_ticket/templates/pages/ticket.html +++ b/support/doctype/support_ticket/templates/pages/ticket.html @@ -38,17 +38,27 @@ {{ utils.formatdate(doc.creation) }}
    -
    -

    Messages

    +
    +

    Messages

    +
    + + +
    +
    + {%- if doclist.get({"doctype":"Communication"}) -%}
    - +
    - {%- for comm in doclist.get({"doctype":"Communication"}) %} + {%- for comm in + (doclist.get({"doctype":"Communication"})|sort(reverse=True, attribute="creation")) %}
    - {{ comm.sender }} on {{ utils.formatdate(doc.modified) }}
    + {{ comm.sender }} on {{ utils.formatdate(comm.creation) }}

    {{ webnotes.utils.is_html(comm.content) and comm.content or comm.content.replace("\n", "
    ")}}

    @@ -64,4 +74,49 @@ {%- endif -%} {% endif -%} +{% endblock %} + +{% block javascript %} + {% endblock %} \ No newline at end of file diff --git a/support/doctype/support_ticket/templates/pages/ticket.py b/support/doctype/support_ticket/templates/pages/ticket.py index 999f69f8b9..ce3100f7e0 100644 --- a/support/doctype/support_ticket/templates/pages/ticket.py +++ b/support/doctype/support_ticket/templates/pages/ticket.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals import webnotes +from webnotes import _ +from webnotes.utils import today no_cache = True @@ -19,3 +21,17 @@ def get_context(): "webnotes": webnotes, "utils": webnotes.utils } + +@webnotes.whitelist() +def add_reply(ticket, message): + if not message: + raise webnotes.throw(_("Please write something")) + + bean = webnotes.bean("Support Ticket", ticket) + if bean.doc.raised_by != webnotes.session.user: + raise webnotes.throw(_("You are not allowed to reply to this ticket."), webnotes.PermissionError) + + from core.doctype.communication.communication import make + make(content=message, sender=bean.doc.raised_by, subject = bean.doc.subject, + doctype="Support Ticket", name=bean.doc.name, + date=today()) \ No newline at end of file diff --git a/support/doctype/support_ticket/templates/pages/tickets.html b/support/doctype/support_ticket/templates/pages/tickets.html index d3e316c5de..d99e6145d9 100644 --- a/support/doctype/support_ticket/templates/pages/tickets.html +++ b/support/doctype/support_ticket/templates/pages/tickets.html @@ -29,5 +29,59 @@ \ ', doc)).appendTo($list); }; + + $(document).ready(function() { + if(!window.$new_ticket) { + window.$new_ticket = $('
    \ + \ + \ +
    ').insertBefore(".transaction-list"); + } + + window.$new_ticket.find("#new-ticket").on("click", function() { + $(this).addClass("hide"); + $(window.$new_ticket).find("#new-ticket-send").removeClass("hide"); + $('
    \ +
    \ +
    \ +
    ') + .insertAfter(window.$new_ticket); + }); + + window.$new_ticket.find("#new-ticket-send").on("click", function() { + var subject = $("#ticket-editor").find('[data-fieldname="subject"]').val().trim(); + var message = $("#ticket-editor").find('[data-fieldname="message"]').val().trim(); + if(!(subject && message)) { + msgprint("Please write something in subject and message!"); + } else { + wn.call({ + type: "POST", + method: "support.doctype.support_ticket.templates.pages.tickets.make_new_ticket", + btn: this, + args: { subject: subject, message: message }, + callback: function(r) { + if(r.exc) { + msgprint(r._server_messages + ? JSON.parse(r._server_messages).join("
    ") + : "Something went wrong!"); + } else { + window.location.href = "ticket?name=" + encodeURIComponent(r.message); + } + } + }) + } + }); + }); + + var msgprint = function(txt) { + if(txt) $("#msgprint-alert").html(txt).toggle(true); + } {%- endblock %} \ No newline at end of file diff --git a/support/doctype/support_ticket/templates/pages/tickets.py b/support/doctype/support_ticket/templates/pages/tickets.py index f434746b70..dd2e52e555 100644 --- a/support/doctype/support_ticket/templates/pages/tickets.py +++ b/support/doctype/support_ticket/templates/pages/tickets.py @@ -25,4 +25,14 @@ def get_tickets(start=0): for t in tickets: t.creation = formatdate(t.creation) - return tickets \ No newline at end of file + return tickets + +@webnotes.whitelist() +def make_new_ticket(subject, message): + if not (subject and message): + raise webnotes.throw(_("Please write something in subject and message!")) + + from support.doctype.support_ticket.get_support_mails import add_support_communication + ticket = add_support_communication(subject, message, webnotes.session.user) + + return ticket.doc.name \ No newline at end of file diff --git a/support/doctype/support_ticket/templates/ticket.html b/support/doctype/support_ticket/templates/ticket.html deleted file mode 100644 index b7d2f2b992..0000000000 --- a/support/doctype/support_ticket/templates/ticket.html +++ /dev/null @@ -1,66 +0,0 @@ -{% extends base_template %} - -{% set title=doc.name %} - -{% set status_label = { - "Open": "label-success", - "To Reply": "label-danger", - "Closed": "label-default" -} %} - -{% block content %} -
    - -

    {{ doc.name }}

    - {% if doc.name == "Not Allowed" -%} - - {% else %} -
    - {%- if doc.status -%} - {% if doc.status == "Waiting for Customer" -%} - {% set status = "To Reply" %} - {% else %} - {% set status = doc.status %} - {%- endif -%} -
    -
    - {{ status }} -
    -
    -
    {{ doc.subject }}
    -
    -
    - {{ utils.formatdate(doc.creation) }} -
    -
    -
    -

    Messages

    - {%- if doclist.get({"doctype":"Communication"}) -%} -
    - - - {%- for comm in doclist.get({"doctype":"Communication"}) %} - - - - {% endfor -%} - -
    -
    - {{ comm.sender }} on {{ utils.formatdate(doc.modified) }}
    -
    -

    {{ webnotes.utils.is_html(comm.content) and comm.content or - comm.content.replace("\n", "
    ")}}

    -
    -
    - {%- else -%} -
    No messages
    - {%- endif -%} - {%- endif -%} - {% endif -%} -
    -{% endblock %} \ No newline at end of file