def add_tag(request): """Add a tag to a lead. Create the tag if needed""" answer = {} answer["tag_created"] = True # indicate if a tag was reused or created answer["tag_url"] = "" # url on tag answer["tag_name"] = "" # tag name if request.POST["tag"]: tagName = capitalize(request.POST["tag"]) lead = Lead.objects.get(id=int(request.POST["lead_id"])) if tagName in lead.tags.all().values_list("name", flat=True): answer["tag_created"] = False lead.tags.add(tagName) if lead.state not in ("WON", "LOST", "FORGIVEN"): compute_leads_state( relearn=False, leads_id=[ lead.id, ] ) # Update (in background) lead proba state as tag are used in computation compute_lead_similarity() # update lead similarity model in background tag = Tag.objects.filter( name=tagName )[0] # We should have only one, but in case of bad data, just take the first one answer["tag_url"] = urlresolvers.reverse("leads.views.tag", args=[ tag.id, ]) answer["tag_remove_url"] = urlresolvers.reverse( "leads.views.remove_tag", args=[tag.id, lead.id]) answer["tag_name"] = tag.name answer["id"] = tag.id return HttpResponse(json.dumps(answer), content_type="application/json")
def remove_tag(request, tag_id, lead_id): """Remove a tag to a lead""" answer = {} answer["error"] = False answer["id"] = tag_id try: tag = Tag.objects.get(id=tag_id) lead = Lead.objects.get(id=lead_id) lead.tags.remove(tag) if lead.state not in ("WON", "LOST", "FORGIVEN"): compute_leads_state(relearn=False, leads_id=[lead.id, ]) # Update (in background) lead proba state as tag are used in computation compute_lead_similarity() # update lead similarity model in background except (Tag.DoesNotExist, Lead.DoesNotExist): answer["error"] = True return HttpResponse(json.dumps(answer), content_type="application/json")
def remove_tag(request, tag_id, lead_id): """Remove a tag to a lead""" answer = {} answer["error"] = False answer["id"] = tag_id try: tag = Tag.objects.get(id=tag_id) lead = Lead.objects.get(id=lead_id) lead.tags.remove(tag) if lead.state not in ("WON", "LOST", "FORGIVEN"): compute_leads_state(relearn=False, leads_id=[lead.id, ]) # Update (in background) lead proba state as tag are used in computation compute_lead_similarity() # update lead similarity model in background if settings.NEXTCLOUD_TAG_IS_ENABLED: remove_lead_tag(lead.id, tag.id) # Remove the lead tag from the lead files except (Tag.DoesNotExist, Lead.DoesNotExist): answer["error"] = True return HttpResponse(json.dumps(answer), content_type="application/json")
def add_tag(request): """Add a tag to a lead. Create the tag if needed""" answer = {} answer["tag_created"] = True # indicate if a tag was reused or created answer["tag_url"] = "" # url on tag answer["tag_name"] = "" # tag name if request.POST["tag"]: tagName = capitalize(request.POST["tag"]) lead = Lead.objects.get(id=int(request.POST["lead_id"])) if tagName in lead.tags.all().values_list("name", flat=True): answer["tag_created"] = False lead.tags.add(tagName) if lead.state not in ("WON", "LOST", "FORGIVEN"): compute_leads_state(relearn=False, leads_id=[lead.id,]) # Update (in background) lead proba state as tag are used in computation compute_lead_similarity() # update lead similarity model in background if settings.NEXTCLOUD_TAG_IS_ENABLED: tag_leads_files([lead.id]) # Update lead tags from lead files tag = Tag.objects.filter(name=tagName)[0] # We should have only one, but in case of bad data, just take the first one answer["tag_url"] = reverse("leads:tag", args=[tag.id, ]) answer["tag_remove_url"] = reverse("leads:remove_tag", args=[tag.id, lead.id]) answer["tag_name"] = tag.name answer["id"] = tag.id return HttpResponse(json.dumps(answer), content_type="application/json")
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"))
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"))
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