Exemple #1
0
def postSaveLead(request, lead, updated_fields):
    mail = False
    if lead.send_email:
        mail = True
        lead.send_email = False

    lead.save()

    # Log it
    LogEntry.objects.log_action(
        user_id=request.user.pk,
        content_type_id=ContentType.objects.get_for_model(lead).pk,
        object_id=lead.pk,
        object_repr=force_unicode(lead),
        action_flag=ADDITION,
        change_message=", ".join(updated_fields),
    )

    if mail:
        try:
            fromAddr = request.user.email or "*****@*****.**"
            send_lead_mail(lead,
                           request,
                           fromAddr=fromAddr,
                           fromName="%s %s" %
                           (request.user.first_name, request.user.last_name))
            messages.add_message(
                request, messages.INFO,
                ugettext("Lead sent to business mailing list"))
        except Exception, e:
            messages.add_message(request, messages.ERROR,
                                 ugettext("Failed to send mail: %s") % e)
Exemple #2
0
def postSaveLead(request, lead, updated_fields):
    mail = False
    if lead.send_email:
        mail = True
        lead.send_email = False

    lead.save()

    # Log it
    LogEntry.objects.log_action(
        user_id         = request.user.pk,
        content_type_id = ContentType.objects.get_for_model(lead).pk,
        object_id       = lead.pk,
        object_repr     = force_unicode(lead),
        action_flag     = ADDITION,
        change_message  = ", ".join(updated_fields),
    )

    if mail:
        try:
            fromAddr = request.user.email or "*****@*****.**"
            send_lead_mail(lead, request, fromAddr=fromAddr,
                           fromName="%s %s" % (request.user.first_name, request.user.last_name))
            messages.add_message(request, messages.INFO, ugettext("Lead sent to business mailing list"))
        except Exception, e:
            messages.add_message(request, messages.ERROR, ugettext("Failed to send mail: %s") % e)
Exemple #3
0
def mail_lead(request, lead_id=0):
    try:
        lead = Lead.objects.get(id=lead_id)
    except Lead.DoesNotExist:
        raise Http404
    try:
        send_lead_mail(lead)
        return HttpResponse(_("Lead %(id)s was sent to %(mail)s !") % {"id": lead_id,
                                                                       "mail": get_parameter("LEAD_MAIL_TO")})
    except Exception as e:
        return HttpResponse(_("Failed to send mail: %s") % e)
Exemple #4
0
def mail_lead(request, lead_id=0):
    try:
        lead = Lead.objects.get(id=lead_id)
    except Lead.DoesNotExist:
        raise Http404
    try:
        send_lead_mail(lead)
        return HttpResponse(_("Lead %(id)s was sent to %(mail)s !") % {"id": lead_id,
                                                                       "mail": get_parameter("LEAD_MAIL_TO")})
    except Exception as e:
        return HttpResponse(_("Failed to send mail: %s") % e)
Exemple #5
0
def mail_lead(request, lead_id=0):
    try:
        lead = Lead.objects.get(id=lead_id)
    except Lead.DoesNotExist:
        raise Http404
    try:
        send_lead_mail(lead)
        return HttpResponse(_("Lead %(id)s was sent to %(mail)s !") % {"id": lead_id,
                                                                       "mail": pydici.settings.LEADS_MAIL_TO})
    except Exception, e:
        return HttpResponse(_("Failed to send mail: %s") % e)
Exemple #6
0
def mail_lead(request, lead_id=0):
    try:
        lead = Lead.objects.get(id=lead_id)
    except Lead.DoesNotExist:
        raise Http404
    try:
        send_lead_mail(lead)
        return HttpResponse(
            _("Lead %(id)s was sent to %(mail)s !") % {
                "id": lead_id,
                "mail": pydici.settings.LEADS_MAIL_TO
            })
    except Exception, e:
        return HttpResponse(_("Failed to send mail: %s") % e)
Exemple #7
0
 def save_model(self, request, obj, form, change):
     mail = False
     if obj.send_email:
         mail = True
         obj.send_email = False
     obj.save()
     form.save_m2m()  # Save many to many relations
     if mail:
         try:
             fromAddr = request.user.email or "*****@*****.**"
             send_lead_mail(obj, request, fromAddr=fromAddr,
                            fromName="%s %s" % (request.user.first_name, request.user.last_name))
             messages.add_message(request, messages.INFO, ugettext("Lead sent to business mailing list"))
         except Exception, e:
             messages.add_message(request, messages.ERROR, ugettext("Failed to send mail: %s") % e)
Exemple #8
0
def postSaveLead(request, lead, updated_fields, created=False, state_changed=False, sync=False):
    mail = False
    if lead.send_email:
        mail = True
        lead.send_email = False

    lead.save()

    # Log it
    LogEntry.objects.log_action(
        user_id         = request.user.pk,
        content_type_id = ContentType.objects.get_for_model(lead).pk,
        object_id       = lead.pk,
        object_repr     = force_text(lead),
        action_flag     = ADDITION,
        change_message  = ", ".join(updated_fields),
    )

    if mail:
        try:
            fromAddr = request.user.email or "*****@*****.**"
            send_lead_mail(lead, request, fromAddr=fromAddr,
                           fromName="%s %s" % (request.user.first_name, request.user.last_name))
            messages.add_message(request, messages.INFO, ugettext("Lead sent to business mailing list"))
        except Exception as e:
            messages.add_message(request, messages.ERROR, ugettext("Failed to send mail: %s") % e)

    if settings.TELEGRAM_IS_ENABLED:
        try:
            bot = telegram.bot.Bot(token=settings.TELEGRAM_TOKEN)
            sticker = None
            url = get_parameter("HOST") + reverse("leads:detail", args=[lead.id, ])
            if created:
                msg = ugettext("New Lead !\n%(lead)s\n%(url)s") % {"lead": lead, "url":url }
                sticker = settings.TELEGRAM_STICKERS.get("happy")
                chat_group = "new_leads"
            elif state_changed:
                # Only notify when lead state changed to avoid useless spam
                try:
                    change = "%s (%s)" % (lead.get_change_history()[0].change_message, lead.get_change_history()[0].user)
                except:
                    change = ""
                msg = ugettext("Lead %(lead)s has been updated\n%(url)s\n%(change)s") % {"lead": lead, "url": url, "change": change}
                if lead.state == "WON":
                    sticker = settings.TELEGRAM_STICKERS.get("happy")
                elif lead.state in ("LOST", "FORGIVEN"):
                    sticker = settings.TELEGRAM_STICKERS.get("sad")
                chat_group = "leads_update"
            else:
                # No notification
                chat_group = ""

            for chat_id in settings.TELEGRAM_CHAT.get(chat_group, []):
                bot.sendMessage(chat_id=chat_id, text=msg, disable_web_page_preview=True)
                if sticker:
                    bot.sendSticker(chat_id=chat_id, sticker=sticker)
        except Exception as e:
            messages.add_message(request, messages.ERROR, ugettext("Failed to send telegram notification: %s") % e)

    # Compute leads probability
    if sync:
        compute = compute_leads_state.now  # Select synchronous flavor of computation function
    else:
        compute = compute_leads_state

    if lead.state in ("WON", "LOST", "SLEEPING", "FORGIVEN"):
        # Remove leads proba, no more needed
        lead.stateproba_set.all().delete()
        # Learn again. This new lead will now be used to training
        compute(relearn=True)
    else:
        # Just update proba for this lead with its new features
        compute(relearn=False, leads_id=[lead.id,])

    # Update lead tags
    compute_leads_tags()

    # update lead similarity model
    compute_lead_similarity()

    # Create or update mission  if needed
    if lead.mission_set.count() == 0:
        if lead.state in ("OFFER_SENT", "NEGOTIATION", "WON"):
            create_default_mission(lead)
            messages.add_message(request, messages.INFO, ugettext("A mission has been initialized for this lead."))

    for mission in lead.mission_set.all():
        if mission.subsidiary != lead.subsidiary:
            mission.subsidiary = lead.subsidiary
            mission.save()
        if lead.state == "WON":
            mission.probability = 100
            mission.active = True
            mission.save()
            messages.add_message(request, messages.INFO, ugettext("Mission's probability has been set to 100%"))
        elif lead.state in ("LOST", "FORGIVEN", "SLEEPING"):
            mission.probability = 0
            mission.active = False
            mission.save()
            messages.add_message(request, messages.INFO, ugettext("According mission has been archived"))
Exemple #9
0
def postSaveLead(request,
                 lead,
                 updated_fields,
                 created=False,
                 state_changed=False,
                 sync=False):
    mail = False
    if lead.send_email:
        mail = True
        lead.send_email = False

    lead.save()

    # Log it
    LogEntry.objects.log_action(
        user_id=request.user.pk,
        content_type_id=ContentType.objects.get_for_model(lead).pk,
        object_id=lead.pk,
        object_repr=force_text(lead),
        action_flag=ADDITION,
        change_message=", ".join(updated_fields),
    )

    if mail:
        try:
            fromAddr = request.user.email or "*****@*****.**"
            send_lead_mail(lead,
                           request,
                           fromAddr=fromAddr,
                           fromName="%s %s" %
                           (request.user.first_name, request.user.last_name))
            messages.add_message(
                request, messages.INFO,
                ugettext("Lead sent to business mailing list"))
        except Exception as e:
            messages.add_message(request, messages.ERROR,
                                 ugettext("Failed to send mail: %s") % e)

    if settings.TELEGRAM_IS_ENABLED:
        try:
            bot = telegram.bot.Bot(token=settings.TELEGRAM_TOKEN)
            sticker = None
            url = get_parameter("HOST") + reverse("leads:detail",
                                                  args=[
                                                      lead.id,
                                                  ])
            if created:
                msg = ugettext("New Lead !\n%(lead)s\n%(url)s") % {
                    "lead": lead,
                    "url": url
                }
                sticker = settings.TELEGRAM_STICKERS.get("happy")
                chat_group = "new_leads"
            elif state_changed:
                # Only notify when lead state changed to avoid useless spam
                try:
                    change = "%s (%s)" % (
                        lead.get_change_history()[0].change_message,
                        lead.get_change_history()[0].user)
                except:
                    change = ""
                msg = ugettext(
                    "Lead %(lead)s has been updated\n%(url)s\n%(change)s") % {
                        "lead": lead,
                        "url": url,
                        "change": change
                    }
                if lead.state == "WON":
                    sticker = settings.TELEGRAM_STICKERS.get("happy")
                elif lead.state in ("LOST", "FORGIVEN"):
                    sticker = settings.TELEGRAM_STICKERS.get("sad")
                chat_group = "leads_update"
            else:
                # No notification
                chat_group = ""

            for chat_id in settings.TELEGRAM_CHAT.get(chat_group, []):
                bot.sendMessage(chat_id=chat_id,
                                text=msg,
                                disable_web_page_preview=True)
                if sticker:
                    bot.sendSticker(chat_id=chat_id, sticker=sticker)
        except Exception as e:
            messages.add_message(
                request, messages.ERROR,
                ugettext("Failed to send telegram notification: %s") % e)

    # Compute leads probability
    if sync:
        compute = compute_leads_state.now  # Select synchronous flavor of computation function
    else:
        compute = compute_leads_state

    if lead.state in ("WON", "LOST", "SLEEPING", "FORGIVEN"):
        # Remove leads proba, no more needed
        lead.stateproba_set.all().delete()
        # Learn again. This new lead will now be used to training
        compute(relearn=True)
    else:
        # Just update proba for this lead with its new features
        compute(relearn=False, leads_id=[
            lead.id,
        ])

    # Update lead tags
    compute_leads_tags()

    # update lead similarity model
    compute_lead_similarity()

    # Create or update mission  if needed
    if lead.mission_set.count() == 0:
        if lead.state in ("OFFER_SENT", "NEGOTIATION", "WON"):
            create_default_mission(lead)
            messages.add_message(
                request, messages.INFO,
                ugettext("A mission has been initialized for this lead."))

    for mission in lead.mission_set.all():
        if mission.subsidiary != lead.subsidiary:
            mission.subsidiary = lead.subsidiary
            mission.save()
        if lead.state == "WON":
            mission.probability = 100
            mission.active = True
            mission.save()
            messages.add_message(
                request, messages.INFO,
                ugettext("Mission's probability has been set to 100%"))
        elif lead.state in ("LOST", "FORGIVEN", "SLEEPING"):
            mission.probability = 0
            mission.active = False
            mission.save()
            messages.add_message(
                request, messages.INFO,
                ugettext("According mission has been archived"))